blob: ab3413e60f6d3fb53e5764d5f34163c277d6374a [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;
Svetoslav Ganovea515ae2011-09-14 18:15:32 -070048import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070050import android.util.FloatProperty;
51import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070053import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070054import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070055import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070057import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080059import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070061import android.view.accessibility.AccessibilityEvent;
62import android.view.accessibility.AccessibilityEventSource;
63import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070064import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070065import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070067import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080068import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070069import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.view.inputmethod.InputConnection;
71import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.widget.ScrollBarDrawable;
73
Romain Guy1ef3fdb2011-09-09 15:30:30 -070074import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070075import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070076
Doug Feltcb3791202011-07-07 11:57:48 -070077import com.android.internal.R;
78import com.android.internal.util.Predicate;
79import com.android.internal.view.menu.MenuBuilder;
80
Christopher Tatea0374192010-10-05 13:06:41 -070081import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070082import java.lang.reflect.InvocationTargetException;
83import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import java.util.ArrayList;
85import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070086import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080087import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088
89/**
90 * <p>
91 * This class represents the basic building block for user interface components. A View
92 * occupies a rectangular area on the screen and is responsible for drawing and
93 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070094 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
96 * are invisible containers that hold other Views (or other ViewGroups) and define
97 * their layout properties.
98 * </p>
99 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700100 * <div class="special reference">
101 * <h3>Developer Guides</h3>
102 * <p>For information about using this class to develop your application's user interface,
103 * 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 -0800104 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700105 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 * <a name="Using"></a>
107 * <h3>Using Views</h3>
108 * <p>
109 * All of the views in a window are arranged in a single tree. You can add views
110 * either from code or by specifying a tree of views in one or more XML layout
111 * files. There are many specialized subclasses of views that act as controls or
112 * are capable of displaying text, images, or other content.
113 * </p>
114 * <p>
115 * Once you have created a tree of views, there are typically a few types of
116 * common operations you may wish to perform:
117 * <ul>
118 * <li><strong>Set properties:</strong> for example setting the text of a
119 * {@link android.widget.TextView}. The available properties and the methods
120 * that set them will vary among the different subclasses of views. Note that
121 * properties that are known at build time can be set in the XML layout
122 * files.</li>
123 * <li><strong>Set focus:</strong> The framework will handled moving focus in
124 * response to user input. To force focus to a specific view, call
125 * {@link #requestFocus}.</li>
126 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
127 * that will be notified when something interesting happens to the view. For
128 * example, all views will let you set a listener to be notified when the view
129 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700130 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700131 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700132 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700134 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 * </ul>
136 * </p>
137 * <p><em>
138 * Note: The Android framework is responsible for measuring, laying out and
139 * drawing views. You should not call methods that perform these actions on
140 * views yourself unless you are actually implementing a
141 * {@link android.view.ViewGroup}.
142 * </em></p>
143 *
144 * <a name="Lifecycle"></a>
145 * <h3>Implementing a Custom View</h3>
146 *
147 * <p>
148 * To implement a custom view, you will usually begin by providing overrides for
149 * some of the standard methods that the framework calls on all views. You do
150 * not need to override all of these methods. In fact, you can start by just
151 * overriding {@link #onDraw(android.graphics.Canvas)}.
152 * <table border="2" width="85%" align="center" cellpadding="5">
153 * <thead>
154 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
155 * </thead>
156 *
157 * <tbody>
158 * <tr>
159 * <td rowspan="2">Creation</td>
160 * <td>Constructors</td>
161 * <td>There is a form of the constructor that are called when the view
162 * is created from code and a form that is called when the view is
163 * inflated from a layout file. The second form should parse and apply
164 * any attributes defined in the layout file.
165 * </td>
166 * </tr>
167 * <tr>
168 * <td><code>{@link #onFinishInflate()}</code></td>
169 * <td>Called after a view and all of its children has been inflated
170 * from XML.</td>
171 * </tr>
172 *
173 * <tr>
174 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700175 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 * <td>Called to determine the size requirements for this view and all
177 * of its children.
178 * </td>
179 * </tr>
180 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700181 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 * <td>Called when this view should assign a size and position to all
183 * of its children.
184 * </td>
185 * </tr>
186 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700187 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 * <td>Called when the size of this view has changed.
189 * </td>
190 * </tr>
191 *
192 * <tr>
193 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700194 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 * <td>Called when the view should render its content.
196 * </td>
197 * </tr>
198 *
199 * <tr>
200 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700201 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 * <td>Called when a new key event occurs.
203 * </td>
204 * </tr>
205 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700206 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * <td>Called when a key up event occurs.
208 * </td>
209 * </tr>
210 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700211 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 * <td>Called when a trackball motion event occurs.
213 * </td>
214 * </tr>
215 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700216 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 * <td>Called when a touch screen motion event occurs.
218 * </td>
219 * </tr>
220 *
221 * <tr>
222 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700223 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 * <td>Called when the view gains or loses focus.
225 * </td>
226 * </tr>
227 *
228 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700229 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 * <td>Called when the window containing the view gains or loses focus.
231 * </td>
232 * </tr>
233 *
234 * <tr>
235 * <td rowspan="3">Attaching</td>
236 * <td><code>{@link #onAttachedToWindow()}</code></td>
237 * <td>Called when the view is attached to a window.
238 * </td>
239 * </tr>
240 *
241 * <tr>
242 * <td><code>{@link #onDetachedFromWindow}</code></td>
243 * <td>Called when the view is detached from its window.
244 * </td>
245 * </tr>
246 *
247 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700248 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 * <td>Called when the visibility of the window containing the view
250 * has changed.
251 * </td>
252 * </tr>
253 * </tbody>
254 *
255 * </table>
256 * </p>
257 *
258 * <a name="IDs"></a>
259 * <h3>IDs</h3>
260 * Views may have an integer id associated with them. These ids are typically
261 * assigned in the layout XML files, and are used to find specific views within
262 * the view tree. A common pattern is to:
263 * <ul>
264 * <li>Define a Button in the layout file and assign it a unique ID.
265 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700266 * &lt;Button
267 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 * android:layout_width="wrap_content"
269 * android:layout_height="wrap_content"
270 * android:text="@string/my_button_text"/&gt;
271 * </pre></li>
272 * <li>From the onCreate method of an Activity, find the Button
273 * <pre class="prettyprint">
274 * Button myButton = (Button) findViewById(R.id.my_button);
275 * </pre></li>
276 * </ul>
277 * <p>
278 * View IDs need not be unique throughout the tree, but it is good practice to
279 * ensure that they are at least unique within the part of the tree you are
280 * searching.
281 * </p>
282 *
283 * <a name="Position"></a>
284 * <h3>Position</h3>
285 * <p>
286 * The geometry of a view is that of a rectangle. A view has a location,
287 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
288 * two dimensions, expressed as a width and a height. The unit for location
289 * and dimensions is the pixel.
290 * </p>
291 *
292 * <p>
293 * It is possible to retrieve the location of a view by invoking the methods
294 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
295 * coordinate of the rectangle representing the view. The latter returns the
296 * top, or Y, coordinate of the rectangle representing the view. These methods
297 * both return the location of the view relative to its parent. For instance,
298 * when getLeft() returns 20, that means the view is located 20 pixels to the
299 * right of the left edge of its direct parent.
300 * </p>
301 *
302 * <p>
303 * In addition, several convenience methods are offered to avoid unnecessary
304 * computations, namely {@link #getRight()} and {@link #getBottom()}.
305 * These methods return the coordinates of the right and bottom edges of the
306 * rectangle representing the view. For instance, calling {@link #getRight()}
307 * is similar to the following computation: <code>getLeft() + getWidth()</code>
308 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
309 * </p>
310 *
311 * <a name="SizePaddingMargins"></a>
312 * <h3>Size, padding and margins</h3>
313 * <p>
314 * The size of a view is expressed with a width and a height. A view actually
315 * possess two pairs of width and height values.
316 * </p>
317 *
318 * <p>
319 * The first pair is known as <em>measured width</em> and
320 * <em>measured height</em>. These dimensions define how big a view wants to be
321 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
322 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
323 * and {@link #getMeasuredHeight()}.
324 * </p>
325 *
326 * <p>
327 * The second pair is simply known as <em>width</em> and <em>height</em>, or
328 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
329 * dimensions define the actual size of the view on screen, at drawing time and
330 * after layout. These values may, but do not have to, be different from the
331 * measured width and height. The width and height can be obtained by calling
332 * {@link #getWidth()} and {@link #getHeight()}.
333 * </p>
334 *
335 * <p>
336 * To measure its dimensions, a view takes into account its padding. The padding
337 * is expressed in pixels for the left, top, right and bottom parts of the view.
338 * Padding can be used to offset the content of the view by a specific amount of
339 * pixels. For instance, a left padding of 2 will push the view's content by
340 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700341 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
342 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
343 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
344 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 * </p>
346 *
347 * <p>
348 * Even though a view can define a padding, it does not provide any support for
349 * margins. However, view groups provide such a support. Refer to
350 * {@link android.view.ViewGroup} and
351 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
352 * </p>
353 *
354 * <a name="Layout"></a>
355 * <h3>Layout</h3>
356 * <p>
357 * Layout is a two pass process: a measure pass and a layout pass. The measuring
358 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
359 * of the view tree. Each view pushes dimension specifications down the tree
360 * during the recursion. At the end of the measure pass, every view has stored
361 * its measurements. The second pass happens in
362 * {@link #layout(int,int,int,int)} and is also top-down. During
363 * this pass each parent is responsible for positioning all of its children
364 * using the sizes computed in the measure pass.
365 * </p>
366 *
367 * <p>
368 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
369 * {@link #getMeasuredHeight()} values must be set, along with those for all of
370 * that view's descendants. A view's measured width and measured height values
371 * must respect the constraints imposed by the view's parents. This guarantees
372 * that at the end of the measure pass, all parents accept all of their
373 * children's measurements. A parent view may call measure() more than once on
374 * its children. For example, the parent may measure each child once with
375 * unspecified dimensions to find out how big they want to be, then call
376 * measure() on them again with actual numbers if the sum of all the children's
377 * unconstrained sizes is too big or too small.
378 * </p>
379 *
380 * <p>
381 * The measure pass uses two classes to communicate dimensions. The
382 * {@link MeasureSpec} class is used by views to tell their parents how they
383 * want to be measured and positioned. The base LayoutParams class just
384 * describes how big the view wants to be for both width and height. For each
385 * dimension, it can specify one of:
386 * <ul>
387 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800388 * <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 -0800389 * (minus padding)
390 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
391 * enclose its content (plus padding).
392 * </ul>
393 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
394 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
395 * an X and Y value.
396 * </p>
397 *
398 * <p>
399 * MeasureSpecs are used to push requirements down the tree from parent to
400 * child. A MeasureSpec can be in one of three modes:
401 * <ul>
402 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
403 * of a child view. For example, a LinearLayout may call measure() on its child
404 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
405 * tall the child view wants to be given a width of 240 pixels.
406 * <li>EXACTLY: This is used by the parent to impose an exact size on the
407 * child. The child must use this size, and guarantee that all of its
408 * descendants will fit within this size.
409 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
410 * child. The child must gurantee that it and all of its descendants will fit
411 * within this size.
412 * </ul>
413 * </p>
414 *
415 * <p>
416 * To intiate a layout, call {@link #requestLayout}. This method is typically
417 * called by a view on itself when it believes that is can no longer fit within
418 * its current bounds.
419 * </p>
420 *
421 * <a name="Drawing"></a>
422 * <h3>Drawing</h3>
423 * <p>
424 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700425 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 * this means that parents will draw before (i.e., behind) their children, with
427 * siblings drawn in the order they appear in the tree.
428 * If you set a background drawable for a View, then the View will draw it for you
429 * before calling back to its <code>onDraw()</code> method.
430 * </p>
431 *
432 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700433 * 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 -0800434 * </p>
435 *
436 * <p>
437 * To force a view to draw, call {@link #invalidate()}.
438 * </p>
439 *
440 * <a name="EventHandlingThreading"></a>
441 * <h3>Event Handling and Threading</h3>
442 * <p>
443 * The basic cycle of a view is as follows:
444 * <ol>
445 * <li>An event comes in and is dispatched to the appropriate view. The view
446 * handles the event and notifies any listeners.</li>
447 * <li>If in the course of processing the event, the view's bounds may need
448 * to be changed, the view will call {@link #requestLayout()}.</li>
449 * <li>Similarly, if in the course of processing the event the view's appearance
450 * may need to be changed, the view will call {@link #invalidate()}.</li>
451 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
452 * the framework will take care of measuring, laying out, and drawing the tree
453 * as appropriate.</li>
454 * </ol>
455 * </p>
456 *
457 * <p><em>Note: The entire view tree is single threaded. You must always be on
458 * the UI thread when calling any method on any view.</em>
459 * If you are doing work on other threads and want to update the state of a view
460 * from that thread, you should use a {@link Handler}.
461 * </p>
462 *
463 * <a name="FocusHandling"></a>
464 * <h3>Focus Handling</h3>
465 * <p>
466 * The framework will handle routine focus movement in response to user input.
467 * This includes changing the focus as views are removed or hidden, or as new
468 * views become available. Views indicate their willingness to take focus
469 * through the {@link #isFocusable} method. To change whether a view can take
470 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
471 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
472 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
473 * </p>
474 * <p>
475 * Focus movement is based on an algorithm which finds the nearest neighbor in a
476 * given direction. In rare cases, the default algorithm may not match the
477 * intended behavior of the developer. In these situations, you can provide
478 * explicit overrides by using these XML attributes in the layout file:
479 * <pre>
480 * nextFocusDown
481 * nextFocusLeft
482 * nextFocusRight
483 * nextFocusUp
484 * </pre>
485 * </p>
486 *
487 *
488 * <p>
489 * To get a particular view to take focus, call {@link #requestFocus()}.
490 * </p>
491 *
492 * <a name="TouchMode"></a>
493 * <h3>Touch Mode</h3>
494 * <p>
495 * When a user is navigating a user interface via directional keys such as a D-pad, it is
496 * necessary to give focus to actionable items such as buttons so the user can see
497 * what will take input. If the device has touch capabilities, however, and the user
498 * begins interacting with the interface by touching it, it is no longer necessary to
499 * always highlight, or give focus to, a particular view. This motivates a mode
500 * for interaction named 'touch mode'.
501 * </p>
502 * <p>
503 * For a touch capable device, once the user touches the screen, the device
504 * will enter touch mode. From this point onward, only views for which
505 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
506 * Other views that are touchable, like buttons, will not take focus when touched; they will
507 * only fire the on click listeners.
508 * </p>
509 * <p>
510 * Any time a user hits a directional key, such as a D-pad direction, the view device will
511 * exit touch mode, and find a view to take focus, so that the user may resume interacting
512 * with the user interface without touching the screen again.
513 * </p>
514 * <p>
515 * The touch mode state is maintained across {@link android.app.Activity}s. Call
516 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
517 * </p>
518 *
519 * <a name="Scrolling"></a>
520 * <h3>Scrolling</h3>
521 * <p>
522 * The framework provides basic support for views that wish to internally
523 * scroll their content. This includes keeping track of the X and Y scroll
524 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800525 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700526 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 * </p>
528 *
529 * <a name="Tags"></a>
530 * <h3>Tags</h3>
531 * <p>
532 * Unlike IDs, tags are not used to identify views. Tags are essentially an
533 * extra piece of information that can be associated with a view. They are most
534 * often used as a convenience to store data related to views in the views
535 * themselves rather than by putting them in a separate structure.
536 * </p>
537 *
538 * <a name="Animation"></a>
539 * <h3>Animation</h3>
540 * <p>
541 * You can attach an {@link Animation} object to a view using
542 * {@link #setAnimation(Animation)} or
543 * {@link #startAnimation(Animation)}. The animation can alter the scale,
544 * rotation, translation and alpha of a view over time. If the animation is
545 * attached to a view that has children, the animation will affect the entire
546 * subtree rooted by that node. When an animation is started, the framework will
547 * take care of redrawing the appropriate views until the animation completes.
548 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800549 * <p>
550 * Starting with Android 3.0, the preferred way of animating views is to use the
551 * {@link android.animation} package APIs.
552 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 *
Jeff Brown85a31762010-09-01 17:01:00 -0700554 * <a name="Security"></a>
555 * <h3>Security</h3>
556 * <p>
557 * Sometimes it is essential that an application be able to verify that an action
558 * is being performed with the full knowledge and consent of the user, such as
559 * granting a permission request, making a purchase or clicking on an advertisement.
560 * Unfortunately, a malicious application could try to spoof the user into
561 * performing these actions, unaware, by concealing the intended purpose of the view.
562 * As a remedy, the framework offers a touch filtering mechanism that can be used to
563 * improve the security of views that provide access to sensitive functionality.
564 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700565 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800566 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700567 * will discard touches that are received whenever the view's window is obscured by
568 * another visible window. As a result, the view will not receive touches whenever a
569 * toast, dialog or other window appears above the view's window.
570 * </p><p>
571 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700572 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
573 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700574 * </p>
575 *
Romain Guy171c5922011-01-06 10:04:23 -0800576 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700577 * @attr ref android.R.styleable#View_background
578 * @attr ref android.R.styleable#View_clickable
579 * @attr ref android.R.styleable#View_contentDescription
580 * @attr ref android.R.styleable#View_drawingCacheQuality
581 * @attr ref android.R.styleable#View_duplicateParentState
582 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700583 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700584 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700585 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700586 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700588 * @attr ref android.R.styleable#View_isScrollContainer
589 * @attr ref android.R.styleable#View_focusable
590 * @attr ref android.R.styleable#View_focusableInTouchMode
591 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
592 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800593 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700594 * @attr ref android.R.styleable#View_longClickable
595 * @attr ref android.R.styleable#View_minHeight
596 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 * @attr ref android.R.styleable#View_nextFocusDown
598 * @attr ref android.R.styleable#View_nextFocusLeft
599 * @attr ref android.R.styleable#View_nextFocusRight
600 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700601 * @attr ref android.R.styleable#View_onClick
602 * @attr ref android.R.styleable#View_padding
603 * @attr ref android.R.styleable#View_paddingBottom
604 * @attr ref android.R.styleable#View_paddingLeft
605 * @attr ref android.R.styleable#View_paddingRight
606 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800607 * @attr ref android.R.styleable#View_paddingStart
608 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700609 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800610 * @attr ref android.R.styleable#View_rotation
611 * @attr ref android.R.styleable#View_rotationX
612 * @attr ref android.R.styleable#View_rotationY
613 * @attr ref android.R.styleable#View_scaleX
614 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 * @attr ref android.R.styleable#View_scrollX
616 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700617 * @attr ref android.R.styleable#View_scrollbarSize
618 * @attr ref android.R.styleable#View_scrollbarStyle
619 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700620 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
621 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
623 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 * @attr ref android.R.styleable#View_scrollbarThumbVertical
625 * @attr ref android.R.styleable#View_scrollbarTrackVertical
626 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
627 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700628 * @attr ref android.R.styleable#View_soundEffectsEnabled
629 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700630 * @attr ref android.R.styleable#View_textAlignment
Chet Haase73066682010-11-29 15:55:32 -0800631 * @attr ref android.R.styleable#View_transformPivotX
632 * @attr ref android.R.styleable#View_transformPivotY
633 * @attr ref android.R.styleable#View_translationX
634 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700635 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 *
637 * @see android.view.ViewGroup
638 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700639public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
640 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 private static final boolean DBG = false;
642
643 /**
644 * The logging tag used by this class with android.util.Log.
645 */
646 protected static final String VIEW_LOG_TAG = "View";
647
648 /**
649 * Used to mark a View that has no ID.
650 */
651 public static final int NO_ID = -1;
652
653 /**
654 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
655 * calling setFlags.
656 */
657 private static final int NOT_FOCUSABLE = 0x00000000;
658
659 /**
660 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
661 * setFlags.
662 */
663 private static final int FOCUSABLE = 0x00000001;
664
665 /**
666 * Mask for use with setFlags indicating bits used for focus.
667 */
668 private static final int FOCUSABLE_MASK = 0x00000001;
669
670 /**
671 * This view will adjust its padding to fit sytem windows (e.g. status bar)
672 */
673 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
674
675 /**
Scott Main812634c22011-07-27 13:22:35 -0700676 * This view is visible.
677 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
678 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 */
680 public static final int VISIBLE = 0x00000000;
681
682 /**
683 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700684 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
685 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 */
687 public static final int INVISIBLE = 0x00000004;
688
689 /**
690 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700691 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
692 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 */
694 public static final int GONE = 0x00000008;
695
696 /**
697 * Mask for use with setFlags indicating bits used for visibility.
698 * {@hide}
699 */
700 static final int VISIBILITY_MASK = 0x0000000C;
701
702 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
703
704 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700705 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 * Use with ENABLED_MASK when calling setFlags.
707 * {@hide}
708 */
709 static final int ENABLED = 0x00000000;
710
711 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700712 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 * Use with ENABLED_MASK when calling setFlags.
714 * {@hide}
715 */
716 static final int DISABLED = 0x00000020;
717
718 /**
719 * Mask for use with setFlags indicating bits used for indicating whether
720 * this view is enabled
721 * {@hide}
722 */
723 static final int ENABLED_MASK = 0x00000020;
724
725 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700726 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
727 * called and further optimizations will be performed. It is okay to have
728 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 * {@hide}
730 */
731 static final int WILL_NOT_DRAW = 0x00000080;
732
733 /**
734 * Mask for use with setFlags indicating bits used for indicating whether
735 * this view is will draw
736 * {@hide}
737 */
738 static final int DRAW_MASK = 0x00000080;
739
740 /**
741 * <p>This view doesn't show scrollbars.</p>
742 * {@hide}
743 */
744 static final int SCROLLBARS_NONE = 0x00000000;
745
746 /**
747 * <p>This view shows horizontal scrollbars.</p>
748 * {@hide}
749 */
750 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
751
752 /**
753 * <p>This view shows vertical scrollbars.</p>
754 * {@hide}
755 */
756 static final int SCROLLBARS_VERTICAL = 0x00000200;
757
758 /**
759 * <p>Mask for use with setFlags indicating bits used for indicating which
760 * scrollbars are enabled.</p>
761 * {@hide}
762 */
763 static final int SCROLLBARS_MASK = 0x00000300;
764
Jeff Brown85a31762010-09-01 17:01:00 -0700765 /**
766 * Indicates that the view should filter touches when its window is obscured.
767 * Refer to the class comments for more information about this security feature.
768 * {@hide}
769 */
770 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
771
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700772 /**
773 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
774 * that they are optional and should be skipped if the window has
775 * requested system UI flags that ignore those insets for layout.
776 */
777 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778
779 /**
780 * <p>This view doesn't show fading edges.</p>
781 * {@hide}
782 */
783 static final int FADING_EDGE_NONE = 0x00000000;
784
785 /**
786 * <p>This view shows horizontal fading edges.</p>
787 * {@hide}
788 */
789 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
790
791 /**
792 * <p>This view shows vertical fading edges.</p>
793 * {@hide}
794 */
795 static final int FADING_EDGE_VERTICAL = 0x00002000;
796
797 /**
798 * <p>Mask for use with setFlags indicating bits used for indicating which
799 * fading edges are enabled.</p>
800 * {@hide}
801 */
802 static final int FADING_EDGE_MASK = 0x00003000;
803
804 /**
805 * <p>Indicates this view can be clicked. When clickable, a View reacts
806 * to clicks by notifying the OnClickListener.<p>
807 * {@hide}
808 */
809 static final int CLICKABLE = 0x00004000;
810
811 /**
812 * <p>Indicates this view is caching its drawing into a bitmap.</p>
813 * {@hide}
814 */
815 static final int DRAWING_CACHE_ENABLED = 0x00008000;
816
817 /**
818 * <p>Indicates that no icicle should be saved for this view.<p>
819 * {@hide}
820 */
821 static final int SAVE_DISABLED = 0x000010000;
822
823 /**
824 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
825 * property.</p>
826 * {@hide}
827 */
828 static final int SAVE_DISABLED_MASK = 0x000010000;
829
830 /**
831 * <p>Indicates that no drawing cache should ever be created for this view.<p>
832 * {@hide}
833 */
834 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
835
836 /**
837 * <p>Indicates this view can take / keep focus when int touch mode.</p>
838 * {@hide}
839 */
840 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
841
842 /**
843 * <p>Enables low quality mode for the drawing cache.</p>
844 */
845 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
846
847 /**
848 * <p>Enables high quality mode for the drawing cache.</p>
849 */
850 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
851
852 /**
853 * <p>Enables automatic quality mode for the drawing cache.</p>
854 */
855 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
856
857 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
858 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
859 };
860
861 /**
862 * <p>Mask for use with setFlags indicating bits used for the cache
863 * quality property.</p>
864 * {@hide}
865 */
866 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
867
868 /**
869 * <p>
870 * Indicates this view can be long clicked. When long clickable, a View
871 * reacts to long clicks by notifying the OnLongClickListener or showing a
872 * context menu.
873 * </p>
874 * {@hide}
875 */
876 static final int LONG_CLICKABLE = 0x00200000;
877
878 /**
879 * <p>Indicates that this view gets its drawable states from its direct parent
880 * and ignores its original internal states.</p>
881 *
882 * @hide
883 */
884 static final int DUPLICATE_PARENT_STATE = 0x00400000;
885
886 /**
887 * The scrollbar style to display the scrollbars inside the content area,
888 * without increasing the padding. The scrollbars will be overlaid with
889 * translucency on the view's content.
890 */
891 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
892
893 /**
894 * The scrollbar style to display the scrollbars inside the padded area,
895 * increasing the padding of the view. The scrollbars will not overlap the
896 * content area of the view.
897 */
898 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
899
900 /**
901 * The scrollbar style to display the scrollbars at the edge of the view,
902 * without increasing the padding. The scrollbars will be overlaid with
903 * translucency.
904 */
905 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
906
907 /**
908 * The scrollbar style to display the scrollbars at the edge of the view,
909 * increasing the padding of the view. The scrollbars will only overlap the
910 * background, if any.
911 */
912 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
913
914 /**
915 * Mask to check if the scrollbar style is overlay or inset.
916 * {@hide}
917 */
918 static final int SCROLLBARS_INSET_MASK = 0x01000000;
919
920 /**
921 * Mask to check if the scrollbar style is inside or outside.
922 * {@hide}
923 */
924 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
925
926 /**
927 * Mask for scrollbar style.
928 * {@hide}
929 */
930 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
931
932 /**
933 * View flag indicating that the screen should remain on while the
934 * window containing this view is visible to the user. This effectively
935 * takes care of automatically setting the WindowManager's
936 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
937 */
938 public static final int KEEP_SCREEN_ON = 0x04000000;
939
940 /**
941 * View flag indicating whether this view should have sound effects enabled
942 * for events such as clicking and touching.
943 */
944 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
945
946 /**
947 * View flag indicating whether this view should have haptic feedback
948 * enabled for events such as long presses.
949 */
950 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
951
952 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700953 * <p>Indicates that the view hierarchy should stop saving state when
954 * it reaches this view. If state saving is initiated immediately at
955 * the view, it will be allowed.
956 * {@hide}
957 */
958 static final int PARENT_SAVE_DISABLED = 0x20000000;
959
960 /**
961 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
962 * {@hide}
963 */
964 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
965
966 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700967 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
968 * should add all focusable Views regardless if they are focusable in touch mode.
969 */
970 public static final int FOCUSABLES_ALL = 0x00000000;
971
972 /**
973 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
974 * should add only Views focusable in touch mode.
975 */
976 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
977
978 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -0700979 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
980 * should add only accessibility focusable Views.
981 *
982 * @hide
983 */
984 public static final int FOCUSABLES_ACCESSIBILITY = 0x00000002;
985
986 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700987 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 * item.
989 */
990 public static final int FOCUS_BACKWARD = 0x00000001;
991
992 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700993 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 * item.
995 */
996 public static final int FOCUS_FORWARD = 0x00000002;
997
998 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700999 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 */
1001 public static final int FOCUS_LEFT = 0x00000011;
1002
1003 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001004 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 */
1006 public static final int FOCUS_UP = 0x00000021;
1007
1008 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001009 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 */
1011 public static final int FOCUS_RIGHT = 0x00000042;
1012
1013 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001014 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 */
1016 public static final int FOCUS_DOWN = 0x00000082;
1017
Svetoslav Ganov42138042012-03-20 11:51:39 -07001018 // Accessibility focus directions.
1019
1020 /**
1021 * The accessibility focus which is the current user position when
1022 * interacting with the accessibility framework.
1023 */
1024 public static final int FOCUS_ACCESSIBILITY = 0x00001000;
1025
1026 /**
1027 * Use with {@link #focusSearch(int)}. Move acessibility focus left.
1028 */
1029 public static final int ACCESSIBILITY_FOCUS_LEFT = FOCUS_LEFT | FOCUS_ACCESSIBILITY;
1030
1031 /**
1032 * Use with {@link #focusSearch(int)}. Move acessibility focus up.
1033 */
1034 public static final int ACCESSIBILITY_FOCUS_UP = FOCUS_UP | FOCUS_ACCESSIBILITY;
1035
1036 /**
1037 * Use with {@link #focusSearch(int)}. Move acessibility focus right.
1038 */
1039 public static final int ACCESSIBILITY_FOCUS_RIGHT = FOCUS_RIGHT | FOCUS_ACCESSIBILITY;
1040
1041 /**
1042 * Use with {@link #focusSearch(int)}. Move acessibility focus down.
1043 */
1044 public static final int ACCESSIBILITY_FOCUS_DOWN = FOCUS_DOWN | FOCUS_ACCESSIBILITY;
1045
1046 /**
1047 * Use with {@link #focusSearch(int)}. Move acessibility focus to the next view.
1048 */
1049 public static final int ACCESSIBILITY_FOCUS_FORWARD = FOCUS_FORWARD | FOCUS_ACCESSIBILITY;
1050
1051 /**
1052 * Use with {@link #focusSearch(int)}. Move acessibility focus to the previous view.
1053 */
1054 public static final int ACCESSIBILITY_FOCUS_BACKWARD = FOCUS_BACKWARD | FOCUS_ACCESSIBILITY;
1055
1056 /**
1057 * Use with {@link #focusSearch(int)}. Move acessibility focus in a view.
1058 */
1059 public static final int ACCESSIBILITY_FOCUS_IN = 0x00000004 | FOCUS_ACCESSIBILITY;
1060
1061 /**
1062 * Use with {@link #focusSearch(int)}. Move acessibility focus out of a view.
1063 */
1064 public static final int ACCESSIBILITY_FOCUS_OUT = 0x00000008 | FOCUS_ACCESSIBILITY;
1065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001067 * Bits of {@link #getMeasuredWidthAndState()} and
1068 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1069 */
1070 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1071
1072 /**
1073 * Bits of {@link #getMeasuredWidthAndState()} and
1074 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1075 */
1076 public static final int MEASURED_STATE_MASK = 0xff000000;
1077
1078 /**
1079 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1080 * for functions that combine both width and height into a single int,
1081 * such as {@link #getMeasuredState()} and the childState argument of
1082 * {@link #resolveSizeAndState(int, int, int)}.
1083 */
1084 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1085
1086 /**
1087 * Bit of {@link #getMeasuredWidthAndState()} and
1088 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1089 * is smaller that the space the view would like to have.
1090 */
1091 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1092
1093 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 * Base View state sets
1095 */
1096 // Singles
1097 /**
1098 * Indicates the view has no states set. States are used with
1099 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1100 * view depending on its state.
1101 *
1102 * @see android.graphics.drawable.Drawable
1103 * @see #getDrawableState()
1104 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001105 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 /**
1107 * Indicates the view is enabled. States are used with
1108 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1109 * view depending on its state.
1110 *
1111 * @see android.graphics.drawable.Drawable
1112 * @see #getDrawableState()
1113 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001114 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 /**
1116 * Indicates the view is focused. States are used with
1117 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1118 * view depending on its state.
1119 *
1120 * @see android.graphics.drawable.Drawable
1121 * @see #getDrawableState()
1122 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001123 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 /**
1125 * Indicates the view is selected. States are used with
1126 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1127 * view depending on its state.
1128 *
1129 * @see android.graphics.drawable.Drawable
1130 * @see #getDrawableState()
1131 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001132 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 /**
1134 * Indicates the view is pressed. States are used with
1135 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1136 * view depending on its state.
1137 *
1138 * @see android.graphics.drawable.Drawable
1139 * @see #getDrawableState()
1140 * @hide
1141 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001142 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 /**
1144 * Indicates the view's window has focus. States are used with
1145 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1146 * view depending on its state.
1147 *
1148 * @see android.graphics.drawable.Drawable
1149 * @see #getDrawableState()
1150 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001151 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 // Doubles
1153 /**
1154 * Indicates the view is enabled and has the focus.
1155 *
1156 * @see #ENABLED_STATE_SET
1157 * @see #FOCUSED_STATE_SET
1158 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001159 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 /**
1161 * Indicates the view is enabled and selected.
1162 *
1163 * @see #ENABLED_STATE_SET
1164 * @see #SELECTED_STATE_SET
1165 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001166 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 /**
1168 * Indicates the view is enabled and that its window has focus.
1169 *
1170 * @see #ENABLED_STATE_SET
1171 * @see #WINDOW_FOCUSED_STATE_SET
1172 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001173 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 /**
1175 * Indicates the view is focused and selected.
1176 *
1177 * @see #FOCUSED_STATE_SET
1178 * @see #SELECTED_STATE_SET
1179 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001180 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 /**
1182 * Indicates the view has the focus and that its window has the focus.
1183 *
1184 * @see #FOCUSED_STATE_SET
1185 * @see #WINDOW_FOCUSED_STATE_SET
1186 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001187 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 /**
1189 * Indicates the view is selected and that its window has the focus.
1190 *
1191 * @see #SELECTED_STATE_SET
1192 * @see #WINDOW_FOCUSED_STATE_SET
1193 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001194 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 // Triples
1196 /**
1197 * Indicates the view is enabled, focused and selected.
1198 *
1199 * @see #ENABLED_STATE_SET
1200 * @see #FOCUSED_STATE_SET
1201 * @see #SELECTED_STATE_SET
1202 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001203 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 /**
1205 * Indicates the view is enabled, focused and its window has the focus.
1206 *
1207 * @see #ENABLED_STATE_SET
1208 * @see #FOCUSED_STATE_SET
1209 * @see #WINDOW_FOCUSED_STATE_SET
1210 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001211 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 /**
1213 * Indicates the view is enabled, selected and its window has the focus.
1214 *
1215 * @see #ENABLED_STATE_SET
1216 * @see #SELECTED_STATE_SET
1217 * @see #WINDOW_FOCUSED_STATE_SET
1218 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001219 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 /**
1221 * Indicates the view is focused, selected and its window has the focus.
1222 *
1223 * @see #FOCUSED_STATE_SET
1224 * @see #SELECTED_STATE_SET
1225 * @see #WINDOW_FOCUSED_STATE_SET
1226 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001227 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 /**
1229 * Indicates the view is enabled, focused, selected and its window
1230 * has the focus.
1231 *
1232 * @see #ENABLED_STATE_SET
1233 * @see #FOCUSED_STATE_SET
1234 * @see #SELECTED_STATE_SET
1235 * @see #WINDOW_FOCUSED_STATE_SET
1236 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001237 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 /**
1239 * Indicates the view is pressed and its window has the focus.
1240 *
1241 * @see #PRESSED_STATE_SET
1242 * @see #WINDOW_FOCUSED_STATE_SET
1243 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001244 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 /**
1246 * Indicates the view is pressed and selected.
1247 *
1248 * @see #PRESSED_STATE_SET
1249 * @see #SELECTED_STATE_SET
1250 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001251 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 /**
1253 * Indicates the view is pressed, selected and its window has the focus.
1254 *
1255 * @see #PRESSED_STATE_SET
1256 * @see #SELECTED_STATE_SET
1257 * @see #WINDOW_FOCUSED_STATE_SET
1258 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001259 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 /**
1261 * Indicates the view is pressed and focused.
1262 *
1263 * @see #PRESSED_STATE_SET
1264 * @see #FOCUSED_STATE_SET
1265 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001266 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 /**
1268 * Indicates the view is pressed, focused and its window has the focus.
1269 *
1270 * @see #PRESSED_STATE_SET
1271 * @see #FOCUSED_STATE_SET
1272 * @see #WINDOW_FOCUSED_STATE_SET
1273 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001274 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 /**
1276 * Indicates the view is pressed, focused and selected.
1277 *
1278 * @see #PRESSED_STATE_SET
1279 * @see #SELECTED_STATE_SET
1280 * @see #FOCUSED_STATE_SET
1281 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001282 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 /**
1284 * Indicates the view is pressed, focused, selected and its window has the focus.
1285 *
1286 * @see #PRESSED_STATE_SET
1287 * @see #FOCUSED_STATE_SET
1288 * @see #SELECTED_STATE_SET
1289 * @see #WINDOW_FOCUSED_STATE_SET
1290 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001291 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 /**
1293 * Indicates the view is pressed and enabled.
1294 *
1295 * @see #PRESSED_STATE_SET
1296 * @see #ENABLED_STATE_SET
1297 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001298 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 /**
1300 * Indicates the view is pressed, enabled and its window has the focus.
1301 *
1302 * @see #PRESSED_STATE_SET
1303 * @see #ENABLED_STATE_SET
1304 * @see #WINDOW_FOCUSED_STATE_SET
1305 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001306 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 /**
1308 * Indicates the view is pressed, enabled and selected.
1309 *
1310 * @see #PRESSED_STATE_SET
1311 * @see #ENABLED_STATE_SET
1312 * @see #SELECTED_STATE_SET
1313 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001314 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 /**
1316 * Indicates the view is pressed, enabled, selected and its window has the
1317 * focus.
1318 *
1319 * @see #PRESSED_STATE_SET
1320 * @see #ENABLED_STATE_SET
1321 * @see #SELECTED_STATE_SET
1322 * @see #WINDOW_FOCUSED_STATE_SET
1323 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001324 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 /**
1326 * Indicates the view is pressed, enabled and focused.
1327 *
1328 * @see #PRESSED_STATE_SET
1329 * @see #ENABLED_STATE_SET
1330 * @see #FOCUSED_STATE_SET
1331 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001332 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 /**
1334 * Indicates the view is pressed, enabled, focused and its window has the
1335 * focus.
1336 *
1337 * @see #PRESSED_STATE_SET
1338 * @see #ENABLED_STATE_SET
1339 * @see #FOCUSED_STATE_SET
1340 * @see #WINDOW_FOCUSED_STATE_SET
1341 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001342 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 /**
1344 * Indicates the view is pressed, enabled, focused and selected.
1345 *
1346 * @see #PRESSED_STATE_SET
1347 * @see #ENABLED_STATE_SET
1348 * @see #SELECTED_STATE_SET
1349 * @see #FOCUSED_STATE_SET
1350 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001351 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 /**
1353 * Indicates the view is pressed, enabled, focused, selected and its window
1354 * has the focus.
1355 *
1356 * @see #PRESSED_STATE_SET
1357 * @see #ENABLED_STATE_SET
1358 * @see #SELECTED_STATE_SET
1359 * @see #FOCUSED_STATE_SET
1360 * @see #WINDOW_FOCUSED_STATE_SET
1361 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001362 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363
1364 /**
1365 * The order here is very important to {@link #getDrawableState()}
1366 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001367 private static final int[][] VIEW_STATE_SETS;
1368
Romain Guyb051e892010-09-28 19:09:36 -07001369 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1370 static final int VIEW_STATE_SELECTED = 1 << 1;
1371 static final int VIEW_STATE_FOCUSED = 1 << 2;
1372 static final int VIEW_STATE_ENABLED = 1 << 3;
1373 static final int VIEW_STATE_PRESSED = 1 << 4;
1374 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001375 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001376 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001377 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1378 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001379
1380 static final int[] VIEW_STATE_IDS = new int[] {
1381 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1382 R.attr.state_selected, VIEW_STATE_SELECTED,
1383 R.attr.state_focused, VIEW_STATE_FOCUSED,
1384 R.attr.state_enabled, VIEW_STATE_ENABLED,
1385 R.attr.state_pressed, VIEW_STATE_PRESSED,
1386 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001387 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001388 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001389 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001390 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 };
1392
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001393 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001394 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1395 throw new IllegalStateException(
1396 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1397 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001398 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001399 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001400 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001401 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001402 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001403 orderedIds[i * 2] = viewState;
1404 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001405 }
1406 }
1407 }
Romain Guyb051e892010-09-28 19:09:36 -07001408 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1409 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1410 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001411 int numBits = Integer.bitCount(i);
1412 int[] set = new int[numBits];
1413 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001414 for (int j = 0; j < orderedIds.length; j += 2) {
1415 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001416 set[pos++] = orderedIds[j];
1417 }
1418 }
1419 VIEW_STATE_SETS[i] = set;
1420 }
1421
1422 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1423 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1424 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1425 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1426 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1427 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1428 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1429 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1430 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1431 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1432 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1433 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1434 | VIEW_STATE_FOCUSED];
1435 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1436 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1437 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1438 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1439 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1440 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1441 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1442 | VIEW_STATE_ENABLED];
1443 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1444 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1445 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1447 | VIEW_STATE_ENABLED];
1448 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1449 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1450 | VIEW_STATE_ENABLED];
1451 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1452 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1453 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1454
1455 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1456 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1457 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1458 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1459 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1460 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1462 | VIEW_STATE_PRESSED];
1463 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1464 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1465 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1467 | VIEW_STATE_PRESSED];
1468 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1470 | VIEW_STATE_PRESSED];
1471 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1472 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1473 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1474 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1475 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1476 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1477 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1478 | VIEW_STATE_PRESSED];
1479 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1480 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1481 | VIEW_STATE_PRESSED];
1482 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1483 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1484 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1485 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1486 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1487 | VIEW_STATE_PRESSED];
1488 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1489 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1490 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1491 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1492 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1493 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1494 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1495 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1496 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1497 | VIEW_STATE_PRESSED];
1498 }
1499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001501 * Accessibility event types that are dispatched for text population.
1502 */
1503 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1504 AccessibilityEvent.TYPE_VIEW_CLICKED
1505 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1506 | AccessibilityEvent.TYPE_VIEW_SELECTED
1507 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1508 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1509 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001510 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001511 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001512 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
1513 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001514
1515 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 * Temporary Rect currently for use in setBackground(). This will probably
1517 * be extended in the future to hold our own class with more than just
1518 * a Rect. :)
1519 */
1520 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001521
1522 /**
Chet Haasea1cff502012-02-21 13:43:44 -08001523 * Temporary flag, used to enable processing of View properties in the native DisplayList
1524 * object instead of during draw(). Soon to be enabled by default for hardware-accelerated
1525 * apps.
1526 * @hide
1527 */
Chet Haase8d56b0e2012-04-02 16:34:48 -07001528 public static final boolean USE_DISPLAY_LIST_PROPERTIES = true;
Chet Haasea1cff502012-02-21 13:43:44 -08001529
1530 /**
Romain Guyd90a3312009-05-06 14:54:28 -07001531 * Map used to store views' tags.
1532 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001533 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001536 * The next available accessiiblity id.
1537 */
1538 private static int sNextAccessibilityViewId;
1539
1540 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 * The animation currently associated with this view.
1542 * @hide
1543 */
1544 protected Animation mCurrentAnimation = null;
1545
1546 /**
1547 * Width as measured during measure pass.
1548 * {@hide}
1549 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001550 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001551 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552
1553 /**
1554 * Height as measured during measure pass.
1555 * {@hide}
1556 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001557 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001558 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559
1560 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001561 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1562 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1563 * its display list. This flag, used only when hw accelerated, allows us to clear the
1564 * flag while retaining this information until it's needed (at getDisplayList() time and
1565 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1566 *
1567 * {@hide}
1568 */
1569 boolean mRecreateDisplayList = false;
1570
1571 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 * The view's identifier.
1573 * {@hide}
1574 *
1575 * @see #setId(int)
1576 * @see #getId()
1577 */
1578 @ViewDebug.ExportedProperty(resolveId = true)
1579 int mID = NO_ID;
1580
1581 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001582 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001583 */
1584 int mAccessibilityViewId = NO_ID;
1585
1586 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 * The view's tag.
1588 * {@hide}
1589 *
1590 * @see #setTag(Object)
1591 * @see #getTag()
1592 */
1593 protected Object mTag;
1594
1595 // for mPrivateFlags:
1596 /** {@hide} */
1597 static final int WANTS_FOCUS = 0x00000001;
1598 /** {@hide} */
1599 static final int FOCUSED = 0x00000002;
1600 /** {@hide} */
1601 static final int SELECTED = 0x00000004;
1602 /** {@hide} */
1603 static final int IS_ROOT_NAMESPACE = 0x00000008;
1604 /** {@hide} */
1605 static final int HAS_BOUNDS = 0x00000010;
1606 /** {@hide} */
1607 static final int DRAWN = 0x00000020;
1608 /**
1609 * When this flag is set, this view is running an animation on behalf of its
1610 * children and should therefore not cancel invalidate requests, even if they
1611 * lie outside of this view's bounds.
1612 *
1613 * {@hide}
1614 */
1615 static final int DRAW_ANIMATION = 0x00000040;
1616 /** {@hide} */
1617 static final int SKIP_DRAW = 0x00000080;
1618 /** {@hide} */
1619 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1620 /** {@hide} */
1621 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1622 /** {@hide} */
1623 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1624 /** {@hide} */
1625 static final int MEASURED_DIMENSION_SET = 0x00000800;
1626 /** {@hide} */
1627 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001628 /** {@hide} */
1629 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630
1631 private static final int PRESSED = 0x00004000;
1632
1633 /** {@hide} */
1634 static final int DRAWING_CACHE_VALID = 0x00008000;
1635 /**
1636 * Flag used to indicate that this view should be drawn once more (and only once
1637 * more) after its animation has completed.
1638 * {@hide}
1639 */
1640 static final int ANIMATION_STARTED = 0x00010000;
1641
1642 private static final int SAVE_STATE_CALLED = 0x00020000;
1643
1644 /**
1645 * Indicates that the View returned true when onSetAlpha() was called and that
1646 * the alpha must be restored.
1647 * {@hide}
1648 */
1649 static final int ALPHA_SET = 0x00040000;
1650
1651 /**
1652 * Set by {@link #setScrollContainer(boolean)}.
1653 */
1654 static final int SCROLL_CONTAINER = 0x00080000;
1655
1656 /**
1657 * Set by {@link #setScrollContainer(boolean)}.
1658 */
1659 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1660
1661 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001662 * View flag indicating whether this view was invalidated (fully or partially.)
1663 *
1664 * @hide
1665 */
1666 static final int DIRTY = 0x00200000;
1667
1668 /**
1669 * View flag indicating whether this view was invalidated by an opaque
1670 * invalidate request.
1671 *
1672 * @hide
1673 */
1674 static final int DIRTY_OPAQUE = 0x00400000;
1675
1676 /**
1677 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1678 *
1679 * @hide
1680 */
1681 static final int DIRTY_MASK = 0x00600000;
1682
1683 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001684 * Indicates whether the background is opaque.
1685 *
1686 * @hide
1687 */
1688 static final int OPAQUE_BACKGROUND = 0x00800000;
1689
1690 /**
1691 * Indicates whether the scrollbars are opaque.
1692 *
1693 * @hide
1694 */
1695 static final int OPAQUE_SCROLLBARS = 0x01000000;
1696
1697 /**
1698 * Indicates whether the view is opaque.
1699 *
1700 * @hide
1701 */
1702 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001703
Adam Powelle14579b2009-12-16 18:39:52 -08001704 /**
1705 * Indicates a prepressed state;
1706 * the short time between ACTION_DOWN and recognizing
1707 * a 'real' press. Prepressed is used to recognize quick taps
1708 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001709 *
Adam Powelle14579b2009-12-16 18:39:52 -08001710 * @hide
1711 */
1712 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001713
Adam Powellc9fbaab2010-02-16 17:16:19 -08001714 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001715 * Indicates whether the view is temporarily detached.
1716 *
1717 * @hide
1718 */
1719 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001720
Adam Powell8568c3a2010-04-19 14:26:11 -07001721 /**
1722 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001723 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001724 * @hide
1725 */
1726 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001727
1728 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001729 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1730 * @hide
1731 */
1732 private static final int HOVERED = 0x10000000;
1733
1734 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001735 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1736 * for transform operations
1737 *
1738 * @hide
1739 */
Adam Powellf37df072010-09-17 16:22:49 -07001740 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001741
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001742 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001743 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001744
Chet Haasefd2b0022010-08-06 13:08:56 -07001745 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001746 * Indicates that this view was specifically invalidated, not just dirtied because some
1747 * child view was invalidated. The flag is used to determine when we need to recreate
1748 * a view's display list (as opposed to just returning a reference to its existing
1749 * display list).
1750 *
1751 * @hide
1752 */
1753 static final int INVALIDATED = 0x80000000;
1754
Christopher Tate3d4bf172011-03-28 16:16:46 -07001755 /* Masks for mPrivateFlags2 */
1756
1757 /**
1758 * Indicates that this view has reported that it can accept the current drag's content.
1759 * Cleared when the drag operation concludes.
1760 * @hide
1761 */
1762 static final int DRAG_CAN_ACCEPT = 0x00000001;
1763
1764 /**
1765 * Indicates that this view is currently directly under the drag location in a
1766 * drag-and-drop operation involving content that it can accept. Cleared when
1767 * the drag exits the view, or when the drag operation concludes.
1768 * @hide
1769 */
1770 static final int DRAG_HOVERED = 0x00000002;
1771
Cibu Johny86666632010-02-22 13:01:02 -08001772 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001773 * Horizontal layout direction of this view is from Left to Right.
1774 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001775 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001776 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001777
1778 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001779 * Horizontal layout direction of this view is from Right to Left.
1780 * Use with {@link #setLayoutDirection}.
1781 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001782 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001783
1784 /**
1785 * Horizontal layout direction of this view is inherited from its parent.
1786 * Use with {@link #setLayoutDirection}.
1787 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001788 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001789
1790 /**
1791 * Horizontal layout direction of this view is from deduced from the default language
1792 * script for the locale. Use with {@link #setLayoutDirection}.
1793 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001794 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001795
1796 /**
1797 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001798 * @hide
1799 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001800 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1801
1802 /**
1803 * Mask for use with private flags indicating bits used for horizontal layout direction.
1804 * @hide
1805 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001806 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001807
1808 /**
1809 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1810 * right-to-left direction.
1811 * @hide
1812 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001813 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001814
1815 /**
1816 * Indicates whether the view horizontal layout direction has been resolved.
1817 * @hide
1818 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001819 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001820
1821 /**
1822 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1823 * @hide
1824 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001825 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001826
1827 /*
1828 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1829 * flag value.
1830 * @hide
1831 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001832 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1833 LAYOUT_DIRECTION_LTR,
1834 LAYOUT_DIRECTION_RTL,
1835 LAYOUT_DIRECTION_INHERIT,
1836 LAYOUT_DIRECTION_LOCALE
1837 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001838
1839 /**
1840 * Default horizontal layout direction.
1841 * @hide
1842 */
1843 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001844
Adam Powell539ee872012-02-03 19:00:49 -08001845 /**
1846 * Indicates that the view is tracking some sort of transient state
1847 * that the app should not need to be aware of, but that the framework
1848 * should take special care to preserve.
1849 *
1850 * @hide
1851 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001852 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001853
1854
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001855 /**
1856 * Text direction is inherited thru {@link ViewGroup}
1857 */
1858 public static final int TEXT_DIRECTION_INHERIT = 0;
1859
1860 /**
1861 * Text direction is using "first strong algorithm". The first strong directional character
1862 * determines the paragraph direction. If there is no strong directional character, the
1863 * paragraph direction is the view's resolved layout direction.
1864 */
1865 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1866
1867 /**
1868 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1869 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1870 * If there are neither, the paragraph direction is the view's resolved layout direction.
1871 */
1872 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1873
1874 /**
1875 * Text direction is forced to LTR.
1876 */
1877 public static final int TEXT_DIRECTION_LTR = 3;
1878
1879 /**
1880 * Text direction is forced to RTL.
1881 */
1882 public static final int TEXT_DIRECTION_RTL = 4;
1883
1884 /**
1885 * Text direction is coming from the system Locale.
1886 */
1887 public static final int TEXT_DIRECTION_LOCALE = 5;
1888
1889 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001890 * Default text direction is inherited
1891 */
1892 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1893
1894 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001895 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1896 * @hide
1897 */
1898 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1899
1900 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001901 * Mask for use with private flags indicating bits used for text direction.
1902 * @hide
1903 */
1904 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1905
1906 /**
1907 * Array of text direction flags for mapping attribute "textDirection" to correct
1908 * flag value.
1909 * @hide
1910 */
1911 private static final int[] TEXT_DIRECTION_FLAGS = {
1912 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1913 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1914 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1915 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1916 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1917 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1918 };
1919
1920 /**
1921 * Indicates whether the view text direction has been resolved.
1922 * @hide
1923 */
1924 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1925
1926 /**
1927 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1928 * @hide
1929 */
1930 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1931
1932 /**
1933 * Mask for use with private flags indicating bits used for resolved text direction.
1934 * @hide
1935 */
1936 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1937
1938 /**
1939 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1940 * @hide
1941 */
1942 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1943 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1944
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001945 /*
1946 * Default text alignment. The text alignment of this View is inherited from its parent.
1947 * Use with {@link #setTextAlignment(int)}
1948 */
1949 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1950
1951 /**
1952 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1953 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1954 *
1955 * Use with {@link #setTextAlignment(int)}
1956 */
1957 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1958
1959 /**
1960 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1961 *
1962 * Use with {@link #setTextAlignment(int)}
1963 */
1964 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1965
1966 /**
1967 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1968 *
1969 * Use with {@link #setTextAlignment(int)}
1970 */
1971 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
1972
1973 /**
1974 * Center the paragraph, e.g. ALIGN_CENTER.
1975 *
1976 * Use with {@link #setTextAlignment(int)}
1977 */
1978 public static final int TEXT_ALIGNMENT_CENTER = 4;
1979
1980 /**
1981 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
1982 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
1983 *
1984 * Use with {@link #setTextAlignment(int)}
1985 */
1986 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
1987
1988 /**
1989 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
1990 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
1991 *
1992 * Use with {@link #setTextAlignment(int)}
1993 */
1994 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
1995
1996 /**
1997 * Default text alignment is inherited
1998 */
1999 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
2000
2001 /**
2002 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2003 * @hide
2004 */
2005 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
2006
2007 /**
2008 * Mask for use with private flags indicating bits used for text alignment.
2009 * @hide
2010 */
2011 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
2012
2013 /**
2014 * Array of text direction flags for mapping attribute "textAlignment" to correct
2015 * flag value.
2016 * @hide
2017 */
2018 private static final int[] TEXT_ALIGNMENT_FLAGS = {
2019 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
2020 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
2021 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
2022 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
2023 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
2024 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
2025 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
2026 };
2027
2028 /**
2029 * Indicates whether the view text alignment has been resolved.
2030 * @hide
2031 */
2032 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
2033
2034 /**
2035 * Bit shift to get the resolved text alignment.
2036 * @hide
2037 */
2038 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
2039
2040 /**
2041 * Mask for use with private flags indicating bits used for text alignment.
2042 * @hide
2043 */
2044 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2045
2046 /**
2047 * Indicates whether if the view text alignment has been resolved to gravity
2048 */
2049 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2050 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2051
Svetoslav Ganov42138042012-03-20 11:51:39 -07002052 // Accessiblity constants for mPrivateFlags2
2053
2054 /**
2055 * Shift for accessibility related bits in {@link #mPrivateFlags2}.
2056 */
2057 static final int IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
2058
2059 /**
2060 * Automatically determine whether a view is important for accessibility.
2061 */
2062 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2063
2064 /**
2065 * The view is important for accessibility.
2066 */
2067 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2068
2069 /**
2070 * The view is not important for accessibility.
2071 */
2072 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2073
2074 /**
2075 * The default whether the view is important for accessiblity.
2076 */
2077 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2078
2079 /**
2080 * Mask for obtainig the bits which specify how to determine
2081 * whether a view is important for accessibility.
2082 */
2083 static final int IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
2084 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
2085 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2086
2087 /**
2088 * Flag indicating whether a view has accessibility focus.
2089 */
2090 static final int ACCESSIBILITY_FOCUSED = 0x00000040 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2091
2092 /**
2093 * Flag indicating whether a view state for accessibility has changed.
2094 */
2095 static final int ACCESSIBILITY_STATE_CHANGED = 0x00000080 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002096
Christopher Tate3d4bf172011-03-28 16:16:46 -07002097 /* End of masks for mPrivateFlags2 */
2098
2099 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2100
Chet Haasedaf98e92011-01-10 14:10:36 -08002101 /**
Adam Powell637d3372010-08-25 14:37:03 -07002102 * Always allow a user to over-scroll this view, provided it is a
2103 * view that can scroll.
2104 *
2105 * @see #getOverScrollMode()
2106 * @see #setOverScrollMode(int)
2107 */
2108 public static final int OVER_SCROLL_ALWAYS = 0;
2109
2110 /**
2111 * Allow a user to over-scroll this view only if the content is large
2112 * enough to meaningfully scroll, provided it is a view that can scroll.
2113 *
2114 * @see #getOverScrollMode()
2115 * @see #setOverScrollMode(int)
2116 */
2117 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2118
2119 /**
2120 * Never allow a user to over-scroll this view.
2121 *
2122 * @see #getOverScrollMode()
2123 * @see #setOverScrollMode(int)
2124 */
2125 public static final int OVER_SCROLL_NEVER = 2;
2126
2127 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002128 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2129 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002130 *
Joe Malin32736f02011-01-19 16:14:20 -08002131 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002132 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002133 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002134
2135 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002136 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2137 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002138 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002139 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002140 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002141 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002142 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002143 *
Joe Malin32736f02011-01-19 16:14:20 -08002144 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002145 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002146 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2147
2148 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002149 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2150 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002151 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002152 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002153 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2154 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2155 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002156 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002157 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2158 * window flags) for displaying content using every last pixel on the display.
2159 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002160 * <p>There is a limitation: because navigation controls are so important, the least user
2161 * interaction will cause them to reappear immediately. When this happens, both
2162 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2163 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002164 *
2165 * @see #setSystemUiVisibility(int)
2166 */
2167 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2168
2169 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002170 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2171 * into the normal fullscreen mode so that its content can take over the screen
2172 * while still allowing the user to interact with the application.
2173 *
2174 * <p>This has the same visual effect as
2175 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2176 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2177 * meaning that non-critical screen decorations (such as the status bar) will be
2178 * hidden while the user is in the View's window, focusing the experience on
2179 * that content. Unlike the window flag, if you are using ActionBar in
2180 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2181 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2182 * hide the action bar.
2183 *
2184 * <p>This approach to going fullscreen is best used over the window flag when
2185 * it is a transient state -- that is, the application does this at certain
2186 * points in its user interaction where it wants to allow the user to focus
2187 * on content, but not as a continuous state. For situations where the application
2188 * would like to simply stay full screen the entire time (such as a game that
2189 * wants to take over the screen), the
2190 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2191 * is usually a better approach. The state set here will be removed by the system
2192 * in various situations (such as the user moving to another application) like
2193 * the other system UI states.
2194 *
2195 * <p>When using this flag, the application should provide some easy facility
2196 * for the user to go out of it. A common example would be in an e-book
2197 * reader, where tapping on the screen brings back whatever screen and UI
2198 * decorations that had been hidden while the user was immersed in reading
2199 * the book.
2200 *
2201 * @see #setSystemUiVisibility(int)
2202 */
2203 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2204
2205 /**
2206 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2207 * flags, we would like a stable view of the content insets given to
2208 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2209 * will always represent the worst case that the application can expect
2210 * as a continue state. In practice this means with any of system bar,
2211 * nav bar, and status bar shown, but not the space that would be needed
2212 * for an input method.
2213 *
2214 * <p>If you are using ActionBar in
2215 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2216 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2217 * insets it adds to those given to the application.
2218 */
2219 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2220
2221 /**
2222 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2223 * to be layed out as if it has requested
2224 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2225 * allows it to avoid artifacts when switching in and out of that mode, at
2226 * the expense that some of its user interface may be covered by screen
2227 * decorations when they are shown. You can perform layout of your inner
2228 * UI elements to account for the navagation system UI through the
2229 * {@link #fitSystemWindows(Rect)} method.
2230 */
2231 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2232
2233 /**
2234 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2235 * to be layed out as if it has requested
2236 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2237 * allows it to avoid artifacts when switching in and out of that mode, at
2238 * the expense that some of its user interface may be covered by screen
2239 * decorations when they are shown. You can perform layout of your inner
2240 * UI elements to account for non-fullscreen system UI through the
2241 * {@link #fitSystemWindows(Rect)} method.
2242 */
2243 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2244
2245 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002246 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2247 */
2248 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2249
2250 /**
2251 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2252 */
2253 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002254
2255 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002256 * @hide
2257 *
2258 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2259 * out of the public fields to keep the undefined bits out of the developer's way.
2260 *
2261 * Flag to make the status bar not expandable. Unless you also
2262 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2263 */
2264 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2265
2266 /**
2267 * @hide
2268 *
2269 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2270 * out of the public fields to keep the undefined bits out of the developer's way.
2271 *
2272 * Flag to hide notification icons and scrolling ticker text.
2273 */
2274 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2275
2276 /**
2277 * @hide
2278 *
2279 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2280 * out of the public fields to keep the undefined bits out of the developer's way.
2281 *
2282 * Flag to disable incoming notification alerts. This will not block
2283 * icons, but it will block sound, vibrating and other visual or aural notifications.
2284 */
2285 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2286
2287 /**
2288 * @hide
2289 *
2290 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2291 * out of the public fields to keep the undefined bits out of the developer's way.
2292 *
2293 * Flag to hide only the scrolling ticker. Note that
2294 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2295 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2296 */
2297 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2298
2299 /**
2300 * @hide
2301 *
2302 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2303 * out of the public fields to keep the undefined bits out of the developer's way.
2304 *
2305 * Flag to hide the center system info area.
2306 */
2307 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2308
2309 /**
2310 * @hide
2311 *
2312 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2313 * out of the public fields to keep the undefined bits out of the developer's way.
2314 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002315 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002316 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2317 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002318 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002319
2320 /**
2321 * @hide
2322 *
2323 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2324 * out of the public fields to keep the undefined bits out of the developer's way.
2325 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002326 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002327 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2328 */
2329 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2330
2331 /**
2332 * @hide
2333 *
2334 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2335 * out of the public fields to keep the undefined bits out of the developer's way.
2336 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002337 * Flag to hide only the clock. You might use this if your activity has
2338 * its own clock making the status bar's clock redundant.
2339 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002340 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2341
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002342 /**
2343 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002344 *
2345 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2346 * out of the public fields to keep the undefined bits out of the developer's way.
2347 *
2348 * Flag to hide only the recent apps button. Don't use this
2349 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2350 */
2351 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2352
2353 /**
2354 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002355 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002356 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002357
2358 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002359 * These are the system UI flags that can be cleared by events outside
2360 * of an application. Currently this is just the ability to tap on the
2361 * screen while hiding the navigation bar to have it return.
2362 * @hide
2363 */
2364 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002365 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2366 | SYSTEM_UI_FLAG_FULLSCREEN;
2367
2368 /**
2369 * Flags that can impact the layout in relation to system UI.
2370 */
2371 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2372 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2373 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002374
2375 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002376 * Find views that render the specified text.
2377 *
2378 * @see #findViewsWithText(ArrayList, CharSequence, int)
2379 */
2380 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2381
2382 /**
2383 * Find find views that contain the specified content description.
2384 *
2385 * @see #findViewsWithText(ArrayList, CharSequence, int)
2386 */
2387 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2388
2389 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002390 * Find views that contain {@link AccessibilityNodeProvider}. Such
2391 * a View is a root of virtual view hierarchy and may contain the searched
2392 * text. If this flag is set Views with providers are automatically
2393 * added and it is a responsibility of the client to call the APIs of
2394 * the provider to determine whether the virtual tree rooted at this View
2395 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2396 * represeting the virtual views with this text.
2397 *
2398 * @see #findViewsWithText(ArrayList, CharSequence, int)
2399 *
2400 * @hide
2401 */
2402 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2403
2404 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002405 * Indicates that the screen has changed state and is now off.
2406 *
2407 * @see #onScreenStateChanged(int)
2408 */
2409 public static final int SCREEN_STATE_OFF = 0x0;
2410
2411 /**
2412 * Indicates that the screen has changed state and is now on.
2413 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002414 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002415 */
2416 public static final int SCREEN_STATE_ON = 0x1;
2417
2418 /**
Adam Powell637d3372010-08-25 14:37:03 -07002419 * Controls the over-scroll mode for this view.
2420 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2421 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2422 * and {@link #OVER_SCROLL_NEVER}.
2423 */
2424 private int mOverScrollMode;
2425
2426 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 * The parent this view is attached to.
2428 * {@hide}
2429 *
2430 * @see #getParent()
2431 */
2432 protected ViewParent mParent;
2433
2434 /**
2435 * {@hide}
2436 */
2437 AttachInfo mAttachInfo;
2438
2439 /**
2440 * {@hide}
2441 */
Romain Guy809a7f62009-05-14 15:44:42 -07002442 @ViewDebug.ExportedProperty(flagMapping = {
2443 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2444 name = "FORCE_LAYOUT"),
2445 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2446 name = "LAYOUT_REQUIRED"),
2447 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002448 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002449 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2450 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2451 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2452 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2453 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002454 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002455 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456
2457 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002458 * This view's request for the visibility of the status bar.
2459 * @hide
2460 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002461 @ViewDebug.ExportedProperty(flagMapping = {
2462 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2463 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2464 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2465 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2466 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2467 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2468 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2469 equals = SYSTEM_UI_FLAG_VISIBLE,
2470 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2471 })
Joe Onorato664644d2011-01-23 17:53:23 -08002472 int mSystemUiVisibility;
2473
2474 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002475 * Reference count for transient state.
2476 * @see #setHasTransientState(boolean)
2477 */
2478 int mTransientStateCount = 0;
2479
2480 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 * Count of how many windows this view has been attached to.
2482 */
2483 int mWindowAttachCount;
2484
2485 /**
2486 * The layout parameters associated with this view and used by the parent
2487 * {@link android.view.ViewGroup} to determine how this view should be
2488 * laid out.
2489 * {@hide}
2490 */
2491 protected ViewGroup.LayoutParams mLayoutParams;
2492
2493 /**
2494 * The view flags hold various views states.
2495 * {@hide}
2496 */
2497 @ViewDebug.ExportedProperty
2498 int mViewFlags;
2499
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002500 static class TransformationInfo {
2501 /**
2502 * The transform matrix for the View. This transform is calculated internally
2503 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2504 * is used by default. Do *not* use this variable directly; instead call
2505 * getMatrix(), which will automatically recalculate the matrix if necessary
2506 * to get the correct matrix based on the latest rotation and scale properties.
2507 */
2508 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002509
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002510 /**
2511 * The transform matrix for the View. This transform is calculated internally
2512 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2513 * is used by default. Do *not* use this variable directly; instead call
2514 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2515 * to get the correct matrix based on the latest rotation and scale properties.
2516 */
2517 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002518
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002519 /**
2520 * An internal variable that tracks whether we need to recalculate the
2521 * transform matrix, based on whether the rotation or scaleX/Y properties
2522 * have changed since the matrix was last calculated.
2523 */
2524 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002525
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002526 /**
2527 * An internal variable that tracks whether we need to recalculate the
2528 * transform matrix, based on whether the rotation or scaleX/Y properties
2529 * have changed since the matrix was last calculated.
2530 */
2531 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002532
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002533 /**
2534 * A variable that tracks whether we need to recalculate the
2535 * transform matrix, based on whether the rotation or scaleX/Y properties
2536 * have changed since the matrix was last calculated. This variable
2537 * is only valid after a call to updateMatrix() or to a function that
2538 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2539 */
2540 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002541
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002542 /**
2543 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2544 */
2545 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002546
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002547 /**
2548 * This matrix is used when computing the matrix for 3D rotations.
2549 */
2550 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002551
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002552 /**
2553 * These prev values are used to recalculate a centered pivot point when necessary. The
2554 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2555 * set), so thes values are only used then as well.
2556 */
2557 private int mPrevWidth = -1;
2558 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002559
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002560 /**
2561 * The degrees rotation around the vertical axis through the pivot point.
2562 */
2563 @ViewDebug.ExportedProperty
2564 float mRotationY = 0f;
2565
2566 /**
2567 * The degrees rotation around the horizontal axis through the pivot point.
2568 */
2569 @ViewDebug.ExportedProperty
2570 float mRotationX = 0f;
2571
2572 /**
2573 * The degrees rotation around the pivot point.
2574 */
2575 @ViewDebug.ExportedProperty
2576 float mRotation = 0f;
2577
2578 /**
2579 * The amount of translation of the object away from its left property (post-layout).
2580 */
2581 @ViewDebug.ExportedProperty
2582 float mTranslationX = 0f;
2583
2584 /**
2585 * The amount of translation of the object away from its top property (post-layout).
2586 */
2587 @ViewDebug.ExportedProperty
2588 float mTranslationY = 0f;
2589
2590 /**
2591 * The amount of scale in the x direction around the pivot point. A
2592 * value of 1 means no scaling is applied.
2593 */
2594 @ViewDebug.ExportedProperty
2595 float mScaleX = 1f;
2596
2597 /**
2598 * The amount of scale in the y direction around the pivot point. A
2599 * value of 1 means no scaling is applied.
2600 */
2601 @ViewDebug.ExportedProperty
2602 float mScaleY = 1f;
2603
2604 /**
Chet Haasea33de552012-02-03 16:28:24 -08002605 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002606 */
2607 @ViewDebug.ExportedProperty
2608 float mPivotX = 0f;
2609
2610 /**
Chet Haasea33de552012-02-03 16:28:24 -08002611 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002612 */
2613 @ViewDebug.ExportedProperty
2614 float mPivotY = 0f;
2615
2616 /**
2617 * The opacity of the View. This is a value from 0 to 1, where 0 means
2618 * completely transparent and 1 means completely opaque.
2619 */
2620 @ViewDebug.ExportedProperty
2621 float mAlpha = 1f;
2622 }
2623
2624 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002625
Joe Malin32736f02011-01-19 16:14:20 -08002626 private boolean mLastIsOpaque;
2627
Chet Haasefd2b0022010-08-06 13:08:56 -07002628 /**
2629 * Convenience value to check for float values that are close enough to zero to be considered
2630 * zero.
2631 */
Romain Guy2542d192010-08-18 11:47:12 -07002632 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002633
2634 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635 * The distance in pixels from the left edge of this view's parent
2636 * to the left edge of this view.
2637 * {@hide}
2638 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002639 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 protected int mLeft;
2641 /**
2642 * The distance in pixels from the left edge of this view's parent
2643 * to the right edge of this view.
2644 * {@hide}
2645 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002646 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 protected int mRight;
2648 /**
2649 * The distance in pixels from the top edge of this view's parent
2650 * to the top edge of this view.
2651 * {@hide}
2652 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002653 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 protected int mTop;
2655 /**
2656 * The distance in pixels from the top edge of this view's parent
2657 * to the bottom edge of this view.
2658 * {@hide}
2659 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002660 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 protected int mBottom;
2662
2663 /**
2664 * The offset, in pixels, by which the content of this view is scrolled
2665 * horizontally.
2666 * {@hide}
2667 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002668 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 protected int mScrollX;
2670 /**
2671 * The offset, in pixels, by which the content of this view is scrolled
2672 * vertically.
2673 * {@hide}
2674 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002675 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 protected int mScrollY;
2677
2678 /**
2679 * The left padding in pixels, that is the distance in pixels between the
2680 * left edge of this view and the left edge of its content.
2681 * {@hide}
2682 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002683 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 protected int mPaddingLeft;
2685 /**
2686 * The right padding in pixels, that is the distance in pixels between the
2687 * right edge of this view and the right edge of its content.
2688 * {@hide}
2689 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002690 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 protected int mPaddingRight;
2692 /**
2693 * The top padding in pixels, that is the distance in pixels between the
2694 * top edge of this view and the top edge of its content.
2695 * {@hide}
2696 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002697 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 protected int mPaddingTop;
2699 /**
2700 * The bottom padding in pixels, that is the distance in pixels between the
2701 * bottom edge of this view and the bottom edge of its content.
2702 * {@hide}
2703 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002704 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 protected int mPaddingBottom;
2706
2707 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002708 * The layout insets in pixels, that is the distance in pixels between the
2709 * visible edges of this view its bounds.
2710 */
2711 private Insets mLayoutInsets;
2712
2713 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002714 * Briefly describes the view and is primarily used for accessibility support.
2715 */
2716 private CharSequence mContentDescription;
2717
2718 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002720 *
2721 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002723 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002724 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725
2726 /**
2727 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002728 *
2729 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002731 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002732 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002734 /**
Adam Powell20232d02010-12-08 21:08:53 -08002735 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002736 *
2737 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002738 */
2739 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002740 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002741
2742 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002743 * Cache if the user padding is relative.
2744 *
2745 */
2746 @ViewDebug.ExportedProperty(category = "padding")
2747 boolean mUserPaddingRelative;
2748
2749 /**
2750 * Cache the paddingStart set by the user to append to the scrollbar's size.
2751 *
2752 */
2753 @ViewDebug.ExportedProperty(category = "padding")
2754 int mUserPaddingStart;
2755
2756 /**
2757 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2758 *
2759 */
2760 @ViewDebug.ExportedProperty(category = "padding")
2761 int mUserPaddingEnd;
2762
2763 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002764 * @hide
2765 */
2766 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2767 /**
2768 * @hide
2769 */
2770 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771
Philip Milne6c8ea062012-04-03 17:38:43 -07002772 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773
2774 private int mBackgroundResource;
2775 private boolean mBackgroundSizeChanged;
2776
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002777 static class ListenerInfo {
2778 /**
2779 * Listener used to dispatch focus change events.
2780 * This field should be made private, so it is hidden from the SDK.
2781 * {@hide}
2782 */
2783 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002785 /**
2786 * Listeners for layout change events.
2787 */
2788 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002789
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002790 /**
2791 * Listeners for attach events.
2792 */
2793 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002794
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002795 /**
2796 * Listener used to dispatch click events.
2797 * This field should be made private, so it is hidden from the SDK.
2798 * {@hide}
2799 */
2800 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002802 /**
2803 * Listener used to dispatch long click events.
2804 * This field should be made private, so it is hidden from the SDK.
2805 * {@hide}
2806 */
2807 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002809 /**
2810 * Listener used to build the context menu.
2811 * This field should be made private, so it is hidden from the SDK.
2812 * {@hide}
2813 */
2814 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002816 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002818 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002820 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002821
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002822 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002823
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002824 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002825
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002826 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2827 }
2828
2829 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 /**
2832 * The application environment this view lives in.
2833 * This field should be made private, so it is hidden from the SDK.
2834 * {@hide}
2835 */
2836 protected Context mContext;
2837
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002838 private final Resources mResources;
2839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 private ScrollabilityCache mScrollCache;
2841
2842 private int[] mDrawableState = null;
2843
Romain Guy0211a0a2011-02-14 16:34:59 -08002844 /**
2845 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002846 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002847 * @hide
2848 */
2849 public boolean mCachingFailed;
2850
Romain Guy02890fd2010-08-06 17:58:44 -07002851 private Bitmap mDrawingCache;
2852 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002853 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002854 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855
2856 /**
2857 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2858 * the user may specify which view to go to next.
2859 */
2860 private int mNextFocusLeftId = View.NO_ID;
2861
2862 /**
2863 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2864 * the user may specify which view to go to next.
2865 */
2866 private int mNextFocusRightId = View.NO_ID;
2867
2868 /**
2869 * When this view has focus and the next focus is {@link #FOCUS_UP},
2870 * the user may specify which view to go to next.
2871 */
2872 private int mNextFocusUpId = View.NO_ID;
2873
2874 /**
2875 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2876 * the user may specify which view to go to next.
2877 */
2878 private int mNextFocusDownId = View.NO_ID;
2879
Jeff Brown4e6319b2010-12-13 10:36:51 -08002880 /**
2881 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2882 * the user may specify which view to go to next.
2883 */
2884 int mNextFocusForwardId = View.NO_ID;
2885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002887 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002888 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002889 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 private UnsetPressedState mUnsetPressedState;
2892
2893 /**
2894 * Whether the long press's action has been invoked. The tap's action is invoked on the
2895 * up event while a long press is invoked as soon as the long press duration is reached, so
2896 * a long press could be performed before the tap is checked, in which case the tap's action
2897 * should not be invoked.
2898 */
2899 private boolean mHasPerformedLongPress;
2900
2901 /**
2902 * The minimum height of the view. We'll try our best to have the height
2903 * of this view to at least this amount.
2904 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002905 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 private int mMinHeight;
2907
2908 /**
2909 * The minimum width of the view. We'll try our best to have the width
2910 * of this view to at least this amount.
2911 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002912 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 private int mMinWidth;
2914
2915 /**
2916 * The delegate to handle touch events that are physically in this view
2917 * but should be handled by another view.
2918 */
2919 private TouchDelegate mTouchDelegate = null;
2920
2921 /**
2922 * Solid color to use as a background when creating the drawing cache. Enables
2923 * the cache to use 16 bit bitmaps instead of 32 bit.
2924 */
2925 private int mDrawingCacheBackgroundColor = 0;
2926
2927 /**
2928 * Special tree observer used when mAttachInfo is null.
2929 */
2930 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002931
Adam Powelle14579b2009-12-16 18:39:52 -08002932 /**
2933 * Cache the touch slop from the context that created the view.
2934 */
2935 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002938 * Object that handles automatic animation of view properties.
2939 */
2940 private ViewPropertyAnimator mAnimator = null;
2941
2942 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002943 * Flag indicating that a drag can cross window boundaries. When
2944 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2945 * with this flag set, all visible applications will be able to participate
2946 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002947 *
2948 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002949 */
2950 public static final int DRAG_FLAG_GLOBAL = 1;
2951
2952 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002953 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2954 */
2955 private float mVerticalScrollFactor;
2956
2957 /**
Adam Powell20232d02010-12-08 21:08:53 -08002958 * Position of the vertical scroll bar.
2959 */
2960 private int mVerticalScrollbarPosition;
2961
2962 /**
2963 * Position the scroll bar at the default position as determined by the system.
2964 */
2965 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2966
2967 /**
2968 * Position the scroll bar along the left edge.
2969 */
2970 public static final int SCROLLBAR_POSITION_LEFT = 1;
2971
2972 /**
2973 * Position the scroll bar along the right edge.
2974 */
2975 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2976
2977 /**
Romain Guy171c5922011-01-06 10:04:23 -08002978 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002979 *
2980 * @see #getLayerType()
2981 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002982 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002983 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002984 */
2985 public static final int LAYER_TYPE_NONE = 0;
2986
2987 /**
2988 * <p>Indicates that the view has a software layer. A software layer is backed
2989 * by a bitmap and causes the view to be rendered using Android's software
2990 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002991 *
Romain Guy171c5922011-01-06 10:04:23 -08002992 * <p>Software layers have various usages:</p>
2993 * <p>When the application is not using hardware acceleration, a software layer
2994 * is useful to apply a specific color filter and/or blending mode and/or
2995 * translucency to a view and all its children.</p>
2996 * <p>When the application is using hardware acceleration, a software layer
2997 * is useful to render drawing primitives not supported by the hardware
2998 * accelerated pipeline. It can also be used to cache a complex view tree
2999 * into a texture and reduce the complexity of drawing operations. For instance,
3000 * when animating a complex view tree with a translation, a software layer can
3001 * be used to render the view tree only once.</p>
3002 * <p>Software layers should be avoided when the affected view tree updates
3003 * often. Every update will require to re-render the software layer, which can
3004 * potentially be slow (particularly when hardware acceleration is turned on
3005 * since the layer will have to be uploaded into a hardware texture after every
3006 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003007 *
3008 * @see #getLayerType()
3009 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003010 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003011 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003012 */
3013 public static final int LAYER_TYPE_SOFTWARE = 1;
3014
3015 /**
3016 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3017 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3018 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3019 * rendering pipeline, but only if hardware acceleration is turned on for the
3020 * view hierarchy. When hardware acceleration is turned off, hardware layers
3021 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003022 *
Romain Guy171c5922011-01-06 10:04:23 -08003023 * <p>A hardware layer is useful to apply a specific color filter and/or
3024 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003025 * <p>A hardware layer can be used to cache a complex view tree into a
3026 * texture and reduce the complexity of drawing operations. For instance,
3027 * when animating a complex view tree with a translation, a hardware layer can
3028 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003029 * <p>A hardware layer can also be used to increase the rendering quality when
3030 * rotation transformations are applied on a view. It can also be used to
3031 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003032 *
3033 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003034 * @see #setLayerType(int, android.graphics.Paint)
3035 * @see #LAYER_TYPE_NONE
3036 * @see #LAYER_TYPE_SOFTWARE
3037 */
3038 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003039
Romain Guy3aaff3a2011-01-12 14:18:47 -08003040 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3041 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3042 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3043 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3044 })
Romain Guy171c5922011-01-06 10:04:23 -08003045 int mLayerType = LAYER_TYPE_NONE;
3046 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003047 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003048
3049 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003050 * Set to true when the view is sending hover accessibility events because it
3051 * is the innermost hovered view.
3052 */
3053 private boolean mSendingHoverAccessibilityEvents;
3054
3055 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 * Simple constructor to use when creating a view from code.
3057 *
3058 * @param context The Context the view is running in, through which it can
3059 * access the current theme, resources, etc.
3060 */
3061 public View(Context context) {
3062 mContext = context;
3063 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003064 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003065 // Set layout and text direction defaults
3066 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003067 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003068 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
3069 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003070 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003071 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003072 mUserPaddingStart = -1;
3073 mUserPaddingEnd = -1;
3074 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003075 }
3076
3077 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003078 * Delegate for injecting accessiblity functionality.
3079 */
3080 AccessibilityDelegate mAccessibilityDelegate;
3081
3082 /**
3083 * Consistency verifier for debugging purposes.
3084 * @hide
3085 */
3086 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3087 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3088 new InputEventConsistencyVerifier(this, 0) : null;
3089
3090 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003091 * Constructor that is called when inflating a view from XML. This is called
3092 * when a view is being constructed from an XML file, supplying attributes
3093 * that were specified in the XML file. This version uses a default style of
3094 * 0, so the only attribute values applied are those in the Context's Theme
3095 * and the given AttributeSet.
3096 *
3097 * <p>
3098 * The method onFinishInflate() will be called after all children have been
3099 * added.
3100 *
3101 * @param context The Context the view is running in, through which it can
3102 * access the current theme, resources, etc.
3103 * @param attrs The attributes of the XML tag that is inflating the view.
3104 * @see #View(Context, AttributeSet, int)
3105 */
3106 public View(Context context, AttributeSet attrs) {
3107 this(context, attrs, 0);
3108 }
3109
3110 /**
3111 * Perform inflation from XML and apply a class-specific base style. This
3112 * constructor of View allows subclasses to use their own base style when
3113 * they are inflating. For example, a Button class's constructor would call
3114 * this version of the super class constructor and supply
3115 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3116 * the theme's button style to modify all of the base view attributes (in
3117 * particular its background) as well as the Button class's attributes.
3118 *
3119 * @param context The Context the view is running in, through which it can
3120 * access the current theme, resources, etc.
3121 * @param attrs The attributes of the XML tag that is inflating the view.
3122 * @param defStyle The default style to apply to this view. If 0, no style
3123 * will be applied (beyond what is included in the theme). This may
3124 * either be an attribute resource, whose value will be retrieved
3125 * from the current theme, or an explicit style resource.
3126 * @see #View(Context, AttributeSet)
3127 */
3128 public View(Context context, AttributeSet attrs, int defStyle) {
3129 this(context);
3130
3131 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3132 defStyle, 0);
3133
3134 Drawable background = null;
3135
3136 int leftPadding = -1;
3137 int topPadding = -1;
3138 int rightPadding = -1;
3139 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003140 int startPadding = -1;
3141 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142
3143 int padding = -1;
3144
3145 int viewFlagValues = 0;
3146 int viewFlagMasks = 0;
3147
3148 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 int x = 0;
3151 int y = 0;
3152
Chet Haase73066682010-11-29 15:55:32 -08003153 float tx = 0;
3154 float ty = 0;
3155 float rotation = 0;
3156 float rotationX = 0;
3157 float rotationY = 0;
3158 float sx = 1f;
3159 float sy = 1f;
3160 boolean transformSet = false;
3161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3163
Adam Powell637d3372010-08-25 14:37:03 -07003164 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165 final int N = a.getIndexCount();
3166 for (int i = 0; i < N; i++) {
3167 int attr = a.getIndex(i);
3168 switch (attr) {
3169 case com.android.internal.R.styleable.View_background:
3170 background = a.getDrawable(attr);
3171 break;
3172 case com.android.internal.R.styleable.View_padding:
3173 padding = a.getDimensionPixelSize(attr, -1);
3174 break;
3175 case com.android.internal.R.styleable.View_paddingLeft:
3176 leftPadding = a.getDimensionPixelSize(attr, -1);
3177 break;
3178 case com.android.internal.R.styleable.View_paddingTop:
3179 topPadding = a.getDimensionPixelSize(attr, -1);
3180 break;
3181 case com.android.internal.R.styleable.View_paddingRight:
3182 rightPadding = a.getDimensionPixelSize(attr, -1);
3183 break;
3184 case com.android.internal.R.styleable.View_paddingBottom:
3185 bottomPadding = a.getDimensionPixelSize(attr, -1);
3186 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003187 case com.android.internal.R.styleable.View_paddingStart:
3188 startPadding = a.getDimensionPixelSize(attr, -1);
3189 break;
3190 case com.android.internal.R.styleable.View_paddingEnd:
3191 endPadding = a.getDimensionPixelSize(attr, -1);
3192 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 case com.android.internal.R.styleable.View_scrollX:
3194 x = a.getDimensionPixelOffset(attr, 0);
3195 break;
3196 case com.android.internal.R.styleable.View_scrollY:
3197 y = a.getDimensionPixelOffset(attr, 0);
3198 break;
Chet Haase73066682010-11-29 15:55:32 -08003199 case com.android.internal.R.styleable.View_alpha:
3200 setAlpha(a.getFloat(attr, 1f));
3201 break;
3202 case com.android.internal.R.styleable.View_transformPivotX:
3203 setPivotX(a.getDimensionPixelOffset(attr, 0));
3204 break;
3205 case com.android.internal.R.styleable.View_transformPivotY:
3206 setPivotY(a.getDimensionPixelOffset(attr, 0));
3207 break;
3208 case com.android.internal.R.styleable.View_translationX:
3209 tx = a.getDimensionPixelOffset(attr, 0);
3210 transformSet = true;
3211 break;
3212 case com.android.internal.R.styleable.View_translationY:
3213 ty = a.getDimensionPixelOffset(attr, 0);
3214 transformSet = true;
3215 break;
3216 case com.android.internal.R.styleable.View_rotation:
3217 rotation = a.getFloat(attr, 0);
3218 transformSet = true;
3219 break;
3220 case com.android.internal.R.styleable.View_rotationX:
3221 rotationX = a.getFloat(attr, 0);
3222 transformSet = true;
3223 break;
3224 case com.android.internal.R.styleable.View_rotationY:
3225 rotationY = a.getFloat(attr, 0);
3226 transformSet = true;
3227 break;
3228 case com.android.internal.R.styleable.View_scaleX:
3229 sx = a.getFloat(attr, 1f);
3230 transformSet = true;
3231 break;
3232 case com.android.internal.R.styleable.View_scaleY:
3233 sy = a.getFloat(attr, 1f);
3234 transformSet = true;
3235 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003236 case com.android.internal.R.styleable.View_id:
3237 mID = a.getResourceId(attr, NO_ID);
3238 break;
3239 case com.android.internal.R.styleable.View_tag:
3240 mTag = a.getText(attr);
3241 break;
3242 case com.android.internal.R.styleable.View_fitsSystemWindows:
3243 if (a.getBoolean(attr, false)) {
3244 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3245 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3246 }
3247 break;
3248 case com.android.internal.R.styleable.View_focusable:
3249 if (a.getBoolean(attr, false)) {
3250 viewFlagValues |= FOCUSABLE;
3251 viewFlagMasks |= FOCUSABLE_MASK;
3252 }
3253 break;
3254 case com.android.internal.R.styleable.View_focusableInTouchMode:
3255 if (a.getBoolean(attr, false)) {
3256 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3257 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3258 }
3259 break;
3260 case com.android.internal.R.styleable.View_clickable:
3261 if (a.getBoolean(attr, false)) {
3262 viewFlagValues |= CLICKABLE;
3263 viewFlagMasks |= CLICKABLE;
3264 }
3265 break;
3266 case com.android.internal.R.styleable.View_longClickable:
3267 if (a.getBoolean(attr, false)) {
3268 viewFlagValues |= LONG_CLICKABLE;
3269 viewFlagMasks |= LONG_CLICKABLE;
3270 }
3271 break;
3272 case com.android.internal.R.styleable.View_saveEnabled:
3273 if (!a.getBoolean(attr, true)) {
3274 viewFlagValues |= SAVE_DISABLED;
3275 viewFlagMasks |= SAVE_DISABLED_MASK;
3276 }
3277 break;
3278 case com.android.internal.R.styleable.View_duplicateParentState:
3279 if (a.getBoolean(attr, false)) {
3280 viewFlagValues |= DUPLICATE_PARENT_STATE;
3281 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3282 }
3283 break;
3284 case com.android.internal.R.styleable.View_visibility:
3285 final int visibility = a.getInt(attr, 0);
3286 if (visibility != 0) {
3287 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3288 viewFlagMasks |= VISIBILITY_MASK;
3289 }
3290 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003291 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003292 // Clear any layout direction flags (included resolved bits) already set
3293 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3294 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003295 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003296 final int value = (layoutDirection != -1) ?
3297 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3298 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003299 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 case com.android.internal.R.styleable.View_drawingCacheQuality:
3301 final int cacheQuality = a.getInt(attr, 0);
3302 if (cacheQuality != 0) {
3303 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3304 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3305 }
3306 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003307 case com.android.internal.R.styleable.View_contentDescription:
3308 mContentDescription = a.getString(attr);
3309 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3311 if (!a.getBoolean(attr, true)) {
3312 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3313 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3314 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003315 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3317 if (!a.getBoolean(attr, true)) {
3318 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3319 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3320 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003321 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 case R.styleable.View_scrollbars:
3323 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3324 if (scrollbars != SCROLLBARS_NONE) {
3325 viewFlagValues |= scrollbars;
3326 viewFlagMasks |= SCROLLBARS_MASK;
3327 initializeScrollbars(a);
3328 }
3329 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003330 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003331 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003332 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3333 // Ignore the attribute starting with ICS
3334 break;
3335 }
3336 // With builds < ICS, fall through and apply fading edges
3337 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3339 if (fadingEdge != FADING_EDGE_NONE) {
3340 viewFlagValues |= fadingEdge;
3341 viewFlagMasks |= FADING_EDGE_MASK;
3342 initializeFadingEdge(a);
3343 }
3344 break;
3345 case R.styleable.View_scrollbarStyle:
3346 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3347 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3348 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3349 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3350 }
3351 break;
3352 case R.styleable.View_isScrollContainer:
3353 setScrollContainer = true;
3354 if (a.getBoolean(attr, false)) {
3355 setScrollContainer(true);
3356 }
3357 break;
3358 case com.android.internal.R.styleable.View_keepScreenOn:
3359 if (a.getBoolean(attr, false)) {
3360 viewFlagValues |= KEEP_SCREEN_ON;
3361 viewFlagMasks |= KEEP_SCREEN_ON;
3362 }
3363 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003364 case R.styleable.View_filterTouchesWhenObscured:
3365 if (a.getBoolean(attr, false)) {
3366 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3367 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3368 }
3369 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 case R.styleable.View_nextFocusLeft:
3371 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3372 break;
3373 case R.styleable.View_nextFocusRight:
3374 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3375 break;
3376 case R.styleable.View_nextFocusUp:
3377 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3378 break;
3379 case R.styleable.View_nextFocusDown:
3380 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3381 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003382 case R.styleable.View_nextFocusForward:
3383 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3384 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003385 case R.styleable.View_minWidth:
3386 mMinWidth = a.getDimensionPixelSize(attr, 0);
3387 break;
3388 case R.styleable.View_minHeight:
3389 mMinHeight = a.getDimensionPixelSize(attr, 0);
3390 break;
Romain Guy9a817362009-05-01 10:57:14 -07003391 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003392 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003393 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003394 + "be used within a restricted context");
3395 }
3396
Romain Guy9a817362009-05-01 10:57:14 -07003397 final String handlerName = a.getString(attr);
3398 if (handlerName != null) {
3399 setOnClickListener(new OnClickListener() {
3400 private Method mHandler;
3401
3402 public void onClick(View v) {
3403 if (mHandler == null) {
3404 try {
3405 mHandler = getContext().getClass().getMethod(handlerName,
3406 View.class);
3407 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003408 int id = getId();
3409 String idText = id == NO_ID ? "" : " with id '"
3410 + getContext().getResources().getResourceEntryName(
3411 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003412 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003413 handlerName + "(View) in the activity "
3414 + getContext().getClass() + " for onClick handler"
3415 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003416 }
3417 }
3418
3419 try {
3420 mHandler.invoke(getContext(), View.this);
3421 } catch (IllegalAccessException e) {
3422 throw new IllegalStateException("Could not execute non "
3423 + "public method of the activity", e);
3424 } catch (InvocationTargetException e) {
3425 throw new IllegalStateException("Could not execute "
3426 + "method of the activity", e);
3427 }
3428 }
3429 });
3430 }
3431 break;
Adam Powell637d3372010-08-25 14:37:03 -07003432 case R.styleable.View_overScrollMode:
3433 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3434 break;
Adam Powell20232d02010-12-08 21:08:53 -08003435 case R.styleable.View_verticalScrollbarPosition:
3436 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3437 break;
Romain Guy171c5922011-01-06 10:04:23 -08003438 case R.styleable.View_layerType:
3439 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3440 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003441 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003442 // Clear any text direction flag already set
3443 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3444 // Set the text direction flags depending on the value of the attribute
3445 final int textDirection = a.getInt(attr, -1);
3446 if (textDirection != -1) {
3447 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3448 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003449 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003450 case R.styleable.View_textAlignment:
3451 // Clear any text alignment flag already set
3452 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3453 // Set the text alignment flag depending on the value of the attribute
3454 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3455 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3456 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003457 case R.styleable.View_importantForAccessibility:
3458 setImportantForAccessibility(a.getInt(attr,
3459 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 }
3461 }
3462
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003463 a.recycle();
3464
Adam Powell637d3372010-08-25 14:37:03 -07003465 setOverScrollMode(overScrollMode);
3466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003467 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003468 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003469 }
3470
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003471 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3472 // layout direction). Those cached values will be used later during padding resolution.
3473 mUserPaddingStart = startPadding;
3474 mUserPaddingEnd = endPadding;
3475
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003476 updateUserPaddingRelative();
3477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003478 if (padding >= 0) {
3479 leftPadding = padding;
3480 topPadding = padding;
3481 rightPadding = padding;
3482 bottomPadding = padding;
3483 }
3484
3485 // If the user specified the padding (either with android:padding or
3486 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3487 // use the default padding or the padding from the background drawable
3488 // (stored at this point in mPadding*)
3489 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3490 topPadding >= 0 ? topPadding : mPaddingTop,
3491 rightPadding >= 0 ? rightPadding : mPaddingRight,
3492 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3493
3494 if (viewFlagMasks != 0) {
3495 setFlags(viewFlagValues, viewFlagMasks);
3496 }
3497
3498 // Needs to be called after mViewFlags is set
3499 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3500 recomputePadding();
3501 }
3502
3503 if (x != 0 || y != 0) {
3504 scrollTo(x, y);
3505 }
3506
Chet Haase73066682010-11-29 15:55:32 -08003507 if (transformSet) {
3508 setTranslationX(tx);
3509 setTranslationY(ty);
3510 setRotation(rotation);
3511 setRotationX(rotationX);
3512 setRotationY(rotationY);
3513 setScaleX(sx);
3514 setScaleY(sy);
3515 }
3516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3518 setScrollContainer(true);
3519 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003520
3521 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003522 }
3523
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003524 private void updateUserPaddingRelative() {
3525 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3526 }
3527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003528 /**
3529 * Non-public constructor for use in testing
3530 */
3531 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003532 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003533 }
3534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003535 /**
3536 * <p>
3537 * Initializes the fading edges from a given set of styled attributes. This
3538 * method should be called by subclasses that need fading edges and when an
3539 * instance of these subclasses is created programmatically rather than
3540 * being inflated from XML. This method is automatically called when the XML
3541 * is inflated.
3542 * </p>
3543 *
3544 * @param a the styled attributes set to initialize the fading edges from
3545 */
3546 protected void initializeFadingEdge(TypedArray a) {
3547 initScrollCache();
3548
3549 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3550 R.styleable.View_fadingEdgeLength,
3551 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3552 }
3553
3554 /**
3555 * Returns the size of the vertical faded edges used to indicate that more
3556 * content in this view is visible.
3557 *
3558 * @return The size in pixels of the vertical faded edge or 0 if vertical
3559 * faded edges are not enabled for this view.
3560 * @attr ref android.R.styleable#View_fadingEdgeLength
3561 */
3562 public int getVerticalFadingEdgeLength() {
3563 if (isVerticalFadingEdgeEnabled()) {
3564 ScrollabilityCache cache = mScrollCache;
3565 if (cache != null) {
3566 return cache.fadingEdgeLength;
3567 }
3568 }
3569 return 0;
3570 }
3571
3572 /**
3573 * Set the size of the faded edge used to indicate that more content in this
3574 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003575 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3576 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3577 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003578 *
3579 * @param length The size in pixels of the faded edge used to indicate that more
3580 * content in this view is visible.
3581 */
3582 public void setFadingEdgeLength(int length) {
3583 initScrollCache();
3584 mScrollCache.fadingEdgeLength = length;
3585 }
3586
3587 /**
3588 * Returns the size of the horizontal faded edges used to indicate that more
3589 * content in this view is visible.
3590 *
3591 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3592 * faded edges are not enabled for this view.
3593 * @attr ref android.R.styleable#View_fadingEdgeLength
3594 */
3595 public int getHorizontalFadingEdgeLength() {
3596 if (isHorizontalFadingEdgeEnabled()) {
3597 ScrollabilityCache cache = mScrollCache;
3598 if (cache != null) {
3599 return cache.fadingEdgeLength;
3600 }
3601 }
3602 return 0;
3603 }
3604
3605 /**
3606 * Returns the width of the vertical scrollbar.
3607 *
3608 * @return The width in pixels of the vertical scrollbar or 0 if there
3609 * is no vertical scrollbar.
3610 */
3611 public int getVerticalScrollbarWidth() {
3612 ScrollabilityCache cache = mScrollCache;
3613 if (cache != null) {
3614 ScrollBarDrawable scrollBar = cache.scrollBar;
3615 if (scrollBar != null) {
3616 int size = scrollBar.getSize(true);
3617 if (size <= 0) {
3618 size = cache.scrollBarSize;
3619 }
3620 return size;
3621 }
3622 return 0;
3623 }
3624 return 0;
3625 }
3626
3627 /**
3628 * Returns the height of the horizontal scrollbar.
3629 *
3630 * @return The height in pixels of the horizontal scrollbar or 0 if
3631 * there is no horizontal scrollbar.
3632 */
3633 protected int getHorizontalScrollbarHeight() {
3634 ScrollabilityCache cache = mScrollCache;
3635 if (cache != null) {
3636 ScrollBarDrawable scrollBar = cache.scrollBar;
3637 if (scrollBar != null) {
3638 int size = scrollBar.getSize(false);
3639 if (size <= 0) {
3640 size = cache.scrollBarSize;
3641 }
3642 return size;
3643 }
3644 return 0;
3645 }
3646 return 0;
3647 }
3648
3649 /**
3650 * <p>
3651 * Initializes the scrollbars from a given set of styled attributes. This
3652 * method should be called by subclasses that need scrollbars and when an
3653 * instance of these subclasses is created programmatically rather than
3654 * being inflated from XML. This method is automatically called when the XML
3655 * is inflated.
3656 * </p>
3657 *
3658 * @param a the styled attributes set to initialize the scrollbars from
3659 */
3660 protected void initializeScrollbars(TypedArray a) {
3661 initScrollCache();
3662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003663 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003664
Mike Cleronf116bf82009-09-27 19:14:12 -07003665 if (scrollabilityCache.scrollBar == null) {
3666 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3667 }
Joe Malin32736f02011-01-19 16:14:20 -08003668
Romain Guy8bda2482010-03-02 11:42:11 -08003669 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670
Mike Cleronf116bf82009-09-27 19:14:12 -07003671 if (!fadeScrollbars) {
3672 scrollabilityCache.state = ScrollabilityCache.ON;
3673 }
3674 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003675
3676
Mike Cleronf116bf82009-09-27 19:14:12 -07003677 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3678 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3679 .getScrollBarFadeDuration());
3680 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3681 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3682 ViewConfiguration.getScrollDefaultDelay());
3683
Joe Malin32736f02011-01-19 16:14:20 -08003684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3686 com.android.internal.R.styleable.View_scrollbarSize,
3687 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3688
3689 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3690 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3691
3692 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3693 if (thumb != null) {
3694 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3695 }
3696
3697 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3698 false);
3699 if (alwaysDraw) {
3700 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3701 }
3702
3703 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3704 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3705
3706 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3707 if (thumb != null) {
3708 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3709 }
3710
3711 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3712 false);
3713 if (alwaysDraw) {
3714 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3715 }
3716
3717 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003718 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003719 }
3720
3721 /**
3722 * <p>
3723 * Initalizes the scrollability cache if necessary.
3724 * </p>
3725 */
3726 private void initScrollCache() {
3727 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003728 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 }
3730 }
3731
Philip Milne6c8ea062012-04-03 17:38:43 -07003732 private ScrollabilityCache getScrollCache() {
3733 initScrollCache();
3734 return mScrollCache;
3735 }
3736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003737 /**
Adam Powell20232d02010-12-08 21:08:53 -08003738 * Set the position of the vertical scroll bar. Should be one of
3739 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3740 * {@link #SCROLLBAR_POSITION_RIGHT}.
3741 *
3742 * @param position Where the vertical scroll bar should be positioned.
3743 */
3744 public void setVerticalScrollbarPosition(int position) {
3745 if (mVerticalScrollbarPosition != position) {
3746 mVerticalScrollbarPosition = position;
3747 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003748 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003749 }
3750 }
3751
3752 /**
3753 * @return The position where the vertical scroll bar will show, if applicable.
3754 * @see #setVerticalScrollbarPosition(int)
3755 */
3756 public int getVerticalScrollbarPosition() {
3757 return mVerticalScrollbarPosition;
3758 }
3759
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003760 ListenerInfo getListenerInfo() {
3761 if (mListenerInfo != null) {
3762 return mListenerInfo;
3763 }
3764 mListenerInfo = new ListenerInfo();
3765 return mListenerInfo;
3766 }
3767
Adam Powell20232d02010-12-08 21:08:53 -08003768 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003769 * Register a callback to be invoked when focus of this view changed.
3770 *
3771 * @param l The callback that will run.
3772 */
3773 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003774 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 }
3776
3777 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003778 * Add a listener that will be called when the bounds of the view change due to
3779 * layout processing.
3780 *
3781 * @param listener The listener that will be called when layout bounds change.
3782 */
3783 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003784 ListenerInfo li = getListenerInfo();
3785 if (li.mOnLayoutChangeListeners == null) {
3786 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003787 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003788 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3789 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003790 }
Chet Haase21cd1382010-09-01 17:42:29 -07003791 }
3792
3793 /**
3794 * Remove a listener for layout changes.
3795 *
3796 * @param listener The listener for layout bounds change.
3797 */
3798 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003799 ListenerInfo li = mListenerInfo;
3800 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003801 return;
3802 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003803 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003804 }
3805
3806 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003807 * Add a listener for attach state changes.
3808 *
3809 * This listener will be called whenever this view is attached or detached
3810 * from a window. Remove the listener using
3811 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3812 *
3813 * @param listener Listener to attach
3814 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3815 */
3816 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003817 ListenerInfo li = getListenerInfo();
3818 if (li.mOnAttachStateChangeListeners == null) {
3819 li.mOnAttachStateChangeListeners
3820 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003821 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003822 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003823 }
3824
3825 /**
3826 * Remove a listener for attach state changes. The listener will receive no further
3827 * notification of window attach/detach events.
3828 *
3829 * @param listener Listener to remove
3830 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3831 */
3832 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003833 ListenerInfo li = mListenerInfo;
3834 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003835 return;
3836 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003837 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003838 }
3839
3840 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003841 * Returns the focus-change callback registered for this view.
3842 *
3843 * @return The callback, or null if one is not registered.
3844 */
3845 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003846 ListenerInfo li = mListenerInfo;
3847 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003848 }
3849
3850 /**
3851 * Register a callback to be invoked when this view is clicked. If this view is not
3852 * clickable, it becomes clickable.
3853 *
3854 * @param l The callback that will run
3855 *
3856 * @see #setClickable(boolean)
3857 */
3858 public void setOnClickListener(OnClickListener l) {
3859 if (!isClickable()) {
3860 setClickable(true);
3861 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003862 getListenerInfo().mOnClickListener = l;
3863 }
3864
3865 /**
3866 * Return whether this view has an attached OnClickListener. Returns
3867 * true if there is a listener, false if there is none.
3868 */
3869 public boolean hasOnClickListeners() {
3870 ListenerInfo li = mListenerInfo;
3871 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003872 }
3873
3874 /**
3875 * Register a callback to be invoked when this view is clicked and held. If this view is not
3876 * long clickable, it becomes long clickable.
3877 *
3878 * @param l The callback that will run
3879 *
3880 * @see #setLongClickable(boolean)
3881 */
3882 public void setOnLongClickListener(OnLongClickListener l) {
3883 if (!isLongClickable()) {
3884 setLongClickable(true);
3885 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003886 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003887 }
3888
3889 /**
3890 * Register a callback to be invoked when the context menu for this view is
3891 * being built. If this view is not long clickable, it becomes long clickable.
3892 *
3893 * @param l The callback that will run
3894 *
3895 */
3896 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3897 if (!isLongClickable()) {
3898 setLongClickable(true);
3899 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003900 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003901 }
3902
3903 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003904 * Call this view's OnClickListener, if it is defined. Performs all normal
3905 * actions associated with clicking: reporting accessibility event, playing
3906 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003907 *
3908 * @return True there was an assigned OnClickListener that was called, false
3909 * otherwise is returned.
3910 */
3911 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003912 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3913
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003914 ListenerInfo li = mListenerInfo;
3915 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003916 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003917 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003918 return true;
3919 }
3920
3921 return false;
3922 }
3923
3924 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003925 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3926 * this only calls the listener, and does not do any associated clicking
3927 * actions like reporting an accessibility event.
3928 *
3929 * @return True there was an assigned OnClickListener that was called, false
3930 * otherwise is returned.
3931 */
3932 public boolean callOnClick() {
3933 ListenerInfo li = mListenerInfo;
3934 if (li != null && li.mOnClickListener != null) {
3935 li.mOnClickListener.onClick(this);
3936 return true;
3937 }
3938 return false;
3939 }
3940
3941 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003942 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3943 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003944 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003945 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003946 */
3947 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003948 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003950 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003951 ListenerInfo li = mListenerInfo;
3952 if (li != null && li.mOnLongClickListener != null) {
3953 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003954 }
3955 if (!handled) {
3956 handled = showContextMenu();
3957 }
3958 if (handled) {
3959 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3960 }
3961 return handled;
3962 }
3963
3964 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003965 * Performs button-related actions during a touch down event.
3966 *
3967 * @param event The event.
3968 * @return True if the down was consumed.
3969 *
3970 * @hide
3971 */
3972 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3973 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3974 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3975 return true;
3976 }
3977 }
3978 return false;
3979 }
3980
3981 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 * Bring up the context menu for this view.
3983 *
3984 * @return Whether a context menu was displayed.
3985 */
3986 public boolean showContextMenu() {
3987 return getParent().showContextMenuForChild(this);
3988 }
3989
3990 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003991 * Bring up the context menu for this view, referring to the item under the specified point.
3992 *
3993 * @param x The referenced x coordinate.
3994 * @param y The referenced y coordinate.
3995 * @param metaState The keyboard modifiers that were pressed.
3996 * @return Whether a context menu was displayed.
3997 *
3998 * @hide
3999 */
4000 public boolean showContextMenu(float x, float y, int metaState) {
4001 return showContextMenu();
4002 }
4003
4004 /**
Adam Powell6e346362010-07-23 10:18:23 -07004005 * Start an action mode.
4006 *
4007 * @param callback Callback that will control the lifecycle of the action mode
4008 * @return The new action mode if it is started, null otherwise
4009 *
4010 * @see ActionMode
4011 */
4012 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004013 ViewParent parent = getParent();
4014 if (parent == null) return null;
4015 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004016 }
4017
4018 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004019 * Register a callback to be invoked when a key is pressed in this view.
4020 * @param l the key listener to attach to this view
4021 */
4022 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004023 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004024 }
4025
4026 /**
4027 * Register a callback to be invoked when a touch event is sent to this view.
4028 * @param l the touch listener to attach to this view
4029 */
4030 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004031 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004032 }
4033
4034 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004035 * Register a callback to be invoked when a generic motion event is sent to this view.
4036 * @param l the generic motion listener to attach to this view
4037 */
4038 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004039 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004040 }
4041
4042 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004043 * Register a callback to be invoked when a hover event is sent to this view.
4044 * @param l the hover listener to attach to this view
4045 */
4046 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004047 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004048 }
4049
4050 /**
Joe Malin32736f02011-01-19 16:14:20 -08004051 * Register a drag event listener callback object for this View. The parameter is
4052 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4053 * View, the system calls the
4054 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4055 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004056 */
4057 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004058 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004059 }
4060
4061 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004062 * Give this view focus. This will cause
4063 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064 *
4065 * Note: this does not check whether this {@link View} should get focus, it just
4066 * gives it focus no matter what. It should only be called internally by framework
4067 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4068 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004069 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4070 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004071 * focus moved when requestFocus() is called. It may not always
4072 * apply, in which case use the default View.FOCUS_DOWN.
4073 * @param previouslyFocusedRect The rectangle of the view that had focus
4074 * prior in this View's coordinate system.
4075 */
4076 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4077 if (DBG) {
4078 System.out.println(this + " requestFocus()");
4079 }
4080
4081 if ((mPrivateFlags & FOCUSED) == 0) {
4082 mPrivateFlags |= FOCUSED;
4083
4084 if (mParent != null) {
4085 mParent.requestChildFocus(this, this);
4086 }
4087
4088 onFocusChanged(true, direction, previouslyFocusedRect);
4089 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004090
4091 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4092 notifyAccessibilityStateChanged();
4093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004094 }
4095 }
4096
4097 /**
4098 * Request that a rectangle of this view be visible on the screen,
4099 * scrolling if necessary just enough.
4100 *
4101 * <p>A View should call this if it maintains some notion of which part
4102 * of its content is interesting. For example, a text editing view
4103 * should call this when its cursor moves.
4104 *
4105 * @param rectangle The rectangle.
4106 * @return Whether any parent scrolled.
4107 */
4108 public boolean requestRectangleOnScreen(Rect rectangle) {
4109 return requestRectangleOnScreen(rectangle, false);
4110 }
4111
4112 /**
4113 * Request that a rectangle of this view be visible on the screen,
4114 * scrolling if necessary just enough.
4115 *
4116 * <p>A View should call this if it maintains some notion of which part
4117 * of its content is interesting. For example, a text editing view
4118 * should call this when its cursor moves.
4119 *
4120 * <p>When <code>immediate</code> is set to true, scrolling will not be
4121 * animated.
4122 *
4123 * @param rectangle The rectangle.
4124 * @param immediate True to forbid animated scrolling, false otherwise
4125 * @return Whether any parent scrolled.
4126 */
4127 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4128 View child = this;
4129 ViewParent parent = mParent;
4130 boolean scrolled = false;
4131 while (parent != null) {
4132 scrolled |= parent.requestChildRectangleOnScreen(child,
4133 rectangle, immediate);
4134
4135 // offset rect so next call has the rectangle in the
4136 // coordinate system of its direct child.
4137 rectangle.offset(child.getLeft(), child.getTop());
4138 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4139
4140 if (!(parent instanceof View)) {
4141 break;
4142 }
Romain Guy8506ab42009-06-11 17:35:47 -07004143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004144 child = (View) parent;
4145 parent = child.getParent();
4146 }
4147 return scrolled;
4148 }
4149
4150 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004151 * Called when this view wants to give up focus. If focus is cleared
4152 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4153 * <p>
4154 * <strong>Note:</strong> When a View clears focus the framework is trying
4155 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004156 * View is the first from the top that can take focus, then all callbacks
4157 * related to clearing focus will be invoked after wich the framework will
4158 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004159 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004160 */
4161 public void clearFocus() {
4162 if (DBG) {
4163 System.out.println(this + " clearFocus()");
4164 }
4165
4166 if ((mPrivateFlags & FOCUSED) != 0) {
4167 mPrivateFlags &= ~FOCUSED;
4168
4169 if (mParent != null) {
4170 mParent.clearChildFocus(this);
4171 }
4172
4173 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004176
4177 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004178
4179 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4180 notifyAccessibilityStateChanged();
4181 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004182 }
4183 }
4184
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004185 void ensureInputFocusOnFirstFocusable() {
4186 View root = getRootView();
4187 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004188 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004189 }
4190 }
4191
4192 /**
4193 * Called internally by the view system when a new view is getting focus.
4194 * This is what clears the old focus.
4195 */
4196 void unFocus() {
4197 if (DBG) {
4198 System.out.println(this + " unFocus()");
4199 }
4200
4201 if ((mPrivateFlags & FOCUSED) != 0) {
4202 mPrivateFlags &= ~FOCUSED;
4203
4204 onFocusChanged(false, 0, null);
4205 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004206
4207 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4208 notifyAccessibilityStateChanged();
4209 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004210 }
4211 }
4212
4213 /**
4214 * Returns true if this view has focus iteself, or is the ancestor of the
4215 * view that has focus.
4216 *
4217 * @return True if this view has or contains focus, false otherwise.
4218 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004219 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004220 public boolean hasFocus() {
4221 return (mPrivateFlags & FOCUSED) != 0;
4222 }
4223
4224 /**
4225 * Returns true if this view is focusable or if it contains a reachable View
4226 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4227 * is a View whose parents do not block descendants focus.
4228 *
4229 * Only {@link #VISIBLE} views are considered focusable.
4230 *
4231 * @return True if the view is focusable or if the view contains a focusable
4232 * View, false otherwise.
4233 *
4234 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4235 */
4236 public boolean hasFocusable() {
4237 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4238 }
4239
4240 /**
4241 * Called by the view system when the focus state of this view changes.
4242 * When the focus change event is caused by directional navigation, direction
4243 * and previouslyFocusedRect provide insight into where the focus is coming from.
4244 * When overriding, be sure to call up through to the super class so that
4245 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004246 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004247 * @param gainFocus True if the View has focus; false otherwise.
4248 * @param direction The direction focus has moved when requestFocus()
4249 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004250 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4251 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4252 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004253 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4254 * system, of the previously focused view. If applicable, this will be
4255 * passed in as finer grained information about where the focus is coming
4256 * from (in addition to direction). Will be <code>null</code> otherwise.
4257 */
4258 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004259 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004260 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4261 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
4262 requestAccessibilityFocus();
4263 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004264 }
4265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004266 InputMethodManager imm = InputMethodManager.peekInstance();
4267 if (!gainFocus) {
4268 if (isPressed()) {
4269 setPressed(false);
4270 }
4271 if (imm != null && mAttachInfo != null
4272 && mAttachInfo.mHasWindowFocus) {
4273 imm.focusOut(this);
4274 }
Romain Guya2431d02009-04-30 16:30:00 -07004275 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004276 } else if (imm != null && mAttachInfo != null
4277 && mAttachInfo.mHasWindowFocus) {
4278 imm.focusIn(this);
4279 }
Romain Guy8506ab42009-06-11 17:35:47 -07004280
Romain Guy0fd89bf2011-01-26 15:41:30 -08004281 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004282 ListenerInfo li = mListenerInfo;
4283 if (li != null && li.mOnFocusChangeListener != null) {
4284 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 }
Joe Malin32736f02011-01-19 16:14:20 -08004286
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004287 if (mAttachInfo != null) {
4288 mAttachInfo.mKeyDispatchState.reset(this);
4289 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004290 }
4291
4292 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004293 * Sends an accessibility event of the given type. If accessiiblity is
4294 * not enabled this method has no effect. The default implementation calls
4295 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4296 * to populate information about the event source (this View), then calls
4297 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4298 * populate the text content of the event source including its descendants,
4299 * and last calls
4300 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4301 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004302 * <p>
4303 * If an {@link AccessibilityDelegate} has been specified via calling
4304 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4305 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4306 * responsible for handling this call.
4307 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004308 *
Scott Mainb303d832011-10-12 16:45:18 -07004309 * @param eventType The type of the event to send, as defined by several types from
4310 * {@link android.view.accessibility.AccessibilityEvent}, such as
4311 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4312 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004313 *
4314 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4315 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4316 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004317 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004318 */
4319 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004320 if (mAccessibilityDelegate != null) {
4321 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4322 } else {
4323 sendAccessibilityEventInternal(eventType);
4324 }
4325 }
4326
4327 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004328 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4329 * {@link AccessibilityEvent} to make an announcement which is related to some
4330 * sort of a context change for which none of the events representing UI transitions
4331 * is a good fit. For example, announcing a new page in a book. If accessibility
4332 * is not enabled this method does nothing.
4333 *
4334 * @param text The announcement text.
4335 */
4336 public void announceForAccessibility(CharSequence text) {
4337 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4338 AccessibilityEvent event = AccessibilityEvent.obtain(
4339 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4340 event.getText().add(text);
4341 sendAccessibilityEventUnchecked(event);
4342 }
4343 }
4344
4345 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004346 * @see #sendAccessibilityEvent(int)
4347 *
4348 * Note: Called from the default {@link AccessibilityDelegate}.
4349 */
4350 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004351 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4352 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4353 }
4354 }
4355
4356 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004357 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4358 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004359 * perform a check whether accessibility is enabled.
4360 * <p>
4361 * If an {@link AccessibilityDelegate} has been specified via calling
4362 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4363 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4364 * is responsible for handling this call.
4365 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004366 *
4367 * @param event The event to send.
4368 *
4369 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004370 */
4371 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004372 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004373 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004374 } else {
4375 sendAccessibilityEventUncheckedInternal(event);
4376 }
4377 }
4378
4379 /**
4380 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4381 *
4382 * Note: Called from the default {@link AccessibilityDelegate}.
4383 */
4384 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004385 if (!isShown()) {
4386 return;
4387 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004388 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004389 // Only a subset of accessibility events populates text content.
4390 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4391 dispatchPopulateAccessibilityEvent(event);
4392 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004393 // Intercept accessibility focus events fired by virtual nodes to keep
4394 // track of accessibility focus position in such nodes.
4395 final int eventType = event.getEventType();
4396 switch (eventType) {
4397 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
4398 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4399 event.getSourceNodeId());
4400 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4401 ViewRootImpl viewRootImpl = getViewRootImpl();
4402 if (viewRootImpl != null) {
4403 viewRootImpl.setAccessibilityFocusedHost(this);
4404 }
4405 }
4406 } break;
4407 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
4408 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4409 event.getSourceNodeId());
4410 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4411 ViewRootImpl viewRootImpl = getViewRootImpl();
4412 if (viewRootImpl != null) {
4413 viewRootImpl.setAccessibilityFocusedHost(null);
4414 }
4415 }
4416 } break;
4417 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004418 // In the beginning we called #isShown(), so we know that getParent() is not null.
4419 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004420 }
4421
4422 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004423 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4424 * to its children for adding their text content to the event. Note that the
4425 * event text is populated in a separate dispatch path since we add to the
4426 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004427 * A typical implementation will call
4428 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4429 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4430 * on each child. Override this method if custom population of the event text
4431 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004432 * <p>
4433 * If an {@link AccessibilityDelegate} has been specified via calling
4434 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4435 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4436 * is responsible for handling this call.
4437 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004438 * <p>
4439 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4440 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4441 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004442 *
4443 * @param event The event.
4444 *
4445 * @return True if the event population was completed.
4446 */
4447 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004448 if (mAccessibilityDelegate != null) {
4449 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4450 } else {
4451 return dispatchPopulateAccessibilityEventInternal(event);
4452 }
4453 }
4454
4455 /**
4456 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4457 *
4458 * Note: Called from the default {@link AccessibilityDelegate}.
4459 */
4460 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004461 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004462 return false;
4463 }
4464
4465 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004466 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004467 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004468 * text content. While this method is free to modify event
4469 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004470 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4471 * <p>
4472 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004473 * to the text added by the super implementation:
4474 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004475 * super.onPopulateAccessibilityEvent(event);
4476 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4477 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4478 * mCurrentDate.getTimeInMillis(), flags);
4479 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004480 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004481 * <p>
4482 * If an {@link AccessibilityDelegate} has been specified via calling
4483 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4484 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4485 * is responsible for handling this call.
4486 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004487 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4488 * information to the event, in case the default implementation has basic information to add.
4489 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004490 *
4491 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004492 *
4493 * @see #sendAccessibilityEvent(int)
4494 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004495 */
4496 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004497 if (mAccessibilityDelegate != null) {
4498 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4499 } else {
4500 onPopulateAccessibilityEventInternal(event);
4501 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004502 }
4503
4504 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004505 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4506 *
4507 * Note: Called from the default {@link AccessibilityDelegate}.
4508 */
4509 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4510
4511 }
4512
4513 /**
4514 * Initializes an {@link AccessibilityEvent} with information about
4515 * this View which is the event source. In other words, the source of
4516 * an accessibility event is the view whose state change triggered firing
4517 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004518 * <p>
4519 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004520 * to properties set by the super implementation:
4521 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4522 * super.onInitializeAccessibilityEvent(event);
4523 * event.setPassword(true);
4524 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004525 * <p>
4526 * If an {@link AccessibilityDelegate} has been specified via calling
4527 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4528 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4529 * is responsible for handling this call.
4530 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004531 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4532 * information to the event, in case the default implementation has basic information to add.
4533 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004534 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004535 *
4536 * @see #sendAccessibilityEvent(int)
4537 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4538 */
4539 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004540 if (mAccessibilityDelegate != null) {
4541 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4542 } else {
4543 onInitializeAccessibilityEventInternal(event);
4544 }
4545 }
4546
4547 /**
4548 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4549 *
4550 * Note: Called from the default {@link AccessibilityDelegate}.
4551 */
4552 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004553 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004554 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004555 event.setPackageName(getContext().getPackageName());
4556 event.setEnabled(isEnabled());
4557 event.setContentDescription(mContentDescription);
4558
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004559 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004560 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004561 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4562 FOCUSABLES_ALL);
4563 event.setItemCount(focusablesTempList.size());
4564 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4565 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004566 }
4567 }
4568
4569 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004570 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4571 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4572 * This method is responsible for obtaining an accessibility node info from a
4573 * pool of reusable instances and calling
4574 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4575 * initialize the former.
4576 * <p>
4577 * Note: The client is responsible for recycling the obtained instance by calling
4578 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4579 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004580 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004581 * @return A populated {@link AccessibilityNodeInfo}.
4582 *
4583 * @see AccessibilityNodeInfo
4584 */
4585 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004586 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4587 if (provider != null) {
4588 return provider.createAccessibilityNodeInfo(View.NO_ID);
4589 } else {
4590 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4591 onInitializeAccessibilityNodeInfo(info);
4592 return info;
4593 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004594 }
4595
4596 /**
4597 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4598 * The base implementation sets:
4599 * <ul>
4600 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004601 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4602 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004603 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4604 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4605 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4606 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4607 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4608 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4609 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4610 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4611 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4612 * </ul>
4613 * <p>
4614 * Subclasses should override this method, call the super implementation,
4615 * and set additional attributes.
4616 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004617 * <p>
4618 * If an {@link AccessibilityDelegate} has been specified via calling
4619 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4620 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4621 * is responsible for handling this call.
4622 * </p>
4623 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004624 * @param info The instance to initialize.
4625 */
4626 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004627 if (mAccessibilityDelegate != null) {
4628 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4629 } else {
4630 onInitializeAccessibilityNodeInfoInternal(info);
4631 }
4632 }
4633
4634 /**
4635 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4636 *
4637 * Note: Called from the default {@link AccessibilityDelegate}.
4638 */
4639 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004640 Rect bounds = mAttachInfo.mTmpInvalRect;
4641 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004642 info.setBoundsInParent(bounds);
4643
Svetoslav Ganovaa6f3de2012-04-09 17:39:00 -07004644 getGlobalVisibleRect(bounds);
4645 bounds.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004646 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004647
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004648 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004649 ViewParent parent = getParentForAccessibility();
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004650 if (parent instanceof View) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004651 info.setParent((View) parent);
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004652 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004653 }
4654
4655 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004656 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004657 info.setContentDescription(getContentDescription());
4658
4659 info.setEnabled(isEnabled());
4660 info.setClickable(isClickable());
4661 info.setFocusable(isFocusable());
4662 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004663 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004664 info.setSelected(isSelected());
4665 info.setLongClickable(isLongClickable());
4666
4667 // TODO: These make sense only if we are in an AdapterView but all
4668 // views can be selected. Maybe from accessiiblity perspective
4669 // we should report as selectable view in an AdapterView.
4670 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4671 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4672
4673 if (isFocusable()) {
4674 if (isFocused()) {
4675 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4676 } else {
4677 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4678 }
4679 }
4680 }
4681
4682 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004683 * Sets a delegate for implementing accessibility support via compositon as
4684 * opposed to inheritance. The delegate's primary use is for implementing
4685 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4686 *
4687 * @param delegate The delegate instance.
4688 *
4689 * @see AccessibilityDelegate
4690 */
4691 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4692 mAccessibilityDelegate = delegate;
4693 }
4694
4695 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004696 * Gets the provider for managing a virtual view hierarchy rooted at this View
4697 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4698 * that explore the window content.
4699 * <p>
4700 * If this method returns an instance, this instance is responsible for managing
4701 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4702 * View including the one representing the View itself. Similarly the returned
4703 * instance is responsible for performing accessibility actions on any virtual
4704 * view or the root view itself.
4705 * </p>
4706 * <p>
4707 * If an {@link AccessibilityDelegate} has been specified via calling
4708 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4709 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4710 * is responsible for handling this call.
4711 * </p>
4712 *
4713 * @return The provider.
4714 *
4715 * @see AccessibilityNodeProvider
4716 */
4717 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4718 if (mAccessibilityDelegate != null) {
4719 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4720 } else {
4721 return null;
4722 }
4723 }
4724
4725 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004726 * Gets the unique identifier of this view on the screen for accessibility purposes.
4727 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4728 *
4729 * @return The view accessibility id.
4730 *
4731 * @hide
4732 */
4733 public int getAccessibilityViewId() {
4734 if (mAccessibilityViewId == NO_ID) {
4735 mAccessibilityViewId = sNextAccessibilityViewId++;
4736 }
4737 return mAccessibilityViewId;
4738 }
4739
4740 /**
4741 * Gets the unique identifier of the window in which this View reseides.
4742 *
4743 * @return The window accessibility id.
4744 *
4745 * @hide
4746 */
4747 public int getAccessibilityWindowId() {
4748 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4749 }
4750
4751 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004752 * Gets the {@link View} description. It briefly describes the view and is
4753 * primarily used for accessibility support. Set this property to enable
4754 * better accessibility support for your application. This is especially
4755 * true for views that do not have textual representation (For example,
4756 * ImageButton).
4757 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07004758 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07004759 *
4760 * @attr ref android.R.styleable#View_contentDescription
4761 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07004762 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07004763 public CharSequence getContentDescription() {
4764 return mContentDescription;
4765 }
4766
4767 /**
4768 * Sets the {@link View} description. It briefly describes the view and is
4769 * primarily used for accessibility support. Set this property to enable
4770 * better accessibility support for your application. This is especially
4771 * true for views that do not have textual representation (For example,
4772 * ImageButton).
4773 *
4774 * @param contentDescription The content description.
4775 *
4776 * @attr ref android.R.styleable#View_contentDescription
4777 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004778 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004779 public void setContentDescription(CharSequence contentDescription) {
4780 mContentDescription = contentDescription;
4781 }
4782
4783 /**
Romain Guya2431d02009-04-30 16:30:00 -07004784 * Invoked whenever this view loses focus, either by losing window focus or by losing
4785 * focus within its window. This method can be used to clear any state tied to the
4786 * focus. For instance, if a button is held pressed with the trackball and the window
4787 * loses focus, this method can be used to cancel the press.
4788 *
4789 * Subclasses of View overriding this method should always call super.onFocusLost().
4790 *
4791 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004792 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004793 *
4794 * @hide pending API council approval
4795 */
4796 protected void onFocusLost() {
4797 resetPressedState();
4798 }
4799
4800 private void resetPressedState() {
4801 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4802 return;
4803 }
4804
4805 if (isPressed()) {
4806 setPressed(false);
4807
4808 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004809 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004810 }
4811 }
4812 }
4813
4814 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004815 * Returns true if this view has focus
4816 *
4817 * @return True if this view has focus, false otherwise.
4818 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004819 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004820 public boolean isFocused() {
4821 return (mPrivateFlags & FOCUSED) != 0;
4822 }
4823
4824 /**
4825 * Find the view in the hierarchy rooted at this view that currently has
4826 * focus.
4827 *
4828 * @return The view that currently has focus, or null if no focused view can
4829 * be found.
4830 */
4831 public View findFocus() {
4832 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4833 }
4834
4835 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004836 * Indicates whether this view is one of the set of scrollable containers in
4837 * its window.
4838 *
4839 * @return whether this view is one of the set of scrollable containers in
4840 * its window
4841 *
4842 * @attr ref android.R.styleable#View_isScrollContainer
4843 */
4844 public boolean isScrollContainer() {
4845 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
4846 }
4847
4848 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004849 * Change whether this view is one of the set of scrollable containers in
4850 * its window. This will be used to determine whether the window can
4851 * resize or must pan when a soft input area is open -- scrollable
4852 * containers allow the window to use resize mode since the container
4853 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07004854 *
4855 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004856 */
4857 public void setScrollContainer(boolean isScrollContainer) {
4858 if (isScrollContainer) {
4859 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4860 mAttachInfo.mScrollContainers.add(this);
4861 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4862 }
4863 mPrivateFlags |= SCROLL_CONTAINER;
4864 } else {
4865 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4866 mAttachInfo.mScrollContainers.remove(this);
4867 }
4868 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4869 }
4870 }
4871
4872 /**
4873 * Returns the quality of the drawing cache.
4874 *
4875 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4876 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4877 *
4878 * @see #setDrawingCacheQuality(int)
4879 * @see #setDrawingCacheEnabled(boolean)
4880 * @see #isDrawingCacheEnabled()
4881 *
4882 * @attr ref android.R.styleable#View_drawingCacheQuality
4883 */
4884 public int getDrawingCacheQuality() {
4885 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4886 }
4887
4888 /**
4889 * Set the drawing cache quality of this view. This value is used only when the
4890 * drawing cache is enabled
4891 *
4892 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4893 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4894 *
4895 * @see #getDrawingCacheQuality()
4896 * @see #setDrawingCacheEnabled(boolean)
4897 * @see #isDrawingCacheEnabled()
4898 *
4899 * @attr ref android.R.styleable#View_drawingCacheQuality
4900 */
4901 public void setDrawingCacheQuality(int quality) {
4902 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4903 }
4904
4905 /**
4906 * Returns whether the screen should remain on, corresponding to the current
4907 * value of {@link #KEEP_SCREEN_ON}.
4908 *
4909 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4910 *
4911 * @see #setKeepScreenOn(boolean)
4912 *
4913 * @attr ref android.R.styleable#View_keepScreenOn
4914 */
4915 public boolean getKeepScreenOn() {
4916 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4917 }
4918
4919 /**
4920 * Controls whether the screen should remain on, modifying the
4921 * value of {@link #KEEP_SCREEN_ON}.
4922 *
4923 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4924 *
4925 * @see #getKeepScreenOn()
4926 *
4927 * @attr ref android.R.styleable#View_keepScreenOn
4928 */
4929 public void setKeepScreenOn(boolean keepScreenOn) {
4930 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4931 }
4932
4933 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004934 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4935 * @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 -08004936 *
4937 * @attr ref android.R.styleable#View_nextFocusLeft
4938 */
4939 public int getNextFocusLeftId() {
4940 return mNextFocusLeftId;
4941 }
4942
4943 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004944 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4945 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4946 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004947 *
4948 * @attr ref android.R.styleable#View_nextFocusLeft
4949 */
4950 public void setNextFocusLeftId(int nextFocusLeftId) {
4951 mNextFocusLeftId = nextFocusLeftId;
4952 }
4953
4954 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004955 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4956 * @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 -08004957 *
4958 * @attr ref android.R.styleable#View_nextFocusRight
4959 */
4960 public int getNextFocusRightId() {
4961 return mNextFocusRightId;
4962 }
4963
4964 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004965 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4966 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4967 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004968 *
4969 * @attr ref android.R.styleable#View_nextFocusRight
4970 */
4971 public void setNextFocusRightId(int nextFocusRightId) {
4972 mNextFocusRightId = nextFocusRightId;
4973 }
4974
4975 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004976 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4977 * @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 -08004978 *
4979 * @attr ref android.R.styleable#View_nextFocusUp
4980 */
4981 public int getNextFocusUpId() {
4982 return mNextFocusUpId;
4983 }
4984
4985 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004986 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4987 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4988 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004989 *
4990 * @attr ref android.R.styleable#View_nextFocusUp
4991 */
4992 public void setNextFocusUpId(int nextFocusUpId) {
4993 mNextFocusUpId = nextFocusUpId;
4994 }
4995
4996 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004997 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4998 * @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 -08004999 *
5000 * @attr ref android.R.styleable#View_nextFocusDown
5001 */
5002 public int getNextFocusDownId() {
5003 return mNextFocusDownId;
5004 }
5005
5006 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005007 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5008 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5009 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005010 *
5011 * @attr ref android.R.styleable#View_nextFocusDown
5012 */
5013 public void setNextFocusDownId(int nextFocusDownId) {
5014 mNextFocusDownId = nextFocusDownId;
5015 }
5016
5017 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005018 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5019 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5020 *
5021 * @attr ref android.R.styleable#View_nextFocusForward
5022 */
5023 public int getNextFocusForwardId() {
5024 return mNextFocusForwardId;
5025 }
5026
5027 /**
5028 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5029 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5030 * decide automatically.
5031 *
5032 * @attr ref android.R.styleable#View_nextFocusForward
5033 */
5034 public void setNextFocusForwardId(int nextFocusForwardId) {
5035 mNextFocusForwardId = nextFocusForwardId;
5036 }
5037
5038 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005039 * Returns the visibility of this view and all of its ancestors
5040 *
5041 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5042 */
5043 public boolean isShown() {
5044 View current = this;
5045 //noinspection ConstantConditions
5046 do {
5047 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5048 return false;
5049 }
5050 ViewParent parent = current.mParent;
5051 if (parent == null) {
5052 return false; // We are not attached to the view root
5053 }
5054 if (!(parent instanceof View)) {
5055 return true;
5056 }
5057 current = (View) parent;
5058 } while (current != null);
5059
5060 return false;
5061 }
5062
5063 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005064 * Called by the view hierarchy when the content insets for a window have
5065 * changed, to allow it to adjust its content to fit within those windows.
5066 * The content insets tell you the space that the status bar, input method,
5067 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005068 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005069 * <p>You do not normally need to deal with this function, since the default
5070 * window decoration given to applications takes care of applying it to the
5071 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5072 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5073 * and your content can be placed under those system elements. You can then
5074 * use this method within your view hierarchy if you have parts of your UI
5075 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005076 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005077 * <p>The default implementation of this method simply applies the content
5078 * inset's to the view's padding. This can be enabled through
5079 * {@link #setFitsSystemWindows(boolean)}. Alternatively, you can override
5080 * the method and handle the insets however you would like. Note that the
5081 * insets provided by the framework are always relative to the far edges
5082 * of the window, not accounting for the location of the called view within
5083 * that window. (In fact when this method is called you do not yet know
5084 * where the layout will place the view, as it is done before layout happens.)
5085 *
5086 * <p>Note: unlike many View methods, there is no dispatch phase to this
5087 * call. If you are overriding it in a ViewGroup and want to allow the
5088 * call to continue to your children, you must be sure to call the super
5089 * implementation.
5090 *
5091 * @param insets Current content insets of the window. Prior to
5092 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5093 * the insets or else you and Android will be unhappy.
5094 *
5095 * @return Return true if this view applied the insets and it should not
5096 * continue propagating further down the hierarchy, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005097 */
5098 protected boolean fitSystemWindows(Rect insets) {
5099 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005100 mUserPaddingStart = -1;
5101 mUserPaddingEnd = -1;
5102 mUserPaddingRelative = false;
5103 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5104 || mAttachInfo == null
5105 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5106 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5107 return true;
5108 } else {
5109 internalSetPadding(0, 0, 0, 0);
5110 return false;
5111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005112 }
5113 return false;
5114 }
5115
5116 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005117 * Set whether or not this view should account for system screen decorations
5118 * such as the status bar and inset its content. This allows this view to be
5119 * positioned in absolute screen coordinates and remain visible to the user.
5120 *
5121 * <p>This should only be used by top-level window decor views.
5122 *
5123 * @param fitSystemWindows true to inset content for system screen decorations, false for
5124 * default behavior.
5125 *
5126 * @attr ref android.R.styleable#View_fitsSystemWindows
5127 */
5128 public void setFitsSystemWindows(boolean fitSystemWindows) {
5129 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5130 }
5131
5132 /**
5133 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
5134 * will account for system screen decorations such as the status bar and inset its
5135 * content. This allows the view to be positioned in absolute screen coordinates
5136 * and remain visible to the user.
5137 *
5138 * @return true if this view will adjust its content bounds for system screen decorations.
5139 *
5140 * @attr ref android.R.styleable#View_fitsSystemWindows
5141 */
5142 public boolean fitsSystemWindows() {
5143 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5144 }
5145
5146 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005147 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5148 */
5149 public void requestFitSystemWindows() {
5150 if (mParent != null) {
5151 mParent.requestFitSystemWindows();
5152 }
5153 }
5154
5155 /**
5156 * For use by PhoneWindow to make its own system window fitting optional.
5157 * @hide
5158 */
5159 public void makeOptionalFitsSystemWindows() {
5160 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5161 }
5162
5163 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005164 * Returns the visibility status for this view.
5165 *
5166 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5167 * @attr ref android.R.styleable#View_visibility
5168 */
5169 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005170 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5171 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5172 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005173 })
5174 public int getVisibility() {
5175 return mViewFlags & VISIBILITY_MASK;
5176 }
5177
5178 /**
5179 * Set the enabled state of this view.
5180 *
5181 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5182 * @attr ref android.R.styleable#View_visibility
5183 */
5184 @RemotableViewMethod
5185 public void setVisibility(int visibility) {
5186 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005187 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005188 }
5189
5190 /**
5191 * Returns the enabled status for this view. The interpretation of the
5192 * enabled state varies by subclass.
5193 *
5194 * @return True if this view is enabled, false otherwise.
5195 */
5196 @ViewDebug.ExportedProperty
5197 public boolean isEnabled() {
5198 return (mViewFlags & ENABLED_MASK) == ENABLED;
5199 }
5200
5201 /**
5202 * Set the enabled state of this view. The interpretation of the enabled
5203 * state varies by subclass.
5204 *
5205 * @param enabled True if this view is enabled, false otherwise.
5206 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005207 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005208 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005209 if (enabled == isEnabled()) return;
5210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005211 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5212
5213 /*
5214 * The View most likely has to change its appearance, so refresh
5215 * the drawable state.
5216 */
5217 refreshDrawableState();
5218
5219 // Invalidate too, since the default behavior for views is to be
5220 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005221 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005222 }
5223
5224 /**
5225 * Set whether this view can receive the focus.
5226 *
5227 * Setting this to false will also ensure that this view is not focusable
5228 * in touch mode.
5229 *
5230 * @param focusable If true, this view can receive the focus.
5231 *
5232 * @see #setFocusableInTouchMode(boolean)
5233 * @attr ref android.R.styleable#View_focusable
5234 */
5235 public void setFocusable(boolean focusable) {
5236 if (!focusable) {
5237 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5238 }
5239 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5240 }
5241
5242 /**
5243 * Set whether this view can receive focus while in touch mode.
5244 *
5245 * Setting this to true will also ensure that this view is focusable.
5246 *
5247 * @param focusableInTouchMode If true, this view can receive the focus while
5248 * in touch mode.
5249 *
5250 * @see #setFocusable(boolean)
5251 * @attr ref android.R.styleable#View_focusableInTouchMode
5252 */
5253 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5254 // Focusable in touch mode should always be set before the focusable flag
5255 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5256 // which, in touch mode, will not successfully request focus on this view
5257 // because the focusable in touch mode flag is not set
5258 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5259 if (focusableInTouchMode) {
5260 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5261 }
5262 }
5263
5264 /**
5265 * Set whether this view should have sound effects enabled for events such as
5266 * clicking and touching.
5267 *
5268 * <p>You may wish to disable sound effects for a view if you already play sounds,
5269 * for instance, a dial key that plays dtmf tones.
5270 *
5271 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5272 * @see #isSoundEffectsEnabled()
5273 * @see #playSoundEffect(int)
5274 * @attr ref android.R.styleable#View_soundEffectsEnabled
5275 */
5276 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5277 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5278 }
5279
5280 /**
5281 * @return whether this view should have sound effects enabled for events such as
5282 * clicking and touching.
5283 *
5284 * @see #setSoundEffectsEnabled(boolean)
5285 * @see #playSoundEffect(int)
5286 * @attr ref android.R.styleable#View_soundEffectsEnabled
5287 */
5288 @ViewDebug.ExportedProperty
5289 public boolean isSoundEffectsEnabled() {
5290 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5291 }
5292
5293 /**
5294 * Set whether this view should have haptic feedback for events such as
5295 * long presses.
5296 *
5297 * <p>You may wish to disable haptic feedback if your view already controls
5298 * its own haptic feedback.
5299 *
5300 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5301 * @see #isHapticFeedbackEnabled()
5302 * @see #performHapticFeedback(int)
5303 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5304 */
5305 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5306 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5307 }
5308
5309 /**
5310 * @return whether this view should have haptic feedback enabled for events
5311 * long presses.
5312 *
5313 * @see #setHapticFeedbackEnabled(boolean)
5314 * @see #performHapticFeedback(int)
5315 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5316 */
5317 @ViewDebug.ExportedProperty
5318 public boolean isHapticFeedbackEnabled() {
5319 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5320 }
5321
5322 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005323 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005324 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005325 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5326 * {@link #LAYOUT_DIRECTION_RTL},
5327 * {@link #LAYOUT_DIRECTION_INHERIT} or
5328 * {@link #LAYOUT_DIRECTION_LOCALE}.
5329 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005330 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005331 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005332 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5333 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5334 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5335 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005336 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005337 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005338 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005339 }
5340
5341 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005342 * Set the layout direction for this view. This will propagate a reset of layout direction
5343 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005344 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005345 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5346 * {@link #LAYOUT_DIRECTION_RTL},
5347 * {@link #LAYOUT_DIRECTION_INHERIT} or
5348 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005349 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005350 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005351 */
5352 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005353 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005354 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005355 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005356 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005357 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005358 // Set the new layout direction (filtered) and ask for a layout pass
5359 mPrivateFlags2 |=
5360 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5361 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005362 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005363 }
5364
5365 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005366 * Returns the resolved layout direction for this view.
5367 *
5368 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005369 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005370 */
5371 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005372 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5373 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005374 })
5375 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005376 // The layout diretion will be resolved only if needed
5377 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5378 resolveLayoutDirection();
5379 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005380 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005381 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5382 }
5383
5384 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005385 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5386 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005387 *
5388 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005389 */
5390 @ViewDebug.ExportedProperty(category = "layout")
5391 public boolean isLayoutRtl() {
5392 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5393 }
5394
5395 /**
Adam Powell539ee872012-02-03 19:00:49 -08005396 * Indicates whether the view is currently tracking transient state that the
5397 * app should not need to concern itself with saving and restoring, but that
5398 * the framework should take special note to preserve when possible.
5399 *
5400 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005401 */
5402 @ViewDebug.ExportedProperty(category = "layout")
5403 public boolean hasTransientState() {
5404 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5405 }
5406
5407 /**
5408 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005409 * framework should attempt to preserve when possible. This flag is reference counted,
5410 * so every call to setHasTransientState(true) should be paired with a later call
5411 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005412 *
5413 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005414 */
5415 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005416 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5417 mTransientStateCount - 1;
5418 if (mTransientStateCount < 0) {
5419 mTransientStateCount = 0;
5420 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5421 "unmatched pair of setHasTransientState calls");
5422 }
5423 if ((hasTransientState && mTransientStateCount == 1) ||
5424 (hasTransientState && mTransientStateCount == 0)) {
5425 // update flag if we've just incremented up from 0 or decremented down to 0
5426 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5427 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5428 if (mParent != null) {
5429 try {
5430 mParent.childHasTransientStateChanged(this, hasTransientState);
5431 } catch (AbstractMethodError e) {
5432 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5433 " does not fully implement ViewParent", e);
5434 }
Adam Powell539ee872012-02-03 19:00:49 -08005435 }
5436 }
5437 }
5438
5439 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005440 * If this view doesn't do any drawing on its own, set this flag to
5441 * allow further optimizations. By default, this flag is not set on
5442 * View, but could be set on some View subclasses such as ViewGroup.
5443 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005444 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5445 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005446 *
5447 * @param willNotDraw whether or not this View draw on its own
5448 */
5449 public void setWillNotDraw(boolean willNotDraw) {
5450 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5451 }
5452
5453 /**
5454 * Returns whether or not this View draws on its own.
5455 *
5456 * @return true if this view has nothing to draw, false otherwise
5457 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005458 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005459 public boolean willNotDraw() {
5460 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5461 }
5462
5463 /**
5464 * When a View's drawing cache is enabled, drawing is redirected to an
5465 * offscreen bitmap. Some views, like an ImageView, must be able to
5466 * bypass this mechanism if they already draw a single bitmap, to avoid
5467 * unnecessary usage of the memory.
5468 *
5469 * @param willNotCacheDrawing true if this view does not cache its
5470 * drawing, false otherwise
5471 */
5472 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5473 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5474 }
5475
5476 /**
5477 * Returns whether or not this View can cache its drawing or not.
5478 *
5479 * @return true if this view does not cache its drawing, false otherwise
5480 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005481 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005482 public boolean willNotCacheDrawing() {
5483 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5484 }
5485
5486 /**
5487 * Indicates whether this view reacts to click events or not.
5488 *
5489 * @return true if the view is clickable, false otherwise
5490 *
5491 * @see #setClickable(boolean)
5492 * @attr ref android.R.styleable#View_clickable
5493 */
5494 @ViewDebug.ExportedProperty
5495 public boolean isClickable() {
5496 return (mViewFlags & CLICKABLE) == CLICKABLE;
5497 }
5498
5499 /**
5500 * Enables or disables click events for this view. When a view
5501 * is clickable it will change its state to "pressed" on every click.
5502 * Subclasses should set the view clickable to visually react to
5503 * user's clicks.
5504 *
5505 * @param clickable true to make the view clickable, false otherwise
5506 *
5507 * @see #isClickable()
5508 * @attr ref android.R.styleable#View_clickable
5509 */
5510 public void setClickable(boolean clickable) {
5511 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5512 }
5513
5514 /**
5515 * Indicates whether this view reacts to long click events or not.
5516 *
5517 * @return true if the view is long clickable, false otherwise
5518 *
5519 * @see #setLongClickable(boolean)
5520 * @attr ref android.R.styleable#View_longClickable
5521 */
5522 public boolean isLongClickable() {
5523 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5524 }
5525
5526 /**
5527 * Enables or disables long click events for this view. When a view is long
5528 * clickable it reacts to the user holding down the button for a longer
5529 * duration than a tap. This event can either launch the listener or a
5530 * context menu.
5531 *
5532 * @param longClickable true to make the view long clickable, false otherwise
5533 * @see #isLongClickable()
5534 * @attr ref android.R.styleable#View_longClickable
5535 */
5536 public void setLongClickable(boolean longClickable) {
5537 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5538 }
5539
5540 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005541 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005542 *
5543 * @see #isClickable()
5544 * @see #setClickable(boolean)
5545 *
5546 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5547 * the View's internal state from a previously set "pressed" state.
5548 */
5549 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005550 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005552 if (pressed) {
5553 mPrivateFlags |= PRESSED;
5554 } else {
5555 mPrivateFlags &= ~PRESSED;
5556 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005557
5558 if (needsRefresh) {
5559 refreshDrawableState();
5560 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005561 dispatchSetPressed(pressed);
5562 }
5563
5564 /**
5565 * Dispatch setPressed to all of this View's children.
5566 *
5567 * @see #setPressed(boolean)
5568 *
5569 * @param pressed The new pressed state
5570 */
5571 protected void dispatchSetPressed(boolean pressed) {
5572 }
5573
5574 /**
5575 * Indicates whether the view is currently in pressed state. Unless
5576 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5577 * the pressed state.
5578 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005579 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005580 * @see #isClickable()
5581 * @see #setClickable(boolean)
5582 *
5583 * @return true if the view is currently pressed, false otherwise
5584 */
5585 public boolean isPressed() {
5586 return (mPrivateFlags & PRESSED) == PRESSED;
5587 }
5588
5589 /**
5590 * Indicates whether this view will save its state (that is,
5591 * whether its {@link #onSaveInstanceState} method will be called).
5592 *
5593 * @return Returns true if the view state saving is enabled, else false.
5594 *
5595 * @see #setSaveEnabled(boolean)
5596 * @attr ref android.R.styleable#View_saveEnabled
5597 */
5598 public boolean isSaveEnabled() {
5599 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5600 }
5601
5602 /**
5603 * Controls whether the saving of this view's state is
5604 * enabled (that is, whether its {@link #onSaveInstanceState} method
5605 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005606 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005607 * for its state to be saved. This flag can only disable the
5608 * saving of this view; any child views may still have their state saved.
5609 *
5610 * @param enabled Set to false to <em>disable</em> state saving, or true
5611 * (the default) to allow it.
5612 *
5613 * @see #isSaveEnabled()
5614 * @see #setId(int)
5615 * @see #onSaveInstanceState()
5616 * @attr ref android.R.styleable#View_saveEnabled
5617 */
5618 public void setSaveEnabled(boolean enabled) {
5619 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5620 }
5621
Jeff Brown85a31762010-09-01 17:01:00 -07005622 /**
5623 * Gets whether the framework should discard touches when the view's
5624 * window is obscured by another visible window.
5625 * Refer to the {@link View} security documentation for more details.
5626 *
5627 * @return True if touch filtering is enabled.
5628 *
5629 * @see #setFilterTouchesWhenObscured(boolean)
5630 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5631 */
5632 @ViewDebug.ExportedProperty
5633 public boolean getFilterTouchesWhenObscured() {
5634 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5635 }
5636
5637 /**
5638 * Sets whether the framework should discard touches when the view's
5639 * window is obscured by another visible window.
5640 * Refer to the {@link View} security documentation for more details.
5641 *
5642 * @param enabled True if touch filtering should be enabled.
5643 *
5644 * @see #getFilterTouchesWhenObscured
5645 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5646 */
5647 public void setFilterTouchesWhenObscured(boolean enabled) {
5648 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5649 FILTER_TOUCHES_WHEN_OBSCURED);
5650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005651
5652 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005653 * Indicates whether the entire hierarchy under this view will save its
5654 * state when a state saving traversal occurs from its parent. The default
5655 * is true; if false, these views will not be saved unless
5656 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5657 *
5658 * @return Returns true if the view state saving from parent is enabled, else false.
5659 *
5660 * @see #setSaveFromParentEnabled(boolean)
5661 */
5662 public boolean isSaveFromParentEnabled() {
5663 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5664 }
5665
5666 /**
5667 * Controls whether the entire hierarchy under this view will save its
5668 * state when a state saving traversal occurs from its parent. The default
5669 * is true; if false, these views will not be saved unless
5670 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5671 *
5672 * @param enabled Set to false to <em>disable</em> state saving, or true
5673 * (the default) to allow it.
5674 *
5675 * @see #isSaveFromParentEnabled()
5676 * @see #setId(int)
5677 * @see #onSaveInstanceState()
5678 */
5679 public void setSaveFromParentEnabled(boolean enabled) {
5680 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5681 }
5682
5683
5684 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005685 * Returns whether this View is able to take focus.
5686 *
5687 * @return True if this view can take focus, or false otherwise.
5688 * @attr ref android.R.styleable#View_focusable
5689 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005690 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005691 public final boolean isFocusable() {
5692 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5693 }
5694
5695 /**
5696 * When a view is focusable, it may not want to take focus when in touch mode.
5697 * For example, a button would like focus when the user is navigating via a D-pad
5698 * so that the user can click on it, but once the user starts touching the screen,
5699 * the button shouldn't take focus
5700 * @return Whether the view is focusable in touch mode.
5701 * @attr ref android.R.styleable#View_focusableInTouchMode
5702 */
5703 @ViewDebug.ExportedProperty
5704 public final boolean isFocusableInTouchMode() {
5705 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5706 }
5707
5708 /**
5709 * Find the nearest view in the specified direction that can take focus.
5710 * This does not actually give focus to that view.
5711 *
5712 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5713 *
5714 * @return The nearest focusable in the specified direction, or null if none
5715 * can be found.
5716 */
5717 public View focusSearch(int direction) {
5718 if (mParent != null) {
5719 return mParent.focusSearch(this, direction);
5720 } else {
5721 return null;
5722 }
5723 }
5724
5725 /**
5726 * This method is the last chance for the focused view and its ancestors to
5727 * respond to an arrow key. This is called when the focused view did not
5728 * consume the key internally, nor could the view system find a new view in
5729 * the requested direction to give focus to.
5730 *
5731 * @param focused The currently focused view.
5732 * @param direction The direction focus wants to move. One of FOCUS_UP,
5733 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5734 * @return True if the this view consumed this unhandled move.
5735 */
5736 public boolean dispatchUnhandledMove(View focused, int direction) {
5737 return false;
5738 }
5739
5740 /**
5741 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005742 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005743 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005744 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5745 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005746 * @return The user specified next view, or null if there is none.
5747 */
5748 View findUserSetNextFocus(View root, int direction) {
5749 switch (direction) {
5750 case FOCUS_LEFT:
5751 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005752 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005753 case FOCUS_RIGHT:
5754 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005755 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005756 case FOCUS_UP:
5757 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005758 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005759 case FOCUS_DOWN:
5760 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005761 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005762 case FOCUS_FORWARD:
5763 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005764 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005765 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005766 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005767 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005768 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005769 @Override
5770 public boolean apply(View t) {
5771 return t.mNextFocusForwardId == id;
5772 }
5773 });
5774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005775 }
5776 return null;
5777 }
5778
Jeff Brown4dfbec22011-08-15 14:55:37 -07005779 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5780 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5781 @Override
5782 public boolean apply(View t) {
5783 return t.mID == childViewId;
5784 }
5785 });
5786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005787 if (result == null) {
5788 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5789 + "by user for id " + childViewId);
5790 }
5791 return result;
5792 }
5793
5794 /**
5795 * Find and return all focusable views that are descendants of this view,
5796 * possibly including this view if it is focusable itself.
5797 *
5798 * @param direction The direction of the focus
5799 * @return A list of focusable views
5800 */
5801 public ArrayList<View> getFocusables(int direction) {
5802 ArrayList<View> result = new ArrayList<View>(24);
5803 addFocusables(result, direction);
5804 return result;
5805 }
5806
5807 /**
5808 * Add any focusable views that are descendants of this view (possibly
5809 * including this view if it is focusable itself) to views. If we are in touch mode,
5810 * only add views that are also focusable in touch mode.
5811 *
5812 * @param views Focusable views found so far
5813 * @param direction The direction of the focus
5814 */
5815 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005816 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005818
svetoslavganov75986cf2009-05-14 22:28:01 -07005819 /**
5820 * Adds any focusable views that are descendants of this view (possibly
5821 * including this view if it is focusable itself) to views. This method
5822 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07005823 * only views focusable in touch mode if we are in touch mode or
5824 * only views that can take accessibility focus if accessibility is enabeld
5825 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07005826 *
5827 * @param views Focusable views found so far or null if all we are interested is
5828 * the number of focusables.
5829 * @param direction The direction of the focus.
5830 * @param focusableMode The type of focusables to be added.
5831 *
5832 * @see #FOCUSABLES_ALL
5833 * @see #FOCUSABLES_TOUCH_MODE
5834 */
5835 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07005836 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005837 return;
5838 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07005839 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
5840 if (AccessibilityManager.getInstance(mContext).isEnabled()
5841 && includeForAccessibility()) {
5842 views.add(this);
5843 return;
5844 }
5845 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005846 if (!isFocusable()) {
5847 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07005848 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005849 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
5850 && isInTouchMode() && !isFocusableInTouchMode()) {
5851 return;
5852 }
5853 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005854 }
5855
5856 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005857 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005858 * The search is performed by either the text that the View renders or the content
5859 * description that describes the view for accessibility purposes and the view does
5860 * not render or both. Clients can specify how the search is to be performed via
5861 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5862 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005863 *
5864 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005865 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005866 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005867 * @see #FIND_VIEWS_WITH_TEXT
5868 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5869 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005870 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005871 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005872 if (getAccessibilityNodeProvider() != null) {
5873 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5874 outViews.add(this);
5875 }
5876 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5877 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005878 String searchedLowerCase = searched.toString().toLowerCase();
5879 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5880 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5881 outViews.add(this);
5882 }
5883 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005884 }
5885
5886 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005887 * Find and return all touchable views that are descendants of this view,
5888 * possibly including this view if it is touchable itself.
5889 *
5890 * @return A list of touchable views
5891 */
5892 public ArrayList<View> getTouchables() {
5893 ArrayList<View> result = new ArrayList<View>();
5894 addTouchables(result);
5895 return result;
5896 }
5897
5898 /**
5899 * Add any touchable views that are descendants of this view (possibly
5900 * including this view if it is touchable itself) to views.
5901 *
5902 * @param views Touchable views found so far
5903 */
5904 public void addTouchables(ArrayList<View> views) {
5905 final int viewFlags = mViewFlags;
5906
5907 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5908 && (viewFlags & ENABLED_MASK) == ENABLED) {
5909 views.add(this);
5910 }
5911 }
5912
5913 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07005914 * Returns whether this View is accessibility focused.
5915 *
5916 * @return True if this View is accessibility focused.
5917 */
5918 boolean isAccessibilityFocused() {
5919 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
5920 }
5921
5922 /**
5923 * Call this to try to give accessibility focus to this view.
5924 *
5925 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
5926 * returns false or the view is no visible or the view already has accessibility
5927 * focus.
5928 *
5929 * See also {@link #focusSearch(int)}, which is what you call to say that you
5930 * have focus, and you want your parent to look for the next one.
5931 *
5932 * @return Whether this view actually took accessibility focus.
5933 *
5934 * @hide
5935 */
5936 public boolean requestAccessibilityFocus() {
5937 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
5938 return false;
5939 }
5940 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5941 return false;
5942 }
5943 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
5944 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
5945 ViewRootImpl viewRootImpl = getViewRootImpl();
5946 if (viewRootImpl != null) {
5947 viewRootImpl.setAccessibilityFocusedHost(this);
5948 }
5949 invalidate();
5950 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
5951 notifyAccessibilityStateChanged();
5952 // Try to give input focus to this view - not a descendant.
5953 requestFocusNoSearch(View.FOCUS_DOWN, null);
5954 return true;
5955 }
5956 return false;
5957 }
5958
5959 /**
5960 * Call this to try to clear accessibility focus of this view.
5961 *
5962 * See also {@link #focusSearch(int)}, which is what you call to say that you
5963 * have focus, and you want your parent to look for the next one.
5964 *
5965 * @hide
5966 */
5967 public void clearAccessibilityFocus() {
5968 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
5969 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
5970 ViewRootImpl viewRootImpl = getViewRootImpl();
5971 if (viewRootImpl != null) {
5972 viewRootImpl.setAccessibilityFocusedHost(null);
5973 }
5974 invalidate();
5975 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
5976 notifyAccessibilityStateChanged();
5977 // Try to move accessibility focus to the input focus.
5978 View rootView = getRootView();
5979 if (rootView != null) {
5980 View inputFocus = rootView.findFocus();
5981 if (inputFocus != null) {
5982 inputFocus.requestAccessibilityFocus();
5983 }
5984 }
5985 }
5986 }
5987
5988 /**
5989 * Find the best view to take accessibility focus from a hover.
5990 * This function finds the deepest actionable view and if that
5991 * fails ask the parent to take accessibility focus from hover.
5992 *
5993 * @param x The X hovered location in this view coorditantes.
5994 * @param y The Y hovered location in this view coorditantes.
5995 * @return Whether the request was handled.
5996 *
5997 * @hide
5998 */
5999 public boolean requestAccessibilityFocusFromHover(float x, float y) {
6000 if (onRequestAccessibilityFocusFromHover(x, y)) {
6001 return true;
6002 }
6003 ViewParent parent = mParent;
6004 if (parent instanceof View) {
6005 View parentView = (View) parent;
6006
6007 float[] position = mAttachInfo.mTmpTransformLocation;
6008 position[0] = x;
6009 position[1] = y;
6010
6011 // Compensate for the transformation of the current matrix.
6012 if (!hasIdentityMatrix()) {
6013 getMatrix().mapPoints(position);
6014 }
6015
6016 // Compensate for the parent scroll and the offset
6017 // of this view stop from the parent top.
6018 position[0] += mLeft - parentView.mScrollX;
6019 position[1] += mTop - parentView.mScrollY;
6020
6021 return parentView.requestAccessibilityFocusFromHover(position[0], position[1]);
6022 }
6023 return false;
6024 }
6025
6026 /**
6027 * Requests to give this View focus from hover.
6028 *
6029 * @param x The X hovered location in this view coorditantes.
6030 * @param y The Y hovered location in this view coorditantes.
6031 * @return Whether the request was handled.
6032 *
6033 * @hide
6034 */
6035 public boolean onRequestAccessibilityFocusFromHover(float x, float y) {
6036 if (includeForAccessibility()
6037 && (isActionableForAccessibility() || hasListenersForAccessibility())) {
6038 return requestAccessibilityFocus();
6039 }
6040 return false;
6041 }
6042
6043 /**
6044 * Clears accessibility focus without calling any callback methods
6045 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6046 * is used for clearing accessibility focus when giving this focus to
6047 * another view.
6048 */
6049 void clearAccessibilityFocusNoCallbacks() {
6050 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6051 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6052 invalidate();
6053 }
6054 }
6055
6056 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006057 * Call this to try to give focus to a specific view or to one of its
6058 * descendants.
6059 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006060 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6061 * false), or if it is focusable and it is not focusable in touch mode
6062 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006063 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006064 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006065 * have focus, and you want your parent to look for the next one.
6066 *
6067 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6068 * {@link #FOCUS_DOWN} and <code>null</code>.
6069 *
6070 * @return Whether this view or one of its descendants actually took focus.
6071 */
6072 public final boolean requestFocus() {
6073 return requestFocus(View.FOCUS_DOWN);
6074 }
6075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006076 /**
6077 * Call this to try to give focus to a specific view or to one of its
6078 * descendants and give it a hint about what direction focus is heading.
6079 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006080 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6081 * false), or if it is focusable and it is not focusable in touch mode
6082 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006083 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006084 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006085 * have focus, and you want your parent to look for the next one.
6086 *
6087 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6088 * <code>null</code> set for the previously focused rectangle.
6089 *
6090 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6091 * @return Whether this view or one of its descendants actually took focus.
6092 */
6093 public final boolean requestFocus(int direction) {
6094 return requestFocus(direction, null);
6095 }
6096
6097 /**
6098 * Call this to try to give focus to a specific view or to one of its descendants
6099 * and give it hints about the direction and a specific rectangle that the focus
6100 * is coming from. The rectangle can help give larger views a finer grained hint
6101 * about where focus is coming from, and therefore, where to show selection, or
6102 * forward focus change internally.
6103 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006104 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6105 * false), or if it is focusable and it is not focusable in touch mode
6106 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006107 *
6108 * A View will not take focus if it is not visible.
6109 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006110 * A View will not take focus if one of its parents has
6111 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6112 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006113 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006114 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006115 * have focus, and you want your parent to look for the next one.
6116 *
6117 * You may wish to override this method if your custom {@link View} has an internal
6118 * {@link View} that it wishes to forward the request to.
6119 *
6120 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6121 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6122 * to give a finer grained hint about where focus is coming from. May be null
6123 * if there is no hint.
6124 * @return Whether this view or one of its descendants actually took focus.
6125 */
6126 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006127 return requestFocusNoSearch(direction, previouslyFocusedRect);
6128 }
6129
6130 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006131 // need to be focusable
6132 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6133 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6134 return false;
6135 }
6136
6137 // need to be focusable in touch mode if in touch mode
6138 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006139 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6140 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006141 }
6142
6143 // need to not have any parents blocking us
6144 if (hasAncestorThatBlocksDescendantFocus()) {
6145 return false;
6146 }
6147
6148 handleFocusGainInternal(direction, previouslyFocusedRect);
6149 return true;
6150 }
6151
6152 /**
6153 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6154 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6155 * touch mode to request focus when they are touched.
6156 *
6157 * @return Whether this view or one of its descendants actually took focus.
6158 *
6159 * @see #isInTouchMode()
6160 *
6161 */
6162 public final boolean requestFocusFromTouch() {
6163 // Leave touch mode if we need to
6164 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006165 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006166 if (viewRoot != null) {
6167 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006168 }
6169 }
6170 return requestFocus(View.FOCUS_DOWN);
6171 }
6172
6173 /**
6174 * @return Whether any ancestor of this view blocks descendant focus.
6175 */
6176 private boolean hasAncestorThatBlocksDescendantFocus() {
6177 ViewParent ancestor = mParent;
6178 while (ancestor instanceof ViewGroup) {
6179 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6180 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6181 return true;
6182 } else {
6183 ancestor = vgAncestor.getParent();
6184 }
6185 }
6186 return false;
6187 }
6188
6189 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006190 * Gets the mode for determining whether this View is important for accessibility
6191 * which is if it fires accessibility events and if it is reported to
6192 * accessibility services that query the screen.
6193 *
6194 * @return The mode for determining whether a View is important for accessibility.
6195 *
6196 * @attr ref android.R.styleable#View_importantForAccessibility
6197 *
6198 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6199 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6200 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6201 */
6202 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6203 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO,
6204 to = "IMPORTANT_FOR_ACCESSIBILITY_AUTO"),
6205 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES,
6206 to = "IMPORTANT_FOR_ACCESSIBILITY_YES"),
6207 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO,
6208 to = "IMPORTANT_FOR_ACCESSIBILITY_NO")
6209 })
6210 public int getImportantForAccessibility() {
6211 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6212 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6213 }
6214
6215 /**
6216 * Sets how to determine whether this view is important for accessibility
6217 * which is if it fires accessibility events and if it is reported to
6218 * accessibility services that query the screen.
6219 *
6220 * @param mode How to determine whether this view is important for accessibility.
6221 *
6222 * @attr ref android.R.styleable#View_importantForAccessibility
6223 *
6224 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6225 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6226 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6227 */
6228 public void setImportantForAccessibility(int mode) {
6229 if (mode != getImportantForAccessibility()) {
6230 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6231 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6232 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6233 notifyAccessibilityStateChanged();
6234 }
6235 }
6236
6237 /**
6238 * Gets whether this view should be exposed for accessibility.
6239 *
6240 * @return Whether the view is exposed for accessibility.
6241 *
6242 * @hide
6243 */
6244 public boolean isImportantForAccessibility() {
6245 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6246 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6247 switch (mode) {
6248 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6249 return true;
6250 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6251 return false;
6252 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6253 return isActionableForAccessibility() || hasListenersForAccessibility();
6254 default:
6255 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6256 + mode);
6257 }
6258 }
6259
6260 /**
6261 * Gets the parent for accessibility purposes. Note that the parent for
6262 * accessibility is not necessary the immediate parent. It is the first
6263 * predecessor that is important for accessibility.
6264 *
6265 * @return The parent for accessibility purposes.
6266 */
6267 public ViewParent getParentForAccessibility() {
6268 if (mParent instanceof View) {
6269 View parentView = (View) mParent;
6270 if (parentView.includeForAccessibility()) {
6271 return mParent;
6272 } else {
6273 return mParent.getParentForAccessibility();
6274 }
6275 }
6276 return null;
6277 }
6278
6279 /**
6280 * Adds the children of a given View for accessibility. Since some Views are
6281 * not important for accessibility the children for accessibility are not
6282 * necessarily direct children of the riew, rather they are the first level of
6283 * descendants important for accessibility.
6284 *
6285 * @param children The list of children for accessibility.
6286 */
6287 public void addChildrenForAccessibility(ArrayList<View> children) {
6288 if (includeForAccessibility()) {
6289 children.add(this);
6290 }
6291 }
6292
6293 /**
6294 * Whether to regard this view for accessibility. A view is regarded for
6295 * accessibility if it is important for accessibility or the querying
6296 * accessibility service has explicitly requested that view not
6297 * important for accessibility are regarded.
6298 *
6299 * @return Whether to regard the view for accessibility.
6300 */
6301 boolean includeForAccessibility() {
6302 if (mAttachInfo != null) {
6303 if (!mAttachInfo.mIncludeNotImportantViews) {
6304 return isImportantForAccessibility();
6305 } else {
6306 return true;
6307 }
6308 }
6309 return false;
6310 }
6311
6312 /**
6313 * Returns whether the View is considered actionable from
6314 * accessibility perspective. Such view are important for
6315 * accessiiblity.
6316 *
6317 * @return True if the view is actionable for accessibility.
6318 */
6319 private boolean isActionableForAccessibility() {
6320 return (isClickable() || isLongClickable() || isFocusable());
6321 }
6322
6323 /**
6324 * Returns whether the View has registered callbacks wich makes it
6325 * important for accessiiblity.
6326 *
6327 * @return True if the view is actionable for accessibility.
6328 */
6329 private boolean hasListenersForAccessibility() {
6330 ListenerInfo info = getListenerInfo();
6331 return mTouchDelegate != null || info.mOnKeyListener != null
6332 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6333 || info.mOnHoverListener != null || info.mOnDragListener != null;
6334 }
6335
6336 /**
6337 * Notifies accessibility services that some view's important for
6338 * accessibility state has changed. Note that such notifications
6339 * are made at most once every
6340 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6341 * to avoid unnecessary load to the system. Also once a view has
6342 * made a notifucation this method is a NOP until the notification has
6343 * been sent to clients.
6344 *
6345 * @hide
6346 *
6347 * TODO: Makse sure this method is called for any view state change
6348 * that is interesting for accessilility purposes.
6349 */
6350 public void notifyAccessibilityStateChanged() {
6351 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6352 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6353 if (mParent != null) {
6354 mParent.childAccessibilityStateChanged(this);
6355 }
6356 }
6357 }
6358
6359 /**
6360 * Reset the state indicating the this view has requested clients
6361 * interested in its accessiblity state to be notified.
6362 *
6363 * @hide
6364 */
6365 public void resetAccessibilityStateChanged() {
6366 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6367 }
6368
6369 /**
6370 * Performs the specified accessibility action on the view. For
6371 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
6372 *
6373 * @param action The action to perform.
6374 * @return Whether the action was performed.
6375 */
6376 public boolean performAccessibilityAction(int action) {
6377 switch (action) {
6378 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006379 if (isClickable()) {
6380 performClick();
6381 }
6382 } break;
6383 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6384 if (isLongClickable()) {
6385 performLongClick();
6386 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006387 } break;
6388 case AccessibilityNodeInfo.ACTION_FOCUS: {
6389 if (!hasFocus()) {
6390 // Get out of touch mode since accessibility
6391 // wants to move focus around.
6392 getViewRootImpl().ensureTouchMode(false);
6393 return requestFocus();
6394 }
6395 } break;
6396 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6397 if (hasFocus()) {
6398 clearFocus();
6399 return !isFocused();
6400 }
6401 } break;
6402 case AccessibilityNodeInfo.ACTION_SELECT: {
6403 if (!isSelected()) {
6404 setSelected(true);
6405 return isSelected();
6406 }
6407 } break;
6408 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6409 if (isSelected()) {
6410 setSelected(false);
6411 return !isSelected();
6412 }
6413 } break;
6414 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
6415 if (!isAccessibilityFocused()) {
6416 return requestAccessibilityFocus();
6417 }
6418 } break;
6419 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6420 if (isAccessibilityFocused()) {
6421 clearAccessibilityFocus();
6422 return true;
6423 }
6424 } break;
6425 }
6426 return false;
6427 }
6428
6429 /**
Romain Guya440b002010-02-24 15:57:54 -08006430 * @hide
6431 */
6432 public void dispatchStartTemporaryDetach() {
6433 onStartTemporaryDetach();
6434 }
6435
6436 /**
6437 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006438 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6439 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006440 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006441 */
6442 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006443 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006444 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006445 }
6446
6447 /**
6448 * @hide
6449 */
6450 public void dispatchFinishTemporaryDetach() {
6451 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006452 }
Romain Guy8506ab42009-06-11 17:35:47 -07006453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006454 /**
6455 * Called after {@link #onStartTemporaryDetach} when the container is done
6456 * changing the view.
6457 */
6458 public void onFinishTemporaryDetach() {
6459 }
Romain Guy8506ab42009-06-11 17:35:47 -07006460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006461 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006462 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6463 * for this view's window. Returns null if the view is not currently attached
6464 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006465 * just use the standard high-level event callbacks like
6466 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006467 */
6468 public KeyEvent.DispatcherState getKeyDispatcherState() {
6469 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6470 }
Joe Malin32736f02011-01-19 16:14:20 -08006471
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006472 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006473 * Dispatch a key event before it is processed by any input method
6474 * associated with the view hierarchy. This can be used to intercept
6475 * key events in special situations before the IME consumes them; a
6476 * typical example would be handling the BACK key to update the application's
6477 * UI instead of allowing the IME to see it and close itself.
6478 *
6479 * @param event The key event to be dispatched.
6480 * @return True if the event was handled, false otherwise.
6481 */
6482 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6483 return onKeyPreIme(event.getKeyCode(), event);
6484 }
6485
6486 /**
6487 * Dispatch a key event to the next view on the focus path. This path runs
6488 * from the top of the view tree down to the currently focused view. If this
6489 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6490 * the next node down the focus path. This method also fires any key
6491 * listeners.
6492 *
6493 * @param event The key event to be dispatched.
6494 * @return True if the event was handled, false otherwise.
6495 */
6496 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006497 if (mInputEventConsistencyVerifier != null) {
6498 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6499 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006500
Jeff Brown21bc5c92011-02-28 18:27:14 -08006501 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006502 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006503 ListenerInfo li = mListenerInfo;
6504 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6505 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006506 return true;
6507 }
6508
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006509 if (event.dispatch(this, mAttachInfo != null
6510 ? mAttachInfo.mKeyDispatchState : null, this)) {
6511 return true;
6512 }
6513
6514 if (mInputEventConsistencyVerifier != null) {
6515 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6516 }
6517 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006518 }
6519
6520 /**
6521 * Dispatches a key shortcut event.
6522 *
6523 * @param event The key event to be dispatched.
6524 * @return True if the event was handled by the view, false otherwise.
6525 */
6526 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6527 return onKeyShortcut(event.getKeyCode(), event);
6528 }
6529
6530 /**
6531 * Pass the touch screen motion event down to the target view, or this
6532 * view if it is the target.
6533 *
6534 * @param event The motion event to be dispatched.
6535 * @return True if the event was handled by the view, false otherwise.
6536 */
6537 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006538 if (mInputEventConsistencyVerifier != null) {
6539 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6540 }
6541
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006542 if (onFilterTouchEventForSecurity(event)) {
6543 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006544 ListenerInfo li = mListenerInfo;
6545 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6546 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006547 return true;
6548 }
6549
6550 if (onTouchEvent(event)) {
6551 return true;
6552 }
Jeff Brown85a31762010-09-01 17:01:00 -07006553 }
6554
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006555 if (mInputEventConsistencyVerifier != null) {
6556 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006557 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006558 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006559 }
6560
6561 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006562 * Filter the touch event to apply security policies.
6563 *
6564 * @param event The motion event to be filtered.
6565 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006566 *
Jeff Brown85a31762010-09-01 17:01:00 -07006567 * @see #getFilterTouchesWhenObscured
6568 */
6569 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006570 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006571 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6572 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6573 // Window is obscured, drop this touch.
6574 return false;
6575 }
6576 return true;
6577 }
6578
6579 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006580 * Pass a trackball motion event down to the focused view.
6581 *
6582 * @param event The motion event to be dispatched.
6583 * @return True if the event was handled by the view, false otherwise.
6584 */
6585 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006586 if (mInputEventConsistencyVerifier != null) {
6587 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6588 }
6589
Romain Guy02ccac62011-06-24 13:20:23 -07006590 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006591 }
6592
6593 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006594 * Dispatch a generic motion event.
6595 * <p>
6596 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6597 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006598 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006599 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006600 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006601 *
6602 * @param event The motion event to be dispatched.
6603 * @return True if the event was handled by the view, false otherwise.
6604 */
6605 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006606 if (mInputEventConsistencyVerifier != null) {
6607 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6608 }
6609
Jeff Browna032cc02011-03-07 16:56:21 -08006610 final int source = event.getSource();
6611 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6612 final int action = event.getAction();
6613 if (action == MotionEvent.ACTION_HOVER_ENTER
6614 || action == MotionEvent.ACTION_HOVER_MOVE
6615 || action == MotionEvent.ACTION_HOVER_EXIT) {
6616 if (dispatchHoverEvent(event)) {
6617 return true;
6618 }
6619 } else if (dispatchGenericPointerEvent(event)) {
6620 return true;
6621 }
6622 } else if (dispatchGenericFocusedEvent(event)) {
6623 return true;
6624 }
6625
Jeff Brown10b62902011-06-20 16:40:37 -07006626 if (dispatchGenericMotionEventInternal(event)) {
6627 return true;
6628 }
6629
6630 if (mInputEventConsistencyVerifier != null) {
6631 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6632 }
6633 return false;
6634 }
6635
6636 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07006637 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006638 ListenerInfo li = mListenerInfo;
6639 if (li != null && li.mOnGenericMotionListener != null
6640 && (mViewFlags & ENABLED_MASK) == ENABLED
6641 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006642 return true;
6643 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006644
6645 if (onGenericMotionEvent(event)) {
6646 return true;
6647 }
6648
6649 if (mInputEventConsistencyVerifier != null) {
6650 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6651 }
6652 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006653 }
6654
6655 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006656 * Dispatch a hover event.
6657 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07006658 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07006659 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006660 * </p>
6661 *
6662 * @param event The motion event to be dispatched.
6663 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006664 */
6665 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07006666 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006667 ListenerInfo li = mListenerInfo;
6668 if (li != null && li.mOnHoverListener != null
6669 && (mViewFlags & ENABLED_MASK) == ENABLED
6670 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07006671 return true;
6672 }
6673
Jeff Browna032cc02011-03-07 16:56:21 -08006674 return onHoverEvent(event);
6675 }
6676
6677 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006678 * Returns true if the view has a child to which it has recently sent
6679 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
6680 * it does not have a hovered child, then it must be the innermost hovered view.
6681 * @hide
6682 */
6683 protected boolean hasHoveredChild() {
6684 return false;
6685 }
6686
6687 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006688 * Dispatch a generic motion event to the view under the first pointer.
6689 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006690 * Do not call this method directly.
6691 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006692 * </p>
6693 *
6694 * @param event The motion event to be dispatched.
6695 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006696 */
6697 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
6698 return false;
6699 }
6700
6701 /**
6702 * Dispatch a generic motion event to the currently focused view.
6703 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006704 * Do not call this method directly.
6705 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006706 * </p>
6707 *
6708 * @param event The motion event to be dispatched.
6709 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006710 */
6711 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
6712 return false;
6713 }
6714
6715 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006716 * Dispatch a pointer event.
6717 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006718 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
6719 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
6720 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08006721 * and should not be expected to handle other pointing device features.
6722 * </p>
6723 *
6724 * @param event The motion event to be dispatched.
6725 * @return True if the event was handled by the view, false otherwise.
6726 * @hide
6727 */
6728 public final boolean dispatchPointerEvent(MotionEvent event) {
6729 if (event.isTouchEvent()) {
6730 return dispatchTouchEvent(event);
6731 } else {
6732 return dispatchGenericMotionEvent(event);
6733 }
6734 }
6735
6736 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006737 * Called when the window containing this view gains or loses window focus.
6738 * ViewGroups should override to route to their children.
6739 *
6740 * @param hasFocus True if the window containing this view now has focus,
6741 * false otherwise.
6742 */
6743 public void dispatchWindowFocusChanged(boolean hasFocus) {
6744 onWindowFocusChanged(hasFocus);
6745 }
6746
6747 /**
6748 * Called when the window containing this view gains or loses focus. Note
6749 * that this is separate from view focus: to receive key events, both
6750 * your view and its window must have focus. If a window is displayed
6751 * on top of yours that takes input focus, then your own window will lose
6752 * focus but the view focus will remain unchanged.
6753 *
6754 * @param hasWindowFocus True if the window containing this view now has
6755 * focus, false otherwise.
6756 */
6757 public void onWindowFocusChanged(boolean hasWindowFocus) {
6758 InputMethodManager imm = InputMethodManager.peekInstance();
6759 if (!hasWindowFocus) {
6760 if (isPressed()) {
6761 setPressed(false);
6762 }
6763 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6764 imm.focusOut(this);
6765 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006766 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08006767 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07006768 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006769 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6770 imm.focusIn(this);
6771 }
6772 refreshDrawableState();
6773 }
6774
6775 /**
6776 * Returns true if this view is in a window that currently has window focus.
6777 * Note that this is not the same as the view itself having focus.
6778 *
6779 * @return True if this view is in a window that currently has window focus.
6780 */
6781 public boolean hasWindowFocus() {
6782 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
6783 }
6784
6785 /**
Adam Powell326d8082009-12-09 15:10:07 -08006786 * Dispatch a view visibility change down the view hierarchy.
6787 * ViewGroups should override to route to their children.
6788 * @param changedView The view whose visibility changed. Could be 'this' or
6789 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006790 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6791 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006792 */
6793 protected void dispatchVisibilityChanged(View changedView, int visibility) {
6794 onVisibilityChanged(changedView, visibility);
6795 }
6796
6797 /**
6798 * Called when the visibility of the view or an ancestor of the view is changed.
6799 * @param changedView The view whose visibility changed. Could be 'this' or
6800 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006801 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6802 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006803 */
6804 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006805 if (visibility == VISIBLE) {
6806 if (mAttachInfo != null) {
6807 initialAwakenScrollBars();
6808 } else {
6809 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
6810 }
6811 }
Adam Powell326d8082009-12-09 15:10:07 -08006812 }
6813
6814 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08006815 * Dispatch a hint about whether this view is displayed. For instance, when
6816 * a View moves out of the screen, it might receives a display hint indicating
6817 * the view is not displayed. Applications should not <em>rely</em> on this hint
6818 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006819 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006820 * @param hint A hint about whether or not this view is displayed:
6821 * {@link #VISIBLE} or {@link #INVISIBLE}.
6822 */
6823 public void dispatchDisplayHint(int hint) {
6824 onDisplayHint(hint);
6825 }
6826
6827 /**
6828 * Gives this view a hint about whether is displayed or not. For instance, when
6829 * a View moves out of the screen, it might receives a display hint indicating
6830 * the view is not displayed. Applications should not <em>rely</em> on this hint
6831 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006832 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006833 * @param hint A hint about whether or not this view is displayed:
6834 * {@link #VISIBLE} or {@link #INVISIBLE}.
6835 */
6836 protected void onDisplayHint(int hint) {
6837 }
6838
6839 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006840 * Dispatch a window visibility change down the view hierarchy.
6841 * ViewGroups should override to route to their children.
6842 *
6843 * @param visibility The new visibility of the window.
6844 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006845 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006846 */
6847 public void dispatchWindowVisibilityChanged(int visibility) {
6848 onWindowVisibilityChanged(visibility);
6849 }
6850
6851 /**
6852 * Called when the window containing has change its visibility
6853 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
6854 * that this tells you whether or not your window is being made visible
6855 * to the window manager; this does <em>not</em> tell you whether or not
6856 * your window is obscured by other windows on the screen, even if it
6857 * is itself visible.
6858 *
6859 * @param visibility The new visibility of the window.
6860 */
6861 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006862 if (visibility == VISIBLE) {
6863 initialAwakenScrollBars();
6864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006865 }
6866
6867 /**
6868 * Returns the current visibility of the window this view is attached to
6869 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
6870 *
6871 * @return Returns the current visibility of the view's window.
6872 */
6873 public int getWindowVisibility() {
6874 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
6875 }
6876
6877 /**
6878 * Retrieve the overall visible display size in which the window this view is
6879 * attached to has been positioned in. This takes into account screen
6880 * decorations above the window, for both cases where the window itself
6881 * is being position inside of them or the window is being placed under
6882 * then and covered insets are used for the window to position its content
6883 * inside. In effect, this tells you the available area where content can
6884 * be placed and remain visible to users.
6885 *
6886 * <p>This function requires an IPC back to the window manager to retrieve
6887 * the requested information, so should not be used in performance critical
6888 * code like drawing.
6889 *
6890 * @param outRect Filled in with the visible display frame. If the view
6891 * is not attached to a window, this is simply the raw display size.
6892 */
6893 public void getWindowVisibleDisplayFrame(Rect outRect) {
6894 if (mAttachInfo != null) {
6895 try {
6896 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
6897 } catch (RemoteException e) {
6898 return;
6899 }
6900 // XXX This is really broken, and probably all needs to be done
6901 // in the window manager, and we need to know more about whether
6902 // we want the area behind or in front of the IME.
6903 final Rect insets = mAttachInfo.mVisibleInsets;
6904 outRect.left += insets.left;
6905 outRect.top += insets.top;
6906 outRect.right -= insets.right;
6907 outRect.bottom -= insets.bottom;
6908 return;
6909 }
6910 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006911 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006912 }
6913
6914 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006915 * Dispatch a notification about a resource configuration change down
6916 * the view hierarchy.
6917 * ViewGroups should override to route to their children.
6918 *
6919 * @param newConfig The new resource configuration.
6920 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006921 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006922 */
6923 public void dispatchConfigurationChanged(Configuration newConfig) {
6924 onConfigurationChanged(newConfig);
6925 }
6926
6927 /**
6928 * Called when the current configuration of the resources being used
6929 * by the application have changed. You can use this to decide when
6930 * to reload resources that can changed based on orientation and other
6931 * configuration characterstics. You only need to use this if you are
6932 * not relying on the normal {@link android.app.Activity} mechanism of
6933 * recreating the activity instance upon a configuration change.
6934 *
6935 * @param newConfig The new resource configuration.
6936 */
6937 protected void onConfigurationChanged(Configuration newConfig) {
6938 }
6939
6940 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006941 * Private function to aggregate all per-view attributes in to the view
6942 * root.
6943 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006944 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6945 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006946 }
6947
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006948 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6949 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08006950 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006951 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006952 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006953 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006954 ListenerInfo li = mListenerInfo;
6955 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006956 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006957 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006958 }
6959 }
6960
6961 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006962 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006963 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006964 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6965 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006966 ai.mRecomputeGlobalAttributes = true;
6967 }
6968 }
6969 }
6970
6971 /**
6972 * Returns whether the device is currently in touch mode. Touch mode is entered
6973 * once the user begins interacting with the device by touch, and affects various
6974 * things like whether focus is always visible to the user.
6975 *
6976 * @return Whether the device is in touch mode.
6977 */
6978 @ViewDebug.ExportedProperty
6979 public boolean isInTouchMode() {
6980 if (mAttachInfo != null) {
6981 return mAttachInfo.mInTouchMode;
6982 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006983 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006984 }
6985 }
6986
6987 /**
6988 * Returns the context the view is running in, through which it can
6989 * access the current theme, resources, etc.
6990 *
6991 * @return The view's Context.
6992 */
6993 @ViewDebug.CapturedViewProperty
6994 public final Context getContext() {
6995 return mContext;
6996 }
6997
6998 /**
6999 * Handle a key event before it is processed by any input method
7000 * associated with the view hierarchy. This can be used to intercept
7001 * key events in special situations before the IME consumes them; a
7002 * typical example would be handling the BACK key to update the application's
7003 * UI instead of allowing the IME to see it and close itself.
7004 *
7005 * @param keyCode The value in event.getKeyCode().
7006 * @param event Description of the key event.
7007 * @return If you handled the event, return true. If you want to allow the
7008 * event to be handled by the next receiver, return false.
7009 */
7010 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7011 return false;
7012 }
7013
7014 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007015 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7016 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007017 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7018 * is released, if the view is enabled and clickable.
7019 *
7020 * @param keyCode A key code that represents the button pressed, from
7021 * {@link android.view.KeyEvent}.
7022 * @param event The KeyEvent object that defines the button action.
7023 */
7024 public boolean onKeyDown(int keyCode, KeyEvent event) {
7025 boolean result = false;
7026
7027 switch (keyCode) {
7028 case KeyEvent.KEYCODE_DPAD_CENTER:
7029 case KeyEvent.KEYCODE_ENTER: {
7030 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7031 return true;
7032 }
7033 // Long clickable items don't necessarily have to be clickable
7034 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7035 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7036 (event.getRepeatCount() == 0)) {
7037 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007038 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007039 return true;
7040 }
7041 break;
7042 }
7043 }
7044 return result;
7045 }
7046
7047 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007048 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7049 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7050 * the event).
7051 */
7052 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7053 return false;
7054 }
7055
7056 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007057 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7058 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007059 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7060 * {@link KeyEvent#KEYCODE_ENTER} is released.
7061 *
7062 * @param keyCode A key code that represents the button pressed, from
7063 * {@link android.view.KeyEvent}.
7064 * @param event The KeyEvent object that defines the button action.
7065 */
7066 public boolean onKeyUp(int keyCode, KeyEvent event) {
7067 boolean result = false;
7068
7069 switch (keyCode) {
7070 case KeyEvent.KEYCODE_DPAD_CENTER:
7071 case KeyEvent.KEYCODE_ENTER: {
7072 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7073 return true;
7074 }
7075 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7076 setPressed(false);
7077
7078 if (!mHasPerformedLongPress) {
7079 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007080 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007081
7082 result = performClick();
7083 }
7084 }
7085 break;
7086 }
7087 }
7088 return result;
7089 }
7090
7091 /**
7092 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7093 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7094 * the event).
7095 *
7096 * @param keyCode A key code that represents the button pressed, from
7097 * {@link android.view.KeyEvent}.
7098 * @param repeatCount The number of times the action was made.
7099 * @param event The KeyEvent object that defines the button action.
7100 */
7101 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7102 return false;
7103 }
7104
7105 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007106 * Called on the focused view when a key shortcut event is not handled.
7107 * Override this method to implement local key shortcuts for the View.
7108 * Key shortcuts can also be implemented by setting the
7109 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007110 *
7111 * @param keyCode The value in event.getKeyCode().
7112 * @param event Description of the key event.
7113 * @return If you handled the event, return true. If you want to allow the
7114 * event to be handled by the next receiver, return false.
7115 */
7116 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7117 return false;
7118 }
7119
7120 /**
7121 * Check whether the called view is a text editor, in which case it
7122 * would make sense to automatically display a soft input window for
7123 * it. Subclasses should override this if they implement
7124 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007125 * a call on that method would return a non-null InputConnection, and
7126 * they are really a first-class editor that the user would normally
7127 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007128 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007129 * <p>The default implementation always returns false. This does
7130 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7131 * will not be called or the user can not otherwise perform edits on your
7132 * view; it is just a hint to the system that this is not the primary
7133 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007134 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007135 * @return Returns true if this view is a text editor, else false.
7136 */
7137 public boolean onCheckIsTextEditor() {
7138 return false;
7139 }
Romain Guy8506ab42009-06-11 17:35:47 -07007140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007141 /**
7142 * Create a new InputConnection for an InputMethod to interact
7143 * with the view. The default implementation returns null, since it doesn't
7144 * support input methods. You can override this to implement such support.
7145 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007146 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007147 * <p>When implementing this, you probably also want to implement
7148 * {@link #onCheckIsTextEditor()} to indicate you will return a
7149 * non-null InputConnection.
7150 *
7151 * @param outAttrs Fill in with attribute information about the connection.
7152 */
7153 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7154 return null;
7155 }
7156
7157 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007158 * Called by the {@link android.view.inputmethod.InputMethodManager}
7159 * when a view who is not the current
7160 * input connection target is trying to make a call on the manager. The
7161 * default implementation returns false; you can override this to return
7162 * true for certain views if you are performing InputConnection proxying
7163 * to them.
7164 * @param view The View that is making the InputMethodManager call.
7165 * @return Return true to allow the call, false to reject.
7166 */
7167 public boolean checkInputConnectionProxy(View view) {
7168 return false;
7169 }
Romain Guy8506ab42009-06-11 17:35:47 -07007170
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007171 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007172 * Show the context menu for this view. It is not safe to hold on to the
7173 * menu after returning from this method.
7174 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007175 * You should normally not overload this method. Overload
7176 * {@link #onCreateContextMenu(ContextMenu)} or define an
7177 * {@link OnCreateContextMenuListener} to add items to the context menu.
7178 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007179 * @param menu The context menu to populate
7180 */
7181 public void createContextMenu(ContextMenu menu) {
7182 ContextMenuInfo menuInfo = getContextMenuInfo();
7183
7184 // Sets the current menu info so all items added to menu will have
7185 // my extra info set.
7186 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7187
7188 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007189 ListenerInfo li = mListenerInfo;
7190 if (li != null && li.mOnCreateContextMenuListener != null) {
7191 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007192 }
7193
7194 // Clear the extra information so subsequent items that aren't mine don't
7195 // have my extra info.
7196 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7197
7198 if (mParent != null) {
7199 mParent.createContextMenu(menu);
7200 }
7201 }
7202
7203 /**
7204 * Views should implement this if they have extra information to associate
7205 * with the context menu. The return result is supplied as a parameter to
7206 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7207 * callback.
7208 *
7209 * @return Extra information about the item for which the context menu
7210 * should be shown. This information will vary across different
7211 * subclasses of View.
7212 */
7213 protected ContextMenuInfo getContextMenuInfo() {
7214 return null;
7215 }
7216
7217 /**
7218 * Views should implement this if the view itself is going to add items to
7219 * the context menu.
7220 *
7221 * @param menu the context menu to populate
7222 */
7223 protected void onCreateContextMenu(ContextMenu menu) {
7224 }
7225
7226 /**
7227 * Implement this method to handle trackball motion events. The
7228 * <em>relative</em> movement of the trackball since the last event
7229 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7230 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7231 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7232 * they will often be fractional values, representing the more fine-grained
7233 * movement information available from a trackball).
7234 *
7235 * @param event The motion event.
7236 * @return True if the event was handled, false otherwise.
7237 */
7238 public boolean onTrackballEvent(MotionEvent event) {
7239 return false;
7240 }
7241
7242 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007243 * Implement this method to handle generic motion events.
7244 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007245 * Generic motion events describe joystick movements, mouse hovers, track pad
7246 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007247 * {@link MotionEvent#getSource() source} of the motion event specifies
7248 * the class of input that was received. Implementations of this method
7249 * must examine the bits in the source before processing the event.
7250 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007251 * </p><p>
7252 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7253 * are delivered to the view under the pointer. All other generic motion events are
7254 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007255 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007256 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007257 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007258 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7259 * // process the joystick movement...
7260 * return true;
7261 * }
7262 * }
7263 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7264 * switch (event.getAction()) {
7265 * case MotionEvent.ACTION_HOVER_MOVE:
7266 * // process the mouse hover movement...
7267 * return true;
7268 * case MotionEvent.ACTION_SCROLL:
7269 * // process the scroll wheel movement...
7270 * return true;
7271 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007272 * }
7273 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007274 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007275 *
7276 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007277 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007278 */
7279 public boolean onGenericMotionEvent(MotionEvent event) {
7280 return false;
7281 }
7282
7283 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007284 * Implement this method to handle hover events.
7285 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007286 * This method is called whenever a pointer is hovering into, over, or out of the
7287 * bounds of a view and the view is not currently being touched.
7288 * Hover events are represented as pointer events with action
7289 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7290 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7291 * </p>
7292 * <ul>
7293 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7294 * when the pointer enters the bounds of the view.</li>
7295 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7296 * when the pointer has already entered the bounds of the view and has moved.</li>
7297 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7298 * when the pointer has exited the bounds of the view or when the pointer is
7299 * about to go down due to a button click, tap, or similar user action that
7300 * causes the view to be touched.</li>
7301 * </ul>
7302 * <p>
7303 * The view should implement this method to return true to indicate that it is
7304 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007305 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007306 * The default implementation calls {@link #setHovered} to update the hovered state
7307 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007308 * is enabled and is clickable. The default implementation also sends hover
7309 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007310 * </p>
7311 *
7312 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007313 * @return True if the view handled the hover event.
7314 *
7315 * @see #isHovered
7316 * @see #setHovered
7317 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007318 */
7319 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007320 // The root view may receive hover (or touch) events that are outside the bounds of
7321 // the window. This code ensures that we only send accessibility events for
7322 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007323 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007324 if (!mSendingHoverAccessibilityEvents) {
7325 if ((action == MotionEvent.ACTION_HOVER_ENTER
7326 || action == MotionEvent.ACTION_HOVER_MOVE)
7327 && !hasHoveredChild()
7328 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007329 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007330 mSendingHoverAccessibilityEvents = true;
7331 requestAccessibilityFocusFromHover((int) event.getX(), (int) event.getY());
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007332 }
7333 } else {
7334 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007335 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007336 && !pointInView(event.getX(), event.getY()))) {
7337 mSendingHoverAccessibilityEvents = false;
7338 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007339 // If the window does not have input focus we take away accessibility
7340 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007341 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007342 getViewRootImpl().setAccessibilityFocusedHost(null);
7343 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007344 }
Jeff Browna1b24182011-07-28 13:38:24 -07007345 }
7346
Jeff Brown87b7f802011-06-21 18:35:45 -07007347 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007348 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007349 case MotionEvent.ACTION_HOVER_ENTER:
7350 setHovered(true);
7351 break;
7352 case MotionEvent.ACTION_HOVER_EXIT:
7353 setHovered(false);
7354 break;
7355 }
Jeff Browna1b24182011-07-28 13:38:24 -07007356
7357 // Dispatch the event to onGenericMotionEvent before returning true.
7358 // This is to provide compatibility with existing applications that
7359 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7360 // break because of the new default handling for hoverable views
7361 // in onHoverEvent.
7362 // Note that onGenericMotionEvent will be called by default when
7363 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7364 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007365 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007366 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007367
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007368 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007369 }
7370
7371 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007372 * Returns true if the view should handle {@link #onHoverEvent}
7373 * by calling {@link #setHovered} to change its hovered state.
7374 *
7375 * @return True if the view is hoverable.
7376 */
7377 private boolean isHoverable() {
7378 final int viewFlags = mViewFlags;
7379 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7380 return false;
7381 }
7382
7383 return (viewFlags & CLICKABLE) == CLICKABLE
7384 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7385 }
7386
7387 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007388 * Returns true if the view is currently hovered.
7389 *
7390 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007391 *
7392 * @see #setHovered
7393 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007394 */
Jeff Brown10b62902011-06-20 16:40:37 -07007395 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007396 public boolean isHovered() {
7397 return (mPrivateFlags & HOVERED) != 0;
7398 }
7399
7400 /**
7401 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007402 * <p>
7403 * Calling this method also changes the drawable state of the view. This
7404 * enables the view to react to hover by using different drawable resources
7405 * to change its appearance.
7406 * </p><p>
7407 * The {@link #onHoverChanged} method is called when the hovered state changes.
7408 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007409 *
7410 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007411 *
7412 * @see #isHovered
7413 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007414 */
7415 public void setHovered(boolean hovered) {
7416 if (hovered) {
7417 if ((mPrivateFlags & HOVERED) == 0) {
7418 mPrivateFlags |= HOVERED;
7419 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007420 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007421 }
7422 } else {
7423 if ((mPrivateFlags & HOVERED) != 0) {
7424 mPrivateFlags &= ~HOVERED;
7425 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007426 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007427 }
7428 }
7429 }
7430
7431 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007432 * Implement this method to handle hover state changes.
7433 * <p>
7434 * This method is called whenever the hover state changes as a result of a
7435 * call to {@link #setHovered}.
7436 * </p>
7437 *
7438 * @param hovered The current hover state, as returned by {@link #isHovered}.
7439 *
7440 * @see #isHovered
7441 * @see #setHovered
7442 */
7443 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007444 }
7445
7446 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007447 * Implement this method to handle touch screen motion events.
7448 *
7449 * @param event The motion event.
7450 * @return True if the event was handled, false otherwise.
7451 */
7452 public boolean onTouchEvent(MotionEvent event) {
7453 final int viewFlags = mViewFlags;
7454
7455 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007456 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007457 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007458 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007459 // A disabled view that is clickable still consumes the touch
7460 // events, it just doesn't respond to them.
7461 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7462 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7463 }
7464
7465 if (mTouchDelegate != null) {
7466 if (mTouchDelegate.onTouchEvent(event)) {
7467 return true;
7468 }
7469 }
7470
7471 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7472 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7473 switch (event.getAction()) {
7474 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007475 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7476 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007477 // take focus if we don't have it already and we should in
7478 // touch mode.
7479 boolean focusTaken = false;
7480 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7481 focusTaken = requestFocus();
7482 }
7483
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007484 if (prepressed) {
7485 // The button is being released before we actually
7486 // showed it as pressed. Make it show the pressed
7487 // state now (before scheduling the click) to ensure
7488 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007489 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007490 }
Joe Malin32736f02011-01-19 16:14:20 -08007491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007492 if (!mHasPerformedLongPress) {
7493 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007494 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007495
7496 // Only perform take click actions if we were in the pressed state
7497 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007498 // Use a Runnable and post this rather than calling
7499 // performClick directly. This lets other visual state
7500 // of the view update before click actions start.
7501 if (mPerformClick == null) {
7502 mPerformClick = new PerformClick();
7503 }
7504 if (!post(mPerformClick)) {
7505 performClick();
7506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007507 }
7508 }
7509
7510 if (mUnsetPressedState == null) {
7511 mUnsetPressedState = new UnsetPressedState();
7512 }
7513
Adam Powelle14579b2009-12-16 18:39:52 -08007514 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007515 postDelayed(mUnsetPressedState,
7516 ViewConfiguration.getPressedStateDuration());
7517 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007518 // If the post failed, unpress right now
7519 mUnsetPressedState.run();
7520 }
Adam Powelle14579b2009-12-16 18:39:52 -08007521 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007522 }
7523 break;
7524
7525 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007526 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007527
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007528 if (performButtonActionOnTouchDown(event)) {
7529 break;
7530 }
7531
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007532 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007533 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007534
7535 // For views inside a scrolling container, delay the pressed feedback for
7536 // a short period in case this is a scroll.
7537 if (isInScrollingContainer) {
7538 mPrivateFlags |= PREPRESSED;
7539 if (mPendingCheckForTap == null) {
7540 mPendingCheckForTap = new CheckForTap();
7541 }
7542 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7543 } else {
7544 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007545 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007546 checkForLongClick(0);
7547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007548 break;
7549
7550 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08007551 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08007552 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007553 break;
7554
7555 case MotionEvent.ACTION_MOVE:
7556 final int x = (int) event.getX();
7557 final int y = (int) event.getY();
7558
7559 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007560 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007561 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007562 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007563 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007564 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007565 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007566
Adam Powell4d6f0662012-02-21 15:11:11 -08007567 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007569 }
7570 break;
7571 }
7572 return true;
7573 }
7574
7575 return false;
7576 }
7577
7578 /**
Adam Powell10298662011-08-14 18:26:30 -07007579 * @hide
7580 */
7581 public boolean isInScrollingContainer() {
7582 ViewParent p = getParent();
7583 while (p != null && p instanceof ViewGroup) {
7584 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7585 return true;
7586 }
7587 p = p.getParent();
7588 }
7589 return false;
7590 }
7591
7592 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007593 * Remove the longpress detection timer.
7594 */
7595 private void removeLongPressCallback() {
7596 if (mPendingCheckForLongPress != null) {
7597 removeCallbacks(mPendingCheckForLongPress);
7598 }
7599 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007600
7601 /**
7602 * Remove the pending click action
7603 */
7604 private void removePerformClickCallback() {
7605 if (mPerformClick != null) {
7606 removeCallbacks(mPerformClick);
7607 }
7608 }
7609
Adam Powelle14579b2009-12-16 18:39:52 -08007610 /**
Romain Guya440b002010-02-24 15:57:54 -08007611 * Remove the prepress detection timer.
7612 */
7613 private void removeUnsetPressCallback() {
7614 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7615 setPressed(false);
7616 removeCallbacks(mUnsetPressedState);
7617 }
7618 }
7619
7620 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007621 * Remove the tap detection timer.
7622 */
7623 private void removeTapCallback() {
7624 if (mPendingCheckForTap != null) {
7625 mPrivateFlags &= ~PREPRESSED;
7626 removeCallbacks(mPendingCheckForTap);
7627 }
7628 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007629
7630 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007631 * Cancels a pending long press. Your subclass can use this if you
7632 * want the context menu to come up if the user presses and holds
7633 * at the same place, but you don't want it to come up if they press
7634 * and then move around enough to cause scrolling.
7635 */
7636 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05007637 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08007638
7639 /*
7640 * The prepressed state handled by the tap callback is a display
7641 * construct, but the tap callback will post a long press callback
7642 * less its own timeout. Remove it here.
7643 */
7644 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007645 }
7646
7647 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07007648 * Remove the pending callback for sending a
7649 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
7650 */
7651 private void removeSendViewScrolledAccessibilityEventCallback() {
7652 if (mSendViewScrolledAccessibilityEvent != null) {
7653 removeCallbacks(mSendViewScrolledAccessibilityEvent);
7654 }
7655 }
7656
7657 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007658 * Sets the TouchDelegate for this View.
7659 */
7660 public void setTouchDelegate(TouchDelegate delegate) {
7661 mTouchDelegate = delegate;
7662 }
7663
7664 /**
7665 * Gets the TouchDelegate for this View.
7666 */
7667 public TouchDelegate getTouchDelegate() {
7668 return mTouchDelegate;
7669 }
7670
7671 /**
7672 * Set flags controlling behavior of this view.
7673 *
7674 * @param flags Constant indicating the value which should be set
7675 * @param mask Constant indicating the bit range that should be changed
7676 */
7677 void setFlags(int flags, int mask) {
7678 int old = mViewFlags;
7679 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
7680
7681 int changed = mViewFlags ^ old;
7682 if (changed == 0) {
7683 return;
7684 }
7685 int privateFlags = mPrivateFlags;
7686
7687 /* Check if the FOCUSABLE bit has changed */
7688 if (((changed & FOCUSABLE_MASK) != 0) &&
7689 ((privateFlags & HAS_BOUNDS) !=0)) {
7690 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
7691 && ((privateFlags & FOCUSED) != 0)) {
7692 /* Give up focus if we are no longer focusable */
7693 clearFocus();
7694 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
7695 && ((privateFlags & FOCUSED) == 0)) {
7696 /*
7697 * Tell the view system that we are now available to take focus
7698 * if no one else already has it.
7699 */
7700 if (mParent != null) mParent.focusableViewAvailable(this);
7701 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007702 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7703 notifyAccessibilityStateChanged();
7704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007705 }
7706
7707 if ((flags & VISIBILITY_MASK) == VISIBLE) {
7708 if ((changed & VISIBILITY_MASK) != 0) {
7709 /*
Chet Haase4324ead2011-08-24 21:31:03 -07007710 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07007711 * it was not visible. Marking it drawn ensures that the invalidation will
7712 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007713 */
Chet Haaseaceafe62011-08-26 15:44:33 -07007714 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07007715 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007716
7717 needGlobalAttributesUpdate(true);
7718
7719 // a view becoming visible is worth notifying the parent
7720 // about in case nothing has focus. even if this specific view
7721 // isn't focusable, it may contain something that is, so let
7722 // the root view try to give this focus if nothing else does.
7723 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
7724 mParent.focusableViewAvailable(this);
7725 }
7726 }
7727 }
7728
7729 /* Check if the GONE bit has changed */
7730 if ((changed & GONE) != 0) {
7731 needGlobalAttributesUpdate(false);
7732 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007733
Romain Guyecd80ee2009-12-03 17:13:02 -08007734 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
7735 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007736 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08007737 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07007738 if (mParent instanceof View) {
7739 // GONE views noop invalidation, so invalidate the parent
7740 ((View) mParent).invalidate(true);
7741 }
7742 // Mark the view drawn to ensure that it gets invalidated properly the next
7743 // time it is visible and gets invalidated
7744 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007745 }
7746 if (mAttachInfo != null) {
7747 mAttachInfo.mViewVisibilityChanged = true;
7748 }
7749 }
7750
7751 /* Check if the VISIBLE bit has changed */
7752 if ((changed & INVISIBLE) != 0) {
7753 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07007754 /*
7755 * If this view is becoming invisible, set the DRAWN flag so that
7756 * the next invalidate() will not be skipped.
7757 */
7758 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007759
7760 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007761 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007762 if (getRootView() != this) {
7763 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007764 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007765 }
7766 }
7767 if (mAttachInfo != null) {
7768 mAttachInfo.mViewVisibilityChanged = true;
7769 }
7770 }
7771
Adam Powell326d8082009-12-09 15:10:07 -08007772 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07007773 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08007774 ((ViewGroup) mParent).onChildVisibilityChanged(this,
7775 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08007776 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07007777 } else if (mParent != null) {
7778 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07007779 }
Adam Powell326d8082009-12-09 15:10:07 -08007780 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
7781 }
7782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007783 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
7784 destroyDrawingCache();
7785 }
7786
7787 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
7788 destroyDrawingCache();
7789 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007790 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007791 }
7792
7793 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
7794 destroyDrawingCache();
7795 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7796 }
7797
7798 if ((changed & DRAW_MASK) != 0) {
7799 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07007800 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007801 mPrivateFlags &= ~SKIP_DRAW;
7802 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
7803 } else {
7804 mPrivateFlags |= SKIP_DRAW;
7805 }
7806 } else {
7807 mPrivateFlags &= ~SKIP_DRAW;
7808 }
7809 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08007810 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007811 }
7812
7813 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08007814 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007815 mParent.recomputeViewAttributes(this);
7816 }
7817 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007818
7819 if (AccessibilityManager.getInstance(mContext).isEnabled()
7820 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
7821 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
7822 notifyAccessibilityStateChanged();
7823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007824 }
7825
7826 /**
7827 * Change the view's z order in the tree, so it's on top of other sibling
7828 * views
7829 */
7830 public void bringToFront() {
7831 if (mParent != null) {
7832 mParent.bringChildToFront(this);
7833 }
7834 }
7835
7836 /**
7837 * This is called in response to an internal scroll in this view (i.e., the
7838 * view scrolled its own contents). This is typically as a result of
7839 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
7840 * called.
7841 *
7842 * @param l Current horizontal scroll origin.
7843 * @param t Current vertical scroll origin.
7844 * @param oldl Previous horizontal scroll origin.
7845 * @param oldt Previous vertical scroll origin.
7846 */
7847 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07007848 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7849 postSendViewScrolledAccessibilityEventCallback();
7850 }
7851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007852 mBackgroundSizeChanged = true;
7853
7854 final AttachInfo ai = mAttachInfo;
7855 if (ai != null) {
7856 ai.mViewScrollChanged = true;
7857 }
7858 }
7859
7860 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007861 * Interface definition for a callback to be invoked when the layout bounds of a view
7862 * changes due to layout processing.
7863 */
7864 public interface OnLayoutChangeListener {
7865 /**
7866 * Called when the focus state of a view has changed.
7867 *
7868 * @param v The view whose state has changed.
7869 * @param left The new value of the view's left property.
7870 * @param top The new value of the view's top property.
7871 * @param right The new value of the view's right property.
7872 * @param bottom The new value of the view's bottom property.
7873 * @param oldLeft The previous value of the view's left property.
7874 * @param oldTop The previous value of the view's top property.
7875 * @param oldRight The previous value of the view's right property.
7876 * @param oldBottom The previous value of the view's bottom property.
7877 */
7878 void onLayoutChange(View v, int left, int top, int right, int bottom,
7879 int oldLeft, int oldTop, int oldRight, int oldBottom);
7880 }
7881
7882 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007883 * This is called during layout when the size of this view has changed. If
7884 * you were just added to the view hierarchy, you're called with the old
7885 * values of 0.
7886 *
7887 * @param w Current width of this view.
7888 * @param h Current height of this view.
7889 * @param oldw Old width of this view.
7890 * @param oldh Old height of this view.
7891 */
7892 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
7893 }
7894
7895 /**
7896 * Called by draw to draw the child views. This may be overridden
7897 * by derived classes to gain control just before its children are drawn
7898 * (but after its own view has been drawn).
7899 * @param canvas the canvas on which to draw the view
7900 */
7901 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007903 }
7904
7905 /**
7906 * Gets the parent of this view. Note that the parent is a
7907 * ViewParent and not necessarily a View.
7908 *
7909 * @return Parent of this view.
7910 */
7911 public final ViewParent getParent() {
7912 return mParent;
7913 }
7914
7915 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007916 * Set the horizontal scrolled position of your view. This will cause a call to
7917 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7918 * invalidated.
7919 * @param value the x position to scroll to
7920 */
7921 public void setScrollX(int value) {
7922 scrollTo(value, mScrollY);
7923 }
7924
7925 /**
7926 * Set the vertical scrolled position of your view. This will cause a call to
7927 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7928 * invalidated.
7929 * @param value the y position to scroll to
7930 */
7931 public void setScrollY(int value) {
7932 scrollTo(mScrollX, value);
7933 }
7934
7935 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007936 * Return the scrolled left position of this view. This is the left edge of
7937 * the displayed part of your view. You do not need to draw any pixels
7938 * farther left, since those are outside of the frame of your view on
7939 * screen.
7940 *
7941 * @return The left edge of the displayed part of your view, in pixels.
7942 */
7943 public final int getScrollX() {
7944 return mScrollX;
7945 }
7946
7947 /**
7948 * Return the scrolled top position of this view. This is the top edge of
7949 * the displayed part of your view. You do not need to draw any pixels above
7950 * it, since those are outside of the frame of your view on screen.
7951 *
7952 * @return The top edge of the displayed part of your view, in pixels.
7953 */
7954 public final int getScrollY() {
7955 return mScrollY;
7956 }
7957
7958 /**
7959 * Return the width of the your view.
7960 *
7961 * @return The width of your view, in pixels.
7962 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007963 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007964 public final int getWidth() {
7965 return mRight - mLeft;
7966 }
7967
7968 /**
7969 * Return the height of your view.
7970 *
7971 * @return The height of your view, in pixels.
7972 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007973 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007974 public final int getHeight() {
7975 return mBottom - mTop;
7976 }
7977
7978 /**
7979 * Return the visible drawing bounds of your view. Fills in the output
7980 * rectangle with the values from getScrollX(), getScrollY(),
7981 * getWidth(), and getHeight().
7982 *
7983 * @param outRect The (scrolled) drawing bounds of the view.
7984 */
7985 public void getDrawingRect(Rect outRect) {
7986 outRect.left = mScrollX;
7987 outRect.top = mScrollY;
7988 outRect.right = mScrollX + (mRight - mLeft);
7989 outRect.bottom = mScrollY + (mBottom - mTop);
7990 }
7991
7992 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007993 * Like {@link #getMeasuredWidthAndState()}, but only returns the
7994 * raw width component (that is the result is masked by
7995 * {@link #MEASURED_SIZE_MASK}).
7996 *
7997 * @return The raw measured width of this view.
7998 */
7999 public final int getMeasuredWidth() {
8000 return mMeasuredWidth & MEASURED_SIZE_MASK;
8001 }
8002
8003 /**
8004 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008005 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008006 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008007 * This should be used during measurement and layout calculations only. Use
8008 * {@link #getWidth()} to see how wide a view is after layout.
8009 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008010 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008011 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008012 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008013 return mMeasuredWidth;
8014 }
8015
8016 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008017 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8018 * raw width component (that is the result is masked by
8019 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008020 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008021 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008022 */
8023 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008024 return mMeasuredHeight & MEASURED_SIZE_MASK;
8025 }
8026
8027 /**
8028 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008029 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008030 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8031 * This should be used during measurement and layout calculations only. Use
8032 * {@link #getHeight()} to see how wide a view is after layout.
8033 *
8034 * @return The measured width of this view as a bit mask.
8035 */
8036 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008037 return mMeasuredHeight;
8038 }
8039
8040 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008041 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8042 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8043 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8044 * and the height component is at the shifted bits
8045 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8046 */
8047 public final int getMeasuredState() {
8048 return (mMeasuredWidth&MEASURED_STATE_MASK)
8049 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8050 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8051 }
8052
8053 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008054 * The transform matrix of this view, which is calculated based on the current
8055 * roation, scale, and pivot properties.
8056 *
8057 * @see #getRotation()
8058 * @see #getScaleX()
8059 * @see #getScaleY()
8060 * @see #getPivotX()
8061 * @see #getPivotY()
8062 * @return The current transform matrix for the view
8063 */
8064 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008065 if (mTransformationInfo != null) {
8066 updateMatrix();
8067 return mTransformationInfo.mMatrix;
8068 }
8069 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008070 }
8071
8072 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008073 * Utility function to determine if the value is far enough away from zero to be
8074 * considered non-zero.
8075 * @param value A floating point value to check for zero-ness
8076 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8077 */
8078 private static boolean nonzero(float value) {
8079 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8080 }
8081
8082 /**
Jeff Brown86671742010-09-30 20:00:15 -07008083 * Returns true if the transform matrix is the identity matrix.
8084 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008085 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008086 * @return True if the transform matrix is the identity matrix, false otherwise.
8087 */
Jeff Brown86671742010-09-30 20:00:15 -07008088 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008089 if (mTransformationInfo != null) {
8090 updateMatrix();
8091 return mTransformationInfo.mMatrixIsIdentity;
8092 }
8093 return true;
8094 }
8095
8096 void ensureTransformationInfo() {
8097 if (mTransformationInfo == null) {
8098 mTransformationInfo = new TransformationInfo();
8099 }
Jeff Brown86671742010-09-30 20:00:15 -07008100 }
8101
8102 /**
8103 * Recomputes the transform matrix if necessary.
8104 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008105 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008106 final TransformationInfo info = mTransformationInfo;
8107 if (info == null) {
8108 return;
8109 }
8110 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008111 // transform-related properties have changed since the last time someone
8112 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008113
8114 // Figure out if we need to update the pivot point
8115 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008116 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8117 info.mPrevWidth = mRight - mLeft;
8118 info.mPrevHeight = mBottom - mTop;
8119 info.mPivotX = info.mPrevWidth / 2f;
8120 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008121 }
8122 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008123 info.mMatrix.reset();
8124 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8125 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8126 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8127 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008128 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008129 if (info.mCamera == null) {
8130 info.mCamera = new Camera();
8131 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008132 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008133 info.mCamera.save();
8134 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8135 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8136 info.mCamera.getMatrix(info.matrix3D);
8137 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8138 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8139 info.mPivotY + info.mTranslationY);
8140 info.mMatrix.postConcat(info.matrix3D);
8141 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008142 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008143 info.mMatrixDirty = false;
8144 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8145 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008146 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008147 }
8148
8149 /**
8150 * Utility method to retrieve the inverse of the current mMatrix property.
8151 * We cache the matrix to avoid recalculating it when transform properties
8152 * have not changed.
8153 *
8154 * @return The inverse of the current matrix of this view.
8155 */
Jeff Brown86671742010-09-30 20:00:15 -07008156 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008157 final TransformationInfo info = mTransformationInfo;
8158 if (info != null) {
8159 updateMatrix();
8160 if (info.mInverseMatrixDirty) {
8161 if (info.mInverseMatrix == null) {
8162 info.mInverseMatrix = new Matrix();
8163 }
8164 info.mMatrix.invert(info.mInverseMatrix);
8165 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008166 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008167 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008168 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008169 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008170 }
8171
8172 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008173 * Gets the distance along the Z axis from the camera to this view.
8174 *
8175 * @see #setCameraDistance(float)
8176 *
8177 * @return The distance along the Z axis.
8178 */
8179 public float getCameraDistance() {
8180 ensureTransformationInfo();
8181 final float dpi = mResources.getDisplayMetrics().densityDpi;
8182 final TransformationInfo info = mTransformationInfo;
8183 if (info.mCamera == null) {
8184 info.mCamera = new Camera();
8185 info.matrix3D = new Matrix();
8186 }
8187 return -(info.mCamera.getLocationZ() * dpi);
8188 }
8189
8190 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008191 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8192 * views are drawn) from the camera to this view. The camera's distance
8193 * affects 3D transformations, for instance rotations around the X and Y
8194 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008195 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008196 * use a camera distance that's greater than the height (X axis rotation) or
8197 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008198 *
Romain Guya5364ee2011-02-24 14:46:04 -08008199 * <p>The distance of the camera from the view plane can have an affect on the
8200 * perspective distortion of the view when it is rotated around the x or y axis.
8201 * For example, a large distance will result in a large viewing angle, and there
8202 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008203 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008204 * also result in some drawing artifacts if the rotated view ends up partially
8205 * behind the camera (which is why the recommendation is to use a distance at
8206 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008207 *
Romain Guya5364ee2011-02-24 14:46:04 -08008208 * <p>The distance is expressed in "depth pixels." The default distance depends
8209 * on the screen density. For instance, on a medium density display, the
8210 * default distance is 1280. On a high density display, the default distance
8211 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008212 *
Romain Guya5364ee2011-02-24 14:46:04 -08008213 * <p>If you want to specify a distance that leads to visually consistent
8214 * results across various densities, use the following formula:</p>
8215 * <pre>
8216 * float scale = context.getResources().getDisplayMetrics().density;
8217 * view.setCameraDistance(distance * scale);
8218 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008219 *
Romain Guya5364ee2011-02-24 14:46:04 -08008220 * <p>The density scale factor of a high density display is 1.5,
8221 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008222 *
Romain Guya5364ee2011-02-24 14:46:04 -08008223 * @param distance The distance in "depth pixels", if negative the opposite
8224 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008225 *
8226 * @see #setRotationX(float)
8227 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008228 */
8229 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008230 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008231
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008232 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008233 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008234 final TransformationInfo info = mTransformationInfo;
8235 if (info.mCamera == null) {
8236 info.mCamera = new Camera();
8237 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008238 }
8239
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008240 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8241 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008242
Chet Haase9d1992d2012-03-13 11:03:25 -07008243 invalidateViewProperty(false, false);
Chet Haasea1cff502012-02-21 13:43:44 -08008244 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008245 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008246 }
Romain Guya5364ee2011-02-24 14:46:04 -08008247 }
8248
8249 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008250 * The degrees that the view is rotated around the pivot point.
8251 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008252 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008253 * @see #getPivotX()
8254 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008255 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008256 * @return The degrees of rotation.
8257 */
Chet Haasea5531132012-02-02 13:41:44 -08008258 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008259 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008260 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008261 }
8262
8263 /**
Chet Haase897247b2010-09-09 14:54:47 -07008264 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8265 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008266 *
8267 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008268 *
8269 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008270 * @see #getPivotX()
8271 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008272 * @see #setRotationX(float)
8273 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008274 *
8275 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008276 */
8277 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008278 ensureTransformationInfo();
8279 final TransformationInfo info = mTransformationInfo;
8280 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008281 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008282 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008283 info.mRotation = rotation;
8284 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008285 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008286 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8287 mDisplayList.setRotation(rotation);
8288 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008289 }
8290 }
8291
8292 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008293 * The degrees that the view is rotated around the vertical axis through the pivot point.
8294 *
8295 * @see #getPivotX()
8296 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008297 * @see #setRotationY(float)
8298 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008299 * @return The degrees of Y rotation.
8300 */
Chet Haasea5531132012-02-02 13:41:44 -08008301 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008302 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008303 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008304 }
8305
8306 /**
Chet Haase897247b2010-09-09 14:54:47 -07008307 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8308 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8309 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008310 *
Romain Guya5364ee2011-02-24 14:46:04 -08008311 * When rotating large views, it is recommended to adjust the camera distance
8312 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008313 *
8314 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008315 *
8316 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008317 * @see #getPivotX()
8318 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008319 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008320 * @see #setRotationX(float)
8321 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008322 *
8323 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008324 */
8325 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008326 ensureTransformationInfo();
8327 final TransformationInfo info = mTransformationInfo;
8328 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008329 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008330 info.mRotationY = rotationY;
8331 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008332 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008333 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8334 mDisplayList.setRotationY(rotationY);
8335 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008336 }
8337 }
8338
8339 /**
8340 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8341 *
8342 * @see #getPivotX()
8343 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008344 * @see #setRotationX(float)
8345 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008346 * @return The degrees of X rotation.
8347 */
Chet Haasea5531132012-02-02 13:41:44 -08008348 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008349 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008350 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008351 }
8352
8353 /**
Chet Haase897247b2010-09-09 14:54:47 -07008354 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8355 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8356 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008357 *
Romain Guya5364ee2011-02-24 14:46:04 -08008358 * When rotating large views, it is recommended to adjust the camera distance
8359 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008360 *
8361 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008362 *
8363 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008364 * @see #getPivotX()
8365 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008366 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008367 * @see #setRotationY(float)
8368 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008369 *
8370 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008371 */
8372 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008373 ensureTransformationInfo();
8374 final TransformationInfo info = mTransformationInfo;
8375 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008376 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008377 info.mRotationX = rotationX;
8378 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008379 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008380 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8381 mDisplayList.setRotationX(rotationX);
8382 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008383 }
8384 }
8385
8386 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008387 * The amount that the view is scaled in x around the pivot point, as a proportion of
8388 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8389 *
Joe Onorato93162322010-09-16 15:42:01 -04008390 * <p>By default, this is 1.0f.
8391 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008392 * @see #getPivotX()
8393 * @see #getPivotY()
8394 * @return The scaling factor.
8395 */
Chet Haasea5531132012-02-02 13:41:44 -08008396 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008397 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008398 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008399 }
8400
8401 /**
8402 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8403 * the view's unscaled width. A value of 1 means that no scaling is applied.
8404 *
8405 * @param scaleX The scaling factor.
8406 * @see #getPivotX()
8407 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008408 *
8409 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008410 */
8411 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008412 ensureTransformationInfo();
8413 final TransformationInfo info = mTransformationInfo;
8414 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008415 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008416 info.mScaleX = scaleX;
8417 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008418 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008419 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8420 mDisplayList.setScaleX(scaleX);
8421 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008422 }
8423 }
8424
8425 /**
8426 * The amount that the view is scaled in y around the pivot point, as a proportion of
8427 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8428 *
Joe Onorato93162322010-09-16 15:42:01 -04008429 * <p>By default, this is 1.0f.
8430 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008431 * @see #getPivotX()
8432 * @see #getPivotY()
8433 * @return The scaling factor.
8434 */
Chet Haasea5531132012-02-02 13:41:44 -08008435 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008436 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008437 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008438 }
8439
8440 /**
8441 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8442 * the view's unscaled width. A value of 1 means that no scaling is applied.
8443 *
8444 * @param scaleY The scaling factor.
8445 * @see #getPivotX()
8446 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008447 *
8448 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008449 */
8450 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008451 ensureTransformationInfo();
8452 final TransformationInfo info = mTransformationInfo;
8453 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008454 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008455 info.mScaleY = scaleY;
8456 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008457 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008458 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8459 mDisplayList.setScaleY(scaleY);
8460 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008461 }
8462 }
8463
8464 /**
8465 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8466 * and {@link #setScaleX(float) scaled}.
8467 *
8468 * @see #getRotation()
8469 * @see #getScaleX()
8470 * @see #getScaleY()
8471 * @see #getPivotY()
8472 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008473 *
8474 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008475 */
Chet Haasea5531132012-02-02 13:41:44 -08008476 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008477 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008478 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008479 }
8480
8481 /**
8482 * Sets the x location of the point around which the view is
8483 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008484 * By default, the pivot point is centered on the object.
8485 * Setting this property disables this behavior and causes the view to use only the
8486 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008487 *
8488 * @param pivotX The x location of the pivot point.
8489 * @see #getRotation()
8490 * @see #getScaleX()
8491 * @see #getScaleY()
8492 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008493 *
8494 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008495 */
8496 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008497 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008498 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008499 final TransformationInfo info = mTransformationInfo;
8500 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008501 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008502 info.mPivotX = pivotX;
8503 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008504 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008505 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8506 mDisplayList.setPivotX(pivotX);
8507 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008508 }
8509 }
8510
8511 /**
8512 * The y location of the point around which the view is {@link #setRotation(float) rotated}
8513 * and {@link #setScaleY(float) scaled}.
8514 *
8515 * @see #getRotation()
8516 * @see #getScaleX()
8517 * @see #getScaleY()
8518 * @see #getPivotY()
8519 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008520 *
8521 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008522 */
Chet Haasea5531132012-02-02 13:41:44 -08008523 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008524 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008525 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008526 }
8527
8528 /**
8529 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07008530 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
8531 * Setting this property disables this behavior and causes the view to use only the
8532 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008533 *
8534 * @param pivotY The y location of the pivot point.
8535 * @see #getRotation()
8536 * @see #getScaleX()
8537 * @see #getScaleY()
8538 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008539 *
8540 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008541 */
8542 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008543 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008544 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008545 final TransformationInfo info = mTransformationInfo;
8546 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008547 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008548 info.mPivotY = pivotY;
8549 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008550 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008551 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8552 mDisplayList.setPivotY(pivotY);
8553 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008554 }
8555 }
8556
8557 /**
8558 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
8559 * completely transparent and 1 means the view is completely opaque.
8560 *
Joe Onorato93162322010-09-16 15:42:01 -04008561 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07008562 * @return The opacity of the view.
8563 */
Chet Haasea5531132012-02-02 13:41:44 -08008564 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008565 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008566 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008567 }
8568
8569 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07008570 * Returns whether this View has content which overlaps. This function, intended to be
8571 * overridden by specific View types, is an optimization when alpha is set on a view. If
8572 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8573 * and then composited it into place, which can be expensive. If the view has no overlapping
8574 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8575 * An example of overlapping rendering is a TextView with a background image, such as a
8576 * Button. An example of non-overlapping rendering is a TextView with no background, or
8577 * an ImageView with only the foreground image. The default implementation returns true;
8578 * subclasses should override if they have cases which can be optimized.
8579 *
8580 * @return true if the content in this view might overlap, false otherwise.
8581 */
8582 public boolean hasOverlappingRendering() {
8583 return true;
8584 }
8585
8586 /**
Romain Guy171c5922011-01-06 10:04:23 -08008587 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8588 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008589 *
Romain Guy171c5922011-01-06 10:04:23 -08008590 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8591 * responsible for applying the opacity itself. Otherwise, calling this method is
8592 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008593 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008594 *
Chet Haasea5531132012-02-02 13:41:44 -08008595 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8596 * performance implications. It is generally best to use the alpha property sparingly and
8597 * transiently, as in the case of fading animations.</p>
8598 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008599 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08008600 *
Joe Malin32736f02011-01-19 16:14:20 -08008601 * @see #setLayerType(int, android.graphics.Paint)
8602 *
Chet Haase73066682010-11-29 15:55:32 -08008603 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07008604 */
8605 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008606 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008607 if (mTransformationInfo.mAlpha != alpha) {
8608 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008609 if (onSetAlpha((int) (alpha * 255))) {
8610 mPrivateFlags |= ALPHA_SET;
8611 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07008612 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008613 invalidate(true);
8614 } else {
8615 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07008616 invalidateViewProperty(true, false);
Chet Haasea1cff502012-02-21 13:43:44 -08008617 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8618 mDisplayList.setAlpha(alpha);
8619 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008620 }
Chet Haaseed032702010-10-01 14:05:54 -07008621 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008622 }
8623
8624 /**
Chet Haasea00f3862011-02-22 06:34:40 -08008625 * Faster version of setAlpha() which performs the same steps except there are
8626 * no calls to invalidate(). The caller of this function should perform proper invalidation
8627 * on the parent and this object. The return value indicates whether the subclass handles
8628 * alpha (the return value for onSetAlpha()).
8629 *
8630 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008631 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
8632 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08008633 */
8634 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008635 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008636 if (mTransformationInfo.mAlpha != alpha) {
8637 mTransformationInfo.mAlpha = alpha;
8638 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
8639 if (subclassHandlesAlpha) {
8640 mPrivateFlags |= ALPHA_SET;
8641 return true;
8642 } else {
8643 mPrivateFlags &= ~ALPHA_SET;
Chet Haasea1cff502012-02-21 13:43:44 -08008644 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8645 mDisplayList.setAlpha(alpha);
8646 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008647 }
Chet Haasea00f3862011-02-22 06:34:40 -08008648 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008649 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08008650 }
8651
8652 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008653 * Top position of this view relative to its parent.
8654 *
8655 * @return The top of this view, in pixels.
8656 */
8657 @ViewDebug.CapturedViewProperty
8658 public final int getTop() {
8659 return mTop;
8660 }
8661
8662 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008663 * Sets the top position of this view relative to its parent. This method is meant to be called
8664 * by the layout system and should not generally be called otherwise, because the property
8665 * may be changed at any time by the layout.
8666 *
8667 * @param top The top of this view, in pixels.
8668 */
8669 public final void setTop(int top) {
8670 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07008671 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008672 final boolean matrixIsIdentity = mTransformationInfo == null
8673 || mTransformationInfo.mMatrixIsIdentity;
8674 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008675 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008676 int minTop;
8677 int yLoc;
8678 if (top < mTop) {
8679 minTop = top;
8680 yLoc = top - mTop;
8681 } else {
8682 minTop = mTop;
8683 yLoc = 0;
8684 }
Chet Haasee9140a72011-02-16 16:23:29 -08008685 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008686 }
8687 } else {
8688 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008689 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008690 }
8691
Chet Haaseed032702010-10-01 14:05:54 -07008692 int width = mRight - mLeft;
8693 int oldHeight = mBottom - mTop;
8694
Chet Haase21cd1382010-09-01 17:42:29 -07008695 mTop = top;
Chet Haasea1cff502012-02-21 13:43:44 -08008696 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8697 mDisplayList.setTop(mTop);
8698 }
Chet Haase21cd1382010-09-01 17:42:29 -07008699
Chet Haaseed032702010-10-01 14:05:54 -07008700 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8701
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008702 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008703 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8704 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008705 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008706 }
Chet Haase21cd1382010-09-01 17:42:29 -07008707 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008708 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008709 }
Chet Haase55dbb652010-12-21 20:15:08 -08008710 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008711 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008712 }
8713 }
8714
8715 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008716 * Bottom position of this view relative to its parent.
8717 *
8718 * @return The bottom of this view, in pixels.
8719 */
8720 @ViewDebug.CapturedViewProperty
8721 public final int getBottom() {
8722 return mBottom;
8723 }
8724
8725 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08008726 * True if this view has changed since the last time being drawn.
8727 *
8728 * @return The dirty state of this view.
8729 */
8730 public boolean isDirty() {
8731 return (mPrivateFlags & DIRTY_MASK) != 0;
8732 }
8733
8734 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008735 * Sets the bottom position of this view relative to its parent. This method is meant to be
8736 * called by the layout system and should not generally be called otherwise, because the
8737 * property may be changed at any time by the layout.
8738 *
8739 * @param bottom The bottom of this view, in pixels.
8740 */
8741 public final void setBottom(int bottom) {
8742 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07008743 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008744 final boolean matrixIsIdentity = mTransformationInfo == null
8745 || mTransformationInfo.mMatrixIsIdentity;
8746 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008747 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008748 int maxBottom;
8749 if (bottom < mBottom) {
8750 maxBottom = mBottom;
8751 } else {
8752 maxBottom = bottom;
8753 }
Chet Haasee9140a72011-02-16 16:23:29 -08008754 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008755 }
8756 } else {
8757 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008758 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008759 }
8760
Chet Haaseed032702010-10-01 14:05:54 -07008761 int width = mRight - mLeft;
8762 int oldHeight = mBottom - mTop;
8763
Chet Haase21cd1382010-09-01 17:42:29 -07008764 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -08008765 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8766 mDisplayList.setBottom(mBottom);
8767 }
Chet Haase21cd1382010-09-01 17:42:29 -07008768
Chet Haaseed032702010-10-01 14:05:54 -07008769 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8770
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008771 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008772 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8773 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008774 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008775 }
Chet Haase21cd1382010-09-01 17:42:29 -07008776 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008777 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008778 }
Chet Haase55dbb652010-12-21 20:15:08 -08008779 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008780 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008781 }
8782 }
8783
8784 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008785 * Left position of this view relative to its parent.
8786 *
8787 * @return The left edge of this view, in pixels.
8788 */
8789 @ViewDebug.CapturedViewProperty
8790 public final int getLeft() {
8791 return mLeft;
8792 }
8793
8794 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008795 * Sets the left position of this view relative to its parent. This method is meant to be called
8796 * by the layout system and should not generally be called otherwise, because the property
8797 * may be changed at any time by the layout.
8798 *
8799 * @param left The bottom of this view, in pixels.
8800 */
8801 public final void setLeft(int left) {
8802 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07008803 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008804 final boolean matrixIsIdentity = mTransformationInfo == null
8805 || mTransformationInfo.mMatrixIsIdentity;
8806 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008807 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008808 int minLeft;
8809 int xLoc;
8810 if (left < mLeft) {
8811 minLeft = left;
8812 xLoc = left - mLeft;
8813 } else {
8814 minLeft = mLeft;
8815 xLoc = 0;
8816 }
Chet Haasee9140a72011-02-16 16:23:29 -08008817 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008818 }
8819 } else {
8820 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008821 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008822 }
8823
Chet Haaseed032702010-10-01 14:05:54 -07008824 int oldWidth = mRight - mLeft;
8825 int height = mBottom - mTop;
8826
Chet Haase21cd1382010-09-01 17:42:29 -07008827 mLeft = left;
Chet Haasea1cff502012-02-21 13:43:44 -08008828 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8829 mDisplayList.setLeft(left);
8830 }
Chet Haase21cd1382010-09-01 17:42:29 -07008831
Chet Haaseed032702010-10-01 14:05:54 -07008832 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8833
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008834 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008835 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8836 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008837 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008838 }
Chet Haase21cd1382010-09-01 17:42:29 -07008839 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008840 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008841 }
Chet Haase55dbb652010-12-21 20:15:08 -08008842 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008843 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008844 if (USE_DISPLAY_LIST_PROPERTIES) {
8845
8846 }
Chet Haase21cd1382010-09-01 17:42:29 -07008847 }
8848 }
8849
8850 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008851 * Right position of this view relative to its parent.
8852 *
8853 * @return The right edge of this view, in pixels.
8854 */
8855 @ViewDebug.CapturedViewProperty
8856 public final int getRight() {
8857 return mRight;
8858 }
8859
8860 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008861 * Sets the right position of this view relative to its parent. This method is meant to be called
8862 * by the layout system and should not generally be called otherwise, because the property
8863 * may be changed at any time by the layout.
8864 *
8865 * @param right The bottom of this view, in pixels.
8866 */
8867 public final void setRight(int right) {
8868 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07008869 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008870 final boolean matrixIsIdentity = mTransformationInfo == null
8871 || mTransformationInfo.mMatrixIsIdentity;
8872 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008873 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008874 int maxRight;
8875 if (right < mRight) {
8876 maxRight = mRight;
8877 } else {
8878 maxRight = right;
8879 }
Chet Haasee9140a72011-02-16 16:23:29 -08008880 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008881 }
8882 } else {
8883 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008884 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008885 }
8886
Chet Haaseed032702010-10-01 14:05:54 -07008887 int oldWidth = mRight - mLeft;
8888 int height = mBottom - mTop;
8889
Chet Haase21cd1382010-09-01 17:42:29 -07008890 mRight = right;
Chet Haasea1cff502012-02-21 13:43:44 -08008891 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8892 mDisplayList.setRight(mRight);
8893 }
Chet Haase21cd1382010-09-01 17:42:29 -07008894
Chet Haaseed032702010-10-01 14:05:54 -07008895 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8896
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008897 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008898 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8899 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008900 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008901 }
Chet Haase21cd1382010-09-01 17:42:29 -07008902 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008903 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008904 }
Chet Haase55dbb652010-12-21 20:15:08 -08008905 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008906 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008907 }
8908 }
8909
8910 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008911 * The visual x position of this view, in pixels. This is equivalent to the
8912 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08008913 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07008914 *
Chet Haasedf030d22010-07-30 17:22:38 -07008915 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008916 */
Chet Haasea5531132012-02-02 13:41:44 -08008917 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008918 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008919 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008920 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008921
Chet Haasedf030d22010-07-30 17:22:38 -07008922 /**
8923 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
8924 * {@link #setTranslationX(float) translationX} property to be the difference between
8925 * the x value passed in and the current {@link #getLeft() left} property.
8926 *
8927 * @param x The visual x position of this view, in pixels.
8928 */
8929 public void setX(float x) {
8930 setTranslationX(x - mLeft);
8931 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008932
Chet Haasedf030d22010-07-30 17:22:38 -07008933 /**
8934 * The visual y position of this view, in pixels. This is equivalent to the
8935 * {@link #setTranslationY(float) translationY} property plus the current
8936 * {@link #getTop() top} property.
8937 *
8938 * @return The visual y position of this view, in pixels.
8939 */
Chet Haasea5531132012-02-02 13:41:44 -08008940 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008941 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008942 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008943 }
8944
8945 /**
8946 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
8947 * {@link #setTranslationY(float) translationY} property to be the difference between
8948 * the y value passed in and the current {@link #getTop() top} property.
8949 *
8950 * @param y The visual y position of this view, in pixels.
8951 */
8952 public void setY(float y) {
8953 setTranslationY(y - mTop);
8954 }
8955
8956
8957 /**
8958 * The horizontal location of this view relative to its {@link #getLeft() left} position.
8959 * This position is post-layout, in addition to wherever the object's
8960 * layout placed it.
8961 *
8962 * @return The horizontal position of this view relative to its left position, in pixels.
8963 */
Chet Haasea5531132012-02-02 13:41:44 -08008964 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008965 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008966 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07008967 }
8968
8969 /**
8970 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
8971 * This effectively positions the object post-layout, in addition to wherever the object's
8972 * layout placed it.
8973 *
8974 * @param translationX The horizontal position of this view relative to its left position,
8975 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008976 *
8977 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07008978 */
8979 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008980 ensureTransformationInfo();
8981 final TransformationInfo info = mTransformationInfo;
8982 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07008983 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008984 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008985 info.mTranslationX = translationX;
8986 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008987 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008988 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8989 mDisplayList.setTranslationX(translationX);
8990 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008991 }
8992 }
8993
8994 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008995 * The horizontal location of this view relative to its {@link #getTop() top} position.
8996 * This position is post-layout, in addition to wherever the object's
8997 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008998 *
Chet Haasedf030d22010-07-30 17:22:38 -07008999 * @return The vertical position of this view relative to its top position,
9000 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009001 */
Chet Haasea5531132012-02-02 13:41:44 -08009002 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009003 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009004 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009005 }
9006
9007 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009008 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9009 * This effectively positions the object post-layout, in addition to wherever the object's
9010 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009011 *
Chet Haasedf030d22010-07-30 17:22:38 -07009012 * @param translationY The vertical position of this view relative to its top position,
9013 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009014 *
9015 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009016 */
Chet Haasedf030d22010-07-30 17:22:38 -07009017 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009018 ensureTransformationInfo();
9019 final TransformationInfo info = mTransformationInfo;
9020 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009021 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009022 info.mTranslationY = translationY;
9023 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009024 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08009025 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9026 mDisplayList.setTranslationY(translationY);
9027 }
Chet Haasedf030d22010-07-30 17:22:38 -07009028 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009029 }
9030
9031 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009032 * Hit rectangle in parent's coordinates
9033 *
9034 * @param outRect The hit rectangle of the view.
9035 */
9036 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009037 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009038 final TransformationInfo info = mTransformationInfo;
9039 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009040 outRect.set(mLeft, mTop, mRight, mBottom);
9041 } else {
9042 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009043 tmpRect.set(-info.mPivotX, -info.mPivotY,
9044 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9045 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009046 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9047 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009048 }
9049 }
9050
9051 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009052 * Determines whether the given point, in local coordinates is inside the view.
9053 */
9054 /*package*/ final boolean pointInView(float localX, float localY) {
9055 return localX >= 0 && localX < (mRight - mLeft)
9056 && localY >= 0 && localY < (mBottom - mTop);
9057 }
9058
9059 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009060 * Utility method to determine whether the given point, in local coordinates,
9061 * is inside the view, where the area of the view is expanded by the slop factor.
9062 * This method is called while processing touch-move events to determine if the event
9063 * is still within the view.
9064 */
9065 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009066 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009067 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009068 }
9069
9070 /**
9071 * When a view has focus and the user navigates away from it, the next view is searched for
9072 * starting from the rectangle filled in by this method.
9073 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009074 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9075 * of the view. However, if your view maintains some idea of internal selection,
9076 * such as a cursor, or a selected row or column, you should override this method and
9077 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009078 *
9079 * @param r The rectangle to fill in, in this view's coordinates.
9080 */
9081 public void getFocusedRect(Rect r) {
9082 getDrawingRect(r);
9083 }
9084
9085 /**
9086 * If some part of this view is not clipped by any of its parents, then
9087 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009088 * coordinates (without taking possible View rotations into account), offset
9089 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9090 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009091 *
9092 * @param r If true is returned, r holds the global coordinates of the
9093 * visible portion of this view.
9094 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9095 * between this view and its root. globalOffet may be null.
9096 * @return true if r is non-empty (i.e. part of the view is visible at the
9097 * root level.
9098 */
9099 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9100 int width = mRight - mLeft;
9101 int height = mBottom - mTop;
9102 if (width > 0 && height > 0) {
9103 r.set(0, 0, width, height);
9104 if (globalOffset != null) {
9105 globalOffset.set(-mScrollX, -mScrollY);
9106 }
9107 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9108 }
9109 return false;
9110 }
9111
9112 public final boolean getGlobalVisibleRect(Rect r) {
9113 return getGlobalVisibleRect(r, null);
9114 }
9115
9116 public final boolean getLocalVisibleRect(Rect r) {
9117 Point offset = new Point();
9118 if (getGlobalVisibleRect(r, offset)) {
9119 r.offset(-offset.x, -offset.y); // make r local
9120 return true;
9121 }
9122 return false;
9123 }
9124
9125 /**
9126 * Offset this view's vertical location by the specified number of pixels.
9127 *
9128 * @param offset the number of pixels to offset the view by
9129 */
9130 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009131 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009132 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009133 final boolean matrixIsIdentity = mTransformationInfo == null
9134 || mTransformationInfo.mMatrixIsIdentity;
9135 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009136 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9137 invalidateViewProperty(false, false);
9138 } else {
9139 final ViewParent p = mParent;
9140 if (p != null && mAttachInfo != null) {
9141 final Rect r = mAttachInfo.mTmpInvalRect;
9142 int minTop;
9143 int maxBottom;
9144 int yLoc;
9145 if (offset < 0) {
9146 minTop = mTop + offset;
9147 maxBottom = mBottom;
9148 yLoc = offset;
9149 } else {
9150 minTop = mTop;
9151 maxBottom = mBottom + offset;
9152 yLoc = 0;
9153 }
9154 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9155 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009156 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009157 }
9158 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009159 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009160 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009161
Chet Haasec3aa3612010-06-17 08:50:37 -07009162 mTop += offset;
9163 mBottom += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08009164 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9165 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009166 invalidateViewProperty(false, false);
9167 } else {
9168 if (!matrixIsIdentity) {
9169 invalidateViewProperty(false, true);
9170 }
9171 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009172 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009174 }
9175
9176 /**
9177 * Offset this view's horizontal location by the specified amount of pixels.
9178 *
9179 * @param offset the numer of pixels to offset the view by
9180 */
9181 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009182 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009183 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009184 final boolean matrixIsIdentity = mTransformationInfo == null
9185 || mTransformationInfo.mMatrixIsIdentity;
9186 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009187 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9188 invalidateViewProperty(false, false);
9189 } else {
9190 final ViewParent p = mParent;
9191 if (p != null && mAttachInfo != null) {
9192 final Rect r = mAttachInfo.mTmpInvalRect;
9193 int minLeft;
9194 int maxRight;
9195 if (offset < 0) {
9196 minLeft = mLeft + offset;
9197 maxRight = mRight;
9198 } else {
9199 minLeft = mLeft;
9200 maxRight = mRight + offset;
9201 }
9202 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9203 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009204 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009205 }
9206 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009207 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009208 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009209
Chet Haasec3aa3612010-06-17 08:50:37 -07009210 mLeft += offset;
9211 mRight += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08009212 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9213 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009214 invalidateViewProperty(false, false);
9215 } else {
9216 if (!matrixIsIdentity) {
9217 invalidateViewProperty(false, true);
9218 }
9219 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009220 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009222 }
9223
9224 /**
9225 * Get the LayoutParams associated with this view. All views should have
9226 * layout parameters. These supply parameters to the <i>parent</i> of this
9227 * view specifying how it should be arranged. There are many subclasses of
9228 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9229 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009230 *
9231 * This method may return null if this View is not attached to a parent
9232 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9233 * was not invoked successfully. When a View is attached to a parent
9234 * ViewGroup, this method must not return null.
9235 *
9236 * @return The LayoutParams associated with this view, or null if no
9237 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009238 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009239 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009240 public ViewGroup.LayoutParams getLayoutParams() {
9241 return mLayoutParams;
9242 }
9243
9244 /**
9245 * Set the layout parameters associated with this view. These supply
9246 * parameters to the <i>parent</i> of this view specifying how it should be
9247 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9248 * correspond to the different subclasses of ViewGroup that are responsible
9249 * for arranging their children.
9250 *
Romain Guy01c174b2011-02-22 11:51:06 -08009251 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009252 */
9253 public void setLayoutParams(ViewGroup.LayoutParams params) {
9254 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009255 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009256 }
9257 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009258 if (mParent instanceof ViewGroup) {
9259 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009261 requestLayout();
9262 }
9263
9264 /**
9265 * Set the scrolled position of your view. This will cause a call to
9266 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9267 * invalidated.
9268 * @param x the x position to scroll to
9269 * @param y the y position to scroll to
9270 */
9271 public void scrollTo(int x, int y) {
9272 if (mScrollX != x || mScrollY != y) {
9273 int oldX = mScrollX;
9274 int oldY = mScrollY;
9275 mScrollX = x;
9276 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009277 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009278 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009279 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009280 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009282 }
9283 }
9284
9285 /**
9286 * Move the scrolled position of your view. This will cause a call to
9287 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9288 * invalidated.
9289 * @param x the amount of pixels to scroll by horizontally
9290 * @param y the amount of pixels to scroll by vertically
9291 */
9292 public void scrollBy(int x, int y) {
9293 scrollTo(mScrollX + x, mScrollY + y);
9294 }
9295
9296 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009297 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9298 * animation to fade the scrollbars out after a default delay. If a subclass
9299 * provides animated scrolling, the start delay should equal the duration
9300 * of the scrolling animation.</p>
9301 *
9302 * <p>The animation starts only if at least one of the scrollbars is
9303 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9304 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9305 * this method returns true, and false otherwise. If the animation is
9306 * started, this method calls {@link #invalidate()}; in that case the
9307 * caller should not call {@link #invalidate()}.</p>
9308 *
9309 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009310 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009311 *
9312 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9313 * and {@link #scrollTo(int, int)}.</p>
9314 *
9315 * @return true if the animation is played, false otherwise
9316 *
9317 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009318 * @see #scrollBy(int, int)
9319 * @see #scrollTo(int, int)
9320 * @see #isHorizontalScrollBarEnabled()
9321 * @see #isVerticalScrollBarEnabled()
9322 * @see #setHorizontalScrollBarEnabled(boolean)
9323 * @see #setVerticalScrollBarEnabled(boolean)
9324 */
9325 protected boolean awakenScrollBars() {
9326 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009327 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009328 }
9329
9330 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009331 * Trigger the scrollbars to draw.
9332 * This method differs from awakenScrollBars() only in its default duration.
9333 * initialAwakenScrollBars() will show the scroll bars for longer than
9334 * usual to give the user more of a chance to notice them.
9335 *
9336 * @return true if the animation is played, false otherwise.
9337 */
9338 private boolean initialAwakenScrollBars() {
9339 return mScrollCache != null &&
9340 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9341 }
9342
9343 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009344 * <p>
9345 * Trigger the scrollbars to draw. When invoked this method starts an
9346 * animation to fade the scrollbars out after a fixed delay. If a subclass
9347 * provides animated scrolling, the start delay should equal the duration of
9348 * the scrolling animation.
9349 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009350 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009351 * <p>
9352 * The animation starts only if at least one of the scrollbars is enabled,
9353 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9354 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9355 * this method returns true, and false otherwise. If the animation is
9356 * started, this method calls {@link #invalidate()}; in that case the caller
9357 * should not call {@link #invalidate()}.
9358 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009359 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009360 * <p>
9361 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009362 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009363 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009364 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009365 * @param startDelay the delay, in milliseconds, after which the animation
9366 * should start; when the delay is 0, the animation starts
9367 * immediately
9368 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009369 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009370 * @see #scrollBy(int, int)
9371 * @see #scrollTo(int, int)
9372 * @see #isHorizontalScrollBarEnabled()
9373 * @see #isVerticalScrollBarEnabled()
9374 * @see #setHorizontalScrollBarEnabled(boolean)
9375 * @see #setVerticalScrollBarEnabled(boolean)
9376 */
9377 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009378 return awakenScrollBars(startDelay, true);
9379 }
Joe Malin32736f02011-01-19 16:14:20 -08009380
Mike Cleron290947b2009-09-29 18:34:32 -07009381 /**
9382 * <p>
9383 * Trigger the scrollbars to draw. When invoked this method starts an
9384 * animation to fade the scrollbars out after a fixed delay. If a subclass
9385 * provides animated scrolling, the start delay should equal the duration of
9386 * the scrolling animation.
9387 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009388 *
Mike Cleron290947b2009-09-29 18:34:32 -07009389 * <p>
9390 * The animation starts only if at least one of the scrollbars is enabled,
9391 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9392 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9393 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009394 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009395 * is set to true; in that case the caller
9396 * should not call {@link #invalidate()}.
9397 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009398 *
Mike Cleron290947b2009-09-29 18:34:32 -07009399 * <p>
9400 * This method should be invoked everytime a subclass directly updates the
9401 * scroll parameters.
9402 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009403 *
Mike Cleron290947b2009-09-29 18:34:32 -07009404 * @param startDelay the delay, in milliseconds, after which the animation
9405 * should start; when the delay is 0, the animation starts
9406 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009407 *
Mike Cleron290947b2009-09-29 18:34:32 -07009408 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009409 *
Mike Cleron290947b2009-09-29 18:34:32 -07009410 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009411 *
Mike Cleron290947b2009-09-29 18:34:32 -07009412 * @see #scrollBy(int, int)
9413 * @see #scrollTo(int, int)
9414 * @see #isHorizontalScrollBarEnabled()
9415 * @see #isVerticalScrollBarEnabled()
9416 * @see #setHorizontalScrollBarEnabled(boolean)
9417 * @see #setVerticalScrollBarEnabled(boolean)
9418 */
9419 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009420 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009421
Mike Cleronf116bf82009-09-27 19:14:12 -07009422 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9423 return false;
9424 }
9425
9426 if (scrollCache.scrollBar == null) {
9427 scrollCache.scrollBar = new ScrollBarDrawable();
9428 }
9429
9430 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9431
Mike Cleron290947b2009-09-29 18:34:32 -07009432 if (invalidate) {
9433 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009434 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009435 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009436
9437 if (scrollCache.state == ScrollabilityCache.OFF) {
9438 // FIXME: this is copied from WindowManagerService.
9439 // We should get this value from the system when it
9440 // is possible to do so.
9441 final int KEY_REPEAT_FIRST_DELAY = 750;
9442 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9443 }
9444
9445 // Tell mScrollCache when we should start fading. This may
9446 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009447 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009448 scrollCache.fadeStartTime = fadeStartTime;
9449 scrollCache.state = ScrollabilityCache.ON;
9450
9451 // Schedule our fader to run, unscheduling any old ones first
9452 if (mAttachInfo != null) {
9453 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9454 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9455 }
9456
9457 return true;
9458 }
9459
9460 return false;
9461 }
9462
9463 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009464 * Do not invalidate views which are not visible and which are not running an animation. They
9465 * will not get drawn and they should not set dirty flags as if they will be drawn
9466 */
9467 private boolean skipInvalidate() {
9468 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9469 (!(mParent instanceof ViewGroup) ||
9470 !((ViewGroup) mParent).isViewTransitioning(this));
9471 }
9472 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009473 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009474 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
9475 * in the future. This must be called from a UI thread. To call from a non-UI
9476 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009477 *
9478 * WARNING: This method is destructive to dirty.
9479 * @param dirty the rectangle representing the bounds of the dirty region
9480 */
9481 public void invalidate(Rect dirty) {
9482 if (ViewDebug.TRACE_HIERARCHY) {
9483 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9484 }
9485
Chet Haaseaceafe62011-08-26 15:44:33 -07009486 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009487 return;
9488 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009489 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009490 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9491 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009492 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009493 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009494 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009495 final ViewParent p = mParent;
9496 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009497 //noinspection PointlessBooleanExpression,ConstantConditions
9498 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9499 if (p != null && ai != null && ai.mHardwareAccelerated) {
9500 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009501 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009502 p.invalidateChild(this, null);
9503 return;
9504 }
Romain Guyaf636eb2010-12-09 17:47:21 -08009505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009506 if (p != null && ai != null) {
9507 final int scrollX = mScrollX;
9508 final int scrollY = mScrollY;
9509 final Rect r = ai.mTmpInvalRect;
9510 r.set(dirty.left - scrollX, dirty.top - scrollY,
9511 dirty.right - scrollX, dirty.bottom - scrollY);
9512 mParent.invalidateChild(this, r);
9513 }
9514 }
9515 }
9516
9517 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009518 * 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 -08009519 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009520 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
9521 * will be called at some point in the future. This must be called from
9522 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009523 * @param l the left position of the dirty region
9524 * @param t the top position of the dirty region
9525 * @param r the right position of the dirty region
9526 * @param b the bottom position of the dirty region
9527 */
9528 public void invalidate(int l, int t, int r, int b) {
9529 if (ViewDebug.TRACE_HIERARCHY) {
9530 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9531 }
9532
Chet Haaseaceafe62011-08-26 15:44:33 -07009533 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009534 return;
9535 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009536 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009537 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9538 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009539 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009540 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009541 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009542 final ViewParent p = mParent;
9543 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009544 //noinspection PointlessBooleanExpression,ConstantConditions
9545 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9546 if (p != null && ai != null && ai.mHardwareAccelerated) {
9547 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009548 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009549 p.invalidateChild(this, null);
9550 return;
9551 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009553 if (p != null && ai != null && l < r && t < b) {
9554 final int scrollX = mScrollX;
9555 final int scrollY = mScrollY;
9556 final Rect tmpr = ai.mTmpInvalRect;
9557 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
9558 p.invalidateChild(this, tmpr);
9559 }
9560 }
9561 }
9562
9563 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009564 * Invalidate the whole view. If the view is visible,
9565 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
9566 * the future. This must be called from a UI thread. To call from a non-UI thread,
9567 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009568 */
9569 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07009570 invalidate(true);
9571 }
Joe Malin32736f02011-01-19 16:14:20 -08009572
Chet Haaseed032702010-10-01 14:05:54 -07009573 /**
9574 * This is where the invalidate() work actually happens. A full invalidate()
9575 * causes the drawing cache to be invalidated, but this function can be called with
9576 * invalidateCache set to false to skip that invalidation step for cases that do not
9577 * need it (for example, a component that remains at the same dimensions with the same
9578 * content).
9579 *
9580 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9581 * well. This is usually true for a full invalidate, but may be set to false if the
9582 * View's contents or dimensions have not changed.
9583 */
Romain Guy849d0a32011-02-01 17:20:48 -08009584 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009585 if (ViewDebug.TRACE_HIERARCHY) {
9586 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9587 }
9588
Chet Haaseaceafe62011-08-26 15:44:33 -07009589 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009590 return;
9591 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009592 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08009593 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08009594 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
9595 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07009596 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07009597 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07009598 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009599 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07009600 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009602 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07009603 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08009604 //noinspection PointlessBooleanExpression,ConstantConditions
9605 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9606 if (p != null && ai != null && ai.mHardwareAccelerated) {
9607 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009608 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009609 p.invalidateChild(this, null);
9610 return;
9611 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009612 }
Michael Jurkaebefea42010-11-15 16:04:01 -08009613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009614 if (p != null && ai != null) {
9615 final Rect r = ai.mTmpInvalRect;
9616 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9617 // Don't call invalidate -- we don't want to internally scroll
9618 // our own bounds
9619 p.invalidateChild(this, r);
9620 }
9621 }
9622 }
9623
9624 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07009625 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
9626 * set any flags or handle all of the cases handled by the default invalidation methods.
9627 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
9628 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
9629 * walk up the hierarchy, transforming the dirty rect as necessary.
9630 *
9631 * The method also handles normal invalidation logic if display list properties are not
9632 * being used in this view. The invalidateParent and forceRedraw flags are used by that
9633 * backup approach, to handle these cases used in the various property-setting methods.
9634 *
9635 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
9636 * are not being used in this view
9637 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
9638 * list properties are not being used in this view
9639 */
9640 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
9641 if (!USE_DISPLAY_LIST_PROPERTIES || mDisplayList == null ||
9642 (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
9643 if (invalidateParent) {
9644 invalidateParentCaches();
9645 }
9646 if (forceRedraw) {
9647 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
9648 }
9649 invalidate(false);
9650 } else {
9651 final AttachInfo ai = mAttachInfo;
9652 final ViewParent p = mParent;
9653 if (p != null && ai != null) {
9654 final Rect r = ai.mTmpInvalRect;
9655 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9656 if (mParent instanceof ViewGroup) {
9657 ((ViewGroup) mParent).invalidateChildFast(this, r);
9658 } else {
9659 mParent.invalidateChild(this, r);
9660 }
9661 }
9662 }
9663 }
9664
9665 /**
9666 * Utility method to transform a given Rect by the current matrix of this view.
9667 */
9668 void transformRect(final Rect rect) {
9669 if (!getMatrix().isIdentity()) {
9670 RectF boundingRect = mAttachInfo.mTmpTransformRect;
9671 boundingRect.set(rect);
9672 getMatrix().mapRect(boundingRect);
9673 rect.set((int) (boundingRect.left - 0.5f),
9674 (int) (boundingRect.top - 0.5f),
9675 (int) (boundingRect.right + 0.5f),
9676 (int) (boundingRect.bottom + 0.5f));
9677 }
9678 }
9679
9680 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08009681 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08009682 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9683 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08009684 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
9685 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08009686 *
9687 * @hide
9688 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08009689 protected void invalidateParentCaches() {
9690 if (mParent instanceof View) {
9691 ((View) mParent).mPrivateFlags |= INVALIDATED;
9692 }
9693 }
Joe Malin32736f02011-01-19 16:14:20 -08009694
Romain Guy0fd89bf2011-01-26 15:41:30 -08009695 /**
9696 * Used to indicate that the parent of this view should be invalidated. This functionality
9697 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9698 * which is necessary when various parent-managed properties of the view change, such as
9699 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
9700 * an invalidation event to the parent.
9701 *
9702 * @hide
9703 */
9704 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08009705 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08009706 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08009707 }
9708 }
9709
9710 /**
Romain Guy24443ea2009-05-11 11:56:30 -07009711 * Indicates whether this View is opaque. An opaque View guarantees that it will
9712 * draw all the pixels overlapping its bounds using a fully opaque color.
9713 *
9714 * Subclasses of View should override this method whenever possible to indicate
9715 * whether an instance is opaque. Opaque Views are treated in a special way by
9716 * the View hierarchy, possibly allowing it to perform optimizations during
9717 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07009718 *
Romain Guy24443ea2009-05-11 11:56:30 -07009719 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07009720 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009721 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07009722 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07009723 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009724 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
9725 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07009726 }
9727
Adam Powell20232d02010-12-08 21:08:53 -08009728 /**
9729 * @hide
9730 */
9731 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07009732 // Opaque if:
9733 // - Has a background
9734 // - Background is opaque
9735 // - Doesn't have scrollbars or scrollbars are inside overlay
9736
Philip Milne6c8ea062012-04-03 17:38:43 -07009737 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -07009738 mPrivateFlags |= OPAQUE_BACKGROUND;
9739 } else {
9740 mPrivateFlags &= ~OPAQUE_BACKGROUND;
9741 }
9742
9743 final int flags = mViewFlags;
9744 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
9745 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
9746 mPrivateFlags |= OPAQUE_SCROLLBARS;
9747 } else {
9748 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
9749 }
9750 }
9751
9752 /**
9753 * @hide
9754 */
9755 protected boolean hasOpaqueScrollbars() {
9756 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07009757 }
9758
9759 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009760 * @return A handler associated with the thread running the View. This
9761 * handler can be used to pump events in the UI events queue.
9762 */
9763 public Handler getHandler() {
9764 if (mAttachInfo != null) {
9765 return mAttachInfo.mHandler;
9766 }
9767 return null;
9768 }
9769
9770 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08009771 * Gets the view root associated with the View.
9772 * @return The view root, or null if none.
9773 * @hide
9774 */
9775 public ViewRootImpl getViewRootImpl() {
9776 if (mAttachInfo != null) {
9777 return mAttachInfo.mViewRootImpl;
9778 }
9779 return null;
9780 }
9781
9782 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009783 * <p>Causes the Runnable to be added to the message queue.
9784 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009785 *
Romain Guye63a4f32011-08-11 11:33:31 -07009786 * <p>This method can be invoked from outside of the UI thread
9787 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009788 *
9789 * @param action The Runnable that will be executed.
9790 *
9791 * @return Returns true if the Runnable was successfully placed in to the
9792 * message queue. Returns false on failure, usually because the
9793 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009794 *
9795 * @see #postDelayed
9796 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009797 */
9798 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009799 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009800 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009801 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009802 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009803 // Assume that post will succeed later
9804 ViewRootImpl.getRunQueue().post(action);
9805 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009806 }
9807
9808 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009809 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009810 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07009811 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009812 *
Romain Guye63a4f32011-08-11 11:33:31 -07009813 * <p>This method can be invoked from outside of the UI thread
9814 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009815 *
9816 * @param action The Runnable that will be executed.
9817 * @param delayMillis The delay (in milliseconds) until the Runnable
9818 * will be executed.
9819 *
9820 * @return true if the Runnable was successfully placed in to the
9821 * message queue. Returns false on failure, usually because the
9822 * looper processing the message queue is exiting. Note that a
9823 * result of true does not mean the Runnable will be processed --
9824 * if the looper is quit before the delivery time of the message
9825 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009826 *
9827 * @see #post
9828 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009829 */
9830 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009831 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009832 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009833 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009834 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009835 // Assume that post will succeed later
9836 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9837 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009838 }
9839
9840 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009841 * <p>Causes the Runnable to execute on the next animation time step.
9842 * The runnable will be run on the user interface thread.</p>
9843 *
9844 * <p>This method can be invoked from outside of the UI thread
9845 * only when this View is attached to a window.</p>
9846 *
9847 * @param action The Runnable that will be executed.
9848 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009849 * @see #postOnAnimationDelayed
9850 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009851 */
9852 public void postOnAnimation(Runnable action) {
9853 final AttachInfo attachInfo = mAttachInfo;
9854 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009855 attachInfo.mViewRootImpl.mChoreographer.postCallback(
9856 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009857 } else {
9858 // Assume that post will succeed later
9859 ViewRootImpl.getRunQueue().post(action);
9860 }
9861 }
9862
9863 /**
9864 * <p>Causes the Runnable to execute on the next animation time step,
9865 * after the specified amount of time elapses.
9866 * The runnable will be run on the user interface thread.</p>
9867 *
9868 * <p>This method can be invoked from outside of the UI thread
9869 * only when this View is attached to a window.</p>
9870 *
9871 * @param action The Runnable that will be executed.
9872 * @param delayMillis The delay (in milliseconds) until the Runnable
9873 * will be executed.
9874 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009875 * @see #postOnAnimation
9876 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009877 */
9878 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
9879 final AttachInfo attachInfo = mAttachInfo;
9880 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009881 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
9882 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009883 } else {
9884 // Assume that post will succeed later
9885 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9886 }
9887 }
9888
9889 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009890 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009891 *
Romain Guye63a4f32011-08-11 11:33:31 -07009892 * <p>This method can be invoked from outside of the UI thread
9893 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009894 *
9895 * @param action The Runnable to remove from the message handling queue
9896 *
9897 * @return true if this view could ask the Handler to remove the Runnable,
9898 * false otherwise. When the returned value is true, the Runnable
9899 * may or may not have been actually removed from the message queue
9900 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009901 *
9902 * @see #post
9903 * @see #postDelayed
9904 * @see #postOnAnimation
9905 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009906 */
9907 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -08009908 if (action != null) {
9909 final AttachInfo attachInfo = mAttachInfo;
9910 if (attachInfo != null) {
9911 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009912 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
9913 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -08009914 } else {
9915 // Assume that post will succeed later
9916 ViewRootImpl.getRunQueue().removeCallbacks(action);
9917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009919 return true;
9920 }
9921
9922 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009923 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
9924 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009925 *
Romain Guye63a4f32011-08-11 11:33:31 -07009926 * <p>This method can be invoked from outside of the UI thread
9927 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009928 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009929 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009930 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009931 */
9932 public void postInvalidate() {
9933 postInvalidateDelayed(0);
9934 }
9935
9936 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009937 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9938 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009939 *
Romain Guye63a4f32011-08-11 11:33:31 -07009940 * <p>This method can be invoked from outside of the UI thread
9941 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009942 *
9943 * @param left The left coordinate of the rectangle to invalidate.
9944 * @param top The top coordinate of the rectangle to invalidate.
9945 * @param right The right coordinate of the rectangle to invalidate.
9946 * @param bottom The bottom coordinate of the rectangle to invalidate.
9947 *
9948 * @see #invalidate(int, int, int, int)
9949 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009950 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009951 */
9952 public void postInvalidate(int left, int top, int right, int bottom) {
9953 postInvalidateDelayed(0, left, top, right, bottom);
9954 }
9955
9956 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009957 * <p>Cause an invalidate to happen on a subsequent cycle through the event
9958 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009959 *
Romain Guye63a4f32011-08-11 11:33:31 -07009960 * <p>This method can be invoked from outside of the UI thread
9961 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009962 *
9963 * @param delayMilliseconds the duration in milliseconds to delay the
9964 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009965 *
9966 * @see #invalidate()
9967 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009968 */
9969 public void postInvalidateDelayed(long delayMilliseconds) {
9970 // We try only with the AttachInfo because there's no point in invalidating
9971 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009972 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009973 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009974 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009975 }
9976 }
9977
9978 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009979 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9980 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009981 *
Romain Guye63a4f32011-08-11 11:33:31 -07009982 * <p>This method can be invoked from outside of the UI thread
9983 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009984 *
9985 * @param delayMilliseconds the duration in milliseconds to delay the
9986 * invalidation by
9987 * @param left The left coordinate of the rectangle to invalidate.
9988 * @param top The top coordinate of the rectangle to invalidate.
9989 * @param right The right coordinate of the rectangle to invalidate.
9990 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009991 *
9992 * @see #invalidate(int, int, int, int)
9993 * @see #invalidate(Rect)
9994 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009995 */
9996 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
9997 int right, int bottom) {
9998
9999 // We try only with the AttachInfo because there's no point in invalidating
10000 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010001 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010002 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010003 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10004 info.target = this;
10005 info.left = left;
10006 info.top = top;
10007 info.right = right;
10008 info.bottom = bottom;
10009
Jeff Browna175a5b2012-02-15 19:18:31 -080010010 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010011 }
10012 }
10013
10014 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010015 * <p>Cause an invalidate to happen on the next animation time step, typically the
10016 * next display frame.</p>
10017 *
10018 * <p>This method can be invoked from outside of the UI thread
10019 * only when this View is attached to a window.</p>
10020 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010021 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010022 */
10023 public void postInvalidateOnAnimation() {
10024 // We try only with the AttachInfo because there's no point in invalidating
10025 // if we are not attached to our window
10026 final AttachInfo attachInfo = mAttachInfo;
10027 if (attachInfo != null) {
10028 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10029 }
10030 }
10031
10032 /**
10033 * <p>Cause an invalidate of the specified area to happen on the next animation
10034 * time step, typically the next display frame.</p>
10035 *
10036 * <p>This method can be invoked from outside of the UI thread
10037 * only when this View is attached to a window.</p>
10038 *
10039 * @param left The left coordinate of the rectangle to invalidate.
10040 * @param top The top coordinate of the rectangle to invalidate.
10041 * @param right The right coordinate of the rectangle to invalidate.
10042 * @param bottom The bottom coordinate of the rectangle to invalidate.
10043 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010044 * @see #invalidate(int, int, int, int)
10045 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010046 */
10047 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10048 // We try only with the AttachInfo because there's no point in invalidating
10049 // if we are not attached to our window
10050 final AttachInfo attachInfo = mAttachInfo;
10051 if (attachInfo != null) {
10052 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10053 info.target = this;
10054 info.left = left;
10055 info.top = top;
10056 info.right = right;
10057 info.bottom = bottom;
10058
10059 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10060 }
10061 }
10062
10063 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010064 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10065 * This event is sent at most once every
10066 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10067 */
10068 private void postSendViewScrolledAccessibilityEventCallback() {
10069 if (mSendViewScrolledAccessibilityEvent == null) {
10070 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10071 }
10072 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10073 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10074 postDelayed(mSendViewScrolledAccessibilityEvent,
10075 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10076 }
10077 }
10078
10079 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010080 * Called by a parent to request that a child update its values for mScrollX
10081 * and mScrollY if necessary. This will typically be done if the child is
10082 * animating a scroll using a {@link android.widget.Scroller Scroller}
10083 * object.
10084 */
10085 public void computeScroll() {
10086 }
10087
10088 /**
10089 * <p>Indicate whether the horizontal edges are faded when the view is
10090 * scrolled horizontally.</p>
10091 *
10092 * @return true if the horizontal edges should are faded on scroll, false
10093 * otherwise
10094 *
10095 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010096 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010097 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010098 */
10099 public boolean isHorizontalFadingEdgeEnabled() {
10100 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10101 }
10102
10103 /**
10104 * <p>Define whether the horizontal edges should be faded when this view
10105 * is scrolled horizontally.</p>
10106 *
10107 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10108 * be faded when the view is scrolled
10109 * horizontally
10110 *
10111 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010112 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010113 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010114 */
10115 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10116 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10117 if (horizontalFadingEdgeEnabled) {
10118 initScrollCache();
10119 }
10120
10121 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10122 }
10123 }
10124
10125 /**
10126 * <p>Indicate whether the vertical edges are faded when the view is
10127 * scrolled horizontally.</p>
10128 *
10129 * @return true if the vertical edges should are faded on scroll, false
10130 * otherwise
10131 *
10132 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010133 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010134 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010135 */
10136 public boolean isVerticalFadingEdgeEnabled() {
10137 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10138 }
10139
10140 /**
10141 * <p>Define whether the vertical edges should be faded when this view
10142 * is scrolled vertically.</p>
10143 *
10144 * @param verticalFadingEdgeEnabled true if the vertical edges should
10145 * be faded when the view is scrolled
10146 * vertically
10147 *
10148 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010149 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010150 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010151 */
10152 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10153 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10154 if (verticalFadingEdgeEnabled) {
10155 initScrollCache();
10156 }
10157
10158 mViewFlags ^= FADING_EDGE_VERTICAL;
10159 }
10160 }
10161
10162 /**
10163 * Returns the strength, or intensity, of the top faded edge. The strength is
10164 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10165 * returns 0.0 or 1.0 but no value in between.
10166 *
10167 * Subclasses should override this method to provide a smoother fade transition
10168 * when scrolling occurs.
10169 *
10170 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10171 */
10172 protected float getTopFadingEdgeStrength() {
10173 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10174 }
10175
10176 /**
10177 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10178 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10179 * returns 0.0 or 1.0 but no value in between.
10180 *
10181 * Subclasses should override this method to provide a smoother fade transition
10182 * when scrolling occurs.
10183 *
10184 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10185 */
10186 protected float getBottomFadingEdgeStrength() {
10187 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10188 computeVerticalScrollRange() ? 1.0f : 0.0f;
10189 }
10190
10191 /**
10192 * Returns the strength, or intensity, of the left faded edge. The strength is
10193 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10194 * returns 0.0 or 1.0 but no value in between.
10195 *
10196 * Subclasses should override this method to provide a smoother fade transition
10197 * when scrolling occurs.
10198 *
10199 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10200 */
10201 protected float getLeftFadingEdgeStrength() {
10202 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10203 }
10204
10205 /**
10206 * Returns the strength, or intensity, of the right faded edge. The strength is
10207 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10208 * returns 0.0 or 1.0 but no value in between.
10209 *
10210 * Subclasses should override this method to provide a smoother fade transition
10211 * when scrolling occurs.
10212 *
10213 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10214 */
10215 protected float getRightFadingEdgeStrength() {
10216 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10217 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10218 }
10219
10220 /**
10221 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10222 * scrollbar is not drawn by default.</p>
10223 *
10224 * @return true if the horizontal scrollbar should be painted, false
10225 * otherwise
10226 *
10227 * @see #setHorizontalScrollBarEnabled(boolean)
10228 */
10229 public boolean isHorizontalScrollBarEnabled() {
10230 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10231 }
10232
10233 /**
10234 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10235 * scrollbar is not drawn by default.</p>
10236 *
10237 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10238 * be painted
10239 *
10240 * @see #isHorizontalScrollBarEnabled()
10241 */
10242 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10243 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10244 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010245 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010246 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010247 }
10248 }
10249
10250 /**
10251 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10252 * scrollbar is not drawn by default.</p>
10253 *
10254 * @return true if the vertical scrollbar should be painted, false
10255 * otherwise
10256 *
10257 * @see #setVerticalScrollBarEnabled(boolean)
10258 */
10259 public boolean isVerticalScrollBarEnabled() {
10260 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10261 }
10262
10263 /**
10264 * <p>Define whether the vertical scrollbar should be drawn or not. The
10265 * scrollbar is not drawn by default.</p>
10266 *
10267 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10268 * be painted
10269 *
10270 * @see #isVerticalScrollBarEnabled()
10271 */
10272 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10273 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10274 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010275 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010276 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010277 }
10278 }
10279
Adam Powell20232d02010-12-08 21:08:53 -080010280 /**
10281 * @hide
10282 */
10283 protected void recomputePadding() {
10284 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010285 }
Joe Malin32736f02011-01-19 16:14:20 -080010286
Mike Cleronfe81d382009-09-28 14:22:16 -070010287 /**
10288 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010289 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010290 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010291 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010292 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010293 */
10294 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10295 initScrollCache();
10296 final ScrollabilityCache scrollabilityCache = mScrollCache;
10297 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010298 if (fadeScrollbars) {
10299 scrollabilityCache.state = ScrollabilityCache.OFF;
10300 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010301 scrollabilityCache.state = ScrollabilityCache.ON;
10302 }
10303 }
Joe Malin32736f02011-01-19 16:14:20 -080010304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010305 /**
Joe Malin32736f02011-01-19 16:14:20 -080010306 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010307 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010308 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010309 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010310 *
10311 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010312 */
10313 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010314 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010315 }
Joe Malin32736f02011-01-19 16:14:20 -080010316
Mike Cleron52f0a642009-09-28 18:21:37 -070010317 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010318 *
10319 * Returns the delay before scrollbars fade.
10320 *
10321 * @return the delay before scrollbars fade
10322 *
10323 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10324 */
10325 public int getScrollBarDefaultDelayBeforeFade() {
10326 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10327 mScrollCache.scrollBarDefaultDelayBeforeFade;
10328 }
10329
10330 /**
10331 * Define the delay before scrollbars fade.
10332 *
10333 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10334 *
10335 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10336 */
10337 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10338 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10339 }
10340
10341 /**
10342 *
10343 * Returns the scrollbar fade duration.
10344 *
10345 * @return the scrollbar fade duration
10346 *
10347 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10348 */
10349 public int getScrollBarFadeDuration() {
10350 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10351 mScrollCache.scrollBarFadeDuration;
10352 }
10353
10354 /**
10355 * Define the scrollbar fade duration.
10356 *
10357 * @param scrollBarFadeDuration - the scrollbar fade duration
10358 *
10359 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10360 */
10361 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10362 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10363 }
10364
10365 /**
10366 *
10367 * Returns the scrollbar size.
10368 *
10369 * @return the scrollbar size
10370 *
10371 * @attr ref android.R.styleable#View_scrollbarSize
10372 */
10373 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010374 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010375 mScrollCache.scrollBarSize;
10376 }
10377
10378 /**
10379 * Define the scrollbar size.
10380 *
10381 * @param scrollBarSize - the scrollbar size
10382 *
10383 * @attr ref android.R.styleable#View_scrollbarSize
10384 */
10385 public void setScrollBarSize(int scrollBarSize) {
10386 getScrollCache().scrollBarSize = scrollBarSize;
10387 }
10388
10389 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010390 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10391 * inset. When inset, they add to the padding of the view. And the scrollbars
10392 * can be drawn inside the padding area or on the edge of the view. For example,
10393 * if a view has a background drawable and you want to draw the scrollbars
10394 * inside the padding specified by the drawable, you can use
10395 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10396 * appear at the edge of the view, ignoring the padding, then you can use
10397 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10398 * @param style the style of the scrollbars. Should be one of
10399 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10400 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10401 * @see #SCROLLBARS_INSIDE_OVERLAY
10402 * @see #SCROLLBARS_INSIDE_INSET
10403 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10404 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010405 *
10406 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010407 */
10408 public void setScrollBarStyle(int style) {
10409 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10410 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010411 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010412 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010413 }
10414 }
10415
10416 /**
10417 * <p>Returns the current scrollbar style.</p>
10418 * @return the current scrollbar style
10419 * @see #SCROLLBARS_INSIDE_OVERLAY
10420 * @see #SCROLLBARS_INSIDE_INSET
10421 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10422 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010423 *
10424 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010425 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010426 @ViewDebug.ExportedProperty(mapping = {
10427 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10428 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10429 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10430 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10431 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010432 public int getScrollBarStyle() {
10433 return mViewFlags & SCROLLBARS_STYLE_MASK;
10434 }
10435
10436 /**
10437 * <p>Compute the horizontal range that the horizontal scrollbar
10438 * represents.</p>
10439 *
10440 * <p>The range is expressed in arbitrary units that must be the same as the
10441 * units used by {@link #computeHorizontalScrollExtent()} and
10442 * {@link #computeHorizontalScrollOffset()}.</p>
10443 *
10444 * <p>The default range is the drawing width of this view.</p>
10445 *
10446 * @return the total horizontal range represented by the horizontal
10447 * scrollbar
10448 *
10449 * @see #computeHorizontalScrollExtent()
10450 * @see #computeHorizontalScrollOffset()
10451 * @see android.widget.ScrollBarDrawable
10452 */
10453 protected int computeHorizontalScrollRange() {
10454 return getWidth();
10455 }
10456
10457 /**
10458 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10459 * within the horizontal range. This value is used to compute the position
10460 * of the thumb within the scrollbar's track.</p>
10461 *
10462 * <p>The range is expressed in arbitrary units that must be the same as the
10463 * units used by {@link #computeHorizontalScrollRange()} and
10464 * {@link #computeHorizontalScrollExtent()}.</p>
10465 *
10466 * <p>The default offset is the scroll offset of this view.</p>
10467 *
10468 * @return the horizontal offset of the scrollbar's thumb
10469 *
10470 * @see #computeHorizontalScrollRange()
10471 * @see #computeHorizontalScrollExtent()
10472 * @see android.widget.ScrollBarDrawable
10473 */
10474 protected int computeHorizontalScrollOffset() {
10475 return mScrollX;
10476 }
10477
10478 /**
10479 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10480 * within the horizontal range. This value is used to compute the length
10481 * of the thumb within the scrollbar's track.</p>
10482 *
10483 * <p>The range is expressed in arbitrary units that must be the same as the
10484 * units used by {@link #computeHorizontalScrollRange()} and
10485 * {@link #computeHorizontalScrollOffset()}.</p>
10486 *
10487 * <p>The default extent is the drawing width of this view.</p>
10488 *
10489 * @return the horizontal extent of the scrollbar's thumb
10490 *
10491 * @see #computeHorizontalScrollRange()
10492 * @see #computeHorizontalScrollOffset()
10493 * @see android.widget.ScrollBarDrawable
10494 */
10495 protected int computeHorizontalScrollExtent() {
10496 return getWidth();
10497 }
10498
10499 /**
10500 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
10501 *
10502 * <p>The range is expressed in arbitrary units that must be the same as the
10503 * units used by {@link #computeVerticalScrollExtent()} and
10504 * {@link #computeVerticalScrollOffset()}.</p>
10505 *
10506 * @return the total vertical range represented by the vertical scrollbar
10507 *
10508 * <p>The default range is the drawing height of this view.</p>
10509 *
10510 * @see #computeVerticalScrollExtent()
10511 * @see #computeVerticalScrollOffset()
10512 * @see android.widget.ScrollBarDrawable
10513 */
10514 protected int computeVerticalScrollRange() {
10515 return getHeight();
10516 }
10517
10518 /**
10519 * <p>Compute the vertical offset of the vertical scrollbar's thumb
10520 * within the horizontal range. This value is used to compute the position
10521 * of the thumb within the scrollbar's track.</p>
10522 *
10523 * <p>The range is expressed in arbitrary units that must be the same as the
10524 * units used by {@link #computeVerticalScrollRange()} and
10525 * {@link #computeVerticalScrollExtent()}.</p>
10526 *
10527 * <p>The default offset is the scroll offset of this view.</p>
10528 *
10529 * @return the vertical offset of the scrollbar's thumb
10530 *
10531 * @see #computeVerticalScrollRange()
10532 * @see #computeVerticalScrollExtent()
10533 * @see android.widget.ScrollBarDrawable
10534 */
10535 protected int computeVerticalScrollOffset() {
10536 return mScrollY;
10537 }
10538
10539 /**
10540 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
10541 * within the vertical range. This value is used to compute the length
10542 * of the thumb within the scrollbar's track.</p>
10543 *
10544 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080010545 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010546 * {@link #computeVerticalScrollOffset()}.</p>
10547 *
10548 * <p>The default extent is the drawing height of this view.</p>
10549 *
10550 * @return the vertical extent of the scrollbar's thumb
10551 *
10552 * @see #computeVerticalScrollRange()
10553 * @see #computeVerticalScrollOffset()
10554 * @see android.widget.ScrollBarDrawable
10555 */
10556 protected int computeVerticalScrollExtent() {
10557 return getHeight();
10558 }
10559
10560 /**
Adam Powell69159442011-06-13 17:53:06 -070010561 * Check if this view can be scrolled horizontally in a certain direction.
10562 *
10563 * @param direction Negative to check scrolling left, positive to check scrolling right.
10564 * @return true if this view can be scrolled in the specified direction, false otherwise.
10565 */
10566 public boolean canScrollHorizontally(int direction) {
10567 final int offset = computeHorizontalScrollOffset();
10568 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
10569 if (range == 0) return false;
10570 if (direction < 0) {
10571 return offset > 0;
10572 } else {
10573 return offset < range - 1;
10574 }
10575 }
10576
10577 /**
10578 * Check if this view can be scrolled vertically in a certain direction.
10579 *
10580 * @param direction Negative to check scrolling up, positive to check scrolling down.
10581 * @return true if this view can be scrolled in the specified direction, false otherwise.
10582 */
10583 public boolean canScrollVertically(int direction) {
10584 final int offset = computeVerticalScrollOffset();
10585 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
10586 if (range == 0) return false;
10587 if (direction < 0) {
10588 return offset > 0;
10589 } else {
10590 return offset < range - 1;
10591 }
10592 }
10593
10594 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010595 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
10596 * scrollbars are painted only if they have been awakened first.</p>
10597 *
10598 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080010599 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010600 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010601 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080010602 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010603 // scrollbars are drawn only when the animation is running
10604 final ScrollabilityCache cache = mScrollCache;
10605 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080010606
Mike Cleronf116bf82009-09-27 19:14:12 -070010607 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080010608
Mike Cleronf116bf82009-09-27 19:14:12 -070010609 if (state == ScrollabilityCache.OFF) {
10610 return;
10611 }
Joe Malin32736f02011-01-19 16:14:20 -080010612
Mike Cleronf116bf82009-09-27 19:14:12 -070010613 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080010614
Mike Cleronf116bf82009-09-27 19:14:12 -070010615 if (state == ScrollabilityCache.FADING) {
10616 // We're fading -- get our fade interpolation
10617 if (cache.interpolatorValues == null) {
10618 cache.interpolatorValues = new float[1];
10619 }
Joe Malin32736f02011-01-19 16:14:20 -080010620
Mike Cleronf116bf82009-09-27 19:14:12 -070010621 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080010622
Mike Cleronf116bf82009-09-27 19:14:12 -070010623 // Stops the animation if we're done
10624 if (cache.scrollBarInterpolator.timeToValues(values) ==
10625 Interpolator.Result.FREEZE_END) {
10626 cache.state = ScrollabilityCache.OFF;
10627 } else {
10628 cache.scrollBar.setAlpha(Math.round(values[0]));
10629 }
Joe Malin32736f02011-01-19 16:14:20 -080010630
10631 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070010632 // drawing. We only want this when we're fading so that
10633 // we prevent excessive redraws
10634 invalidate = true;
10635 } else {
10636 // We're just on -- but we may have been fading before so
10637 // reset alpha
10638 cache.scrollBar.setAlpha(255);
10639 }
10640
Joe Malin32736f02011-01-19 16:14:20 -080010641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010642 final int viewFlags = mViewFlags;
10643
10644 final boolean drawHorizontalScrollBar =
10645 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10646 final boolean drawVerticalScrollBar =
10647 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
10648 && !isVerticalScrollBarHidden();
10649
10650 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
10651 final int width = mRight - mLeft;
10652 final int height = mBottom - mTop;
10653
10654 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010655
Mike Reede8853fc2009-09-04 14:01:48 -040010656 final int scrollX = mScrollX;
10657 final int scrollY = mScrollY;
10658 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
10659
Mike Cleronf116bf82009-09-27 19:14:12 -070010660 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080010661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010662 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010663 int size = scrollBar.getSize(false);
10664 if (size <= 0) {
10665 size = cache.scrollBarSize;
10666 }
10667
Mike Cleronf116bf82009-09-27 19:14:12 -070010668 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040010669 computeHorizontalScrollOffset(),
10670 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040010671 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070010672 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080010673 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070010674 left = scrollX + (mPaddingLeft & inside);
10675 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
10676 bottom = top + size;
10677 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
10678 if (invalidate) {
10679 invalidate(left, top, right, bottom);
10680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010681 }
10682
10683 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010684 int size = scrollBar.getSize(true);
10685 if (size <= 0) {
10686 size = cache.scrollBarSize;
10687 }
10688
Mike Reede8853fc2009-09-04 14:01:48 -040010689 scrollBar.setParameters(computeVerticalScrollRange(),
10690 computeVerticalScrollOffset(),
10691 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080010692 switch (mVerticalScrollbarPosition) {
10693 default:
10694 case SCROLLBAR_POSITION_DEFAULT:
10695 case SCROLLBAR_POSITION_RIGHT:
10696 left = scrollX + width - size - (mUserPaddingRight & inside);
10697 break;
10698 case SCROLLBAR_POSITION_LEFT:
10699 left = scrollX + (mUserPaddingLeft & inside);
10700 break;
10701 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010702 top = scrollY + (mPaddingTop & inside);
10703 right = left + size;
10704 bottom = scrollY + height - (mUserPaddingBottom & inside);
10705 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
10706 if (invalidate) {
10707 invalidate(left, top, right, bottom);
10708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010709 }
10710 }
10711 }
10712 }
Romain Guy8506ab42009-06-11 17:35:47 -070010713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010714 /**
Romain Guy8506ab42009-06-11 17:35:47 -070010715 * 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 -080010716 * FastScroller is visible.
10717 * @return whether to temporarily hide the vertical scrollbar
10718 * @hide
10719 */
10720 protected boolean isVerticalScrollBarHidden() {
10721 return false;
10722 }
10723
10724 /**
10725 * <p>Draw the horizontal scrollbar if
10726 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
10727 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010728 * @param canvas the canvas on which to draw the scrollbar
10729 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010730 *
10731 * @see #isHorizontalScrollBarEnabled()
10732 * @see #computeHorizontalScrollRange()
10733 * @see #computeHorizontalScrollExtent()
10734 * @see #computeHorizontalScrollOffset()
10735 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070010736 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010737 */
Romain Guy8fb95422010-08-17 18:38:51 -070010738 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
10739 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010740 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010741 scrollBar.draw(canvas);
10742 }
Mike Reede8853fc2009-09-04 14:01:48 -040010743
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010744 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010745 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
10746 * returns true.</p>
10747 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010748 * @param canvas the canvas on which to draw the scrollbar
10749 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010750 *
10751 * @see #isVerticalScrollBarEnabled()
10752 * @see #computeVerticalScrollRange()
10753 * @see #computeVerticalScrollExtent()
10754 * @see #computeVerticalScrollOffset()
10755 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040010756 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010757 */
Romain Guy8fb95422010-08-17 18:38:51 -070010758 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
10759 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040010760 scrollBar.setBounds(l, t, r, b);
10761 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010762 }
10763
10764 /**
10765 * Implement this to do your drawing.
10766 *
10767 * @param canvas the canvas on which the background will be drawn
10768 */
10769 protected void onDraw(Canvas canvas) {
10770 }
10771
10772 /*
10773 * Caller is responsible for calling requestLayout if necessary.
10774 * (This allows addViewInLayout to not request a new layout.)
10775 */
10776 void assignParent(ViewParent parent) {
10777 if (mParent == null) {
10778 mParent = parent;
10779 } else if (parent == null) {
10780 mParent = null;
10781 } else {
10782 throw new RuntimeException("view " + this + " being added, but"
10783 + " it already has a parent");
10784 }
10785 }
10786
10787 /**
10788 * This is called when the view is attached to a window. At this point it
10789 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010790 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
10791 * however it may be called any time before the first onDraw -- including
10792 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010793 *
10794 * @see #onDetachedFromWindow()
10795 */
10796 protected void onAttachedToWindow() {
10797 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
10798 mParent.requestTransparentRegion(this);
10799 }
Adam Powell8568c3a2010-04-19 14:26:11 -070010800 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
10801 initialAwakenScrollBars();
10802 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
10803 }
Chet Haasea9b61ac2010-12-20 07:40:25 -080010804 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070010805 // Order is important here: LayoutDirection MUST be resolved before Padding
10806 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010807 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010808 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010809 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070010810 resolveTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070010811 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070010812 if (isFocused()) {
10813 InputMethodManager imm = InputMethodManager.peekInstance();
10814 imm.focusIn(this);
10815 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010816 }
Cibu Johny86666632010-02-22 13:01:02 -080010817
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010818 /**
Romain Guybb9908b2012-03-08 11:14:07 -080010819 * @see #onScreenStateChanged(int)
10820 */
10821 void dispatchScreenStateChanged(int screenState) {
10822 onScreenStateChanged(screenState);
10823 }
10824
10825 /**
10826 * This method is called whenever the state of the screen this view is
10827 * attached to changes. A state change will usually occurs when the screen
10828 * turns on or off (whether it happens automatically or the user does it
10829 * manually.)
10830 *
10831 * @param screenState The new state of the screen. Can be either
10832 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
10833 */
10834 public void onScreenStateChanged(int screenState) {
10835 }
10836
10837 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010838 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
10839 */
10840 private boolean hasRtlSupport() {
10841 return mContext.getApplicationInfo().hasRtlSupport();
10842 }
10843
10844 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010845 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
10846 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010847 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010848 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010849 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010850 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010851 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010852
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010853 if (hasRtlSupport()) {
10854 // Set resolved depending on layout direction
10855 switch (getLayoutDirection()) {
10856 case LAYOUT_DIRECTION_INHERIT:
10857 // If this is root view, no need to look at parent's layout dir.
10858 if (canResolveLayoutDirection()) {
10859 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010860
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010861 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
10862 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10863 }
10864 } else {
10865 // Nothing to do, LTR by default
10866 }
10867 break;
10868 case LAYOUT_DIRECTION_RTL:
10869 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10870 break;
10871 case LAYOUT_DIRECTION_LOCALE:
10872 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010873 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10874 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010875 break;
10876 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010877 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010878 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010879 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010880
10881 // Set to resolved
10882 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010883 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010884 // Resolve padding
10885 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010886 }
10887
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010888 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010889 * Called when layout direction has been resolved.
10890 *
10891 * The default implementation does nothing.
10892 */
10893 public void onResolvedLayoutDirectionChanged() {
10894 }
10895
10896 /**
10897 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010898 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010899 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010900 // If the user specified the absolute padding (either with android:padding or
10901 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
10902 // use the default padding or the padding from the background drawable
10903 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010904 int resolvedLayoutDirection = getResolvedLayoutDirection();
10905 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010906 case LAYOUT_DIRECTION_RTL:
10907 // Start user padding override Right user padding. Otherwise, if Right user
10908 // padding is not defined, use the default Right padding. If Right user padding
10909 // is defined, just use it.
10910 if (mUserPaddingStart >= 0) {
10911 mUserPaddingRight = mUserPaddingStart;
10912 } else if (mUserPaddingRight < 0) {
10913 mUserPaddingRight = mPaddingRight;
10914 }
10915 if (mUserPaddingEnd >= 0) {
10916 mUserPaddingLeft = mUserPaddingEnd;
10917 } else if (mUserPaddingLeft < 0) {
10918 mUserPaddingLeft = mPaddingLeft;
10919 }
10920 break;
10921 case LAYOUT_DIRECTION_LTR:
10922 default:
10923 // Start user padding override Left user padding. Otherwise, if Left user
10924 // padding is not defined, use the default left padding. If Left user padding
10925 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010926 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010927 mUserPaddingLeft = mUserPaddingStart;
10928 } else if (mUserPaddingLeft < 0) {
10929 mUserPaddingLeft = mPaddingLeft;
10930 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010931 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010932 mUserPaddingRight = mUserPaddingEnd;
10933 } else if (mUserPaddingRight < 0) {
10934 mUserPaddingRight = mPaddingRight;
10935 }
10936 }
10937
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010938 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
10939
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010940 if(isPaddingRelative()) {
10941 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
10942 } else {
10943 recomputePadding();
10944 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010945 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010946 }
10947
10948 /**
10949 * Resolve padding depending on the layout direction. Subclasses that care about
10950 * padding resolution should override this method. The default implementation does
10951 * nothing.
10952 *
10953 * @param layoutDirection the direction of the layout
10954 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080010955 * @see {@link #LAYOUT_DIRECTION_LTR}
10956 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010957 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010958 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010959 }
10960
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010961 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010962 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010963 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010964 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010965 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010966 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010967 switch (getLayoutDirection()) {
10968 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010969 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010970 default:
10971 return true;
10972 }
10973 }
10974
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010975 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010976 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
10977 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010978 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010979 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010980 // Reset the current resolved bits
10981 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010982 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080010983 // Reset also the text direction
10984 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010985 }
10986
10987 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010988 * Called during reset of resolved layout direction.
10989 *
10990 * Subclasses need to override this method to clear cached information that depends on the
10991 * resolved layout direction, or to inform child views that inherit their layout direction.
10992 *
10993 * The default implementation does nothing.
10994 */
10995 public void onResolvedLayoutDirectionReset() {
10996 }
10997
10998 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010999 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011000 *
11001 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011002 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011003 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011004 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011005 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011006 }
11007
11008 /**
11009 * This is called when the view is detached from a window. At this point it
11010 * no longer has a surface for drawing.
11011 *
11012 * @see #onAttachedToWindow()
11013 */
11014 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011015 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011016
Romain Guya440b002010-02-24 15:57:54 -080011017 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011018 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011019 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011020 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011022 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011023
Romain Guya998dff2012-03-23 18:58:36 -070011024 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011025
11026 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011027 if (mDisplayList != null) {
11028 mAttachInfo.mViewRootImpl.invalidateDisplayList(mDisplayList);
11029 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011030 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011031 } else {
11032 if (mDisplayList != null) {
11033 // Should never happen
11034 mDisplayList.invalidate();
11035 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011036 }
11037
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011038 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011039
11040 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011041 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011042 resetAccessibilityStateChanged();
11043 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011044 }
11045
11046 /**
11047 * @return The number of times this view has been attached to a window
11048 */
11049 protected int getWindowAttachCount() {
11050 return mWindowAttachCount;
11051 }
11052
11053 /**
11054 * Retrieve a unique token identifying the window this view is attached to.
11055 * @return Return the window's token for use in
11056 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11057 */
11058 public IBinder getWindowToken() {
11059 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11060 }
11061
11062 /**
11063 * Retrieve a unique token identifying the top-level "real" window of
11064 * the window that this view is attached to. That is, this is like
11065 * {@link #getWindowToken}, except if the window this view in is a panel
11066 * window (attached to another containing window), then the token of
11067 * the containing window is returned instead.
11068 *
11069 * @return Returns the associated window token, either
11070 * {@link #getWindowToken()} or the containing window's token.
11071 */
11072 public IBinder getApplicationWindowToken() {
11073 AttachInfo ai = mAttachInfo;
11074 if (ai != null) {
11075 IBinder appWindowToken = ai.mPanelParentWindowToken;
11076 if (appWindowToken == null) {
11077 appWindowToken = ai.mWindowToken;
11078 }
11079 return appWindowToken;
11080 }
11081 return null;
11082 }
11083
11084 /**
11085 * Retrieve private session object this view hierarchy is using to
11086 * communicate with the window manager.
11087 * @return the session object to communicate with the window manager
11088 */
11089 /*package*/ IWindowSession getWindowSession() {
11090 return mAttachInfo != null ? mAttachInfo.mSession : null;
11091 }
11092
11093 /**
11094 * @param info the {@link android.view.View.AttachInfo} to associated with
11095 * this view
11096 */
11097 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11098 //System.out.println("Attached! " + this);
11099 mAttachInfo = info;
11100 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011101 // We will need to evaluate the drawable state at least once.
11102 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011103 if (mFloatingTreeObserver != null) {
11104 info.mTreeObserver.merge(mFloatingTreeObserver);
11105 mFloatingTreeObserver = null;
11106 }
11107 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11108 mAttachInfo.mScrollContainers.add(this);
11109 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11110 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011111 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011112 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011113
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011114 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011115 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011116 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011117 if (listeners != null && listeners.size() > 0) {
11118 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11119 // perform the dispatching. The iterator is a safe guard against listeners that
11120 // could mutate the list by calling the various add/remove methods. This prevents
11121 // the array from being modified while we iterate it.
11122 for (OnAttachStateChangeListener listener : listeners) {
11123 listener.onViewAttachedToWindow(this);
11124 }
11125 }
11126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011127 int vis = info.mWindowVisibility;
11128 if (vis != GONE) {
11129 onWindowVisibilityChanged(vis);
11130 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011131 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11132 // If nobody has evaluated the drawable state yet, then do it now.
11133 refreshDrawableState();
11134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011135 }
11136
11137 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011138 AttachInfo info = mAttachInfo;
11139 if (info != null) {
11140 int vis = info.mWindowVisibility;
11141 if (vis != GONE) {
11142 onWindowVisibilityChanged(GONE);
11143 }
11144 }
11145
11146 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011147
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011148 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011149 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011150 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011151 if (listeners != null && listeners.size() > 0) {
11152 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11153 // perform the dispatching. The iterator is a safe guard against listeners that
11154 // could mutate the list by calling the various add/remove methods. This prevents
11155 // the array from being modified while we iterate it.
11156 for (OnAttachStateChangeListener listener : listeners) {
11157 listener.onViewDetachedFromWindow(this);
11158 }
11159 }
11160
Romain Guy01d5edc2011-01-28 11:28:53 -080011161 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011162 mAttachInfo.mScrollContainers.remove(this);
11163 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11164 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011166 mAttachInfo = null;
11167 }
11168
11169 /**
11170 * Store this view hierarchy's frozen state into the given container.
11171 *
11172 * @param container The SparseArray in which to save the view's state.
11173 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011174 * @see #restoreHierarchyState(android.util.SparseArray)
11175 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11176 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011177 */
11178 public void saveHierarchyState(SparseArray<Parcelable> container) {
11179 dispatchSaveInstanceState(container);
11180 }
11181
11182 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011183 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11184 * this view and its children. May be overridden to modify how freezing happens to a
11185 * 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 -080011186 *
11187 * @param container The SparseArray in which to save the view's state.
11188 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011189 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11190 * @see #saveHierarchyState(android.util.SparseArray)
11191 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011192 */
11193 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11194 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11195 mPrivateFlags &= ~SAVE_STATE_CALLED;
11196 Parcelable state = onSaveInstanceState();
11197 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11198 throw new IllegalStateException(
11199 "Derived class did not call super.onSaveInstanceState()");
11200 }
11201 if (state != null) {
11202 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11203 // + ": " + state);
11204 container.put(mID, state);
11205 }
11206 }
11207 }
11208
11209 /**
11210 * Hook allowing a view to generate a representation of its internal state
11211 * that can later be used to create a new instance with that same state.
11212 * This state should only contain information that is not persistent or can
11213 * not be reconstructed later. For example, you will never store your
11214 * current position on screen because that will be computed again when a
11215 * new instance of the view is placed in its view hierarchy.
11216 * <p>
11217 * Some examples of things you may store here: the current cursor position
11218 * in a text view (but usually not the text itself since that is stored in a
11219 * content provider or other persistent storage), the currently selected
11220 * item in a list view.
11221 *
11222 * @return Returns a Parcelable object containing the view's current dynamic
11223 * state, or null if there is nothing interesting to save. The
11224 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011225 * @see #onRestoreInstanceState(android.os.Parcelable)
11226 * @see #saveHierarchyState(android.util.SparseArray)
11227 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011228 * @see #setSaveEnabled(boolean)
11229 */
11230 protected Parcelable onSaveInstanceState() {
11231 mPrivateFlags |= SAVE_STATE_CALLED;
11232 return BaseSavedState.EMPTY_STATE;
11233 }
11234
11235 /**
11236 * Restore this view hierarchy's frozen state from the given container.
11237 *
11238 * @param container The SparseArray which holds previously frozen states.
11239 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011240 * @see #saveHierarchyState(android.util.SparseArray)
11241 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11242 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011243 */
11244 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11245 dispatchRestoreInstanceState(container);
11246 }
11247
11248 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011249 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11250 * state for this view and its children. May be overridden to modify how restoring
11251 * happens to a view's children; for example, some views may want to not store state
11252 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011253 *
11254 * @param container The SparseArray which holds previously saved state.
11255 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011256 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11257 * @see #restoreHierarchyState(android.util.SparseArray)
11258 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011259 */
11260 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11261 if (mID != NO_ID) {
11262 Parcelable state = container.get(mID);
11263 if (state != null) {
11264 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11265 // + ": " + state);
11266 mPrivateFlags &= ~SAVE_STATE_CALLED;
11267 onRestoreInstanceState(state);
11268 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11269 throw new IllegalStateException(
11270 "Derived class did not call super.onRestoreInstanceState()");
11271 }
11272 }
11273 }
11274 }
11275
11276 /**
11277 * Hook allowing a view to re-apply a representation of its internal state that had previously
11278 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11279 * null state.
11280 *
11281 * @param state The frozen state that had previously been returned by
11282 * {@link #onSaveInstanceState}.
11283 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011284 * @see #onSaveInstanceState()
11285 * @see #restoreHierarchyState(android.util.SparseArray)
11286 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011287 */
11288 protected void onRestoreInstanceState(Parcelable state) {
11289 mPrivateFlags |= SAVE_STATE_CALLED;
11290 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011291 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11292 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011293 + "when two views of different type have the same id in the same hierarchy. "
11294 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011295 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011296 }
11297 }
11298
11299 /**
11300 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11301 *
11302 * @return the drawing start time in milliseconds
11303 */
11304 public long getDrawingTime() {
11305 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11306 }
11307
11308 /**
11309 * <p>Enables or disables the duplication of the parent's state into this view. When
11310 * duplication is enabled, this view gets its drawable state from its parent rather
11311 * than from its own internal properties.</p>
11312 *
11313 * <p>Note: in the current implementation, setting this property to true after the
11314 * view was added to a ViewGroup might have no effect at all. This property should
11315 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11316 *
11317 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11318 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011319 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011320 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11321 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011322 *
11323 * @param enabled True to enable duplication of the parent's drawable state, false
11324 * to disable it.
11325 *
11326 * @see #getDrawableState()
11327 * @see #isDuplicateParentStateEnabled()
11328 */
11329 public void setDuplicateParentStateEnabled(boolean enabled) {
11330 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11331 }
11332
11333 /**
11334 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11335 *
11336 * @return True if this view's drawable state is duplicated from the parent,
11337 * false otherwise
11338 *
11339 * @see #getDrawableState()
11340 * @see #setDuplicateParentStateEnabled(boolean)
11341 */
11342 public boolean isDuplicateParentStateEnabled() {
11343 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11344 }
11345
11346 /**
Romain Guy171c5922011-01-06 10:04:23 -080011347 * <p>Specifies the type of layer backing this view. The layer can be
11348 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11349 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011350 *
Romain Guy171c5922011-01-06 10:04:23 -080011351 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11352 * instance that controls how the layer is composed on screen. The following
11353 * properties of the paint are taken into account when composing the layer:</p>
11354 * <ul>
11355 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11356 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11357 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11358 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011359 *
Romain Guy171c5922011-01-06 10:04:23 -080011360 * <p>If this view has an alpha value set to < 1.0 by calling
11361 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11362 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11363 * equivalent to setting a hardware layer on this view and providing a paint with
11364 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011365 *
Romain Guy171c5922011-01-06 10:04:23 -080011366 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11367 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11368 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011369 *
Romain Guy171c5922011-01-06 10:04:23 -080011370 * @param layerType The ype of layer to use with this view, must be one of
11371 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11372 * {@link #LAYER_TYPE_HARDWARE}
11373 * @param paint The paint used to compose the layer. This argument is optional
11374 * and can be null. It is ignored when the layer type is
11375 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011376 *
11377 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011378 * @see #LAYER_TYPE_NONE
11379 * @see #LAYER_TYPE_SOFTWARE
11380 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011381 * @see #setAlpha(float)
11382 *
Romain Guy171c5922011-01-06 10:04:23 -080011383 * @attr ref android.R.styleable#View_layerType
11384 */
11385 public void setLayerType(int layerType, Paint paint) {
11386 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011387 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011388 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11389 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011390
Romain Guyd6cd5722011-01-17 14:42:41 -080011391 if (layerType == mLayerType) {
11392 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11393 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011394 invalidateParentCaches();
11395 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011396 }
11397 return;
11398 }
Romain Guy171c5922011-01-06 10:04:23 -080011399
11400 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011401 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011402 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011403 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011404 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011405 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011406 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011407 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011408 default:
11409 break;
Romain Guy171c5922011-01-06 10:04:23 -080011410 }
11411
11412 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011413 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11414 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11415 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011416
Romain Guy0fd89bf2011-01-26 15:41:30 -080011417 invalidateParentCaches();
11418 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011419 }
11420
11421 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011422 * Indicates whether this view has a static layer. A view with layer type
11423 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11424 * dynamic.
11425 */
11426 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011427 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011428 }
11429
11430 /**
Romain Guy171c5922011-01-06 10:04:23 -080011431 * Indicates what type of layer is currently associated with this view. By default
11432 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11433 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11434 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011435 *
Romain Guy171c5922011-01-06 10:04:23 -080011436 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11437 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011438 *
11439 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011440 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011441 * @see #LAYER_TYPE_NONE
11442 * @see #LAYER_TYPE_SOFTWARE
11443 * @see #LAYER_TYPE_HARDWARE
11444 */
11445 public int getLayerType() {
11446 return mLayerType;
11447 }
Joe Malin32736f02011-01-19 16:14:20 -080011448
Romain Guy6c319ca2011-01-11 14:29:25 -080011449 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011450 * Forces this view's layer to be created and this view to be rendered
11451 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11452 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011453 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011454 * This method can for instance be used to render a view into its layer before
11455 * starting an animation. If this view is complex, rendering into the layer
11456 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011457 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011458 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011459 *
11460 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080011461 */
11462 public void buildLayer() {
11463 if (mLayerType == LAYER_TYPE_NONE) return;
11464
11465 if (mAttachInfo == null) {
11466 throw new IllegalStateException("This view must be attached to a window first");
11467 }
11468
11469 switch (mLayerType) {
11470 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080011471 if (mAttachInfo.mHardwareRenderer != null &&
11472 mAttachInfo.mHardwareRenderer.isEnabled() &&
11473 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011474 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080011475 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011476 break;
11477 case LAYER_TYPE_SOFTWARE:
11478 buildDrawingCache(true);
11479 break;
11480 }
11481 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011482
Romain Guy9c4b79a2011-11-10 19:23:58 -080011483 // Make sure the HardwareRenderer.validate() was invoked before calling this method
11484 void flushLayer() {
11485 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
11486 mHardwareLayer.flush();
11487 }
11488 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011489
11490 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080011491 * <p>Returns a hardware layer that can be used to draw this view again
11492 * without executing its draw method.</p>
11493 *
11494 * @return A HardwareLayer ready to render, or null if an error occurred.
11495 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080011496 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070011497 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
11498 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080011499 return null;
11500 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011501
Romain Guy9c4b79a2011-11-10 19:23:58 -080011502 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080011503
11504 final int width = mRight - mLeft;
11505 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080011506
Romain Guy6c319ca2011-01-11 14:29:25 -080011507 if (width == 0 || height == 0) {
11508 return null;
11509 }
11510
11511 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
11512 if (mHardwareLayer == null) {
11513 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
11514 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070011515 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011516 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
11517 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070011518 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011519 }
11520
Romain Guy5cd5c3f2011-10-17 17:10:02 -070011521 // The layer is not valid if the underlying GPU resources cannot be allocated
11522 if (!mHardwareLayer.isValid()) {
11523 return null;
11524 }
11525
Chet Haasea1cff502012-02-21 13:43:44 -080011526 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080011527 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080011528 }
11529
11530 return mHardwareLayer;
11531 }
Romain Guy171c5922011-01-06 10:04:23 -080011532
Romain Guy589b0bb2011-10-10 13:57:47 -070011533 /**
11534 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070011535 *
Romain Guy589b0bb2011-10-10 13:57:47 -070011536 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070011537 *
11538 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070011539 * @see #LAYER_TYPE_HARDWARE
11540 */
Romain Guya998dff2012-03-23 18:58:36 -070011541 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070011542 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011543 AttachInfo info = mAttachInfo;
11544 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070011545 info.mHardwareRenderer.isEnabled() &&
11546 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011547 mHardwareLayer.destroy();
11548 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080011549
Romain Guy9c4b79a2011-11-10 19:23:58 -080011550 invalidate(true);
11551 invalidateParentCaches();
11552 }
Romain Guy65b345f2011-07-27 18:51:50 -070011553 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070011554 }
Romain Guy65b345f2011-07-27 18:51:50 -070011555 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070011556 }
11557
Romain Guy171c5922011-01-06 10:04:23 -080011558 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080011559 * Destroys all hardware rendering resources. This method is invoked
11560 * when the system needs to reclaim resources. Upon execution of this
11561 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070011562 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011563 * Note: you <strong>must</strong> call
11564 * <code>super.destroyHardwareResources()</code> when overriding
11565 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070011566 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011567 * @hide
11568 */
11569 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070011570 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011571 }
11572
11573 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011574 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
11575 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
11576 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
11577 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
11578 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
11579 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011580 *
Romain Guy171c5922011-01-06 10:04:23 -080011581 * <p>Enabling the drawing cache is similar to
11582 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080011583 * acceleration is turned off. When hardware acceleration is turned on, enabling the
11584 * drawing cache has no effect on rendering because the system uses a different mechanism
11585 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
11586 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
11587 * for information on how to enable software and hardware layers.</p>
11588 *
11589 * <p>This API can be used to manually generate
11590 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
11591 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011592 *
11593 * @param enabled true to enable the drawing cache, false otherwise
11594 *
11595 * @see #isDrawingCacheEnabled()
11596 * @see #getDrawingCache()
11597 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080011598 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011599 */
11600 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011601 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011602 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
11603 }
11604
11605 /**
11606 * <p>Indicates whether the drawing cache is enabled for this view.</p>
11607 *
11608 * @return true if the drawing cache is enabled
11609 *
11610 * @see #setDrawingCacheEnabled(boolean)
11611 * @see #getDrawingCache()
11612 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011613 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011614 public boolean isDrawingCacheEnabled() {
11615 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
11616 }
11617
11618 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080011619 * Debugging utility which recursively outputs the dirty state of a view and its
11620 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080011621 *
Chet Haasedaf98e92011-01-10 14:10:36 -080011622 * @hide
11623 */
Romain Guy676b1732011-02-14 14:45:33 -080011624 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080011625 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
11626 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
11627 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
11628 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
11629 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
11630 if (clear) {
11631 mPrivateFlags &= clearMask;
11632 }
11633 if (this instanceof ViewGroup) {
11634 ViewGroup parent = (ViewGroup) this;
11635 final int count = parent.getChildCount();
11636 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080011637 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080011638 child.outputDirtyFlags(indent + " ", clear, clearMask);
11639 }
11640 }
11641 }
11642
11643 /**
11644 * This method is used by ViewGroup to cause its children to restore or recreate their
11645 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
11646 * to recreate its own display list, which would happen if it went through the normal
11647 * draw/dispatchDraw mechanisms.
11648 *
11649 * @hide
11650 */
11651 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080011652
11653 /**
11654 * A view that is not attached or hardware accelerated cannot create a display list.
11655 * This method checks these conditions and returns the appropriate result.
11656 *
11657 * @return true if view has the ability to create a display list, false otherwise.
11658 *
11659 * @hide
11660 */
11661 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080011662 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080011663 }
Joe Malin32736f02011-01-19 16:14:20 -080011664
Chet Haasedaf98e92011-01-10 14:10:36 -080011665 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080011666 * @return The HardwareRenderer associated with that view or null if hardware rendering
11667 * is not supported or this this has not been attached to a window.
11668 *
11669 * @hide
11670 */
11671 public HardwareRenderer getHardwareRenderer() {
11672 if (mAttachInfo != null) {
11673 return mAttachInfo.mHardwareRenderer;
11674 }
11675 return null;
11676 }
11677
11678 /**
Chet Haasea1cff502012-02-21 13:43:44 -080011679 * Returns a DisplayList. If the incoming displayList is null, one will be created.
11680 * Otherwise, the same display list will be returned (after having been rendered into
11681 * along the way, depending on the invalidation state of the view).
11682 *
11683 * @param displayList The previous version of this displayList, could be null.
11684 * @param isLayer Whether the requester of the display list is a layer. If so,
11685 * the view will avoid creating a layer inside the resulting display list.
11686 * @return A new or reused DisplayList object.
11687 */
11688 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
11689 if (!canHaveDisplayList()) {
11690 return null;
11691 }
11692
11693 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
11694 displayList == null || !displayList.isValid() ||
11695 (!isLayer && mRecreateDisplayList))) {
11696 // Don't need to recreate the display list, just need to tell our
11697 // children to restore/recreate theirs
11698 if (displayList != null && displayList.isValid() &&
11699 !isLayer && !mRecreateDisplayList) {
11700 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11701 mPrivateFlags &= ~DIRTY_MASK;
11702 dispatchGetDisplayList();
11703
11704 return displayList;
11705 }
11706
11707 if (!isLayer) {
11708 // If we got here, we're recreating it. Mark it as such to ensure that
11709 // we copy in child display lists into ours in drawChild()
11710 mRecreateDisplayList = true;
11711 }
11712 if (displayList == null) {
11713 final String name = getClass().getSimpleName();
11714 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
11715 // If we're creating a new display list, make sure our parent gets invalidated
11716 // since they will need to recreate their display list to account for this
11717 // new child display list.
11718 invalidateParentCaches();
11719 }
11720
11721 boolean caching = false;
11722 final HardwareCanvas canvas = displayList.start();
11723 int restoreCount = 0;
11724 int width = mRight - mLeft;
11725 int height = mBottom - mTop;
11726
11727 try {
11728 canvas.setViewport(width, height);
11729 // The dirty rect should always be null for a display list
11730 canvas.onPreDraw(null);
11731 int layerType = (
11732 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
11733 getLayerType() : LAYER_TYPE_NONE;
Chet Haaseb85967b2012-03-26 14:37:51 -070011734 if (!isLayer && layerType != LAYER_TYPE_NONE && USE_DISPLAY_LIST_PROPERTIES) {
11735 if (layerType == LAYER_TYPE_HARDWARE) {
11736 final HardwareLayer layer = getHardwareLayer();
11737 if (layer != null && layer.isValid()) {
11738 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
11739 } else {
11740 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
11741 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
11742 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11743 }
11744 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080011745 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070011746 buildDrawingCache(true);
11747 Bitmap cache = getDrawingCache(true);
11748 if (cache != null) {
11749 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
11750 caching = true;
11751 }
Chet Haasea1cff502012-02-21 13:43:44 -080011752 }
Chet Haasea1cff502012-02-21 13:43:44 -080011753 } else {
11754
11755 computeScroll();
11756
11757 if (!USE_DISPLAY_LIST_PROPERTIES) {
11758 restoreCount = canvas.save();
11759 }
11760 canvas.translate(-mScrollX, -mScrollY);
11761 if (!isLayer) {
11762 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11763 mPrivateFlags &= ~DIRTY_MASK;
11764 }
11765
11766 // Fast path for layouts with no backgrounds
11767 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11768 dispatchDraw(canvas);
11769 } else {
11770 draw(canvas);
11771 }
11772 }
11773 } finally {
11774 if (USE_DISPLAY_LIST_PROPERTIES) {
11775 canvas.restoreToCount(restoreCount);
11776 }
11777 canvas.onPostDraw();
11778
11779 displayList.end();
11780 if (USE_DISPLAY_LIST_PROPERTIES) {
11781 displayList.setCaching(caching);
11782 }
11783 if (isLayer && USE_DISPLAY_LIST_PROPERTIES) {
11784 displayList.setLeftTopRightBottom(0, 0, width, height);
11785 } else {
11786 setDisplayListProperties(displayList);
11787 }
11788 }
11789 } else if (!isLayer) {
11790 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11791 mPrivateFlags &= ~DIRTY_MASK;
11792 }
11793
11794 return displayList;
11795 }
11796
11797 /**
11798 * Get the DisplayList for the HardwareLayer
11799 *
11800 * @param layer The HardwareLayer whose DisplayList we want
11801 * @return A DisplayList fopr the specified HardwareLayer
11802 */
11803 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
11804 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
11805 layer.setDisplayList(displayList);
11806 return displayList;
11807 }
11808
11809
11810 /**
Romain Guyb051e892010-09-28 19:09:36 -070011811 * <p>Returns a display list that can be used to draw this view again
11812 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011813 *
Romain Guyb051e892010-09-28 19:09:36 -070011814 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080011815 *
11816 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070011817 */
Chet Haasedaf98e92011-01-10 14:10:36 -080011818 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080011819 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070011820 return mDisplayList;
11821 }
11822
11823 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011824 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011825 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011826 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011827 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011828 * @see #getDrawingCache(boolean)
11829 */
11830 public Bitmap getDrawingCache() {
11831 return getDrawingCache(false);
11832 }
11833
11834 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011835 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
11836 * is null when caching is disabled. If caching is enabled and the cache is not ready,
11837 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
11838 * draw from the cache when the cache is enabled. To benefit from the cache, you must
11839 * request the drawing cache by calling this method and draw it on screen if the
11840 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011841 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011842 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11843 * this method will create a bitmap of the same size as this view. Because this bitmap
11844 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11845 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11846 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11847 * size than the view. This implies that your application must be able to handle this
11848 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011849 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011850 * @param autoScale Indicates whether the generated bitmap should be scaled based on
11851 * the current density of the screen when the application is in compatibility
11852 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011853 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011854 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011855 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011856 * @see #setDrawingCacheEnabled(boolean)
11857 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070011858 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011859 * @see #destroyDrawingCache()
11860 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011861 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011862 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
11863 return null;
11864 }
11865 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011866 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011867 }
Romain Guy02890fd2010-08-06 17:58:44 -070011868 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011869 }
11870
11871 /**
11872 * <p>Frees the resources used by the drawing cache. If you call
11873 * {@link #buildDrawingCache()} manually without calling
11874 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11875 * should cleanup the cache with this method afterwards.</p>
11876 *
11877 * @see #setDrawingCacheEnabled(boolean)
11878 * @see #buildDrawingCache()
11879 * @see #getDrawingCache()
11880 */
11881 public void destroyDrawingCache() {
11882 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011883 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011884 mDrawingCache = null;
11885 }
Romain Guyfbd8f692009-06-26 14:51:58 -070011886 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011887 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070011888 mUnscaledDrawingCache = null;
11889 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011890 }
11891
11892 /**
11893 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070011894 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011895 * view will always be drawn on top of a solid color.
11896 *
11897 * @param color The background color to use for the drawing cache's bitmap
11898 *
11899 * @see #setDrawingCacheEnabled(boolean)
11900 * @see #buildDrawingCache()
11901 * @see #getDrawingCache()
11902 */
11903 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080011904 if (color != mDrawingCacheBackgroundColor) {
11905 mDrawingCacheBackgroundColor = color;
11906 mPrivateFlags &= ~DRAWING_CACHE_VALID;
11907 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011908 }
11909
11910 /**
11911 * @see #setDrawingCacheBackgroundColor(int)
11912 *
11913 * @return The background color to used for the drawing cache's bitmap
11914 */
11915 public int getDrawingCacheBackgroundColor() {
11916 return mDrawingCacheBackgroundColor;
11917 }
11918
11919 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011920 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011921 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011922 * @see #buildDrawingCache(boolean)
11923 */
11924 public void buildDrawingCache() {
11925 buildDrawingCache(false);
11926 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080011927
Romain Guyfbd8f692009-06-26 14:51:58 -070011928 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011929 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
11930 *
11931 * <p>If you call {@link #buildDrawingCache()} manually without calling
11932 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11933 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011934 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011935 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11936 * this method will create a bitmap of the same size as this view. Because this bitmap
11937 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11938 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11939 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11940 * size than the view. This implies that your application must be able to handle this
11941 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011942 *
Romain Guy0d9275e2010-10-26 14:22:30 -070011943 * <p>You should avoid calling this method when hardware acceleration is enabled. If
11944 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080011945 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070011946 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011947 *
11948 * @see #getDrawingCache()
11949 * @see #destroyDrawingCache()
11950 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011951 public void buildDrawingCache(boolean autoScale) {
11952 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070011953 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011954 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011955
11956 if (ViewDebug.TRACE_HIERARCHY) {
11957 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
11958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011959
Romain Guy8506ab42009-06-11 17:35:47 -070011960 int width = mRight - mLeft;
11961 int height = mBottom - mTop;
11962
11963 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070011964 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070011965
Romain Guye1123222009-06-29 14:24:56 -070011966 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011967 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
11968 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070011969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011970
11971 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070011972 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080011973 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011974
11975 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070011976 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080011977 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011978 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
11979 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080011980 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011981 return;
11982 }
11983
11984 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070011985 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011986
11987 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011988 Bitmap.Config quality;
11989 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080011990 // Never pick ARGB_4444 because it looks awful
11991 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011992 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
11993 case DRAWING_CACHE_QUALITY_AUTO:
11994 quality = Bitmap.Config.ARGB_8888;
11995 break;
11996 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080011997 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011998 break;
11999 case DRAWING_CACHE_QUALITY_HIGH:
12000 quality = Bitmap.Config.ARGB_8888;
12001 break;
12002 default:
12003 quality = Bitmap.Config.ARGB_8888;
12004 break;
12005 }
12006 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012007 // Optimization for translucent windows
12008 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012009 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012010 }
12011
12012 // Try to cleanup memory
12013 if (bitmap != null) bitmap.recycle();
12014
12015 try {
12016 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012017 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012018 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012019 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012020 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012021 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012022 }
Adam Powell26153a32010-11-08 15:22:27 -080012023 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012024 } catch (OutOfMemoryError e) {
12025 // If there is not enough memory to create the bitmap cache, just
12026 // ignore the issue as bitmap caches are not required to draw the
12027 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012028 if (autoScale) {
12029 mDrawingCache = null;
12030 } else {
12031 mUnscaledDrawingCache = null;
12032 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012033 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012034 return;
12035 }
12036
12037 clear = drawingCacheBackgroundColor != 0;
12038 }
12039
12040 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012041 if (attachInfo != null) {
12042 canvas = attachInfo.mCanvas;
12043 if (canvas == null) {
12044 canvas = new Canvas();
12045 }
12046 canvas.setBitmap(bitmap);
12047 // Temporarily clobber the cached Canvas in case one of our children
12048 // is also using a drawing cache. Without this, the children would
12049 // steal the canvas by attaching their own bitmap to it and bad, bad
12050 // thing would happen (invisible views, corrupted drawings, etc.)
12051 attachInfo.mCanvas = null;
12052 } else {
12053 // This case should hopefully never or seldom happen
12054 canvas = new Canvas(bitmap);
12055 }
12056
12057 if (clear) {
12058 bitmap.eraseColor(drawingCacheBackgroundColor);
12059 }
12060
12061 computeScroll();
12062 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012063
Romain Guye1123222009-06-29 14:24:56 -070012064 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012065 final float scale = attachInfo.mApplicationScale;
12066 canvas.scale(scale, scale);
12067 }
Joe Malin32736f02011-01-19 16:14:20 -080012068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012069 canvas.translate(-mScrollX, -mScrollY);
12070
Romain Guy5bcdff42009-05-14 21:27:18 -070012071 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012072 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12073 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012074 mPrivateFlags |= DRAWING_CACHE_VALID;
12075 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012076
12077 // Fast path for layouts with no backgrounds
12078 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12079 if (ViewDebug.TRACE_HIERARCHY) {
12080 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12081 }
Romain Guy5bcdff42009-05-14 21:27:18 -070012082 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012083 dispatchDraw(canvas);
12084 } else {
12085 draw(canvas);
12086 }
12087
12088 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012089 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012090
12091 if (attachInfo != null) {
12092 // Restore the cached Canvas for our siblings
12093 attachInfo.mCanvas = canvas;
12094 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012095 }
12096 }
12097
12098 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012099 * Create a snapshot of the view into a bitmap. We should probably make
12100 * some form of this public, but should think about the API.
12101 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012102 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012103 int width = mRight - mLeft;
12104 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012105
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012106 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012107 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012108 width = (int) ((width * scale) + 0.5f);
12109 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012110
Romain Guy8c11e312009-09-14 15:15:30 -070012111 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012112 if (bitmap == null) {
12113 throw new OutOfMemoryError();
12114 }
12115
Romain Guyc529d8d2011-09-06 15:01:39 -070012116 Resources resources = getResources();
12117 if (resources != null) {
12118 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12119 }
Joe Malin32736f02011-01-19 16:14:20 -080012120
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012121 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012122 if (attachInfo != null) {
12123 canvas = attachInfo.mCanvas;
12124 if (canvas == null) {
12125 canvas = new Canvas();
12126 }
12127 canvas.setBitmap(bitmap);
12128 // Temporarily clobber the cached Canvas in case one of our children
12129 // is also using a drawing cache. Without this, the children would
12130 // steal the canvas by attaching their own bitmap to it and bad, bad
12131 // things would happen (invisible views, corrupted drawings, etc.)
12132 attachInfo.mCanvas = null;
12133 } else {
12134 // This case should hopefully never or seldom happen
12135 canvas = new Canvas(bitmap);
12136 }
12137
Romain Guy5bcdff42009-05-14 21:27:18 -070012138 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012139 bitmap.eraseColor(backgroundColor);
12140 }
12141
12142 computeScroll();
12143 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012144 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012145 canvas.translate(-mScrollX, -mScrollY);
12146
Romain Guy5bcdff42009-05-14 21:27:18 -070012147 // Temporarily remove the dirty mask
12148 int flags = mPrivateFlags;
12149 mPrivateFlags &= ~DIRTY_MASK;
12150
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012151 // Fast path for layouts with no backgrounds
12152 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12153 dispatchDraw(canvas);
12154 } else {
12155 draw(canvas);
12156 }
12157
Romain Guy5bcdff42009-05-14 21:27:18 -070012158 mPrivateFlags = flags;
12159
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012160 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012161 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012162
12163 if (attachInfo != null) {
12164 // Restore the cached Canvas for our siblings
12165 attachInfo.mCanvas = canvas;
12166 }
Romain Guy8506ab42009-06-11 17:35:47 -070012167
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012168 return bitmap;
12169 }
12170
12171 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012172 * Indicates whether this View is currently in edit mode. A View is usually
12173 * in edit mode when displayed within a developer tool. For instance, if
12174 * this View is being drawn by a visual user interface builder, this method
12175 * should return true.
12176 *
12177 * Subclasses should check the return value of this method to provide
12178 * different behaviors if their normal behavior might interfere with the
12179 * host environment. For instance: the class spawns a thread in its
12180 * constructor, the drawing code relies on device-specific features, etc.
12181 *
12182 * This method is usually checked in the drawing code of custom widgets.
12183 *
12184 * @return True if this View is in edit mode, false otherwise.
12185 */
12186 public boolean isInEditMode() {
12187 return false;
12188 }
12189
12190 /**
12191 * If the View draws content inside its padding and enables fading edges,
12192 * it needs to support padding offsets. Padding offsets are added to the
12193 * fading edges to extend the length of the fade so that it covers pixels
12194 * drawn inside the padding.
12195 *
12196 * Subclasses of this class should override this method if they need
12197 * to draw content inside the padding.
12198 *
12199 * @return True if padding offset must be applied, false otherwise.
12200 *
12201 * @see #getLeftPaddingOffset()
12202 * @see #getRightPaddingOffset()
12203 * @see #getTopPaddingOffset()
12204 * @see #getBottomPaddingOffset()
12205 *
12206 * @since CURRENT
12207 */
12208 protected boolean isPaddingOffsetRequired() {
12209 return false;
12210 }
12211
12212 /**
12213 * Amount by which to extend the left fading region. Called only when
12214 * {@link #isPaddingOffsetRequired()} returns true.
12215 *
12216 * @return The left padding offset in pixels.
12217 *
12218 * @see #isPaddingOffsetRequired()
12219 *
12220 * @since CURRENT
12221 */
12222 protected int getLeftPaddingOffset() {
12223 return 0;
12224 }
12225
12226 /**
12227 * Amount by which to extend the right fading region. Called only when
12228 * {@link #isPaddingOffsetRequired()} returns true.
12229 *
12230 * @return The right padding offset in pixels.
12231 *
12232 * @see #isPaddingOffsetRequired()
12233 *
12234 * @since CURRENT
12235 */
12236 protected int getRightPaddingOffset() {
12237 return 0;
12238 }
12239
12240 /**
12241 * Amount by which to extend the top fading region. Called only when
12242 * {@link #isPaddingOffsetRequired()} returns true.
12243 *
12244 * @return The top padding offset in pixels.
12245 *
12246 * @see #isPaddingOffsetRequired()
12247 *
12248 * @since CURRENT
12249 */
12250 protected int getTopPaddingOffset() {
12251 return 0;
12252 }
12253
12254 /**
12255 * Amount by which to extend the bottom fading region. Called only when
12256 * {@link #isPaddingOffsetRequired()} returns true.
12257 *
12258 * @return The bottom padding offset in pixels.
12259 *
12260 * @see #isPaddingOffsetRequired()
12261 *
12262 * @since CURRENT
12263 */
12264 protected int getBottomPaddingOffset() {
12265 return 0;
12266 }
12267
12268 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012269 * @hide
12270 * @param offsetRequired
12271 */
12272 protected int getFadeTop(boolean offsetRequired) {
12273 int top = mPaddingTop;
12274 if (offsetRequired) top += getTopPaddingOffset();
12275 return top;
12276 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012277
Romain Guyf2fc4602011-07-19 15:20:03 -070012278 /**
12279 * @hide
12280 * @param offsetRequired
12281 */
12282 protected int getFadeHeight(boolean offsetRequired) {
12283 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012284 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012285 return mBottom - mTop - mPaddingBottom - padding;
12286 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012287
Romain Guyf2fc4602011-07-19 15:20:03 -070012288 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012289 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012290 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012291 *
Romain Guy2bffd262010-09-12 17:40:02 -070012292 * <p>Even if this method returns true, it does not mean that every call
12293 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12294 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012295 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012296 * window is hardware accelerated,
12297 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12298 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012299 *
Romain Guy2bffd262010-09-12 17:40:02 -070012300 * @return True if the view is attached to a window and the window is
12301 * hardware accelerated; false in any other case.
12302 */
12303 public boolean isHardwareAccelerated() {
12304 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12305 }
Joe Malin32736f02011-01-19 16:14:20 -080012306
Romain Guy2bffd262010-09-12 17:40:02 -070012307 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012308 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12309 * case of an active Animation being run on the view.
12310 */
12311 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12312 Animation a, boolean scalingRequired) {
12313 Transformation invalidationTransform;
12314 final int flags = parent.mGroupFlags;
12315 final boolean initialized = a.isInitialized();
12316 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012317 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012318 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
12319 onAnimationStart();
12320 }
12321
12322 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12323 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12324 if (parent.mInvalidationTransformation == null) {
12325 parent.mInvalidationTransformation = new Transformation();
12326 }
12327 invalidationTransform = parent.mInvalidationTransformation;
12328 a.getTransformation(drawingTime, invalidationTransform, 1f);
12329 } else {
12330 invalidationTransform = parent.mChildTransformation;
12331 }
12332 if (more) {
12333 if (!a.willChangeBounds()) {
12334 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12335 parent.FLAG_OPTIMIZE_INVALIDATE) {
12336 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12337 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12338 // The child need to draw an animation, potentially offscreen, so
12339 // make sure we do not cancel invalidate requests
12340 parent.mPrivateFlags |= DRAW_ANIMATION;
12341 parent.invalidate(mLeft, mTop, mRight, mBottom);
12342 }
12343 } else {
12344 if (parent.mInvalidateRegion == null) {
12345 parent.mInvalidateRegion = new RectF();
12346 }
12347 final RectF region = parent.mInvalidateRegion;
12348 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12349 invalidationTransform);
12350
12351 // The child need to draw an animation, potentially offscreen, so
12352 // make sure we do not cancel invalidate requests
12353 parent.mPrivateFlags |= DRAW_ANIMATION;
12354
12355 final int left = mLeft + (int) region.left;
12356 final int top = mTop + (int) region.top;
12357 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12358 top + (int) (region.height() + .5f));
12359 }
12360 }
12361 return more;
12362 }
12363
Chet Haasea1cff502012-02-21 13:43:44 -080012364 void setDisplayListProperties() {
12365 setDisplayListProperties(mDisplayList);
12366 }
12367
12368 /**
12369 * This method is called by getDisplayList() when a display list is created or re-rendered.
12370 * It sets or resets the current value of all properties on that display list (resetting is
12371 * necessary when a display list is being re-created, because we need to make sure that
12372 * previously-set transform values
12373 */
12374 void setDisplayListProperties(DisplayList displayList) {
12375 if (USE_DISPLAY_LIST_PROPERTIES && displayList != null) {
12376 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012377 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012378 if (mParent instanceof ViewGroup) {
12379 displayList.setClipChildren(
12380 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12381 }
Chet Haase9420abd2012-03-29 16:28:32 -070012382 float alpha = 1;
12383 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12384 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12385 ViewGroup parentVG = (ViewGroup) mParent;
12386 final boolean hasTransform =
12387 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12388 if (hasTransform) {
12389 Transformation transform = parentVG.mChildTransformation;
12390 final int transformType = parentVG.mChildTransformation.getTransformationType();
12391 if (transformType != Transformation.TYPE_IDENTITY) {
12392 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12393 alpha = transform.getAlpha();
12394 }
12395 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12396 displayList.setStaticMatrix(transform.getMatrix());
12397 }
12398 }
12399 }
Chet Haasea1cff502012-02-21 13:43:44 -080012400 }
12401 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012402 alpha *= mTransformationInfo.mAlpha;
12403 if (alpha < 1) {
12404 final int multipliedAlpha = (int) (255 * alpha);
12405 if (onSetAlpha(multipliedAlpha)) {
12406 alpha = 1;
12407 }
12408 }
12409 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012410 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12411 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12412 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12413 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012414 if (mTransformationInfo.mCamera == null) {
12415 mTransformationInfo.mCamera = new Camera();
12416 mTransformationInfo.matrix3D = new Matrix();
12417 }
12418 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012419 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12420 displayList.setPivotX(getPivotX());
12421 displayList.setPivotY(getPivotY());
12422 }
Chet Haase9420abd2012-03-29 16:28:32 -070012423 } else if (alpha < 1) {
12424 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012425 }
12426 }
12427 }
12428
Chet Haasebcca79a2012-02-14 08:45:14 -080012429 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012430 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12431 * This draw() method is an implementation detail and is not intended to be overridden or
12432 * to be called from anywhere else other than ViewGroup.drawChild().
12433 */
12434 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haasea1cff502012-02-21 13:43:44 -080012435 boolean useDisplayListProperties = USE_DISPLAY_LIST_PROPERTIES && mAttachInfo != null &&
12436 mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012437 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012438 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012439 final int flags = parent.mGroupFlags;
12440
Chet Haasea1cff502012-02-21 13:43:44 -080012441 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012442 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012443 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012444 }
12445
12446 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012447 boolean concatMatrix = false;
12448
12449 boolean scalingRequired = false;
12450 boolean caching;
12451 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
12452
12453 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012454 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12455 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012456 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012457 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012458 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12459 } else {
12460 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12461 }
12462
Chet Haasebcca79a2012-02-14 08:45:14 -080012463 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080012464 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012465 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080012466 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080012467 transformToApply = parent.mChildTransformation;
Chet Haase9420abd2012-03-29 16:28:32 -070012468 } else if (!useDisplayListProperties &&
12469 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012470 final boolean hasTransform =
12471 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080012472 if (hasTransform) {
12473 final int transformType = parent.mChildTransformation.getTransformationType();
12474 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
12475 parent.mChildTransformation : null;
12476 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
12477 }
12478 }
12479
12480 concatMatrix |= !childHasIdentityMatrix;
12481
12482 // Sets the flag as early as possible to allow draw() implementations
12483 // to call invalidate() successfully when doing animations
12484 mPrivateFlags |= DRAWN;
12485
Chet Haasebcca79a2012-02-14 08:45:14 -080012486 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080012487 (mPrivateFlags & DRAW_ANIMATION) == 0) {
12488 return more;
12489 }
12490
12491 if (hardwareAccelerated) {
12492 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
12493 // retain the flag's value temporarily in the mRecreateDisplayList flag
12494 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
12495 mPrivateFlags &= ~INVALIDATED;
12496 }
12497
12498 computeScroll();
12499
12500 final int sx = mScrollX;
12501 final int sy = mScrollY;
12502
12503 DisplayList displayList = null;
12504 Bitmap cache = null;
12505 boolean hasDisplayList = false;
12506 if (caching) {
12507 if (!hardwareAccelerated) {
12508 if (layerType != LAYER_TYPE_NONE) {
12509 layerType = LAYER_TYPE_SOFTWARE;
12510 buildDrawingCache(true);
12511 }
12512 cache = getDrawingCache(true);
12513 } else {
12514 switch (layerType) {
12515 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070012516 if (useDisplayListProperties) {
12517 hasDisplayList = canHaveDisplayList();
12518 } else {
12519 buildDrawingCache(true);
12520 cache = getDrawingCache(true);
12521 }
Chet Haase64a48c12012-02-13 16:33:29 -080012522 break;
Chet Haasea1cff502012-02-21 13:43:44 -080012523 case LAYER_TYPE_HARDWARE:
12524 if (useDisplayListProperties) {
12525 hasDisplayList = canHaveDisplayList();
12526 }
12527 break;
Chet Haase64a48c12012-02-13 16:33:29 -080012528 case LAYER_TYPE_NONE:
12529 // Delay getting the display list until animation-driven alpha values are
12530 // set up and possibly passed on to the view
12531 hasDisplayList = canHaveDisplayList();
12532 break;
12533 }
12534 }
12535 }
Chet Haasea1cff502012-02-21 13:43:44 -080012536 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070012537 if (useDisplayListProperties) {
12538 displayList = getDisplayList();
12539 if (!displayList.isValid()) {
12540 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12541 // to getDisplayList(), the display list will be marked invalid and we should not
12542 // try to use it again.
12543 displayList = null;
12544 hasDisplayList = false;
12545 useDisplayListProperties = false;
12546 }
12547 }
Chet Haase64a48c12012-02-13 16:33:29 -080012548
12549 final boolean hasNoCache = cache == null || hasDisplayList;
12550 final boolean offsetForScroll = cache == null && !hasDisplayList &&
12551 layerType != LAYER_TYPE_HARDWARE;
12552
Chet Haasea1cff502012-02-21 13:43:44 -080012553 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070012554 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012555 restoreTo = canvas.save();
12556 }
Chet Haase64a48c12012-02-13 16:33:29 -080012557 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012558 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080012559 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080012560 if (!useDisplayListProperties) {
12561 canvas.translate(mLeft, mTop);
12562 }
Chet Haase64a48c12012-02-13 16:33:29 -080012563 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080012564 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070012565 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080012566 restoreTo = canvas.save();
12567 }
Chet Haase64a48c12012-02-13 16:33:29 -080012568 // mAttachInfo cannot be null, otherwise scalingRequired == false
12569 final float scale = 1.0f / mAttachInfo.mApplicationScale;
12570 canvas.scale(scale, scale);
12571 }
12572 }
12573
Chet Haasea1cff502012-02-21 13:43:44 -080012574 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012575 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080012576 if (transformToApply != null || !childHasIdentityMatrix) {
12577 int transX = 0;
12578 int transY = 0;
12579
12580 if (offsetForScroll) {
12581 transX = -sx;
12582 transY = -sy;
12583 }
12584
12585 if (transformToApply != null) {
12586 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070012587 if (useDisplayListProperties) {
12588 displayList.setAnimationMatrix(transformToApply.getMatrix());
12589 } else {
12590 // Undo the scroll translation, apply the transformation matrix,
12591 // then redo the scroll translate to get the correct result.
12592 canvas.translate(-transX, -transY);
12593 canvas.concat(transformToApply.getMatrix());
12594 canvas.translate(transX, transY);
12595 }
Chet Haasea1cff502012-02-21 13:43:44 -080012596 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012597 }
12598
12599 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012600 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012601 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080012602 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012603 }
12604 }
12605
Chet Haasea1cff502012-02-21 13:43:44 -080012606 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012607 canvas.translate(-transX, -transY);
12608 canvas.concat(getMatrix());
12609 canvas.translate(transX, transY);
12610 }
12611 }
12612
Chet Haase9420abd2012-03-29 16:28:32 -070012613 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080012614 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012615 if (hasNoCache) {
12616 final int multipliedAlpha = (int) (255 * alpha);
12617 if (!onSetAlpha(multipliedAlpha)) {
12618 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080012619 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080012620 layerType != LAYER_TYPE_NONE) {
12621 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
12622 }
Chet Haase9420abd2012-03-29 16:28:32 -070012623 if (useDisplayListProperties) {
12624 displayList.setAlpha(alpha * getAlpha());
12625 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070012626 final int scrollX = hasDisplayList ? 0 : sx;
12627 final int scrollY = hasDisplayList ? 0 : sy;
12628 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
12629 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080012630 }
12631 } else {
12632 // Alpha is handled by the child directly, clobber the layer's alpha
12633 mPrivateFlags |= ALPHA_SET;
12634 }
12635 }
12636 }
12637 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12638 onSetAlpha(255);
12639 mPrivateFlags &= ~ALPHA_SET;
12640 }
12641
Chet Haasea1cff502012-02-21 13:43:44 -080012642 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
12643 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012644 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012645 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080012646 } else {
12647 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012648 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080012649 } else {
12650 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
12651 }
12652 }
12653 }
12654
Chet Haase9420abd2012-03-29 16:28:32 -070012655 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080012656 displayList = getDisplayList();
12657 if (!displayList.isValid()) {
12658 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12659 // to getDisplayList(), the display list will be marked invalid and we should not
12660 // try to use it again.
12661 displayList = null;
12662 hasDisplayList = false;
12663 }
12664 }
12665
12666 if (hasNoCache) {
12667 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080012668 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012669 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080012670 if (layer != null && layer.isValid()) {
12671 mLayerPaint.setAlpha((int) (alpha * 255));
12672 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
12673 layerRendered = true;
12674 } else {
12675 final int scrollX = hasDisplayList ? 0 : sx;
12676 final int scrollY = hasDisplayList ? 0 : sy;
12677 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080012678 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080012679 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12680 }
12681 }
12682
12683 if (!layerRendered) {
12684 if (!hasDisplayList) {
12685 // Fast path for layouts with no backgrounds
12686 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12687 if (ViewDebug.TRACE_HIERARCHY) {
12688 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
12689 }
12690 mPrivateFlags &= ~DIRTY_MASK;
12691 dispatchDraw(canvas);
12692 } else {
12693 draw(canvas);
12694 }
12695 } else {
12696 mPrivateFlags &= ~DIRTY_MASK;
Chet Haasebcca79a2012-02-14 08:45:14 -080012697 ((HardwareCanvas) canvas).drawDisplayList(displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -080012698 mRight - mLeft, mBottom - mTop, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080012699 }
12700 }
12701 } else if (cache != null) {
12702 mPrivateFlags &= ~DIRTY_MASK;
12703 Paint cachePaint;
12704
12705 if (layerType == LAYER_TYPE_NONE) {
12706 cachePaint = parent.mCachePaint;
12707 if (cachePaint == null) {
12708 cachePaint = new Paint();
12709 cachePaint.setDither(false);
12710 parent.mCachePaint = cachePaint;
12711 }
Chet Haase9420abd2012-03-29 16:28:32 -070012712 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012713 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080012714 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
12715 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012716 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080012717 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080012718 }
12719 } else {
12720 cachePaint = mLayerPaint;
12721 cachePaint.setAlpha((int) (alpha * 255));
12722 }
12723 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
12724 }
12725
Chet Haasea1cff502012-02-21 13:43:44 -080012726 if (restoreTo >= 0) {
12727 canvas.restoreToCount(restoreTo);
12728 }
Chet Haase64a48c12012-02-13 16:33:29 -080012729
12730 if (a != null && !more) {
12731 if (!hardwareAccelerated && !a.getFillAfter()) {
12732 onSetAlpha(255);
12733 }
12734 parent.finishAnimatingView(this, a);
12735 }
12736
12737 if (more && hardwareAccelerated) {
12738 // invalidation is the trigger to recreate display lists, so if we're using
12739 // display lists to render, force an invalidate to allow the animation to
12740 // continue drawing another frame
12741 parent.invalidate(true);
12742 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12743 // alpha animations should cause the child to recreate its display list
12744 invalidate(true);
12745 }
12746 }
12747
12748 mRecreateDisplayList = false;
12749
12750 return more;
12751 }
12752
12753 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012754 * Manually render this view (and all of its children) to the given Canvas.
12755 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070012756 * called. When implementing a view, implement
12757 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
12758 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012759 *
12760 * @param canvas The Canvas to which the View is rendered.
12761 */
12762 public void draw(Canvas canvas) {
12763 if (ViewDebug.TRACE_HIERARCHY) {
12764 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12765 }
12766
Romain Guy5bcdff42009-05-14 21:27:18 -070012767 final int privateFlags = mPrivateFlags;
12768 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
12769 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
12770 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070012771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012772 /*
12773 * Draw traversal performs several drawing steps which must be executed
12774 * in the appropriate order:
12775 *
12776 * 1. Draw the background
12777 * 2. If necessary, save the canvas' layers to prepare for fading
12778 * 3. Draw view's content
12779 * 4. Draw children
12780 * 5. If necessary, draw the fading edges and restore layers
12781 * 6. Draw decorations (scrollbars for instance)
12782 */
12783
12784 // Step 1, draw the background, if needed
12785 int saveCount;
12786
Romain Guy24443ea2009-05-11 11:56:30 -070012787 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012788 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070012789 if (background != null) {
12790 final int scrollX = mScrollX;
12791 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012792
Romain Guy24443ea2009-05-11 11:56:30 -070012793 if (mBackgroundSizeChanged) {
12794 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
12795 mBackgroundSizeChanged = false;
12796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012797
Romain Guy24443ea2009-05-11 11:56:30 -070012798 if ((scrollX | scrollY) == 0) {
12799 background.draw(canvas);
12800 } else {
12801 canvas.translate(scrollX, scrollY);
12802 background.draw(canvas);
12803 canvas.translate(-scrollX, -scrollY);
12804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012805 }
12806 }
12807
12808 // skip step 2 & 5 if possible (common case)
12809 final int viewFlags = mViewFlags;
12810 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
12811 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
12812 if (!verticalEdges && !horizontalEdges) {
12813 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012814 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012815
12816 // Step 4, draw the children
12817 dispatchDraw(canvas);
12818
12819 // Step 6, draw decorations (scrollbars)
12820 onDrawScrollBars(canvas);
12821
12822 // we're done...
12823 return;
12824 }
12825
12826 /*
12827 * Here we do the full fledged routine...
12828 * (this is an uncommon case where speed matters less,
12829 * this is why we repeat some of the tests that have been
12830 * done above)
12831 */
12832
12833 boolean drawTop = false;
12834 boolean drawBottom = false;
12835 boolean drawLeft = false;
12836 boolean drawRight = false;
12837
12838 float topFadeStrength = 0.0f;
12839 float bottomFadeStrength = 0.0f;
12840 float leftFadeStrength = 0.0f;
12841 float rightFadeStrength = 0.0f;
12842
12843 // Step 2, save the canvas' layers
12844 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012845
12846 final boolean offsetRequired = isPaddingOffsetRequired();
12847 if (offsetRequired) {
12848 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012849 }
12850
12851 int left = mScrollX + paddingLeft;
12852 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070012853 int top = mScrollY + getFadeTop(offsetRequired);
12854 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012855
12856 if (offsetRequired) {
12857 right += getRightPaddingOffset();
12858 bottom += getBottomPaddingOffset();
12859 }
12860
12861 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070012862 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012863 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012864
12865 // clip the fade length if top and bottom fades overlap
12866 // overlapping fades produce odd-looking artifacts
12867 if (verticalEdges && (top + length > bottom - length)) {
12868 length = (bottom - top) / 2;
12869 }
12870
12871 // also clip horizontal fades if necessary
12872 if (horizontalEdges && (left + length > right - length)) {
12873 length = (right - left) / 2;
12874 }
12875
12876 if (verticalEdges) {
12877 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012878 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012879 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012880 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012881 }
12882
12883 if (horizontalEdges) {
12884 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012885 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012886 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012887 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012888 }
12889
12890 saveCount = canvas.getSaveCount();
12891
12892 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070012893 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012894 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
12895
12896 if (drawTop) {
12897 canvas.saveLayer(left, top, right, top + length, null, flags);
12898 }
12899
12900 if (drawBottom) {
12901 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
12902 }
12903
12904 if (drawLeft) {
12905 canvas.saveLayer(left, top, left + length, bottom, null, flags);
12906 }
12907
12908 if (drawRight) {
12909 canvas.saveLayer(right - length, top, right, bottom, null, flags);
12910 }
12911 } else {
12912 scrollabilityCache.setFadeColor(solidColor);
12913 }
12914
12915 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012916 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012917
12918 // Step 4, draw the children
12919 dispatchDraw(canvas);
12920
12921 // Step 5, draw the fade effect and restore layers
12922 final Paint p = scrollabilityCache.paint;
12923 final Matrix matrix = scrollabilityCache.matrix;
12924 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012925
12926 if (drawTop) {
12927 matrix.setScale(1, fadeHeight * topFadeStrength);
12928 matrix.postTranslate(left, top);
12929 fade.setLocalMatrix(matrix);
12930 canvas.drawRect(left, top, right, top + length, p);
12931 }
12932
12933 if (drawBottom) {
12934 matrix.setScale(1, fadeHeight * bottomFadeStrength);
12935 matrix.postRotate(180);
12936 matrix.postTranslate(left, bottom);
12937 fade.setLocalMatrix(matrix);
12938 canvas.drawRect(left, bottom - length, right, bottom, p);
12939 }
12940
12941 if (drawLeft) {
12942 matrix.setScale(1, fadeHeight * leftFadeStrength);
12943 matrix.postRotate(-90);
12944 matrix.postTranslate(left, top);
12945 fade.setLocalMatrix(matrix);
12946 canvas.drawRect(left, top, left + length, bottom, p);
12947 }
12948
12949 if (drawRight) {
12950 matrix.setScale(1, fadeHeight * rightFadeStrength);
12951 matrix.postRotate(90);
12952 matrix.postTranslate(right, top);
12953 fade.setLocalMatrix(matrix);
12954 canvas.drawRect(right - length, top, right, bottom, p);
12955 }
12956
12957 canvas.restoreToCount(saveCount);
12958
12959 // Step 6, draw decorations (scrollbars)
12960 onDrawScrollBars(canvas);
12961 }
12962
12963 /**
12964 * Override this if your view is known to always be drawn on top of a solid color background,
12965 * and needs to draw fading edges. Returning a non-zero color enables the view system to
12966 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
12967 * should be set to 0xFF.
12968 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012969 * @see #setVerticalFadingEdgeEnabled(boolean)
12970 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012971 *
12972 * @return The known solid color background for this view, or 0 if the color may vary
12973 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012974 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012975 public int getSolidColor() {
12976 return 0;
12977 }
12978
12979 /**
12980 * Build a human readable string representation of the specified view flags.
12981 *
12982 * @param flags the view flags to convert to a string
12983 * @return a String representing the supplied flags
12984 */
12985 private static String printFlags(int flags) {
12986 String output = "";
12987 int numFlags = 0;
12988 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
12989 output += "TAKES_FOCUS";
12990 numFlags++;
12991 }
12992
12993 switch (flags & VISIBILITY_MASK) {
12994 case INVISIBLE:
12995 if (numFlags > 0) {
12996 output += " ";
12997 }
12998 output += "INVISIBLE";
12999 // USELESS HERE numFlags++;
13000 break;
13001 case GONE:
13002 if (numFlags > 0) {
13003 output += " ";
13004 }
13005 output += "GONE";
13006 // USELESS HERE numFlags++;
13007 break;
13008 default:
13009 break;
13010 }
13011 return output;
13012 }
13013
13014 /**
13015 * Build a human readable string representation of the specified private
13016 * view flags.
13017 *
13018 * @param privateFlags the private view flags to convert to a string
13019 * @return a String representing the supplied flags
13020 */
13021 private static String printPrivateFlags(int privateFlags) {
13022 String output = "";
13023 int numFlags = 0;
13024
13025 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13026 output += "WANTS_FOCUS";
13027 numFlags++;
13028 }
13029
13030 if ((privateFlags & FOCUSED) == FOCUSED) {
13031 if (numFlags > 0) {
13032 output += " ";
13033 }
13034 output += "FOCUSED";
13035 numFlags++;
13036 }
13037
13038 if ((privateFlags & SELECTED) == SELECTED) {
13039 if (numFlags > 0) {
13040 output += " ";
13041 }
13042 output += "SELECTED";
13043 numFlags++;
13044 }
13045
13046 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13047 if (numFlags > 0) {
13048 output += " ";
13049 }
13050 output += "IS_ROOT_NAMESPACE";
13051 numFlags++;
13052 }
13053
13054 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13055 if (numFlags > 0) {
13056 output += " ";
13057 }
13058 output += "HAS_BOUNDS";
13059 numFlags++;
13060 }
13061
13062 if ((privateFlags & DRAWN) == DRAWN) {
13063 if (numFlags > 0) {
13064 output += " ";
13065 }
13066 output += "DRAWN";
13067 // USELESS HERE numFlags++;
13068 }
13069 return output;
13070 }
13071
13072 /**
13073 * <p>Indicates whether or not this view's layout will be requested during
13074 * the next hierarchy layout pass.</p>
13075 *
13076 * @return true if the layout will be forced during next layout pass
13077 */
13078 public boolean isLayoutRequested() {
13079 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13080 }
13081
13082 /**
13083 * Assign a size and position to a view and all of its
13084 * descendants
13085 *
13086 * <p>This is the second phase of the layout mechanism.
13087 * (The first is measuring). In this phase, each parent calls
13088 * layout on all of its children to position them.
13089 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013090 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013091 *
Chet Haase9c087442011-01-12 16:20:16 -080013092 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013093 * Derived classes with children should override
13094 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013095 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013096 *
13097 * @param l Left position, relative to parent
13098 * @param t Top position, relative to parent
13099 * @param r Right position, relative to parent
13100 * @param b Bottom position, relative to parent
13101 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013102 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013103 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013104 int oldL = mLeft;
13105 int oldT = mTop;
13106 int oldB = mBottom;
13107 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013108 boolean changed = setFrame(l, t, r, b);
13109 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
13110 if (ViewDebug.TRACE_HIERARCHY) {
13111 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
13112 }
13113
13114 onLayout(changed, l, t, r, b);
13115 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013116
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013117 ListenerInfo li = mListenerInfo;
13118 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013119 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013120 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013121 int numListeners = listenersCopy.size();
13122 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013123 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013124 }
13125 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013126 }
13127 mPrivateFlags &= ~FORCE_LAYOUT;
13128 }
13129
13130 /**
13131 * Called from layout when this view should
13132 * assign a size and position to each of its children.
13133 *
13134 * Derived classes with children should override
13135 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013136 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013137 * @param changed This is a new size or position for this view
13138 * @param left Left position, relative to parent
13139 * @param top Top position, relative to parent
13140 * @param right Right position, relative to parent
13141 * @param bottom Bottom position, relative to parent
13142 */
13143 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13144 }
13145
13146 /**
13147 * Assign a size and position to this view.
13148 *
13149 * This is called from layout.
13150 *
13151 * @param left Left position, relative to parent
13152 * @param top Top position, relative to parent
13153 * @param right Right position, relative to parent
13154 * @param bottom Bottom position, relative to parent
13155 * @return true if the new size and position are different than the
13156 * previous ones
13157 * {@hide}
13158 */
13159 protected boolean setFrame(int left, int top, int right, int bottom) {
13160 boolean changed = false;
13161
13162 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013163 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013164 + right + "," + bottom + ")");
13165 }
13166
13167 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13168 changed = true;
13169
13170 // Remember our drawn bit
13171 int drawn = mPrivateFlags & DRAWN;
13172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013173 int oldWidth = mRight - mLeft;
13174 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013175 int newWidth = right - left;
13176 int newHeight = bottom - top;
13177 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13178
13179 // Invalidate our old position
13180 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013181
13182 mLeft = left;
13183 mTop = top;
13184 mRight = right;
13185 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -080013186 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
13187 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013189
13190 mPrivateFlags |= HAS_BOUNDS;
13191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013192
Chet Haase75755e22011-07-18 17:48:25 -070013193 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013194 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13195 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013196 if (mTransformationInfo != null) {
13197 mTransformationInfo.mMatrixDirty = true;
13198 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013200 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13201 }
13202
13203 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13204 // If we are visible, force the DRAWN bit to on so that
13205 // this invalidate will go through (at least to our parent).
13206 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013207 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013208 // the DRAWN bit.
13209 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013210 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013211 // parent display list may need to be recreated based on a change in the bounds
13212 // of any child
13213 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013214 }
13215
13216 // Reset drawn bit to original value (invalidate turns it off)
13217 mPrivateFlags |= drawn;
13218
13219 mBackgroundSizeChanged = true;
13220 }
13221 return changed;
13222 }
13223
13224 /**
13225 * Finalize inflating a view from XML. This is called as the last phase
13226 * of inflation, after all child views have been added.
13227 *
13228 * <p>Even if the subclass overrides onFinishInflate, they should always be
13229 * sure to call the super method, so that we get called.
13230 */
13231 protected void onFinishInflate() {
13232 }
13233
13234 /**
13235 * Returns the resources associated with this view.
13236 *
13237 * @return Resources object.
13238 */
13239 public Resources getResources() {
13240 return mResources;
13241 }
13242
13243 /**
13244 * Invalidates the specified Drawable.
13245 *
13246 * @param drawable the drawable to invalidate
13247 */
13248 public void invalidateDrawable(Drawable drawable) {
13249 if (verifyDrawable(drawable)) {
13250 final Rect dirty = drawable.getBounds();
13251 final int scrollX = mScrollX;
13252 final int scrollY = mScrollY;
13253
13254 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13255 dirty.right + scrollX, dirty.bottom + scrollY);
13256 }
13257 }
13258
13259 /**
13260 * Schedules an action on a drawable to occur at a specified time.
13261 *
13262 * @param who the recipient of the action
13263 * @param what the action to run on the drawable
13264 * @param when the time at which the action must occur. Uses the
13265 * {@link SystemClock#uptimeMillis} timebase.
13266 */
13267 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013268 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013269 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013270 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013271 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13272 Choreographer.CALLBACK_ANIMATION, what, who,
13273 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013274 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013275 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013276 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013277 }
13278 }
13279
13280 /**
13281 * Cancels a scheduled action on a drawable.
13282 *
13283 * @param who the recipient of the action
13284 * @param what the action to cancel
13285 */
13286 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013287 if (verifyDrawable(who) && what != null) {
13288 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013289 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13290 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013291 } else {
13292 ViewRootImpl.getRunQueue().removeCallbacks(what);
13293 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013294 }
13295 }
13296
13297 /**
13298 * Unschedule any events associated with the given Drawable. This can be
13299 * used when selecting a new Drawable into a view, so that the previous
13300 * one is completely unscheduled.
13301 *
13302 * @param who The Drawable to unschedule.
13303 *
13304 * @see #drawableStateChanged
13305 */
13306 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013307 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013308 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13309 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013310 }
13311 }
13312
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013313 /**
13314 * Return the layout direction of a given Drawable.
13315 *
13316 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013317 */
13318 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013319 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013320 }
13321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013322 /**
13323 * If your view subclass is displaying its own Drawable objects, it should
13324 * override this function and return true for any Drawable it is
13325 * displaying. This allows animations for those drawables to be
13326 * scheduled.
13327 *
13328 * <p>Be sure to call through to the super class when overriding this
13329 * function.
13330 *
13331 * @param who The Drawable to verify. Return true if it is one you are
13332 * displaying, else return the result of calling through to the
13333 * super class.
13334 *
13335 * @return boolean If true than the Drawable is being displayed in the
13336 * view; else false and it is not allowed to animate.
13337 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013338 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13339 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013340 */
13341 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013342 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013343 }
13344
13345 /**
13346 * This function is called whenever the state of the view changes in such
13347 * a way that it impacts the state of drawables being shown.
13348 *
13349 * <p>Be sure to call through to the superclass when overriding this
13350 * function.
13351 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013352 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013353 */
13354 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013355 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013356 if (d != null && d.isStateful()) {
13357 d.setState(getDrawableState());
13358 }
13359 }
13360
13361 /**
13362 * Call this to force a view to update its drawable state. This will cause
13363 * drawableStateChanged to be called on this view. Views that are interested
13364 * in the new state should call getDrawableState.
13365 *
13366 * @see #drawableStateChanged
13367 * @see #getDrawableState
13368 */
13369 public void refreshDrawableState() {
13370 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13371 drawableStateChanged();
13372
13373 ViewParent parent = mParent;
13374 if (parent != null) {
13375 parent.childDrawableStateChanged(this);
13376 }
13377 }
13378
13379 /**
13380 * Return an array of resource IDs of the drawable states representing the
13381 * current state of the view.
13382 *
13383 * @return The current drawable state
13384 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013385 * @see Drawable#setState(int[])
13386 * @see #drawableStateChanged()
13387 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013388 */
13389 public final int[] getDrawableState() {
13390 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13391 return mDrawableState;
13392 } else {
13393 mDrawableState = onCreateDrawableState(0);
13394 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13395 return mDrawableState;
13396 }
13397 }
13398
13399 /**
13400 * Generate the new {@link android.graphics.drawable.Drawable} state for
13401 * this view. This is called by the view
13402 * system when the cached Drawable state is determined to be invalid. To
13403 * retrieve the current state, you should use {@link #getDrawableState}.
13404 *
13405 * @param extraSpace if non-zero, this is the number of extra entries you
13406 * would like in the returned array in which you can place your own
13407 * states.
13408 *
13409 * @return Returns an array holding the current {@link Drawable} state of
13410 * the view.
13411 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013412 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013413 */
13414 protected int[] onCreateDrawableState(int extraSpace) {
13415 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13416 mParent instanceof View) {
13417 return ((View) mParent).onCreateDrawableState(extraSpace);
13418 }
13419
13420 int[] drawableState;
13421
13422 int privateFlags = mPrivateFlags;
13423
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013424 int viewStateIndex = 0;
13425 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13426 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13427 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013428 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013429 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13430 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013431 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13432 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013433 // This is set if HW acceleration is requested, even if the current
13434 // process doesn't allow it. This is just to allow app preview
13435 // windows to better match their app.
13436 viewStateIndex |= VIEW_STATE_ACCELERATED;
13437 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013438 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013439
Christopher Tate3d4bf172011-03-28 16:16:46 -070013440 final int privateFlags2 = mPrivateFlags2;
13441 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13442 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013444 drawableState = VIEW_STATE_SETS[viewStateIndex];
13445
13446 //noinspection ConstantIfStatement
13447 if (false) {
13448 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13449 Log.i("View", toString()
13450 + " pressed=" + ((privateFlags & PRESSED) != 0)
13451 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13452 + " fo=" + hasFocus()
13453 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013454 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013455 + ": " + Arrays.toString(drawableState));
13456 }
13457
13458 if (extraSpace == 0) {
13459 return drawableState;
13460 }
13461
13462 final int[] fullState;
13463 if (drawableState != null) {
13464 fullState = new int[drawableState.length + extraSpace];
13465 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
13466 } else {
13467 fullState = new int[extraSpace];
13468 }
13469
13470 return fullState;
13471 }
13472
13473 /**
13474 * Merge your own state values in <var>additionalState</var> into the base
13475 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013476 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013477 *
13478 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013479 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013480 * own additional state values.
13481 *
13482 * @param additionalState The additional state values you would like
13483 * added to <var>baseState</var>; this array is not modified.
13484 *
13485 * @return As a convenience, the <var>baseState</var> array you originally
13486 * passed into the function is returned.
13487 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013488 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013489 */
13490 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
13491 final int N = baseState.length;
13492 int i = N - 1;
13493 while (i >= 0 && baseState[i] == 0) {
13494 i--;
13495 }
13496 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
13497 return baseState;
13498 }
13499
13500 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070013501 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
13502 * on all Drawable objects associated with this view.
13503 */
13504 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013505 if (mBackground != null) {
13506 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070013507 }
13508 }
13509
13510 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013511 * Sets the background color for this view.
13512 * @param color the color of the background
13513 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013514 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013515 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013516 if (mBackground instanceof ColorDrawable) {
13517 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070013518 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070013519 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070013520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013521 }
13522
13523 /**
13524 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070013525 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013526 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070013527 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013528 * @attr ref android.R.styleable#View_background
13529 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013530 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013531 public void setBackgroundResource(int resid) {
13532 if (resid != 0 && resid == mBackgroundResource) {
13533 return;
13534 }
13535
13536 Drawable d= null;
13537 if (resid != 0) {
13538 d = mResources.getDrawable(resid);
13539 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013540 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013541
13542 mBackgroundResource = resid;
13543 }
13544
13545 /**
13546 * Set the background to a given Drawable, or remove the background. If the
13547 * background has padding, this View's padding is set to the background's
13548 * padding. However, when a background is removed, this View's padding isn't
13549 * touched. If setting the padding is desired, please use
13550 * {@link #setPadding(int, int, int, int)}.
13551 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013552 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013553 * background
13554 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013555 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070013556 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070013557 setBackgroundDrawable(background);
13558 }
13559
13560 /**
13561 * @deprecated use {@link #setBackground(Drawable)} instead
13562 */
13563 @Deprecated
13564 public void setBackgroundDrawable(Drawable background) {
13565 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070013566 return;
13567 }
13568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013569 boolean requestLayout = false;
13570
13571 mBackgroundResource = 0;
13572
13573 /*
13574 * Regardless of whether we're setting a new background or not, we want
13575 * to clear the previous drawable.
13576 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013577 if (mBackground != null) {
13578 mBackground.setCallback(null);
13579 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013580 }
13581
Philip Milne6c8ea062012-04-03 17:38:43 -070013582 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013583 Rect padding = sThreadLocal.get();
13584 if (padding == null) {
13585 padding = new Rect();
13586 sThreadLocal.set(padding);
13587 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013588 if (background.getPadding(padding)) {
13589 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013590 case LAYOUT_DIRECTION_RTL:
13591 setPadding(padding.right, padding.top, padding.left, padding.bottom);
13592 break;
13593 case LAYOUT_DIRECTION_LTR:
13594 default:
13595 setPadding(padding.left, padding.top, padding.right, padding.bottom);
13596 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013597 }
13598
13599 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
13600 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070013601 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
13602 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013603 requestLayout = true;
13604 }
13605
Philip Milne6c8ea062012-04-03 17:38:43 -070013606 background.setCallback(this);
13607 if (background.isStateful()) {
13608 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013609 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013610 background.setVisible(getVisibility() == VISIBLE, false);
13611 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013612
13613 if ((mPrivateFlags & SKIP_DRAW) != 0) {
13614 mPrivateFlags &= ~SKIP_DRAW;
13615 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
13616 requestLayout = true;
13617 }
13618 } else {
13619 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070013620 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013621
13622 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
13623 /*
13624 * This view ONLY drew the background before and we're removing
13625 * the background, so now it won't draw anything
13626 * (hence we SKIP_DRAW)
13627 */
13628 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
13629 mPrivateFlags |= SKIP_DRAW;
13630 }
13631
13632 /*
13633 * When the background is set, we try to apply its padding to this
13634 * View. When the background is removed, we don't touch this View's
13635 * padding. This is noted in the Javadocs. Hence, we don't need to
13636 * requestLayout(), the invalidate() below is sufficient.
13637 */
13638
13639 // The old background's minimum size could have affected this
13640 // View's layout, so let's requestLayout
13641 requestLayout = true;
13642 }
13643
Romain Guy8f1344f52009-05-15 16:03:59 -070013644 computeOpaqueFlags();
13645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013646 if (requestLayout) {
13647 requestLayout();
13648 }
13649
13650 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013651 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013652 }
13653
13654 /**
13655 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070013656 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013657 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070013658 *
13659 * @see #setBackground(Drawable)
13660 *
13661 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013662 */
13663 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013664 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013665 }
13666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013667 /**
13668 * Sets the padding. The view may add on the space required to display
13669 * the scrollbars, depending on the style and visibility of the scrollbars.
13670 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
13671 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
13672 * from the values set in this call.
13673 *
13674 * @attr ref android.R.styleable#View_padding
13675 * @attr ref android.R.styleable#View_paddingBottom
13676 * @attr ref android.R.styleable#View_paddingLeft
13677 * @attr ref android.R.styleable#View_paddingRight
13678 * @attr ref android.R.styleable#View_paddingTop
13679 * @param left the left padding in pixels
13680 * @param top the top padding in pixels
13681 * @param right the right padding in pixels
13682 * @param bottom the bottom padding in pixels
13683 */
13684 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013685 mUserPaddingStart = -1;
13686 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013687 mUserPaddingRelative = false;
13688
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013689 internalSetPadding(left, top, right, bottom);
13690 }
13691
13692 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080013693 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013694 mUserPaddingRight = right;
13695 mUserPaddingBottom = bottom;
13696
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013697 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013698 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070013699
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013700 // Common case is there are no scroll bars.
13701 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013702 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080013703 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013704 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080013705 switch (mVerticalScrollbarPosition) {
13706 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013707 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
13708 left += offset;
13709 } else {
13710 right += offset;
13711 }
13712 break;
Adam Powell20232d02010-12-08 21:08:53 -080013713 case SCROLLBAR_POSITION_RIGHT:
13714 right += offset;
13715 break;
13716 case SCROLLBAR_POSITION_LEFT:
13717 left += offset;
13718 break;
13719 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013720 }
Adam Powell20232d02010-12-08 21:08:53 -080013721 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013722 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
13723 ? 0 : getHorizontalScrollbarHeight();
13724 }
13725 }
Romain Guy8506ab42009-06-11 17:35:47 -070013726
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013727 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013728 changed = true;
13729 mPaddingLeft = left;
13730 }
13731 if (mPaddingTop != top) {
13732 changed = true;
13733 mPaddingTop = top;
13734 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013735 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013736 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013737 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013738 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013739 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013740 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013741 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013742 }
13743
13744 if (changed) {
13745 requestLayout();
13746 }
13747 }
13748
13749 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013750 * Sets the relative padding. The view may add on the space required to display
13751 * the scrollbars, depending on the style and visibility of the scrollbars.
13752 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
13753 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
13754 * from the values set in this call.
13755 *
13756 * @attr ref android.R.styleable#View_padding
13757 * @attr ref android.R.styleable#View_paddingBottom
13758 * @attr ref android.R.styleable#View_paddingStart
13759 * @attr ref android.R.styleable#View_paddingEnd
13760 * @attr ref android.R.styleable#View_paddingTop
13761 * @param start the start padding in pixels
13762 * @param top the top padding in pixels
13763 * @param end the end padding in pixels
13764 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013765 */
13766 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013767 mUserPaddingStart = start;
13768 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013769 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013770
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013771 switch(getResolvedLayoutDirection()) {
13772 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013773 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013774 break;
13775 case LAYOUT_DIRECTION_LTR:
13776 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013777 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013778 }
13779 }
13780
13781 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013782 * Returns the top padding of this view.
13783 *
13784 * @return the top padding in pixels
13785 */
13786 public int getPaddingTop() {
13787 return mPaddingTop;
13788 }
13789
13790 /**
13791 * Returns the bottom padding of this view. If there are inset and enabled
13792 * scrollbars, this value may include the space required to display the
13793 * scrollbars as well.
13794 *
13795 * @return the bottom padding in pixels
13796 */
13797 public int getPaddingBottom() {
13798 return mPaddingBottom;
13799 }
13800
13801 /**
13802 * Returns the left padding of this view. If there are inset and enabled
13803 * scrollbars, this value may include the space required to display the
13804 * scrollbars as well.
13805 *
13806 * @return the left padding in pixels
13807 */
13808 public int getPaddingLeft() {
13809 return mPaddingLeft;
13810 }
13811
13812 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013813 * Returns the start padding of this view depending on its resolved layout direction.
13814 * If there are inset and enabled scrollbars, this value may include the space
13815 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013816 *
13817 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013818 */
13819 public int getPaddingStart() {
13820 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13821 mPaddingRight : mPaddingLeft;
13822 }
13823
13824 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013825 * Returns the right padding of this view. If there are inset and enabled
13826 * scrollbars, this value may include the space required to display the
13827 * scrollbars as well.
13828 *
13829 * @return the right padding in pixels
13830 */
13831 public int getPaddingRight() {
13832 return mPaddingRight;
13833 }
13834
13835 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013836 * Returns the end padding of this view depending on its resolved layout direction.
13837 * If there are inset and enabled scrollbars, this value may include the space
13838 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013839 *
13840 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013841 */
13842 public int getPaddingEnd() {
13843 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13844 mPaddingLeft : mPaddingRight;
13845 }
13846
13847 /**
13848 * Return if the padding as been set thru relative values
13849 * {@link #setPaddingRelative(int, int, int, int)} or thru
13850 * @attr ref android.R.styleable#View_paddingStart or
13851 * @attr ref android.R.styleable#View_paddingEnd
13852 *
13853 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013854 */
13855 public boolean isPaddingRelative() {
13856 return mUserPaddingRelative;
13857 }
13858
13859 /**
Philip Milne1557fd72012-04-04 23:41:34 -070013860 * @hide
13861 */
13862 public Insets getLayoutInsets() {
13863 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070013864 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070013865 }
13866 return mLayoutInsets;
13867 }
13868
13869 /**
13870 * @hide
13871 */
13872 public void setLayoutInsets(Insets layoutInsets) {
13873 mLayoutInsets = layoutInsets;
13874 }
13875
13876 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013877 * Changes the selection state of this view. A view can be selected or not.
13878 * Note that selection is not the same as focus. Views are typically
13879 * selected in the context of an AdapterView like ListView or GridView;
13880 * the selected view is the view that is highlighted.
13881 *
13882 * @param selected true if the view must be selected, false otherwise
13883 */
13884 public void setSelected(boolean selected) {
13885 if (((mPrivateFlags & SELECTED) != 0) != selected) {
13886 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070013887 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080013888 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013889 refreshDrawableState();
13890 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070013891 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
13892 notifyAccessibilityStateChanged();
13893 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013894 }
13895 }
13896
13897 /**
13898 * Dispatch setSelected to all of this View's children.
13899 *
13900 * @see #setSelected(boolean)
13901 *
13902 * @param selected The new selected state
13903 */
13904 protected void dispatchSetSelected(boolean selected) {
13905 }
13906
13907 /**
13908 * Indicates the selection state of this view.
13909 *
13910 * @return true if the view is selected, false otherwise
13911 */
13912 @ViewDebug.ExportedProperty
13913 public boolean isSelected() {
13914 return (mPrivateFlags & SELECTED) != 0;
13915 }
13916
13917 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013918 * Changes the activated state of this view. A view can be activated or not.
13919 * Note that activation is not the same as selection. Selection is
13920 * a transient property, representing the view (hierarchy) the user is
13921 * currently interacting with. Activation is a longer-term state that the
13922 * user can move views in and out of. For example, in a list view with
13923 * single or multiple selection enabled, the views in the current selection
13924 * set are activated. (Um, yeah, we are deeply sorry about the terminology
13925 * here.) The activated state is propagated down to children of the view it
13926 * is set on.
13927 *
13928 * @param activated true if the view must be activated, false otherwise
13929 */
13930 public void setActivated(boolean activated) {
13931 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
13932 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013933 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013934 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070013935 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013936 }
13937 }
13938
13939 /**
13940 * Dispatch setActivated to all of this View's children.
13941 *
13942 * @see #setActivated(boolean)
13943 *
13944 * @param activated The new activated state
13945 */
13946 protected void dispatchSetActivated(boolean activated) {
13947 }
13948
13949 /**
13950 * Indicates the activation state of this view.
13951 *
13952 * @return true if the view is activated, false otherwise
13953 */
13954 @ViewDebug.ExportedProperty
13955 public boolean isActivated() {
13956 return (mPrivateFlags & ACTIVATED) != 0;
13957 }
13958
13959 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013960 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
13961 * observer can be used to get notifications when global events, like
13962 * layout, happen.
13963 *
13964 * The returned ViewTreeObserver observer is not guaranteed to remain
13965 * valid for the lifetime of this View. If the caller of this method keeps
13966 * a long-lived reference to ViewTreeObserver, it should always check for
13967 * the return value of {@link ViewTreeObserver#isAlive()}.
13968 *
13969 * @return The ViewTreeObserver for this view's hierarchy.
13970 */
13971 public ViewTreeObserver getViewTreeObserver() {
13972 if (mAttachInfo != null) {
13973 return mAttachInfo.mTreeObserver;
13974 }
13975 if (mFloatingTreeObserver == null) {
13976 mFloatingTreeObserver = new ViewTreeObserver();
13977 }
13978 return mFloatingTreeObserver;
13979 }
13980
13981 /**
13982 * <p>Finds the topmost view in the current view hierarchy.</p>
13983 *
13984 * @return the topmost view containing this view
13985 */
13986 public View getRootView() {
13987 if (mAttachInfo != null) {
13988 final View v = mAttachInfo.mRootView;
13989 if (v != null) {
13990 return v;
13991 }
13992 }
Romain Guy8506ab42009-06-11 17:35:47 -070013993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013994 View parent = this;
13995
13996 while (parent.mParent != null && parent.mParent instanceof View) {
13997 parent = (View) parent.mParent;
13998 }
13999
14000 return parent;
14001 }
14002
14003 /**
14004 * <p>Computes the coordinates of this view on the screen. The argument
14005 * must be an array of two integers. After the method returns, the array
14006 * contains the x and y location in that order.</p>
14007 *
14008 * @param location an array of two integers in which to hold the coordinates
14009 */
14010 public void getLocationOnScreen(int[] location) {
14011 getLocationInWindow(location);
14012
14013 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014014 if (info != null) {
14015 location[0] += info.mWindowLeft;
14016 location[1] += info.mWindowTop;
14017 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014018 }
14019
14020 /**
14021 * <p>Computes the coordinates of this view in its window. The argument
14022 * must be an array of two integers. After the method returns, the array
14023 * contains the x and y location in that order.</p>
14024 *
14025 * @param location an array of two integers in which to hold the coordinates
14026 */
14027 public void getLocationInWindow(int[] location) {
14028 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014029 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014030 }
14031
Gilles Debunne6583ce52011-12-06 18:09:02 -080014032 if (mAttachInfo == null) {
14033 // When the view is not attached to a window, this method does not make sense
14034 location[0] = location[1] = 0;
14035 return;
14036 }
14037
Gilles Debunnecea45132011-11-24 02:19:27 +010014038 float[] position = mAttachInfo.mTmpTransformLocation;
14039 position[0] = position[1] = 0.0f;
14040
14041 if (!hasIdentityMatrix()) {
14042 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014044
Gilles Debunnecea45132011-11-24 02:19:27 +010014045 position[0] += mLeft;
14046 position[1] += mTop;
14047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014048 ViewParent viewParent = mParent;
14049 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014050 final View view = (View) viewParent;
14051
14052 position[0] -= view.mScrollX;
14053 position[1] -= view.mScrollY;
14054
14055 if (!view.hasIdentityMatrix()) {
14056 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014057 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014058
14059 position[0] += view.mLeft;
14060 position[1] += view.mTop;
14061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014062 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014063 }
Romain Guy8506ab42009-06-11 17:35:47 -070014064
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014065 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014066 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014067 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14068 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014069 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014070
14071 location[0] = (int) (position[0] + 0.5f);
14072 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014073 }
14074
14075 /**
14076 * {@hide}
14077 * @param id the id of the view to be found
14078 * @return the view of the specified id, null if cannot be found
14079 */
14080 protected View findViewTraversal(int id) {
14081 if (id == mID) {
14082 return this;
14083 }
14084 return null;
14085 }
14086
14087 /**
14088 * {@hide}
14089 * @param tag the tag of the view to be found
14090 * @return the view of specified tag, null if cannot be found
14091 */
14092 protected View findViewWithTagTraversal(Object tag) {
14093 if (tag != null && tag.equals(mTag)) {
14094 return this;
14095 }
14096 return null;
14097 }
14098
14099 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014100 * {@hide}
14101 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014102 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014103 * @return The first view that matches the predicate or null.
14104 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014105 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014106 if (predicate.apply(this)) {
14107 return this;
14108 }
14109 return null;
14110 }
14111
14112 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014113 * Look for a child view with the given id. If this view has the given
14114 * id, return this view.
14115 *
14116 * @param id The id to search for.
14117 * @return The view that has the given id in the hierarchy or null
14118 */
14119 public final View findViewById(int id) {
14120 if (id < 0) {
14121 return null;
14122 }
14123 return findViewTraversal(id);
14124 }
14125
14126 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014127 * Finds a view by its unuque and stable accessibility id.
14128 *
14129 * @param accessibilityId The searched accessibility id.
14130 * @return The found view.
14131 */
14132 final View findViewByAccessibilityId(int accessibilityId) {
14133 if (accessibilityId < 0) {
14134 return null;
14135 }
14136 return findViewByAccessibilityIdTraversal(accessibilityId);
14137 }
14138
14139 /**
14140 * Performs the traversal to find a view by its unuque and stable accessibility id.
14141 *
14142 * <strong>Note:</strong>This method does not stop at the root namespace
14143 * boundary since the user can touch the screen at an arbitrary location
14144 * potentially crossing the root namespace bounday which will send an
14145 * accessibility event to accessibility services and they should be able
14146 * to obtain the event source. Also accessibility ids are guaranteed to be
14147 * unique in the window.
14148 *
14149 * @param accessibilityId The accessibility id.
14150 * @return The found view.
14151 */
14152 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14153 if (getAccessibilityViewId() == accessibilityId) {
14154 return this;
14155 }
14156 return null;
14157 }
14158
14159 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014160 * Look for a child view with the given tag. If this view has the given
14161 * tag, return this view.
14162 *
14163 * @param tag The tag to search for, using "tag.equals(getTag())".
14164 * @return The View that has the given tag in the hierarchy or null
14165 */
14166 public final View findViewWithTag(Object tag) {
14167 if (tag == null) {
14168 return null;
14169 }
14170 return findViewWithTagTraversal(tag);
14171 }
14172
14173 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014174 * {@hide}
14175 * Look for a child view that matches the specified predicate.
14176 * If this view matches the predicate, return this view.
14177 *
14178 * @param predicate The predicate to evaluate.
14179 * @return The first view that matches the predicate or null.
14180 */
14181 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014182 return findViewByPredicateTraversal(predicate, null);
14183 }
14184
14185 /**
14186 * {@hide}
14187 * Look for a child view that matches the specified predicate,
14188 * starting with the specified view and its descendents and then
14189 * recusively searching the ancestors and siblings of that view
14190 * until this view is reached.
14191 *
14192 * This method is useful in cases where the predicate does not match
14193 * a single unique view (perhaps multiple views use the same id)
14194 * and we are trying to find the view that is "closest" in scope to the
14195 * starting view.
14196 *
14197 * @param start The view to start from.
14198 * @param predicate The predicate to evaluate.
14199 * @return The first view that matches the predicate or null.
14200 */
14201 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14202 View childToSkip = null;
14203 for (;;) {
14204 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14205 if (view != null || start == this) {
14206 return view;
14207 }
14208
14209 ViewParent parent = start.getParent();
14210 if (parent == null || !(parent instanceof View)) {
14211 return null;
14212 }
14213
14214 childToSkip = start;
14215 start = (View) parent;
14216 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014217 }
14218
14219 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014220 * Sets the identifier for this view. The identifier does not have to be
14221 * unique in this view's hierarchy. The identifier should be a positive
14222 * number.
14223 *
14224 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014225 * @see #getId()
14226 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014227 *
14228 * @param id a number used to identify the view
14229 *
14230 * @attr ref android.R.styleable#View_id
14231 */
14232 public void setId(int id) {
14233 mID = id;
14234 }
14235
14236 /**
14237 * {@hide}
14238 *
14239 * @param isRoot true if the view belongs to the root namespace, false
14240 * otherwise
14241 */
14242 public void setIsRootNamespace(boolean isRoot) {
14243 if (isRoot) {
14244 mPrivateFlags |= IS_ROOT_NAMESPACE;
14245 } else {
14246 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14247 }
14248 }
14249
14250 /**
14251 * {@hide}
14252 *
14253 * @return true if the view belongs to the root namespace, false otherwise
14254 */
14255 public boolean isRootNamespace() {
14256 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14257 }
14258
14259 /**
14260 * Returns this view's identifier.
14261 *
14262 * @return a positive integer used to identify the view or {@link #NO_ID}
14263 * if the view has no ID
14264 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014265 * @see #setId(int)
14266 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014267 * @attr ref android.R.styleable#View_id
14268 */
14269 @ViewDebug.CapturedViewProperty
14270 public int getId() {
14271 return mID;
14272 }
14273
14274 /**
14275 * Returns this view's tag.
14276 *
14277 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014278 *
14279 * @see #setTag(Object)
14280 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014281 */
14282 @ViewDebug.ExportedProperty
14283 public Object getTag() {
14284 return mTag;
14285 }
14286
14287 /**
14288 * Sets the tag associated with this view. A tag can be used to mark
14289 * a view in its hierarchy and does not have to be unique within the
14290 * hierarchy. Tags can also be used to store data within a view without
14291 * resorting to another data structure.
14292 *
14293 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014294 *
14295 * @see #getTag()
14296 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014297 */
14298 public void setTag(final Object tag) {
14299 mTag = tag;
14300 }
14301
14302 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014303 * Returns the tag associated with this view and the specified key.
14304 *
14305 * @param key The key identifying the tag
14306 *
14307 * @return the Object stored in this view as a tag
14308 *
14309 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014310 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014311 */
14312 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014313 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014314 return null;
14315 }
14316
14317 /**
14318 * Sets a tag associated with this view and a key. A tag can be used
14319 * to mark a view in its hierarchy and does not have to be unique within
14320 * the hierarchy. Tags can also be used to store data within a view
14321 * without resorting to another data structure.
14322 *
14323 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014324 * application to ensure it is unique (see the <a
14325 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14326 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014327 * the Android framework or not associated with any package will cause
14328 * an {@link IllegalArgumentException} to be thrown.
14329 *
14330 * @param key The key identifying the tag
14331 * @param tag An Object to tag the view with
14332 *
14333 * @throws IllegalArgumentException If they specified key is not valid
14334 *
14335 * @see #setTag(Object)
14336 * @see #getTag(int)
14337 */
14338 public void setTag(int key, final Object tag) {
14339 // If the package id is 0x00 or 0x01, it's either an undefined package
14340 // or a framework id
14341 if ((key >>> 24) < 2) {
14342 throw new IllegalArgumentException("The key must be an application-specific "
14343 + "resource id.");
14344 }
14345
Adam Powell2b2f6d62011-09-23 11:15:39 -070014346 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014347 }
14348
14349 /**
14350 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14351 * framework id.
14352 *
14353 * @hide
14354 */
14355 public void setTagInternal(int key, Object tag) {
14356 if ((key >>> 24) != 0x1) {
14357 throw new IllegalArgumentException("The key must be a framework-specific "
14358 + "resource id.");
14359 }
14360
Adam Powell2b2f6d62011-09-23 11:15:39 -070014361 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014362 }
14363
Adam Powell2b2f6d62011-09-23 11:15:39 -070014364 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014365 if (mKeyedTags == null) {
14366 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014367 }
14368
Adam Powell7db82ac2011-09-22 19:44:04 -070014369 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014370 }
14371
14372 /**
Romain Guy13922e02009-05-12 17:56:14 -070014373 * @param consistency The type of consistency. See ViewDebug for more information.
14374 *
14375 * @hide
14376 */
14377 protected boolean dispatchConsistencyCheck(int consistency) {
14378 return onConsistencyCheck(consistency);
14379 }
14380
14381 /**
14382 * Method that subclasses should implement to check their consistency. The type of
14383 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070014384 *
Romain Guy13922e02009-05-12 17:56:14 -070014385 * @param consistency The type of consistency. See ViewDebug for more information.
14386 *
14387 * @throws IllegalStateException if the view is in an inconsistent state.
14388 *
14389 * @hide
14390 */
14391 protected boolean onConsistencyCheck(int consistency) {
14392 boolean result = true;
14393
14394 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
14395 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
14396
14397 if (checkLayout) {
14398 if (getParent() == null) {
14399 result = false;
14400 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14401 "View " + this + " does not have a parent.");
14402 }
14403
14404 if (mAttachInfo == null) {
14405 result = false;
14406 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14407 "View " + this + " is not attached to a window.");
14408 }
14409 }
14410
14411 if (checkDrawing) {
14412 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
14413 // from their draw() method
14414
14415 if ((mPrivateFlags & DRAWN) != DRAWN &&
14416 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
14417 result = false;
14418 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14419 "View " + this + " was invalidated but its drawing cache is valid.");
14420 }
14421 }
14422
14423 return result;
14424 }
14425
14426 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014427 * Prints information about this view in the log output, with the tag
14428 * {@link #VIEW_LOG_TAG}.
14429 *
14430 * @hide
14431 */
14432 public void debug() {
14433 debug(0);
14434 }
14435
14436 /**
14437 * Prints information about this view in the log output, with the tag
14438 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14439 * indentation defined by the <code>depth</code>.
14440 *
14441 * @param depth the indentation level
14442 *
14443 * @hide
14444 */
14445 protected void debug(int depth) {
14446 String output = debugIndent(depth - 1);
14447
14448 output += "+ " + this;
14449 int id = getId();
14450 if (id != -1) {
14451 output += " (id=" + id + ")";
14452 }
14453 Object tag = getTag();
14454 if (tag != null) {
14455 output += " (tag=" + tag + ")";
14456 }
14457 Log.d(VIEW_LOG_TAG, output);
14458
14459 if ((mPrivateFlags & FOCUSED) != 0) {
14460 output = debugIndent(depth) + " FOCUSED";
14461 Log.d(VIEW_LOG_TAG, output);
14462 }
14463
14464 output = debugIndent(depth);
14465 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14466 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14467 + "} ";
14468 Log.d(VIEW_LOG_TAG, output);
14469
14470 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14471 || mPaddingBottom != 0) {
14472 output = debugIndent(depth);
14473 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14474 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14475 Log.d(VIEW_LOG_TAG, output);
14476 }
14477
14478 output = debugIndent(depth);
14479 output += "mMeasureWidth=" + mMeasuredWidth +
14480 " mMeasureHeight=" + mMeasuredHeight;
14481 Log.d(VIEW_LOG_TAG, output);
14482
14483 output = debugIndent(depth);
14484 if (mLayoutParams == null) {
14485 output += "BAD! no layout params";
14486 } else {
14487 output = mLayoutParams.debug(output);
14488 }
14489 Log.d(VIEW_LOG_TAG, output);
14490
14491 output = debugIndent(depth);
14492 output += "flags={";
14493 output += View.printFlags(mViewFlags);
14494 output += "}";
14495 Log.d(VIEW_LOG_TAG, output);
14496
14497 output = debugIndent(depth);
14498 output += "privateFlags={";
14499 output += View.printPrivateFlags(mPrivateFlags);
14500 output += "}";
14501 Log.d(VIEW_LOG_TAG, output);
14502 }
14503
14504 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014505 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014506 *
14507 * @param depth the indentation level
14508 * @return a String containing (depth * 2 + 3) * 2 white spaces
14509 *
14510 * @hide
14511 */
14512 protected static String debugIndent(int depth) {
14513 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
14514 for (int i = 0; i < (depth * 2) + 3; i++) {
14515 spaces.append(' ').append(' ');
14516 }
14517 return spaces.toString();
14518 }
14519
14520 /**
14521 * <p>Return the offset of the widget's text baseline from the widget's top
14522 * boundary. If this widget does not support baseline alignment, this
14523 * method returns -1. </p>
14524 *
14525 * @return the offset of the baseline within the widget's bounds or -1
14526 * if baseline alignment is not supported
14527 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070014528 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014529 public int getBaseline() {
14530 return -1;
14531 }
14532
14533 /**
14534 * Call this when something has changed which has invalidated the
14535 * layout of this view. This will schedule a layout pass of the view
14536 * tree.
14537 */
14538 public void requestLayout() {
14539 if (ViewDebug.TRACE_HIERARCHY) {
14540 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
14541 }
14542
14543 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014544 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014545
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070014546 if (mLayoutParams != null) {
14547 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
14548 }
14549
14550 if (mParent != null && !mParent.isLayoutRequested()) {
14551 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014552 }
14553 }
14554
14555 /**
14556 * Forces this view to be laid out during the next layout pass.
14557 * This method does not call requestLayout() or forceLayout()
14558 * on the parent.
14559 */
14560 public void forceLayout() {
14561 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014562 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014563 }
14564
14565 /**
14566 * <p>
14567 * This is called to find out how big a view should be. The parent
14568 * supplies constraint information in the width and height parameters.
14569 * </p>
14570 *
14571 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080014572 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014573 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014574 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014575 * </p>
14576 *
14577 *
14578 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14579 * parent
14580 * @param heightMeasureSpec Vertical space requirements as imposed by the
14581 * parent
14582 *
14583 * @see #onMeasure(int, int)
14584 */
14585 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14586 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14587 widthMeasureSpec != mOldWidthMeasureSpec ||
14588 heightMeasureSpec != mOldHeightMeasureSpec) {
14589
14590 // first clears the measured dimension flag
14591 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14592
14593 if (ViewDebug.TRACE_HIERARCHY) {
14594 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
14595 }
14596
14597 // measure ourselves, this should set the measured dimension flag back
14598 onMeasure(widthMeasureSpec, heightMeasureSpec);
14599
14600 // flag not set, setMeasuredDimension() was not invoked, we raise
14601 // an exception to warn the developer
14602 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14603 throw new IllegalStateException("onMeasure() did not set the"
14604 + " measured dimension by calling"
14605 + " setMeasuredDimension()");
14606 }
14607
14608 mPrivateFlags |= LAYOUT_REQUIRED;
14609 }
14610
14611 mOldWidthMeasureSpec = widthMeasureSpec;
14612 mOldHeightMeasureSpec = heightMeasureSpec;
14613 }
14614
14615 /**
14616 * <p>
14617 * Measure the view and its content to determine the measured width and the
14618 * measured height. This method is invoked by {@link #measure(int, int)} and
14619 * should be overriden by subclasses to provide accurate and efficient
14620 * measurement of their contents.
14621 * </p>
14622 *
14623 * <p>
14624 * <strong>CONTRACT:</strong> When overriding this method, you
14625 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14626 * measured width and height of this view. Failure to do so will trigger an
14627 * <code>IllegalStateException</code>, thrown by
14628 * {@link #measure(int, int)}. Calling the superclass'
14629 * {@link #onMeasure(int, int)} is a valid use.
14630 * </p>
14631 *
14632 * <p>
14633 * The base class implementation of measure defaults to the background size,
14634 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14635 * override {@link #onMeasure(int, int)} to provide better measurements of
14636 * their content.
14637 * </p>
14638 *
14639 * <p>
14640 * If this method is overridden, it is the subclass's responsibility to make
14641 * sure the measured height and width are at least the view's minimum height
14642 * and width ({@link #getSuggestedMinimumHeight()} and
14643 * {@link #getSuggestedMinimumWidth()}).
14644 * </p>
14645 *
14646 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
14647 * The requirements are encoded with
14648 * {@link android.view.View.MeasureSpec}.
14649 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
14650 * The requirements are encoded with
14651 * {@link android.view.View.MeasureSpec}.
14652 *
14653 * @see #getMeasuredWidth()
14654 * @see #getMeasuredHeight()
14655 * @see #setMeasuredDimension(int, int)
14656 * @see #getSuggestedMinimumHeight()
14657 * @see #getSuggestedMinimumWidth()
14658 * @see android.view.View.MeasureSpec#getMode(int)
14659 * @see android.view.View.MeasureSpec#getSize(int)
14660 */
14661 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
14662 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
14663 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
14664 }
14665
14666 /**
14667 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
14668 * measured width and measured height. Failing to do so will trigger an
14669 * exception at measurement time.</p>
14670 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080014671 * @param measuredWidth The measured width of this view. May be a complex
14672 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14673 * {@link #MEASURED_STATE_TOO_SMALL}.
14674 * @param measuredHeight The measured height of this view. May be a complex
14675 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14676 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014677 */
14678 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
14679 mMeasuredWidth = measuredWidth;
14680 mMeasuredHeight = measuredHeight;
14681
14682 mPrivateFlags |= MEASURED_DIMENSION_SET;
14683 }
14684
14685 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080014686 * Merge two states as returned by {@link #getMeasuredState()}.
14687 * @param curState The current state as returned from a view or the result
14688 * of combining multiple views.
14689 * @param newState The new view state to combine.
14690 * @return Returns a new integer reflecting the combination of the two
14691 * states.
14692 */
14693 public static int combineMeasuredStates(int curState, int newState) {
14694 return curState | newState;
14695 }
14696
14697 /**
14698 * Version of {@link #resolveSizeAndState(int, int, int)}
14699 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
14700 */
14701 public static int resolveSize(int size, int measureSpec) {
14702 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
14703 }
14704
14705 /**
14706 * Utility to reconcile a desired size and state, with constraints imposed
14707 * by a MeasureSpec. Will take the desired size, unless a different size
14708 * is imposed by the constraints. The returned value is a compound integer,
14709 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
14710 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
14711 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014712 *
14713 * @param size How big the view wants to be
14714 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080014715 * @return Size information bit mask as defined by
14716 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014717 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080014718 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014719 int result = size;
14720 int specMode = MeasureSpec.getMode(measureSpec);
14721 int specSize = MeasureSpec.getSize(measureSpec);
14722 switch (specMode) {
14723 case MeasureSpec.UNSPECIFIED:
14724 result = size;
14725 break;
14726 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080014727 if (specSize < size) {
14728 result = specSize | MEASURED_STATE_TOO_SMALL;
14729 } else {
14730 result = size;
14731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014732 break;
14733 case MeasureSpec.EXACTLY:
14734 result = specSize;
14735 break;
14736 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080014737 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014738 }
14739
14740 /**
14741 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070014742 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014743 * by the MeasureSpec.
14744 *
14745 * @param size Default size for this view
14746 * @param measureSpec Constraints imposed by the parent
14747 * @return The size this view should be.
14748 */
14749 public static int getDefaultSize(int size, int measureSpec) {
14750 int result = size;
14751 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070014752 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014753
14754 switch (specMode) {
14755 case MeasureSpec.UNSPECIFIED:
14756 result = size;
14757 break;
14758 case MeasureSpec.AT_MOST:
14759 case MeasureSpec.EXACTLY:
14760 result = specSize;
14761 break;
14762 }
14763 return result;
14764 }
14765
14766 /**
14767 * Returns the suggested minimum height that the view should use. This
14768 * returns the maximum of the view's minimum height
14769 * and the background's minimum height
14770 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
14771 * <p>
14772 * When being used in {@link #onMeasure(int, int)}, the caller should still
14773 * ensure the returned height is within the requirements of the parent.
14774 *
14775 * @return The suggested minimum height of the view.
14776 */
14777 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014778 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014780 }
14781
14782 /**
14783 * Returns the suggested minimum width that the view should use. This
14784 * returns the maximum of the view's minimum width)
14785 * and the background's minimum width
14786 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
14787 * <p>
14788 * When being used in {@link #onMeasure(int, int)}, the caller should still
14789 * ensure the returned width is within the requirements of the parent.
14790 *
14791 * @return The suggested minimum width of the view.
14792 */
14793 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014794 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
14795 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014796
Philip Milne6c8ea062012-04-03 17:38:43 -070014797 /**
14798 * Returns the minimum height of the view.
14799 *
14800 * @return the minimum height the view will try to be.
14801 *
14802 * @see #setMinimumHeight(int)
14803 *
14804 * @attr ref android.R.styleable#View_minHeight
14805 */
14806 public int getMinimumHeight() {
14807 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014808 }
14809
14810 /**
14811 * Sets the minimum height of the view. It is not guaranteed the view will
14812 * be able to achieve this minimum height (for example, if its parent layout
14813 * constrains it with less available height).
14814 *
14815 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014816 *
14817 * @see #getMinimumHeight()
14818 *
14819 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014820 */
14821 public void setMinimumHeight(int minHeight) {
14822 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070014823 requestLayout();
14824 }
14825
14826 /**
14827 * Returns the minimum width of the view.
14828 *
14829 * @return the minimum width the view will try to be.
14830 *
14831 * @see #setMinimumWidth(int)
14832 *
14833 * @attr ref android.R.styleable#View_minWidth
14834 */
14835 public int getMinimumWidth() {
14836 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014837 }
14838
14839 /**
14840 * Sets the minimum width of the view. It is not guaranteed the view will
14841 * be able to achieve this minimum width (for example, if its parent layout
14842 * constrains it with less available width).
14843 *
14844 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014845 *
14846 * @see #getMinimumWidth()
14847 *
14848 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014849 */
14850 public void setMinimumWidth(int minWidth) {
14851 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070014852 requestLayout();
14853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014854 }
14855
14856 /**
14857 * Get the animation currently associated with this view.
14858 *
14859 * @return The animation that is currently playing or
14860 * scheduled to play for this view.
14861 */
14862 public Animation getAnimation() {
14863 return mCurrentAnimation;
14864 }
14865
14866 /**
14867 * Start the specified animation now.
14868 *
14869 * @param animation the animation to start now
14870 */
14871 public void startAnimation(Animation animation) {
14872 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
14873 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014874 invalidateParentCaches();
14875 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014876 }
14877
14878 /**
14879 * Cancels any animations for this view.
14880 */
14881 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080014882 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080014883 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080014884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014885 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014886 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014887 }
14888
14889 /**
14890 * Sets the next animation to play for this view.
14891 * If you want the animation to play immediately, use
14892 * startAnimation. This method provides allows fine-grained
14893 * control over the start time and invalidation, but you
14894 * must make sure that 1) the animation has a start time set, and
14895 * 2) the view will be invalidated when the animation is supposed to
14896 * start.
14897 *
14898 * @param animation The next animation, or null.
14899 */
14900 public void setAnimation(Animation animation) {
14901 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070014902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014903 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070014904 // If the screen is off assume the animation start time is now instead of
14905 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
14906 // would cause the animation to start when the screen turns back on
14907 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
14908 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
14909 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
14910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014911 animation.reset();
14912 }
14913 }
14914
14915 /**
14916 * Invoked by a parent ViewGroup to notify the start of the animation
14917 * currently associated with this view. If you override this method,
14918 * always call super.onAnimationStart();
14919 *
14920 * @see #setAnimation(android.view.animation.Animation)
14921 * @see #getAnimation()
14922 */
14923 protected void onAnimationStart() {
14924 mPrivateFlags |= ANIMATION_STARTED;
14925 }
14926
14927 /**
14928 * Invoked by a parent ViewGroup to notify the end of the animation
14929 * currently associated with this view. If you override this method,
14930 * always call super.onAnimationEnd();
14931 *
14932 * @see #setAnimation(android.view.animation.Animation)
14933 * @see #getAnimation()
14934 */
14935 protected void onAnimationEnd() {
14936 mPrivateFlags &= ~ANIMATION_STARTED;
14937 }
14938
14939 /**
14940 * Invoked if there is a Transform that involves alpha. Subclass that can
14941 * draw themselves with the specified alpha should return true, and then
14942 * respect that alpha when their onDraw() is called. If this returns false
14943 * then the view may be redirected to draw into an offscreen buffer to
14944 * fulfill the request, which will look fine, but may be slower than if the
14945 * subclass handles it internally. The default implementation returns false.
14946 *
14947 * @param alpha The alpha (0..255) to apply to the view's drawing
14948 * @return true if the view can draw with the specified alpha.
14949 */
14950 protected boolean onSetAlpha(int alpha) {
14951 return false;
14952 }
14953
14954 /**
14955 * This is used by the RootView to perform an optimization when
14956 * the view hierarchy contains one or several SurfaceView.
14957 * SurfaceView is always considered transparent, but its children are not,
14958 * therefore all View objects remove themselves from the global transparent
14959 * region (passed as a parameter to this function).
14960 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014961 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014962 *
14963 * @return Returns true if the effective visibility of the view at this
14964 * point is opaque, regardless of the transparent region; returns false
14965 * if it is possible for underlying windows to be seen behind the view.
14966 *
14967 * {@hide}
14968 */
14969 public boolean gatherTransparentRegion(Region region) {
14970 final AttachInfo attachInfo = mAttachInfo;
14971 if (region != null && attachInfo != null) {
14972 final int pflags = mPrivateFlags;
14973 if ((pflags & SKIP_DRAW) == 0) {
14974 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
14975 // remove it from the transparent region.
14976 final int[] location = attachInfo.mTransparentLocation;
14977 getLocationInWindow(location);
14978 region.op(location[0], location[1], location[0] + mRight - mLeft,
14979 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070014980 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014981 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
14982 // exists, so we remove the background drawable's non-transparent
14983 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070014984 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014985 }
14986 }
14987 return true;
14988 }
14989
14990 /**
14991 * Play a sound effect for this view.
14992 *
14993 * <p>The framework will play sound effects for some built in actions, such as
14994 * clicking, but you may wish to play these effects in your widget,
14995 * for instance, for internal navigation.
14996 *
14997 * <p>The sound effect will only be played if sound effects are enabled by the user, and
14998 * {@link #isSoundEffectsEnabled()} is true.
14999 *
15000 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15001 */
15002 public void playSoundEffect(int soundConstant) {
15003 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15004 return;
15005 }
15006 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15007 }
15008
15009 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015010 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015011 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015012 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015013 *
15014 * <p>The framework will provide haptic feedback for some built in actions,
15015 * such as long presses, but you may wish to provide feedback for your
15016 * own widget.
15017 *
15018 * <p>The feedback will only be performed if
15019 * {@link #isHapticFeedbackEnabled()} is true.
15020 *
15021 * @param feedbackConstant One of the constants defined in
15022 * {@link HapticFeedbackConstants}
15023 */
15024 public boolean performHapticFeedback(int feedbackConstant) {
15025 return performHapticFeedback(feedbackConstant, 0);
15026 }
15027
15028 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015029 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015030 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015031 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015032 *
15033 * @param feedbackConstant One of the constants defined in
15034 * {@link HapticFeedbackConstants}
15035 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15036 */
15037 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15038 if (mAttachInfo == null) {
15039 return false;
15040 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015041 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015042 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015043 && !isHapticFeedbackEnabled()) {
15044 return false;
15045 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015046 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15047 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015048 }
15049
15050 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015051 * Request that the visibility of the status bar or other screen/window
15052 * decorations be changed.
15053 *
15054 * <p>This method is used to put the over device UI into temporary modes
15055 * where the user's attention is focused more on the application content,
15056 * by dimming or hiding surrounding system affordances. This is typically
15057 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15058 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15059 * to be placed behind the action bar (and with these flags other system
15060 * affordances) so that smooth transitions between hiding and showing them
15061 * can be done.
15062 *
15063 * <p>Two representative examples of the use of system UI visibility is
15064 * implementing a content browsing application (like a magazine reader)
15065 * and a video playing application.
15066 *
15067 * <p>The first code shows a typical implementation of a View in a content
15068 * browsing application. In this implementation, the application goes
15069 * into a content-oriented mode by hiding the status bar and action bar,
15070 * and putting the navigation elements into lights out mode. The user can
15071 * then interact with content while in this mode. Such an application should
15072 * provide an easy way for the user to toggle out of the mode (such as to
15073 * check information in the status bar or access notifications). In the
15074 * implementation here, this is done simply by tapping on the content.
15075 *
15076 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15077 * content}
15078 *
15079 * <p>This second code sample shows a typical implementation of a View
15080 * in a video playing application. In this situation, while the video is
15081 * playing the application would like to go into a complete full-screen mode,
15082 * to use as much of the display as possible for the video. When in this state
15083 * the user can not interact with the application; the system intercepts
15084 * touching on the screen to pop the UI out of full screen mode.
15085 *
15086 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15087 * content}
15088 *
15089 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15090 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15091 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15092 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015093 */
15094 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015095 if (visibility != mSystemUiVisibility) {
15096 mSystemUiVisibility = visibility;
15097 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15098 mParent.recomputeViewAttributes(this);
15099 }
Joe Onorato664644d2011-01-23 17:53:23 -080015100 }
15101 }
15102
15103 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015104 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15105 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15106 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15107 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15108 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015109 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015110 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015111 return mSystemUiVisibility;
15112 }
15113
Scott Mainec6331b2011-05-24 16:55:56 -070015114 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015115 * Returns the current system UI visibility that is currently set for
15116 * the entire window. This is the combination of the
15117 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15118 * views in the window.
15119 */
15120 public int getWindowSystemUiVisibility() {
15121 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15122 }
15123
15124 /**
15125 * Override to find out when the window's requested system UI visibility
15126 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15127 * This is different from the callbacks recieved through
15128 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15129 * in that this is only telling you about the local request of the window,
15130 * not the actual values applied by the system.
15131 */
15132 public void onWindowSystemUiVisibilityChanged(int visible) {
15133 }
15134
15135 /**
15136 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15137 * the view hierarchy.
15138 */
15139 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15140 onWindowSystemUiVisibilityChanged(visible);
15141 }
15142
15143 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015144 * Set a listener to receive callbacks when the visibility of the system bar changes.
15145 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15146 */
Joe Onorato664644d2011-01-23 17:53:23 -080015147 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015148 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015149 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15150 mParent.recomputeViewAttributes(this);
15151 }
15152 }
15153
15154 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015155 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15156 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015157 */
15158 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015159 ListenerInfo li = mListenerInfo;
15160 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15161 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015162 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015163 }
15164 }
15165
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015166 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
15167 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15168 if (val != mSystemUiVisibility) {
15169 setSystemUiVisibility(val);
15170 }
15171 }
15172
Joe Onorato664644d2011-01-23 17:53:23 -080015173 /**
Joe Malin32736f02011-01-19 16:14:20 -080015174 * Creates an image that the system displays during the drag and drop
15175 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15176 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15177 * appearance as the given View. The default also positions the center of the drag shadow
15178 * directly under the touch point. If no View is provided (the constructor with no parameters
15179 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15180 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15181 * default is an invisible drag shadow.
15182 * <p>
15183 * You are not required to use the View you provide to the constructor as the basis of the
15184 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15185 * anything you want as the drag shadow.
15186 * </p>
15187 * <p>
15188 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15189 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15190 * size and position of the drag shadow. It uses this data to construct a
15191 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15192 * so that your application can draw the shadow image in the Canvas.
15193 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015194 *
15195 * <div class="special reference">
15196 * <h3>Developer Guides</h3>
15197 * <p>For a guide to implementing drag and drop features, read the
15198 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15199 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015200 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015201 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015202 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015203
15204 /**
Joe Malin32736f02011-01-19 16:14:20 -080015205 * Constructs a shadow image builder based on a View. By default, the resulting drag
15206 * shadow will have the same appearance and dimensions as the View, with the touch point
15207 * over the center of the View.
15208 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015209 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015210 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015211 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015212 }
15213
Christopher Tate17ed60c2011-01-18 12:50:26 -080015214 /**
15215 * Construct a shadow builder object with no associated View. This
15216 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15217 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15218 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015219 * reference to any View object. If they are not overridden, then the result is an
15220 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015221 */
15222 public DragShadowBuilder() {
15223 mView = new WeakReference<View>(null);
15224 }
15225
15226 /**
15227 * Returns the View object that had been passed to the
15228 * {@link #View.DragShadowBuilder(View)}
15229 * constructor. If that View parameter was {@code null} or if the
15230 * {@link #View.DragShadowBuilder()}
15231 * constructor was used to instantiate the builder object, this method will return
15232 * null.
15233 *
15234 * @return The View object associate with this builder object.
15235 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015236 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015237 final public View getView() {
15238 return mView.get();
15239 }
15240
Christopher Tate2c095f32010-10-04 14:13:40 -070015241 /**
Joe Malin32736f02011-01-19 16:14:20 -080015242 * Provides the metrics for the shadow image. These include the dimensions of
15243 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015244 * be centered under the touch location while dragging.
15245 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015246 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015247 * same as the dimensions of the View itself and centers the shadow under
15248 * the touch point.
15249 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015250 *
Joe Malin32736f02011-01-19 16:14:20 -080015251 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15252 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15253 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15254 * image.
15255 *
15256 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15257 * shadow image that should be underneath the touch point during the drag and drop
15258 * operation. Your application must set {@link android.graphics.Point#x} to the
15259 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015260 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015261 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015262 final View view = mView.get();
15263 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015264 shadowSize.set(view.getWidth(), view.getHeight());
15265 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015266 } else {
15267 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15268 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015269 }
15270
15271 /**
Joe Malin32736f02011-01-19 16:14:20 -080015272 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15273 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015274 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015275 *
Joe Malin32736f02011-01-19 16:14:20 -080015276 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015277 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015278 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015279 final View view = mView.get();
15280 if (view != null) {
15281 view.draw(canvas);
15282 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015283 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015284 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015285 }
15286 }
15287
15288 /**
Joe Malin32736f02011-01-19 16:14:20 -080015289 * Starts a drag and drop operation. When your application calls this method, it passes a
15290 * {@link android.view.View.DragShadowBuilder} object to the system. The
15291 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15292 * to get metrics for the drag shadow, and then calls the object's
15293 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15294 * <p>
15295 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15296 * drag events to all the View objects in your application that are currently visible. It does
15297 * this either by calling the View object's drag listener (an implementation of
15298 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15299 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15300 * Both are passed a {@link android.view.DragEvent} object that has a
15301 * {@link android.view.DragEvent#getAction()} value of
15302 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15303 * </p>
15304 * <p>
15305 * Your application can invoke startDrag() on any attached View object. The View object does not
15306 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15307 * be related to the View the user selected for dragging.
15308 * </p>
15309 * @param data A {@link android.content.ClipData} object pointing to the data to be
15310 * transferred by the drag and drop operation.
15311 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15312 * drag shadow.
15313 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15314 * drop operation. This Object is put into every DragEvent object sent by the system during the
15315 * current drag.
15316 * <p>
15317 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15318 * to the target Views. For example, it can contain flags that differentiate between a
15319 * a copy operation and a move operation.
15320 * </p>
15321 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15322 * so the parameter should be set to 0.
15323 * @return {@code true} if the method completes successfully, or
15324 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15325 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015326 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015327 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015328 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015329 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015330 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015331 }
15332 boolean okay = false;
15333
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015334 Point shadowSize = new Point();
15335 Point shadowTouchPoint = new Point();
15336 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015337
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015338 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15339 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15340 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015341 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015342
Chris Tatea32dcf72010-10-14 12:13:50 -070015343 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015344 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15345 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015346 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015347 Surface surface = new Surface();
15348 try {
15349 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015350 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015351 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015352 + " surface=" + surface);
15353 if (token != null) {
15354 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015355 try {
Chris Tate6b391282010-10-14 15:48:59 -070015356 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015357 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015358 } finally {
15359 surface.unlockCanvasAndPost(canvas);
15360 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015361
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015362 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015363
15364 // Cache the local state object for delivery with DragEvents
15365 root.setLocalDragState(myLocalState);
15366
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015367 // repurpose 'shadowSize' for the last touch point
15368 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015369
Christopher Tatea53146c2010-09-07 11:57:52 -070015370 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015371 shadowSize.x, shadowSize.y,
15372 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015373 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015374
15375 // Off and running! Release our local surface instance; the drag
15376 // shadow surface is now managed by the system process.
15377 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015378 }
15379 } catch (Exception e) {
15380 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15381 surface.destroy();
15382 }
15383
15384 return okay;
15385 }
15386
Christopher Tatea53146c2010-09-07 11:57:52 -070015387 /**
Joe Malin32736f02011-01-19 16:14:20 -080015388 * Handles drag events sent by the system following a call to
15389 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15390 *<p>
15391 * When the system calls this method, it passes a
15392 * {@link android.view.DragEvent} object. A call to
15393 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15394 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15395 * operation.
15396 * @param event The {@link android.view.DragEvent} sent by the system.
15397 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15398 * in DragEvent, indicating the type of drag event represented by this object.
15399 * @return {@code true} if the method was successful, otherwise {@code false}.
15400 * <p>
15401 * The method should return {@code true} in response to an action type of
15402 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15403 * operation.
15404 * </p>
15405 * <p>
15406 * The method should also return {@code true} in response to an action type of
15407 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15408 * {@code false} if it didn't.
15409 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015410 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015411 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015412 return false;
15413 }
15414
15415 /**
Joe Malin32736f02011-01-19 16:14:20 -080015416 * Detects if this View is enabled and has a drag event listener.
15417 * If both are true, then it calls the drag event listener with the
15418 * {@link android.view.DragEvent} it received. If the drag event listener returns
15419 * {@code true}, then dispatchDragEvent() returns {@code true}.
15420 * <p>
15421 * For all other cases, the method calls the
15422 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15423 * method and returns its result.
15424 * </p>
15425 * <p>
15426 * This ensures that a drag event is always consumed, even if the View does not have a drag
15427 * event listener. However, if the View has a listener and the listener returns true, then
15428 * onDragEvent() is not called.
15429 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015430 */
15431 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015432 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015433 ListenerInfo li = mListenerInfo;
15434 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15435 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015436 return true;
15437 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015438 return onDragEvent(event);
15439 }
15440
Christopher Tate3d4bf172011-03-28 16:16:46 -070015441 boolean canAcceptDrag() {
15442 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15443 }
15444
Christopher Tatea53146c2010-09-07 11:57:52 -070015445 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015446 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15447 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015448 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015449 */
15450 public void onCloseSystemDialogs(String reason) {
15451 }
Joe Malin32736f02011-01-19 16:14:20 -080015452
Dianne Hackbornffa42482009-09-23 22:20:11 -070015453 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015454 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015455 * update a Region being computed for
15456 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015457 * that any non-transparent parts of the Drawable are removed from the
15458 * given transparent region.
15459 *
15460 * @param dr The Drawable whose transparency is to be applied to the region.
15461 * @param region A Region holding the current transparency information,
15462 * where any parts of the region that are set are considered to be
15463 * transparent. On return, this region will be modified to have the
15464 * transparency information reduced by the corresponding parts of the
15465 * Drawable that are not transparent.
15466 * {@hide}
15467 */
15468 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15469 if (DBG) {
15470 Log.i("View", "Getting transparent region for: " + this);
15471 }
15472 final Region r = dr.getTransparentRegion();
15473 final Rect db = dr.getBounds();
15474 final AttachInfo attachInfo = mAttachInfo;
15475 if (r != null && attachInfo != null) {
15476 final int w = getRight()-getLeft();
15477 final int h = getBottom()-getTop();
15478 if (db.left > 0) {
15479 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15480 r.op(0, 0, db.left, h, Region.Op.UNION);
15481 }
15482 if (db.right < w) {
15483 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15484 r.op(db.right, 0, w, h, Region.Op.UNION);
15485 }
15486 if (db.top > 0) {
15487 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15488 r.op(0, 0, w, db.top, Region.Op.UNION);
15489 }
15490 if (db.bottom < h) {
15491 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15492 r.op(0, db.bottom, w, h, Region.Op.UNION);
15493 }
15494 final int[] location = attachInfo.mTransparentLocation;
15495 getLocationInWindow(location);
15496 r.translate(location[0], location[1]);
15497 region.op(r, Region.Op.INTERSECT);
15498 } else {
15499 region.op(db, Region.Op.DIFFERENCE);
15500 }
15501 }
15502
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015503 private void checkForLongClick(int delayOffset) {
15504 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
15505 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015506
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015507 if (mPendingCheckForLongPress == null) {
15508 mPendingCheckForLongPress = new CheckForLongPress();
15509 }
15510 mPendingCheckForLongPress.rememberWindowAttachCount();
15511 postDelayed(mPendingCheckForLongPress,
15512 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015514 }
15515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015516 /**
15517 * Inflate a view from an XML resource. This convenience method wraps the {@link
15518 * LayoutInflater} class, which provides a full range of options for view inflation.
15519 *
15520 * @param context The Context object for your activity or application.
15521 * @param resource The resource ID to inflate
15522 * @param root A view group that will be the parent. Used to properly inflate the
15523 * layout_* parameters.
15524 * @see LayoutInflater
15525 */
15526 public static View inflate(Context context, int resource, ViewGroup root) {
15527 LayoutInflater factory = LayoutInflater.from(context);
15528 return factory.inflate(resource, root);
15529 }
Romain Guy33e72ae2010-07-17 12:40:29 -070015530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015531 /**
Adam Powell637d3372010-08-25 14:37:03 -070015532 * Scroll the view with standard behavior for scrolling beyond the normal
15533 * content boundaries. Views that call this method should override
15534 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
15535 * results of an over-scroll operation.
15536 *
15537 * Views can use this method to handle any touch or fling-based scrolling.
15538 *
15539 * @param deltaX Change in X in pixels
15540 * @param deltaY Change in Y in pixels
15541 * @param scrollX Current X scroll value in pixels before applying deltaX
15542 * @param scrollY Current Y scroll value in pixels before applying deltaY
15543 * @param scrollRangeX Maximum content scroll range along the X axis
15544 * @param scrollRangeY Maximum content scroll range along the Y axis
15545 * @param maxOverScrollX Number of pixels to overscroll by in either direction
15546 * along the X axis.
15547 * @param maxOverScrollY Number of pixels to overscroll by in either direction
15548 * along the Y axis.
15549 * @param isTouchEvent true if this scroll operation is the result of a touch event.
15550 * @return true if scrolling was clamped to an over-scroll boundary along either
15551 * axis, false otherwise.
15552 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070015553 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070015554 protected boolean overScrollBy(int deltaX, int deltaY,
15555 int scrollX, int scrollY,
15556 int scrollRangeX, int scrollRangeY,
15557 int maxOverScrollX, int maxOverScrollY,
15558 boolean isTouchEvent) {
15559 final int overScrollMode = mOverScrollMode;
15560 final boolean canScrollHorizontal =
15561 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
15562 final boolean canScrollVertical =
15563 computeVerticalScrollRange() > computeVerticalScrollExtent();
15564 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
15565 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
15566 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
15567 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15568
15569 int newScrollX = scrollX + deltaX;
15570 if (!overScrollHorizontal) {
15571 maxOverScrollX = 0;
15572 }
15573
15574 int newScrollY = scrollY + deltaY;
15575 if (!overScrollVertical) {
15576 maxOverScrollY = 0;
15577 }
15578
15579 // Clamp values if at the limits and record
15580 final int left = -maxOverScrollX;
15581 final int right = maxOverScrollX + scrollRangeX;
15582 final int top = -maxOverScrollY;
15583 final int bottom = maxOverScrollY + scrollRangeY;
15584
15585 boolean clampedX = false;
15586 if (newScrollX > right) {
15587 newScrollX = right;
15588 clampedX = true;
15589 } else if (newScrollX < left) {
15590 newScrollX = left;
15591 clampedX = true;
15592 }
15593
15594 boolean clampedY = false;
15595 if (newScrollY > bottom) {
15596 newScrollY = bottom;
15597 clampedY = true;
15598 } else if (newScrollY < top) {
15599 newScrollY = top;
15600 clampedY = true;
15601 }
15602
15603 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
15604
15605 return clampedX || clampedY;
15606 }
15607
15608 /**
15609 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
15610 * respond to the results of an over-scroll operation.
15611 *
15612 * @param scrollX New X scroll value in pixels
15613 * @param scrollY New Y scroll value in pixels
15614 * @param clampedX True if scrollX was clamped to an over-scroll boundary
15615 * @param clampedY True if scrollY was clamped to an over-scroll boundary
15616 */
15617 protected void onOverScrolled(int scrollX, int scrollY,
15618 boolean clampedX, boolean clampedY) {
15619 // Intentionally empty.
15620 }
15621
15622 /**
15623 * Returns the over-scroll mode for this view. The result will be
15624 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15625 * (allow over-scrolling only if the view content is larger than the container),
15626 * or {@link #OVER_SCROLL_NEVER}.
15627 *
15628 * @return This view's over-scroll mode.
15629 */
15630 public int getOverScrollMode() {
15631 return mOverScrollMode;
15632 }
15633
15634 /**
15635 * Set the over-scroll mode for this view. Valid over-scroll modes are
15636 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15637 * (allow over-scrolling only if the view content is larger than the container),
15638 * or {@link #OVER_SCROLL_NEVER}.
15639 *
15640 * Setting the over-scroll mode of a view will have an effect only if the
15641 * view is capable of scrolling.
15642 *
15643 * @param overScrollMode The new over-scroll mode for this view.
15644 */
15645 public void setOverScrollMode(int overScrollMode) {
15646 if (overScrollMode != OVER_SCROLL_ALWAYS &&
15647 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
15648 overScrollMode != OVER_SCROLL_NEVER) {
15649 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
15650 }
15651 mOverScrollMode = overScrollMode;
15652 }
15653
15654 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015655 * Gets a scale factor that determines the distance the view should scroll
15656 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
15657 * @return The vertical scroll scale factor.
15658 * @hide
15659 */
15660 protected float getVerticalScrollFactor() {
15661 if (mVerticalScrollFactor == 0) {
15662 TypedValue outValue = new TypedValue();
15663 if (!mContext.getTheme().resolveAttribute(
15664 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
15665 throw new IllegalStateException(
15666 "Expected theme to define listPreferredItemHeight.");
15667 }
15668 mVerticalScrollFactor = outValue.getDimension(
15669 mContext.getResources().getDisplayMetrics());
15670 }
15671 return mVerticalScrollFactor;
15672 }
15673
15674 /**
15675 * Gets a scale factor that determines the distance the view should scroll
15676 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
15677 * @return The horizontal scroll scale factor.
15678 * @hide
15679 */
15680 protected float getHorizontalScrollFactor() {
15681 // TODO: Should use something else.
15682 return getVerticalScrollFactor();
15683 }
15684
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015685 /**
15686 * Return the value specifying the text direction or policy that was set with
15687 * {@link #setTextDirection(int)}.
15688 *
15689 * @return the defined text direction. It can be one of:
15690 *
15691 * {@link #TEXT_DIRECTION_INHERIT},
15692 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15693 * {@link #TEXT_DIRECTION_ANY_RTL},
15694 * {@link #TEXT_DIRECTION_LTR},
15695 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015696 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015697 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015698 @ViewDebug.ExportedProperty(category = "text", mapping = {
15699 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
15700 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
15701 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
15702 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
15703 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
15704 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
15705 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015706 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015707 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015708 }
15709
15710 /**
15711 * Set the text direction.
15712 *
15713 * @param textDirection the direction to set. Should be one of:
15714 *
15715 * {@link #TEXT_DIRECTION_INHERIT},
15716 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15717 * {@link #TEXT_DIRECTION_ANY_RTL},
15718 * {@link #TEXT_DIRECTION_LTR},
15719 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015720 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015721 */
15722 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015723 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015724 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015725 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015726 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015727 // Set the new text direction
15728 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015729 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015730 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015731 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015732 }
15733 }
15734
15735 /**
15736 * Return the resolved text direction.
15737 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015738 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
15739 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
15740 * up the parent chain of the view. if there is no parent, then it will return the default
15741 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
15742 *
15743 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015744 *
Doug Feltcb3791202011-07-07 11:57:48 -070015745 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15746 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015747 * {@link #TEXT_DIRECTION_LTR},
15748 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015749 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015750 */
15751 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070015752 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015753 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015754 resolveTextDirection();
15755 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015756 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015757 }
15758
15759 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015760 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
15761 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015762 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015763 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015764 // Reset any previous text direction resolution
15765 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
15766
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015767 if (hasRtlSupport()) {
15768 // Set resolved text direction flag depending on text direction flag
15769 final int textDirection = getTextDirection();
15770 switch(textDirection) {
15771 case TEXT_DIRECTION_INHERIT:
15772 if (canResolveTextDirection()) {
15773 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015774
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015775 // Set current resolved direction to the same value as the parent's one
15776 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
15777 switch (parentResolvedDirection) {
15778 case TEXT_DIRECTION_FIRST_STRONG:
15779 case TEXT_DIRECTION_ANY_RTL:
15780 case TEXT_DIRECTION_LTR:
15781 case TEXT_DIRECTION_RTL:
15782 case TEXT_DIRECTION_LOCALE:
15783 mPrivateFlags2 |=
15784 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15785 break;
15786 default:
15787 // Default resolved direction is "first strong" heuristic
15788 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
15789 }
15790 } else {
15791 // We cannot do the resolution if there is no parent, so use the default one
15792 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015793 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015794 break;
15795 case TEXT_DIRECTION_FIRST_STRONG:
15796 case TEXT_DIRECTION_ANY_RTL:
15797 case TEXT_DIRECTION_LTR:
15798 case TEXT_DIRECTION_RTL:
15799 case TEXT_DIRECTION_LOCALE:
15800 // Resolved direction is the same as text direction
15801 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15802 break;
15803 default:
15804 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015805 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015806 }
15807 } else {
15808 // Default resolved direction is "first strong" heuristic
15809 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015810 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015811
15812 // Set to resolved
15813 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015814 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015815 }
15816
15817 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015818 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015819 * resolution should override this method.
15820 *
15821 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015822 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015823 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015824 }
15825
15826 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015827 * Check if text direction resolution can be done.
15828 *
15829 * @return true if text direction resolution can be done otherwise return false.
15830 */
15831 public boolean canResolveTextDirection() {
15832 switch (getTextDirection()) {
15833 case TEXT_DIRECTION_INHERIT:
15834 return (mParent != null) && (mParent instanceof ViewGroup);
15835 default:
15836 return true;
15837 }
15838 }
15839
15840 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015841 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015842 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015843 * reset is done.
15844 */
15845 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015846 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015847 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015848 }
15849
15850 /**
15851 * Called when text direction is reset. Subclasses that care about text direction reset should
15852 * override this method and do a reset of the text direction of their children. The default
15853 * implementation does nothing.
15854 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015855 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015856 }
15857
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015858 /**
15859 * Return the value specifying the text alignment or policy that was set with
15860 * {@link #setTextAlignment(int)}.
15861 *
15862 * @return the defined text alignment. It can be one of:
15863 *
15864 * {@link #TEXT_ALIGNMENT_INHERIT},
15865 * {@link #TEXT_ALIGNMENT_GRAVITY},
15866 * {@link #TEXT_ALIGNMENT_CENTER},
15867 * {@link #TEXT_ALIGNMENT_TEXT_START},
15868 * {@link #TEXT_ALIGNMENT_TEXT_END},
15869 * {@link #TEXT_ALIGNMENT_VIEW_START},
15870 * {@link #TEXT_ALIGNMENT_VIEW_END}
15871 */
15872 @ViewDebug.ExportedProperty(category = "text", mapping = {
15873 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15874 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15875 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15876 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15877 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15878 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15879 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15880 })
15881 public int getTextAlignment() {
15882 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
15883 }
15884
15885 /**
15886 * Set the text alignment.
15887 *
15888 * @param textAlignment The text alignment to set. Should be one of
15889 *
15890 * {@link #TEXT_ALIGNMENT_INHERIT},
15891 * {@link #TEXT_ALIGNMENT_GRAVITY},
15892 * {@link #TEXT_ALIGNMENT_CENTER},
15893 * {@link #TEXT_ALIGNMENT_TEXT_START},
15894 * {@link #TEXT_ALIGNMENT_TEXT_END},
15895 * {@link #TEXT_ALIGNMENT_VIEW_START},
15896 * {@link #TEXT_ALIGNMENT_VIEW_END}
15897 *
15898 * @attr ref android.R.styleable#View_textAlignment
15899 */
15900 public void setTextAlignment(int textAlignment) {
15901 if (textAlignment != getTextAlignment()) {
15902 // Reset the current and resolved text alignment
15903 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
15904 resetResolvedTextAlignment();
15905 // Set the new text alignment
15906 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
15907 // Refresh
15908 requestLayout();
15909 invalidate(true);
15910 }
15911 }
15912
15913 /**
15914 * Return the resolved text alignment.
15915 *
15916 * The resolved text alignment. This needs resolution if the value is
15917 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
15918 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
15919 *
15920 * @return the resolved text alignment. Returns one of:
15921 *
15922 * {@link #TEXT_ALIGNMENT_GRAVITY},
15923 * {@link #TEXT_ALIGNMENT_CENTER},
15924 * {@link #TEXT_ALIGNMENT_TEXT_START},
15925 * {@link #TEXT_ALIGNMENT_TEXT_END},
15926 * {@link #TEXT_ALIGNMENT_VIEW_START},
15927 * {@link #TEXT_ALIGNMENT_VIEW_END}
15928 */
15929 @ViewDebug.ExportedProperty(category = "text", mapping = {
15930 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15931 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15932 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15933 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15934 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15935 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15936 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15937 })
15938 public int getResolvedTextAlignment() {
15939 // If text alignment is not resolved, then resolve it
15940 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
15941 resolveTextAlignment();
15942 }
15943 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
15944 }
15945
15946 /**
15947 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
15948 * resolution is done.
15949 */
15950 public void resolveTextAlignment() {
15951 // Reset any previous text alignment resolution
15952 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
15953
15954 if (hasRtlSupport()) {
15955 // Set resolved text alignment flag depending on text alignment flag
15956 final int textAlignment = getTextAlignment();
15957 switch (textAlignment) {
15958 case TEXT_ALIGNMENT_INHERIT:
15959 // Check if we can resolve the text alignment
15960 if (canResolveLayoutDirection() && mParent instanceof View) {
15961 View view = (View) mParent;
15962
15963 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
15964 switch (parentResolvedTextAlignment) {
15965 case TEXT_ALIGNMENT_GRAVITY:
15966 case TEXT_ALIGNMENT_TEXT_START:
15967 case TEXT_ALIGNMENT_TEXT_END:
15968 case TEXT_ALIGNMENT_CENTER:
15969 case TEXT_ALIGNMENT_VIEW_START:
15970 case TEXT_ALIGNMENT_VIEW_END:
15971 // Resolved text alignment is the same as the parent resolved
15972 // text alignment
15973 mPrivateFlags2 |=
15974 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
15975 break;
15976 default:
15977 // Use default resolved text alignment
15978 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15979 }
15980 }
15981 else {
15982 // We cannot do the resolution if there is no parent so use the default
15983 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15984 }
15985 break;
15986 case TEXT_ALIGNMENT_GRAVITY:
15987 case TEXT_ALIGNMENT_TEXT_START:
15988 case TEXT_ALIGNMENT_TEXT_END:
15989 case TEXT_ALIGNMENT_CENTER:
15990 case TEXT_ALIGNMENT_VIEW_START:
15991 case TEXT_ALIGNMENT_VIEW_END:
15992 // Resolved text alignment is the same as text alignment
15993 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
15994 break;
15995 default:
15996 // Use default resolved text alignment
15997 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15998 }
15999 } else {
16000 // Use default resolved text alignment
16001 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16002 }
16003
16004 // Set the resolved
16005 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16006 onResolvedTextAlignmentChanged();
16007 }
16008
16009 /**
16010 * Check if text alignment resolution can be done.
16011 *
16012 * @return true if text alignment resolution can be done otherwise return false.
16013 */
16014 public boolean canResolveTextAlignment() {
16015 switch (getTextAlignment()) {
16016 case TEXT_DIRECTION_INHERIT:
16017 return (mParent != null);
16018 default:
16019 return true;
16020 }
16021 }
16022
16023 /**
16024 * Called when text alignment has been resolved. Subclasses that care about text alignment
16025 * resolution should override this method.
16026 *
16027 * The default implementation does nothing.
16028 */
16029 public void onResolvedTextAlignmentChanged() {
16030 }
16031
16032 /**
16033 * Reset resolved text alignment. Text alignment can be resolved with a call to
16034 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16035 * reset is done.
16036 */
16037 public void resetResolvedTextAlignment() {
16038 // Reset any previous text alignment resolution
16039 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16040 onResolvedTextAlignmentReset();
16041 }
16042
16043 /**
16044 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16045 * override this method and do a reset of the text alignment of their children. The default
16046 * implementation does nothing.
16047 */
16048 public void onResolvedTextAlignmentReset() {
16049 }
16050
Chet Haaseb39f0512011-05-24 14:36:40 -070016051 //
16052 // Properties
16053 //
16054 /**
16055 * A Property wrapper around the <code>alpha</code> functionality handled by the
16056 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16057 */
Chet Haased47f1532011-12-16 11:18:52 -080016058 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016059 @Override
16060 public void setValue(View object, float value) {
16061 object.setAlpha(value);
16062 }
16063
16064 @Override
16065 public Float get(View object) {
16066 return object.getAlpha();
16067 }
16068 };
16069
16070 /**
16071 * A Property wrapper around the <code>translationX</code> functionality handled by the
16072 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16073 */
Chet Haased47f1532011-12-16 11:18:52 -080016074 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016075 @Override
16076 public void setValue(View object, float value) {
16077 object.setTranslationX(value);
16078 }
16079
16080 @Override
16081 public Float get(View object) {
16082 return object.getTranslationX();
16083 }
16084 };
16085
16086 /**
16087 * A Property wrapper around the <code>translationY</code> functionality handled by the
16088 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16089 */
Chet Haased47f1532011-12-16 11:18:52 -080016090 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016091 @Override
16092 public void setValue(View object, float value) {
16093 object.setTranslationY(value);
16094 }
16095
16096 @Override
16097 public Float get(View object) {
16098 return object.getTranslationY();
16099 }
16100 };
16101
16102 /**
16103 * A Property wrapper around the <code>x</code> functionality handled by the
16104 * {@link View#setX(float)} and {@link View#getX()} methods.
16105 */
Chet Haased47f1532011-12-16 11:18:52 -080016106 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016107 @Override
16108 public void setValue(View object, float value) {
16109 object.setX(value);
16110 }
16111
16112 @Override
16113 public Float get(View object) {
16114 return object.getX();
16115 }
16116 };
16117
16118 /**
16119 * A Property wrapper around the <code>y</code> functionality handled by the
16120 * {@link View#setY(float)} and {@link View#getY()} methods.
16121 */
Chet Haased47f1532011-12-16 11:18:52 -080016122 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016123 @Override
16124 public void setValue(View object, float value) {
16125 object.setY(value);
16126 }
16127
16128 @Override
16129 public Float get(View object) {
16130 return object.getY();
16131 }
16132 };
16133
16134 /**
16135 * A Property wrapper around the <code>rotation</code> functionality handled by the
16136 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16137 */
Chet Haased47f1532011-12-16 11:18:52 -080016138 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016139 @Override
16140 public void setValue(View object, float value) {
16141 object.setRotation(value);
16142 }
16143
16144 @Override
16145 public Float get(View object) {
16146 return object.getRotation();
16147 }
16148 };
16149
16150 /**
16151 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16152 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16153 */
Chet Haased47f1532011-12-16 11:18:52 -080016154 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016155 @Override
16156 public void setValue(View object, float value) {
16157 object.setRotationX(value);
16158 }
16159
16160 @Override
16161 public Float get(View object) {
16162 return object.getRotationX();
16163 }
16164 };
16165
16166 /**
16167 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16168 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16169 */
Chet Haased47f1532011-12-16 11:18:52 -080016170 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016171 @Override
16172 public void setValue(View object, float value) {
16173 object.setRotationY(value);
16174 }
16175
16176 @Override
16177 public Float get(View object) {
16178 return object.getRotationY();
16179 }
16180 };
16181
16182 /**
16183 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16184 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16185 */
Chet Haased47f1532011-12-16 11:18:52 -080016186 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016187 @Override
16188 public void setValue(View object, float value) {
16189 object.setScaleX(value);
16190 }
16191
16192 @Override
16193 public Float get(View object) {
16194 return object.getScaleX();
16195 }
16196 };
16197
16198 /**
16199 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16200 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16201 */
Chet Haased47f1532011-12-16 11:18:52 -080016202 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016203 @Override
16204 public void setValue(View object, float value) {
16205 object.setScaleY(value);
16206 }
16207
16208 @Override
16209 public Float get(View object) {
16210 return object.getScaleY();
16211 }
16212 };
16213
Jeff Brown33bbfd22011-02-24 20:55:35 -080016214 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016215 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16216 * Each MeasureSpec represents a requirement for either the width or the height.
16217 * A MeasureSpec is comprised of a size and a mode. There are three possible
16218 * modes:
16219 * <dl>
16220 * <dt>UNSPECIFIED</dt>
16221 * <dd>
16222 * The parent has not imposed any constraint on the child. It can be whatever size
16223 * it wants.
16224 * </dd>
16225 *
16226 * <dt>EXACTLY</dt>
16227 * <dd>
16228 * The parent has determined an exact size for the child. The child is going to be
16229 * given those bounds regardless of how big it wants to be.
16230 * </dd>
16231 *
16232 * <dt>AT_MOST</dt>
16233 * <dd>
16234 * The child can be as large as it wants up to the specified size.
16235 * </dd>
16236 * </dl>
16237 *
16238 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16239 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16240 */
16241 public static class MeasureSpec {
16242 private static final int MODE_SHIFT = 30;
16243 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16244
16245 /**
16246 * Measure specification mode: The parent has not imposed any constraint
16247 * on the child. It can be whatever size it wants.
16248 */
16249 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16250
16251 /**
16252 * Measure specification mode: The parent has determined an exact size
16253 * for the child. The child is going to be given those bounds regardless
16254 * of how big it wants to be.
16255 */
16256 public static final int EXACTLY = 1 << MODE_SHIFT;
16257
16258 /**
16259 * Measure specification mode: The child can be as large as it wants up
16260 * to the specified size.
16261 */
16262 public static final int AT_MOST = 2 << MODE_SHIFT;
16263
16264 /**
16265 * Creates a measure specification based on the supplied size and mode.
16266 *
16267 * The mode must always be one of the following:
16268 * <ul>
16269 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16270 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16271 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16272 * </ul>
16273 *
16274 * @param size the size of the measure specification
16275 * @param mode the mode of the measure specification
16276 * @return the measure specification based on size and mode
16277 */
16278 public static int makeMeasureSpec(int size, int mode) {
16279 return size + mode;
16280 }
16281
16282 /**
16283 * Extracts the mode from the supplied measure specification.
16284 *
16285 * @param measureSpec the measure specification to extract the mode from
16286 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16287 * {@link android.view.View.MeasureSpec#AT_MOST} or
16288 * {@link android.view.View.MeasureSpec#EXACTLY}
16289 */
16290 public static int getMode(int measureSpec) {
16291 return (measureSpec & MODE_MASK);
16292 }
16293
16294 /**
16295 * Extracts the size from the supplied measure specification.
16296 *
16297 * @param measureSpec the measure specification to extract the size from
16298 * @return the size in pixels defined in the supplied measure specification
16299 */
16300 public static int getSize(int measureSpec) {
16301 return (measureSpec & ~MODE_MASK);
16302 }
16303
16304 /**
16305 * Returns a String representation of the specified measure
16306 * specification.
16307 *
16308 * @param measureSpec the measure specification to convert to a String
16309 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16310 */
16311 public static String toString(int measureSpec) {
16312 int mode = getMode(measureSpec);
16313 int size = getSize(measureSpec);
16314
16315 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16316
16317 if (mode == UNSPECIFIED)
16318 sb.append("UNSPECIFIED ");
16319 else if (mode == EXACTLY)
16320 sb.append("EXACTLY ");
16321 else if (mode == AT_MOST)
16322 sb.append("AT_MOST ");
16323 else
16324 sb.append(mode).append(" ");
16325
16326 sb.append(size);
16327 return sb.toString();
16328 }
16329 }
16330
16331 class CheckForLongPress implements Runnable {
16332
16333 private int mOriginalWindowAttachCount;
16334
16335 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016336 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016337 && mOriginalWindowAttachCount == mWindowAttachCount) {
16338 if (performLongClick()) {
16339 mHasPerformedLongPress = true;
16340 }
16341 }
16342 }
16343
16344 public void rememberWindowAttachCount() {
16345 mOriginalWindowAttachCount = mWindowAttachCount;
16346 }
16347 }
Joe Malin32736f02011-01-19 16:14:20 -080016348
Adam Powelle14579b2009-12-16 18:39:52 -080016349 private final class CheckForTap implements Runnable {
16350 public void run() {
16351 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016352 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016353 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016354 }
16355 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016356
Adam Powella35d7682010-03-12 14:48:13 -080016357 private final class PerformClick implements Runnable {
16358 public void run() {
16359 performClick();
16360 }
16361 }
16362
Dianne Hackborn63042d62011-01-26 18:56:29 -080016363 /** @hide */
16364 public void hackTurnOffWindowResizeAnim(boolean off) {
16365 mAttachInfo.mTurnOffWindowResizeAnim = off;
16366 }
Joe Malin32736f02011-01-19 16:14:20 -080016367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016368 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016369 * This method returns a ViewPropertyAnimator object, which can be used to animate
16370 * specific properties on this View.
16371 *
16372 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16373 */
16374 public ViewPropertyAnimator animate() {
16375 if (mAnimator == null) {
16376 mAnimator = new ViewPropertyAnimator(this);
16377 }
16378 return mAnimator;
16379 }
16380
16381 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016382 * Interface definition for a callback to be invoked when a key event is
16383 * dispatched to this view. The callback will be invoked before the key
16384 * event is given to the view.
16385 */
16386 public interface OnKeyListener {
16387 /**
16388 * Called when a key is dispatched to a view. This allows listeners to
16389 * get a chance to respond before the target view.
16390 *
16391 * @param v The view the key has been dispatched to.
16392 * @param keyCode The code for the physical key that was pressed
16393 * @param event The KeyEvent object containing full information about
16394 * the event.
16395 * @return True if the listener has consumed the event, false otherwise.
16396 */
16397 boolean onKey(View v, int keyCode, KeyEvent event);
16398 }
16399
16400 /**
16401 * Interface definition for a callback to be invoked when a touch event is
16402 * dispatched to this view. The callback will be invoked before the touch
16403 * event is given to the view.
16404 */
16405 public interface OnTouchListener {
16406 /**
16407 * Called when a touch event is dispatched to a view. This allows listeners to
16408 * get a chance to respond before the target view.
16409 *
16410 * @param v The view the touch event has been dispatched to.
16411 * @param event The MotionEvent object containing full information about
16412 * the event.
16413 * @return True if the listener has consumed the event, false otherwise.
16414 */
16415 boolean onTouch(View v, MotionEvent event);
16416 }
16417
16418 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016419 * Interface definition for a callback to be invoked when a hover event is
16420 * dispatched to this view. The callback will be invoked before the hover
16421 * event is given to the view.
16422 */
16423 public interface OnHoverListener {
16424 /**
16425 * Called when a hover event is dispatched to a view. This allows listeners to
16426 * get a chance to respond before the target view.
16427 *
16428 * @param v The view the hover event has been dispatched to.
16429 * @param event The MotionEvent object containing full information about
16430 * the event.
16431 * @return True if the listener has consumed the event, false otherwise.
16432 */
16433 boolean onHover(View v, MotionEvent event);
16434 }
16435
16436 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016437 * Interface definition for a callback to be invoked when a generic motion event is
16438 * dispatched to this view. The callback will be invoked before the generic motion
16439 * event is given to the view.
16440 */
16441 public interface OnGenericMotionListener {
16442 /**
16443 * Called when a generic motion event is dispatched to a view. This allows listeners to
16444 * get a chance to respond before the target view.
16445 *
16446 * @param v The view the generic motion event has been dispatched to.
16447 * @param event The MotionEvent object containing full information about
16448 * the event.
16449 * @return True if the listener has consumed the event, false otherwise.
16450 */
16451 boolean onGenericMotion(View v, MotionEvent event);
16452 }
16453
16454 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016455 * Interface definition for a callback to be invoked when a view has been clicked and held.
16456 */
16457 public interface OnLongClickListener {
16458 /**
16459 * Called when a view has been clicked and held.
16460 *
16461 * @param v The view that was clicked and held.
16462 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016463 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016464 */
16465 boolean onLongClick(View v);
16466 }
16467
16468 /**
Chris Tate32affef2010-10-18 15:29:21 -070016469 * Interface definition for a callback to be invoked when a drag is being dispatched
16470 * to this view. The callback will be invoked before the hosting view's own
16471 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16472 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016473 *
16474 * <div class="special reference">
16475 * <h3>Developer Guides</h3>
16476 * <p>For a guide to implementing drag and drop features, read the
16477 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16478 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016479 */
16480 public interface OnDragListener {
16481 /**
16482 * Called when a drag event is dispatched to a view. This allows listeners
16483 * to get a chance to override base View behavior.
16484 *
Joe Malin32736f02011-01-19 16:14:20 -080016485 * @param v The View that received the drag event.
16486 * @param event The {@link android.view.DragEvent} object for the drag event.
16487 * @return {@code true} if the drag event was handled successfully, or {@code false}
16488 * if the drag event was not handled. Note that {@code false} will trigger the View
16489 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070016490 */
16491 boolean onDrag(View v, DragEvent event);
16492 }
16493
16494 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016495 * Interface definition for a callback to be invoked when the focus state of
16496 * a view changed.
16497 */
16498 public interface OnFocusChangeListener {
16499 /**
16500 * Called when the focus state of a view has changed.
16501 *
16502 * @param v The view whose state has changed.
16503 * @param hasFocus The new focus state of v.
16504 */
16505 void onFocusChange(View v, boolean hasFocus);
16506 }
16507
16508 /**
16509 * Interface definition for a callback to be invoked when a view is clicked.
16510 */
16511 public interface OnClickListener {
16512 /**
16513 * Called when a view has been clicked.
16514 *
16515 * @param v The view that was clicked.
16516 */
16517 void onClick(View v);
16518 }
16519
16520 /**
16521 * Interface definition for a callback to be invoked when the context menu
16522 * for this view is being built.
16523 */
16524 public interface OnCreateContextMenuListener {
16525 /**
16526 * Called when the context menu for this view is being built. It is not
16527 * safe to hold onto the menu after this method returns.
16528 *
16529 * @param menu The context menu that is being built
16530 * @param v The view for which the context menu is being built
16531 * @param menuInfo Extra information about the item for which the
16532 * context menu should be shown. This information will vary
16533 * depending on the class of v.
16534 */
16535 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
16536 }
16537
Joe Onorato664644d2011-01-23 17:53:23 -080016538 /**
16539 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016540 * visibility. This reports <strong>global</strong> changes to the system UI
16541 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080016542 *
Philip Milne6c8ea062012-04-03 17:38:43 -070016543 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080016544 */
16545 public interface OnSystemUiVisibilityChangeListener {
16546 /**
16547 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070016548 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080016549 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040016550 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016551 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
16552 * <strong>global</strong> state of the UI visibility flags, not what your
16553 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080016554 */
16555 public void onSystemUiVisibilityChange(int visibility);
16556 }
16557
Adam Powell4afd62b2011-02-18 15:02:18 -080016558 /**
16559 * Interface definition for a callback to be invoked when this view is attached
16560 * or detached from its window.
16561 */
16562 public interface OnAttachStateChangeListener {
16563 /**
16564 * Called when the view is attached to a window.
16565 * @param v The view that was attached
16566 */
16567 public void onViewAttachedToWindow(View v);
16568 /**
16569 * Called when the view is detached from a window.
16570 * @param v The view that was detached
16571 */
16572 public void onViewDetachedFromWindow(View v);
16573 }
16574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016575 private final class UnsetPressedState implements Runnable {
16576 public void run() {
16577 setPressed(false);
16578 }
16579 }
16580
16581 /**
16582 * Base class for derived classes that want to save and restore their own
16583 * state in {@link android.view.View#onSaveInstanceState()}.
16584 */
16585 public static class BaseSavedState extends AbsSavedState {
16586 /**
16587 * Constructor used when reading from a parcel. Reads the state of the superclass.
16588 *
16589 * @param source
16590 */
16591 public BaseSavedState(Parcel source) {
16592 super(source);
16593 }
16594
16595 /**
16596 * Constructor called by derived classes when creating their SavedState objects
16597 *
16598 * @param superState The state of the superclass of this view
16599 */
16600 public BaseSavedState(Parcelable superState) {
16601 super(superState);
16602 }
16603
16604 public static final Parcelable.Creator<BaseSavedState> CREATOR =
16605 new Parcelable.Creator<BaseSavedState>() {
16606 public BaseSavedState createFromParcel(Parcel in) {
16607 return new BaseSavedState(in);
16608 }
16609
16610 public BaseSavedState[] newArray(int size) {
16611 return new BaseSavedState[size];
16612 }
16613 };
16614 }
16615
16616 /**
16617 * A set of information given to a view when it is attached to its parent
16618 * window.
16619 */
16620 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016621 interface Callbacks {
16622 void playSoundEffect(int effectId);
16623 boolean performHapticFeedback(int effectId, boolean always);
16624 }
16625
16626 /**
16627 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
16628 * to a Handler. This class contains the target (View) to invalidate and
16629 * the coordinates of the dirty rectangle.
16630 *
16631 * For performance purposes, this class also implements a pool of up to
16632 * POOL_LIMIT objects that get reused. This reduces memory allocations
16633 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016634 */
Romain Guyd928d682009-03-31 17:52:16 -070016635 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016636 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070016637 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
16638 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070016639 public InvalidateInfo newInstance() {
16640 return new InvalidateInfo();
16641 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016642
Romain Guyd928d682009-03-31 17:52:16 -070016643 public void onAcquired(InvalidateInfo element) {
16644 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016645
Romain Guyd928d682009-03-31 17:52:16 -070016646 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070016647 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070016648 }
16649 }, POOL_LIMIT)
16650 );
16651
16652 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016653 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016654
16655 View target;
16656
16657 int left;
16658 int top;
16659 int right;
16660 int bottom;
16661
Romain Guyd928d682009-03-31 17:52:16 -070016662 public void setNextPoolable(InvalidateInfo element) {
16663 mNext = element;
16664 }
16665
16666 public InvalidateInfo getNextPoolable() {
16667 return mNext;
16668 }
16669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016670 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070016671 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016672 }
16673
16674 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070016675 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016676 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016677
16678 public boolean isPooled() {
16679 return mIsPooled;
16680 }
16681
16682 public void setPooled(boolean isPooled) {
16683 mIsPooled = isPooled;
16684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016685 }
16686
16687 final IWindowSession mSession;
16688
16689 final IWindow mWindow;
16690
16691 final IBinder mWindowToken;
16692
16693 final Callbacks mRootCallbacks;
16694
Romain Guy59a12ca2011-06-09 17:48:21 -070016695 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080016696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016697 /**
16698 * The top view of the hierarchy.
16699 */
16700 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070016701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016702 IBinder mPanelParentWindowToken;
16703 Surface mSurface;
16704
Romain Guyb051e892010-09-28 19:09:36 -070016705 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080016706 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070016707 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080016708
Romain Guy7e4e5612012-03-05 14:37:29 -080016709 boolean mScreenOn;
16710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016711 /**
Romain Guy8506ab42009-06-11 17:35:47 -070016712 * Scale factor used by the compatibility mode
16713 */
16714 float mApplicationScale;
16715
16716 /**
16717 * Indicates whether the application is in compatibility mode
16718 */
16719 boolean mScalingRequired;
16720
16721 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016722 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080016723 */
16724 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080016725
Dianne Hackborn63042d62011-01-26 18:56:29 -080016726 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016727 * Left position of this view's window
16728 */
16729 int mWindowLeft;
16730
16731 /**
16732 * Top position of this view's window
16733 */
16734 int mWindowTop;
16735
16736 /**
Adam Powell26153a32010-11-08 15:22:27 -080016737 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070016738 */
Adam Powell26153a32010-11-08 15:22:27 -080016739 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070016740
16741 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016742 * For windows that are full-screen but using insets to layout inside
16743 * of the screen decorations, these are the current insets for the
16744 * content of the window.
16745 */
16746 final Rect mContentInsets = new Rect();
16747
16748 /**
16749 * For windows that are full-screen but using insets to layout inside
16750 * of the screen decorations, these are the current insets for the
16751 * actual visible parts of the window.
16752 */
16753 final Rect mVisibleInsets = new Rect();
16754
16755 /**
16756 * The internal insets given by this window. This value is
16757 * supplied by the client (through
16758 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
16759 * be given to the window manager when changed to be used in laying
16760 * out windows behind it.
16761 */
16762 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
16763 = new ViewTreeObserver.InternalInsetsInfo();
16764
16765 /**
16766 * All views in the window's hierarchy that serve as scroll containers,
16767 * used to determine if the window can be resized or must be panned
16768 * to adjust for a soft input area.
16769 */
16770 final ArrayList<View> mScrollContainers = new ArrayList<View>();
16771
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070016772 final KeyEvent.DispatcherState mKeyDispatchState
16773 = new KeyEvent.DispatcherState();
16774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016775 /**
16776 * Indicates whether the view's window currently has the focus.
16777 */
16778 boolean mHasWindowFocus;
16779
16780 /**
16781 * The current visibility of the window.
16782 */
16783 int mWindowVisibility;
16784
16785 /**
16786 * Indicates the time at which drawing started to occur.
16787 */
16788 long mDrawingTime;
16789
16790 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070016791 * Indicates whether or not ignoring the DIRTY_MASK flags.
16792 */
16793 boolean mIgnoreDirtyState;
16794
16795 /**
Romain Guy02ccac62011-06-24 13:20:23 -070016796 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
16797 * to avoid clearing that flag prematurely.
16798 */
16799 boolean mSetIgnoreDirtyState = false;
16800
16801 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016802 * Indicates whether the view's window is currently in touch mode.
16803 */
16804 boolean mInTouchMode;
16805
16806 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016807 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016808 * the next time it performs a traversal
16809 */
16810 boolean mRecomputeGlobalAttributes;
16811
16812 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016813 * Always report new attributes at next traversal.
16814 */
16815 boolean mForceReportNewAttributes;
16816
16817 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016818 * Set during a traveral if any views want to keep the screen on.
16819 */
16820 boolean mKeepScreenOn;
16821
16822 /**
Joe Onorato664644d2011-01-23 17:53:23 -080016823 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
16824 */
16825 int mSystemUiVisibility;
16826
16827 /**
16828 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
16829 * attached.
16830 */
16831 boolean mHasSystemUiListeners;
16832
16833 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016834 * Set if the visibility of any views has changed.
16835 */
16836 boolean mViewVisibilityChanged;
16837
16838 /**
16839 * Set to true if a view has been scrolled.
16840 */
16841 boolean mViewScrollChanged;
16842
16843 /**
16844 * Global to the view hierarchy used as a temporary for dealing with
16845 * x/y points in the transparent region computations.
16846 */
16847 final int[] mTransparentLocation = new int[2];
16848
16849 /**
16850 * Global to the view hierarchy used as a temporary for dealing with
16851 * x/y points in the ViewGroup.invalidateChild implementation.
16852 */
16853 final int[] mInvalidateChildLocation = new int[2];
16854
Chet Haasec3aa3612010-06-17 08:50:37 -070016855
16856 /**
16857 * Global to the view hierarchy used as a temporary for dealing with
16858 * x/y location when view is transformed.
16859 */
16860 final float[] mTmpTransformLocation = new float[2];
16861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016862 /**
16863 * The view tree observer used to dispatch global events like
16864 * layout, pre-draw, touch mode change, etc.
16865 */
16866 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
16867
16868 /**
16869 * A Canvas used by the view hierarchy to perform bitmap caching.
16870 */
16871 Canvas mCanvas;
16872
16873 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080016874 * The view root impl.
16875 */
16876 final ViewRootImpl mViewRootImpl;
16877
16878 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016879 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016880 * handler can be used to pump events in the UI events queue.
16881 */
16882 final Handler mHandler;
16883
16884 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016885 * Temporary for use in computing invalidate rectangles while
16886 * calling up the hierarchy.
16887 */
16888 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070016889
16890 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070016891 * Temporary for use in computing hit areas with transformed views
16892 */
16893 final RectF mTmpTransformRect = new RectF();
16894
16895 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070016896 * Temporary list for use in collecting focusable descendents of a view.
16897 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070016898 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070016899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016900 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016901 * The id of the window for accessibility purposes.
16902 */
16903 int mAccessibilityWindowId = View.NO_ID;
16904
16905 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070016906 * Whether to ingore not exposed for accessibility Views when
16907 * reporting the view tree to accessibility services.
16908 */
16909 boolean mIncludeNotImportantViews;
16910
16911 /**
16912 * The drawable for highlighting accessibility focus.
16913 */
16914 Drawable mAccessibilityFocusDrawable;
16915
16916 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016917 * Creates a new set of attachment information with the specified
16918 * events handler and thread.
16919 *
16920 * @param handler the events handler the view must use
16921 */
16922 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080016923 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016924 mSession = session;
16925 mWindow = window;
16926 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080016927 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016928 mHandler = handler;
16929 mRootCallbacks = effectPlayer;
16930 }
16931 }
16932
16933 /**
16934 * <p>ScrollabilityCache holds various fields used by a View when scrolling
16935 * is supported. This avoids keeping too many unused fields in most
16936 * instances of View.</p>
16937 */
Mike Cleronf116bf82009-09-27 19:14:12 -070016938 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080016939
Mike Cleronf116bf82009-09-27 19:14:12 -070016940 /**
16941 * Scrollbars are not visible
16942 */
16943 public static final int OFF = 0;
16944
16945 /**
16946 * Scrollbars are visible
16947 */
16948 public static final int ON = 1;
16949
16950 /**
16951 * Scrollbars are fading away
16952 */
16953 public static final int FADING = 2;
16954
16955 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080016956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016957 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070016958 public int scrollBarDefaultDelayBeforeFade;
16959 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016960
16961 public int scrollBarSize;
16962 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070016963 public float[] interpolatorValues;
16964 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016965
16966 public final Paint paint;
16967 public final Matrix matrix;
16968 public Shader shader;
16969
Mike Cleronf116bf82009-09-27 19:14:12 -070016970 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
16971
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016972 private static final float[] OPAQUE = { 255 };
16973 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080016974
Mike Cleronf116bf82009-09-27 19:14:12 -070016975 /**
16976 * When fading should start. This time moves into the future every time
16977 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
16978 */
16979 public long fadeStartTime;
16980
16981
16982 /**
16983 * The current state of the scrollbars: ON, OFF, or FADING
16984 */
16985 public int state = OFF;
16986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016987 private int mLastColor;
16988
Mike Cleronf116bf82009-09-27 19:14:12 -070016989 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016990 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
16991 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070016992 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
16993 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016994
16995 paint = new Paint();
16996 matrix = new Matrix();
16997 // use use a height of 1, and then wack the matrix each time we
16998 // actually use it.
16999 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017001 paint.setShader(shader);
17002 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017003 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017004 }
Romain Guy8506ab42009-06-11 17:35:47 -070017005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017006 public void setFadeColor(int color) {
17007 if (color != 0 && color != mLastColor) {
17008 mLastColor = color;
17009 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017010
Romain Guye55e1a72009-08-27 10:42:26 -070017011 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17012 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017014 paint.setShader(shader);
17015 // Restore the default transfer mode (src_over)
17016 paint.setXfermode(null);
17017 }
17018 }
Joe Malin32736f02011-01-19 16:14:20 -080017019
Mike Cleronf116bf82009-09-27 19:14:12 -070017020 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017021 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017022 if (now >= fadeStartTime) {
17023
17024 // the animation fades the scrollbars out by changing
17025 // the opacity (alpha) from fully opaque to fully
17026 // transparent
17027 int nextFrame = (int) now;
17028 int framesCount = 0;
17029
17030 Interpolator interpolator = scrollBarInterpolator;
17031
17032 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017033 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017034
17035 // End transparent
17036 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017037 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017038
17039 state = FADING;
17040
17041 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017042 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017043 }
17044 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017045 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017046
Svetoslav Ganova0156172011-06-26 17:55:44 -070017047 /**
17048 * Resuable callback for sending
17049 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17050 */
17051 private class SendViewScrolledAccessibilityEvent implements Runnable {
17052 public volatile boolean mIsPending;
17053
17054 public void run() {
17055 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17056 mIsPending = false;
17057 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017058 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017059
17060 /**
17061 * <p>
17062 * This class represents a delegate that can be registered in a {@link View}
17063 * to enhance accessibility support via composition rather via inheritance.
17064 * It is specifically targeted to widget developers that extend basic View
17065 * classes i.e. classes in package android.view, that would like their
17066 * applications to be backwards compatible.
17067 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017068 * <div class="special reference">
17069 * <h3>Developer Guides</h3>
17070 * <p>For more information about making applications accessible, read the
17071 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17072 * developer guide.</p>
17073 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017074 * <p>
17075 * A scenario in which a developer would like to use an accessibility delegate
17076 * is overriding a method introduced in a later API version then the minimal API
17077 * version supported by the application. For example, the method
17078 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17079 * in API version 4 when the accessibility APIs were first introduced. If a
17080 * developer would like his application to run on API version 4 devices (assuming
17081 * all other APIs used by the application are version 4 or lower) and take advantage
17082 * of this method, instead of overriding the method which would break the application's
17083 * backwards compatibility, he can override the corresponding method in this
17084 * delegate and register the delegate in the target View if the API version of
17085 * the system is high enough i.e. the API version is same or higher to the API
17086 * version that introduced
17087 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17088 * </p>
17089 * <p>
17090 * Here is an example implementation:
17091 * </p>
17092 * <code><pre><p>
17093 * if (Build.VERSION.SDK_INT >= 14) {
17094 * // If the API version is equal of higher than the version in
17095 * // which onInitializeAccessibilityNodeInfo was introduced we
17096 * // register a delegate with a customized implementation.
17097 * View view = findViewById(R.id.view_id);
17098 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17099 * public void onInitializeAccessibilityNodeInfo(View host,
17100 * AccessibilityNodeInfo info) {
17101 * // Let the default implementation populate the info.
17102 * super.onInitializeAccessibilityNodeInfo(host, info);
17103 * // Set some other information.
17104 * info.setEnabled(host.isEnabled());
17105 * }
17106 * });
17107 * }
17108 * </code></pre></p>
17109 * <p>
17110 * This delegate contains methods that correspond to the accessibility methods
17111 * in View. If a delegate has been specified the implementation in View hands
17112 * off handling to the corresponding method in this delegate. The default
17113 * implementation the delegate methods behaves exactly as the corresponding
17114 * method in View for the case of no accessibility delegate been set. Hence,
17115 * to customize the behavior of a View method, clients can override only the
17116 * corresponding delegate method without altering the behavior of the rest
17117 * accessibility related methods of the host view.
17118 * </p>
17119 */
17120 public static class AccessibilityDelegate {
17121
17122 /**
17123 * Sends an accessibility event of the given type. If accessibility is not
17124 * enabled this method has no effect.
17125 * <p>
17126 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17127 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17128 * been set.
17129 * </p>
17130 *
17131 * @param host The View hosting the delegate.
17132 * @param eventType The type of the event to send.
17133 *
17134 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17135 */
17136 public void sendAccessibilityEvent(View host, int eventType) {
17137 host.sendAccessibilityEventInternal(eventType);
17138 }
17139
17140 /**
17141 * Sends an accessibility event. This method behaves exactly as
17142 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17143 * empty {@link AccessibilityEvent} and does not perform a check whether
17144 * accessibility is enabled.
17145 * <p>
17146 * The default implementation behaves as
17147 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17148 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17149 * the case of no accessibility delegate been set.
17150 * </p>
17151 *
17152 * @param host The View hosting the delegate.
17153 * @param event The event to send.
17154 *
17155 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17156 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17157 */
17158 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17159 host.sendAccessibilityEventUncheckedInternal(event);
17160 }
17161
17162 /**
17163 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17164 * to its children for adding their text content to the event.
17165 * <p>
17166 * The default implementation behaves as
17167 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17168 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17169 * the case of no accessibility delegate been set.
17170 * </p>
17171 *
17172 * @param host The View hosting the delegate.
17173 * @param event The event.
17174 * @return True if the event population was completed.
17175 *
17176 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17177 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17178 */
17179 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17180 return host.dispatchPopulateAccessibilityEventInternal(event);
17181 }
17182
17183 /**
17184 * Gives a chance to the host View to populate the accessibility event with its
17185 * text content.
17186 * <p>
17187 * The default implementation behaves as
17188 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17189 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17190 * the case of no accessibility delegate been set.
17191 * </p>
17192 *
17193 * @param host The View hosting the delegate.
17194 * @param event The accessibility event which to populate.
17195 *
17196 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17197 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17198 */
17199 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17200 host.onPopulateAccessibilityEventInternal(event);
17201 }
17202
17203 /**
17204 * Initializes an {@link AccessibilityEvent} with information about the
17205 * the host View which is the event source.
17206 * <p>
17207 * The default implementation behaves as
17208 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17209 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17210 * the case of no accessibility delegate been set.
17211 * </p>
17212 *
17213 * @param host The View hosting the delegate.
17214 * @param event The event to initialize.
17215 *
17216 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17217 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17218 */
17219 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17220 host.onInitializeAccessibilityEventInternal(event);
17221 }
17222
17223 /**
17224 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17225 * <p>
17226 * The default implementation behaves as
17227 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17228 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17229 * the case of no accessibility delegate been set.
17230 * </p>
17231 *
17232 * @param host The View hosting the delegate.
17233 * @param info The instance to initialize.
17234 *
17235 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17236 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17237 */
17238 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17239 host.onInitializeAccessibilityNodeInfoInternal(info);
17240 }
17241
17242 /**
17243 * Called when a child of the host View has requested sending an
17244 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17245 * to augment the event.
17246 * <p>
17247 * The default implementation behaves as
17248 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17249 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17250 * the case of no accessibility delegate been set.
17251 * </p>
17252 *
17253 * @param host The View hosting the delegate.
17254 * @param child The child which requests sending the event.
17255 * @param event The event to be sent.
17256 * @return True if the event should be sent
17257 *
17258 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17259 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17260 */
17261 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17262 AccessibilityEvent event) {
17263 return host.onRequestSendAccessibilityEventInternal(child, event);
17264 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017265
17266 /**
17267 * Gets the provider for managing a virtual view hierarchy rooted at this View
17268 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17269 * that explore the window content.
17270 * <p>
17271 * The default implementation behaves as
17272 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17273 * the case of no accessibility delegate been set.
17274 * </p>
17275 *
17276 * @return The provider.
17277 *
17278 * @see AccessibilityNodeProvider
17279 */
17280 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17281 return null;
17282 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017283 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017284}