blob: ce021132809335546746e86759e43cc02dd592db [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 Ganov749e7962012-04-19 17:13:46 -07004683 * Computes whether this view is visible on the screen.
4684 *
4685 * @return Whether the view is visible on the screen.
4686 */
4687 boolean isDisplayedOnScreen() {
4688 // The first two checks are made also made by isShown() which
4689 // however traverses the tree up to the parent to catch that.
4690 // Therefore, we do some fail fast check to minimize the up
4691 // tree traversal.
4692 return (mAttachInfo != null
4693 && mAttachInfo.mWindowVisibility == View.VISIBLE
4694 && getAlpha() > 0
4695 && isShown()
4696 && getGlobalVisibleRect(mAttachInfo.mTmpInvalRect));
4697 }
4698
4699 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004700 * Sets a delegate for implementing accessibility support via compositon as
4701 * opposed to inheritance. The delegate's primary use is for implementing
4702 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4703 *
4704 * @param delegate The delegate instance.
4705 *
4706 * @see AccessibilityDelegate
4707 */
4708 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4709 mAccessibilityDelegate = delegate;
4710 }
4711
4712 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004713 * Gets the provider for managing a virtual view hierarchy rooted at this View
4714 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4715 * that explore the window content.
4716 * <p>
4717 * If this method returns an instance, this instance is responsible for managing
4718 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4719 * View including the one representing the View itself. Similarly the returned
4720 * instance is responsible for performing accessibility actions on any virtual
4721 * view or the root view itself.
4722 * </p>
4723 * <p>
4724 * If an {@link AccessibilityDelegate} has been specified via calling
4725 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4726 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4727 * is responsible for handling this call.
4728 * </p>
4729 *
4730 * @return The provider.
4731 *
4732 * @see AccessibilityNodeProvider
4733 */
4734 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4735 if (mAccessibilityDelegate != null) {
4736 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4737 } else {
4738 return null;
4739 }
4740 }
4741
4742 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004743 * Gets the unique identifier of this view on the screen for accessibility purposes.
4744 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4745 *
4746 * @return The view accessibility id.
4747 *
4748 * @hide
4749 */
4750 public int getAccessibilityViewId() {
4751 if (mAccessibilityViewId == NO_ID) {
4752 mAccessibilityViewId = sNextAccessibilityViewId++;
4753 }
4754 return mAccessibilityViewId;
4755 }
4756
4757 /**
4758 * Gets the unique identifier of the window in which this View reseides.
4759 *
4760 * @return The window accessibility id.
4761 *
4762 * @hide
4763 */
4764 public int getAccessibilityWindowId() {
4765 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4766 }
4767
4768 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004769 * Gets the {@link View} description. It briefly describes the view and is
4770 * primarily used for accessibility support. Set this property to enable
4771 * better accessibility support for your application. This is especially
4772 * true for views that do not have textual representation (For example,
4773 * ImageButton).
4774 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07004775 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07004776 *
4777 * @attr ref android.R.styleable#View_contentDescription
4778 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07004779 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07004780 public CharSequence getContentDescription() {
4781 return mContentDescription;
4782 }
4783
4784 /**
4785 * Sets the {@link View} description. It briefly describes the view and is
4786 * primarily used for accessibility support. Set this property to enable
4787 * better accessibility support for your application. This is especially
4788 * true for views that do not have textual representation (For example,
4789 * ImageButton).
4790 *
4791 * @param contentDescription The content description.
4792 *
4793 * @attr ref android.R.styleable#View_contentDescription
4794 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004795 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004796 public void setContentDescription(CharSequence contentDescription) {
4797 mContentDescription = contentDescription;
4798 }
4799
4800 /**
Romain Guya2431d02009-04-30 16:30:00 -07004801 * Invoked whenever this view loses focus, either by losing window focus or by losing
4802 * focus within its window. This method can be used to clear any state tied to the
4803 * focus. For instance, if a button is held pressed with the trackball and the window
4804 * loses focus, this method can be used to cancel the press.
4805 *
4806 * Subclasses of View overriding this method should always call super.onFocusLost().
4807 *
4808 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004809 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004810 *
4811 * @hide pending API council approval
4812 */
4813 protected void onFocusLost() {
4814 resetPressedState();
4815 }
4816
4817 private void resetPressedState() {
4818 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4819 return;
4820 }
4821
4822 if (isPressed()) {
4823 setPressed(false);
4824
4825 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004826 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004827 }
4828 }
4829 }
4830
4831 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004832 * Returns true if this view has focus
4833 *
4834 * @return True if this view has focus, false otherwise.
4835 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004836 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004837 public boolean isFocused() {
4838 return (mPrivateFlags & FOCUSED) != 0;
4839 }
4840
4841 /**
4842 * Find the view in the hierarchy rooted at this view that currently has
4843 * focus.
4844 *
4845 * @return The view that currently has focus, or null if no focused view can
4846 * be found.
4847 */
4848 public View findFocus() {
4849 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4850 }
4851
4852 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004853 * Indicates whether this view is one of the set of scrollable containers in
4854 * its window.
4855 *
4856 * @return whether this view is one of the set of scrollable containers in
4857 * its window
4858 *
4859 * @attr ref android.R.styleable#View_isScrollContainer
4860 */
4861 public boolean isScrollContainer() {
4862 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
4863 }
4864
4865 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004866 * Change whether this view is one of the set of scrollable containers in
4867 * its window. This will be used to determine whether the window can
4868 * resize or must pan when a soft input area is open -- scrollable
4869 * containers allow the window to use resize mode since the container
4870 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07004871 *
4872 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004873 */
4874 public void setScrollContainer(boolean isScrollContainer) {
4875 if (isScrollContainer) {
4876 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4877 mAttachInfo.mScrollContainers.add(this);
4878 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4879 }
4880 mPrivateFlags |= SCROLL_CONTAINER;
4881 } else {
4882 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4883 mAttachInfo.mScrollContainers.remove(this);
4884 }
4885 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4886 }
4887 }
4888
4889 /**
4890 * Returns the quality of the drawing cache.
4891 *
4892 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4893 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4894 *
4895 * @see #setDrawingCacheQuality(int)
4896 * @see #setDrawingCacheEnabled(boolean)
4897 * @see #isDrawingCacheEnabled()
4898 *
4899 * @attr ref android.R.styleable#View_drawingCacheQuality
4900 */
4901 public int getDrawingCacheQuality() {
4902 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4903 }
4904
4905 /**
4906 * Set the drawing cache quality of this view. This value is used only when the
4907 * drawing cache is enabled
4908 *
4909 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4910 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4911 *
4912 * @see #getDrawingCacheQuality()
4913 * @see #setDrawingCacheEnabled(boolean)
4914 * @see #isDrawingCacheEnabled()
4915 *
4916 * @attr ref android.R.styleable#View_drawingCacheQuality
4917 */
4918 public void setDrawingCacheQuality(int quality) {
4919 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4920 }
4921
4922 /**
4923 * Returns whether the screen should remain on, corresponding to the current
4924 * value of {@link #KEEP_SCREEN_ON}.
4925 *
4926 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4927 *
4928 * @see #setKeepScreenOn(boolean)
4929 *
4930 * @attr ref android.R.styleable#View_keepScreenOn
4931 */
4932 public boolean getKeepScreenOn() {
4933 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4934 }
4935
4936 /**
4937 * Controls whether the screen should remain on, modifying the
4938 * value of {@link #KEEP_SCREEN_ON}.
4939 *
4940 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4941 *
4942 * @see #getKeepScreenOn()
4943 *
4944 * @attr ref android.R.styleable#View_keepScreenOn
4945 */
4946 public void setKeepScreenOn(boolean keepScreenOn) {
4947 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4948 }
4949
4950 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004951 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4952 * @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 -08004953 *
4954 * @attr ref android.R.styleable#View_nextFocusLeft
4955 */
4956 public int getNextFocusLeftId() {
4957 return mNextFocusLeftId;
4958 }
4959
4960 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004961 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4962 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4963 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004964 *
4965 * @attr ref android.R.styleable#View_nextFocusLeft
4966 */
4967 public void setNextFocusLeftId(int nextFocusLeftId) {
4968 mNextFocusLeftId = nextFocusLeftId;
4969 }
4970
4971 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004972 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4973 * @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 -08004974 *
4975 * @attr ref android.R.styleable#View_nextFocusRight
4976 */
4977 public int getNextFocusRightId() {
4978 return mNextFocusRightId;
4979 }
4980
4981 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004982 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4983 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4984 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004985 *
4986 * @attr ref android.R.styleable#View_nextFocusRight
4987 */
4988 public void setNextFocusRightId(int nextFocusRightId) {
4989 mNextFocusRightId = nextFocusRightId;
4990 }
4991
4992 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004993 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4994 * @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 -08004995 *
4996 * @attr ref android.R.styleable#View_nextFocusUp
4997 */
4998 public int getNextFocusUpId() {
4999 return mNextFocusUpId;
5000 }
5001
5002 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005003 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5004 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5005 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005006 *
5007 * @attr ref android.R.styleable#View_nextFocusUp
5008 */
5009 public void setNextFocusUpId(int nextFocusUpId) {
5010 mNextFocusUpId = nextFocusUpId;
5011 }
5012
5013 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005014 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5015 * @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 -08005016 *
5017 * @attr ref android.R.styleable#View_nextFocusDown
5018 */
5019 public int getNextFocusDownId() {
5020 return mNextFocusDownId;
5021 }
5022
5023 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005024 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5025 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5026 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005027 *
5028 * @attr ref android.R.styleable#View_nextFocusDown
5029 */
5030 public void setNextFocusDownId(int nextFocusDownId) {
5031 mNextFocusDownId = nextFocusDownId;
5032 }
5033
5034 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005035 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5036 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5037 *
5038 * @attr ref android.R.styleable#View_nextFocusForward
5039 */
5040 public int getNextFocusForwardId() {
5041 return mNextFocusForwardId;
5042 }
5043
5044 /**
5045 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5046 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5047 * decide automatically.
5048 *
5049 * @attr ref android.R.styleable#View_nextFocusForward
5050 */
5051 public void setNextFocusForwardId(int nextFocusForwardId) {
5052 mNextFocusForwardId = nextFocusForwardId;
5053 }
5054
5055 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005056 * Returns the visibility of this view and all of its ancestors
5057 *
5058 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5059 */
5060 public boolean isShown() {
5061 View current = this;
5062 //noinspection ConstantConditions
5063 do {
5064 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5065 return false;
5066 }
5067 ViewParent parent = current.mParent;
5068 if (parent == null) {
5069 return false; // We are not attached to the view root
5070 }
5071 if (!(parent instanceof View)) {
5072 return true;
5073 }
5074 current = (View) parent;
5075 } while (current != null);
5076
5077 return false;
5078 }
5079
5080 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005081 * Called by the view hierarchy when the content insets for a window have
5082 * changed, to allow it to adjust its content to fit within those windows.
5083 * The content insets tell you the space that the status bar, input method,
5084 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005085 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005086 * <p>You do not normally need to deal with this function, since the default
5087 * window decoration given to applications takes care of applying it to the
5088 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5089 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5090 * and your content can be placed under those system elements. You can then
5091 * use this method within your view hierarchy if you have parts of your UI
5092 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005093 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005094 * <p>The default implementation of this method simply applies the content
5095 * inset's to the view's padding. This can be enabled through
5096 * {@link #setFitsSystemWindows(boolean)}. Alternatively, you can override
5097 * the method and handle the insets however you would like. Note that the
5098 * insets provided by the framework are always relative to the far edges
5099 * of the window, not accounting for the location of the called view within
5100 * that window. (In fact when this method is called you do not yet know
5101 * where the layout will place the view, as it is done before layout happens.)
5102 *
5103 * <p>Note: unlike many View methods, there is no dispatch phase to this
5104 * call. If you are overriding it in a ViewGroup and want to allow the
5105 * call to continue to your children, you must be sure to call the super
5106 * implementation.
5107 *
5108 * @param insets Current content insets of the window. Prior to
5109 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5110 * the insets or else you and Android will be unhappy.
5111 *
5112 * @return Return true if this view applied the insets and it should not
5113 * continue propagating further down the hierarchy, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005114 */
5115 protected boolean fitSystemWindows(Rect insets) {
5116 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005117 mUserPaddingStart = -1;
5118 mUserPaddingEnd = -1;
5119 mUserPaddingRelative = false;
5120 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5121 || mAttachInfo == null
5122 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5123 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5124 return true;
5125 } else {
5126 internalSetPadding(0, 0, 0, 0);
5127 return false;
5128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005129 }
5130 return false;
5131 }
5132
5133 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005134 * Set whether or not this view should account for system screen decorations
5135 * such as the status bar and inset its content. This allows this view to be
5136 * positioned in absolute screen coordinates and remain visible to the user.
5137 *
5138 * <p>This should only be used by top-level window decor views.
5139 *
5140 * @param fitSystemWindows true to inset content for system screen decorations, false for
5141 * default behavior.
5142 *
5143 * @attr ref android.R.styleable#View_fitsSystemWindows
5144 */
5145 public void setFitsSystemWindows(boolean fitSystemWindows) {
5146 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5147 }
5148
5149 /**
5150 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
5151 * will account for system screen decorations such as the status bar and inset its
5152 * content. This allows the view to be positioned in absolute screen coordinates
5153 * and remain visible to the user.
5154 *
5155 * @return true if this view will adjust its content bounds for system screen decorations.
5156 *
5157 * @attr ref android.R.styleable#View_fitsSystemWindows
5158 */
5159 public boolean fitsSystemWindows() {
5160 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5161 }
5162
5163 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005164 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5165 */
5166 public void requestFitSystemWindows() {
5167 if (mParent != null) {
5168 mParent.requestFitSystemWindows();
5169 }
5170 }
5171
5172 /**
5173 * For use by PhoneWindow to make its own system window fitting optional.
5174 * @hide
5175 */
5176 public void makeOptionalFitsSystemWindows() {
5177 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5178 }
5179
5180 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005181 * Returns the visibility status for this view.
5182 *
5183 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5184 * @attr ref android.R.styleable#View_visibility
5185 */
5186 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005187 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5188 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5189 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005190 })
5191 public int getVisibility() {
5192 return mViewFlags & VISIBILITY_MASK;
5193 }
5194
5195 /**
5196 * Set the enabled state of this view.
5197 *
5198 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5199 * @attr ref android.R.styleable#View_visibility
5200 */
5201 @RemotableViewMethod
5202 public void setVisibility(int visibility) {
5203 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005204 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005205 }
5206
5207 /**
5208 * Returns the enabled status for this view. The interpretation of the
5209 * enabled state varies by subclass.
5210 *
5211 * @return True if this view is enabled, false otherwise.
5212 */
5213 @ViewDebug.ExportedProperty
5214 public boolean isEnabled() {
5215 return (mViewFlags & ENABLED_MASK) == ENABLED;
5216 }
5217
5218 /**
5219 * Set the enabled state of this view. The interpretation of the enabled
5220 * state varies by subclass.
5221 *
5222 * @param enabled True if this view is enabled, false otherwise.
5223 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005224 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005226 if (enabled == isEnabled()) return;
5227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005228 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5229
5230 /*
5231 * The View most likely has to change its appearance, so refresh
5232 * the drawable state.
5233 */
5234 refreshDrawableState();
5235
5236 // Invalidate too, since the default behavior for views is to be
5237 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005238 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005239 }
5240
5241 /**
5242 * Set whether this view can receive the focus.
5243 *
5244 * Setting this to false will also ensure that this view is not focusable
5245 * in touch mode.
5246 *
5247 * @param focusable If true, this view can receive the focus.
5248 *
5249 * @see #setFocusableInTouchMode(boolean)
5250 * @attr ref android.R.styleable#View_focusable
5251 */
5252 public void setFocusable(boolean focusable) {
5253 if (!focusable) {
5254 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5255 }
5256 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5257 }
5258
5259 /**
5260 * Set whether this view can receive focus while in touch mode.
5261 *
5262 * Setting this to true will also ensure that this view is focusable.
5263 *
5264 * @param focusableInTouchMode If true, this view can receive the focus while
5265 * in touch mode.
5266 *
5267 * @see #setFocusable(boolean)
5268 * @attr ref android.R.styleable#View_focusableInTouchMode
5269 */
5270 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5271 // Focusable in touch mode should always be set before the focusable flag
5272 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5273 // which, in touch mode, will not successfully request focus on this view
5274 // because the focusable in touch mode flag is not set
5275 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5276 if (focusableInTouchMode) {
5277 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5278 }
5279 }
5280
5281 /**
5282 * Set whether this view should have sound effects enabled for events such as
5283 * clicking and touching.
5284 *
5285 * <p>You may wish to disable sound effects for a view if you already play sounds,
5286 * for instance, a dial key that plays dtmf tones.
5287 *
5288 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5289 * @see #isSoundEffectsEnabled()
5290 * @see #playSoundEffect(int)
5291 * @attr ref android.R.styleable#View_soundEffectsEnabled
5292 */
5293 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5294 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5295 }
5296
5297 /**
5298 * @return whether this view should have sound effects enabled for events such as
5299 * clicking and touching.
5300 *
5301 * @see #setSoundEffectsEnabled(boolean)
5302 * @see #playSoundEffect(int)
5303 * @attr ref android.R.styleable#View_soundEffectsEnabled
5304 */
5305 @ViewDebug.ExportedProperty
5306 public boolean isSoundEffectsEnabled() {
5307 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5308 }
5309
5310 /**
5311 * Set whether this view should have haptic feedback for events such as
5312 * long presses.
5313 *
5314 * <p>You may wish to disable haptic feedback if your view already controls
5315 * its own haptic feedback.
5316 *
5317 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5318 * @see #isHapticFeedbackEnabled()
5319 * @see #performHapticFeedback(int)
5320 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5321 */
5322 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5323 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5324 }
5325
5326 /**
5327 * @return whether this view should have haptic feedback enabled for events
5328 * long presses.
5329 *
5330 * @see #setHapticFeedbackEnabled(boolean)
5331 * @see #performHapticFeedback(int)
5332 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5333 */
5334 @ViewDebug.ExportedProperty
5335 public boolean isHapticFeedbackEnabled() {
5336 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5337 }
5338
5339 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005340 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005341 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005342 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5343 * {@link #LAYOUT_DIRECTION_RTL},
5344 * {@link #LAYOUT_DIRECTION_INHERIT} or
5345 * {@link #LAYOUT_DIRECTION_LOCALE}.
5346 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005347 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005348 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005349 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5350 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5351 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5352 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005353 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005354 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005355 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005356 }
5357
5358 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005359 * Set the layout direction for this view. This will propagate a reset of layout direction
5360 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005361 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005362 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5363 * {@link #LAYOUT_DIRECTION_RTL},
5364 * {@link #LAYOUT_DIRECTION_INHERIT} or
5365 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005366 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005367 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005368 */
5369 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005370 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005371 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005372 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005373 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005374 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005375 // Set the new layout direction (filtered) and ask for a layout pass
5376 mPrivateFlags2 |=
5377 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5378 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005379 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005380 }
5381
5382 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005383 * Returns the resolved layout direction for this view.
5384 *
5385 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005386 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005387 */
5388 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005389 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5390 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005391 })
5392 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005393 // The layout diretion will be resolved only if needed
5394 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5395 resolveLayoutDirection();
5396 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005397 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005398 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5399 }
5400
5401 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005402 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5403 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005404 *
5405 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005406 */
5407 @ViewDebug.ExportedProperty(category = "layout")
5408 public boolean isLayoutRtl() {
5409 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5410 }
5411
5412 /**
Adam Powell539ee872012-02-03 19:00:49 -08005413 * Indicates whether the view is currently tracking transient state that the
5414 * app should not need to concern itself with saving and restoring, but that
5415 * the framework should take special note to preserve when possible.
5416 *
5417 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005418 */
5419 @ViewDebug.ExportedProperty(category = "layout")
5420 public boolean hasTransientState() {
5421 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5422 }
5423
5424 /**
5425 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005426 * framework should attempt to preserve when possible. This flag is reference counted,
5427 * so every call to setHasTransientState(true) should be paired with a later call
5428 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005429 *
5430 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005431 */
5432 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005433 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5434 mTransientStateCount - 1;
5435 if (mTransientStateCount < 0) {
5436 mTransientStateCount = 0;
5437 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5438 "unmatched pair of setHasTransientState calls");
5439 }
5440 if ((hasTransientState && mTransientStateCount == 1) ||
5441 (hasTransientState && mTransientStateCount == 0)) {
5442 // update flag if we've just incremented up from 0 or decremented down to 0
5443 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5444 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5445 if (mParent != null) {
5446 try {
5447 mParent.childHasTransientStateChanged(this, hasTransientState);
5448 } catch (AbstractMethodError e) {
5449 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5450 " does not fully implement ViewParent", e);
5451 }
Adam Powell539ee872012-02-03 19:00:49 -08005452 }
5453 }
5454 }
5455
5456 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005457 * If this view doesn't do any drawing on its own, set this flag to
5458 * allow further optimizations. By default, this flag is not set on
5459 * View, but could be set on some View subclasses such as ViewGroup.
5460 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005461 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5462 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005463 *
5464 * @param willNotDraw whether or not this View draw on its own
5465 */
5466 public void setWillNotDraw(boolean willNotDraw) {
5467 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5468 }
5469
5470 /**
5471 * Returns whether or not this View draws on its own.
5472 *
5473 * @return true if this view has nothing to draw, false otherwise
5474 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005475 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005476 public boolean willNotDraw() {
5477 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5478 }
5479
5480 /**
5481 * When a View's drawing cache is enabled, drawing is redirected to an
5482 * offscreen bitmap. Some views, like an ImageView, must be able to
5483 * bypass this mechanism if they already draw a single bitmap, to avoid
5484 * unnecessary usage of the memory.
5485 *
5486 * @param willNotCacheDrawing true if this view does not cache its
5487 * drawing, false otherwise
5488 */
5489 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5490 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5491 }
5492
5493 /**
5494 * Returns whether or not this View can cache its drawing or not.
5495 *
5496 * @return true if this view does not cache its drawing, false otherwise
5497 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005498 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005499 public boolean willNotCacheDrawing() {
5500 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5501 }
5502
5503 /**
5504 * Indicates whether this view reacts to click events or not.
5505 *
5506 * @return true if the view is clickable, false otherwise
5507 *
5508 * @see #setClickable(boolean)
5509 * @attr ref android.R.styleable#View_clickable
5510 */
5511 @ViewDebug.ExportedProperty
5512 public boolean isClickable() {
5513 return (mViewFlags & CLICKABLE) == CLICKABLE;
5514 }
5515
5516 /**
5517 * Enables or disables click events for this view. When a view
5518 * is clickable it will change its state to "pressed" on every click.
5519 * Subclasses should set the view clickable to visually react to
5520 * user's clicks.
5521 *
5522 * @param clickable true to make the view clickable, false otherwise
5523 *
5524 * @see #isClickable()
5525 * @attr ref android.R.styleable#View_clickable
5526 */
5527 public void setClickable(boolean clickable) {
5528 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5529 }
5530
5531 /**
5532 * Indicates whether this view reacts to long click events or not.
5533 *
5534 * @return true if the view is long clickable, false otherwise
5535 *
5536 * @see #setLongClickable(boolean)
5537 * @attr ref android.R.styleable#View_longClickable
5538 */
5539 public boolean isLongClickable() {
5540 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5541 }
5542
5543 /**
5544 * Enables or disables long click events for this view. When a view is long
5545 * clickable it reacts to the user holding down the button for a longer
5546 * duration than a tap. This event can either launch the listener or a
5547 * context menu.
5548 *
5549 * @param longClickable true to make the view long clickable, false otherwise
5550 * @see #isLongClickable()
5551 * @attr ref android.R.styleable#View_longClickable
5552 */
5553 public void setLongClickable(boolean longClickable) {
5554 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5555 }
5556
5557 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005558 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005559 *
5560 * @see #isClickable()
5561 * @see #setClickable(boolean)
5562 *
5563 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5564 * the View's internal state from a previously set "pressed" state.
5565 */
5566 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005567 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005569 if (pressed) {
5570 mPrivateFlags |= PRESSED;
5571 } else {
5572 mPrivateFlags &= ~PRESSED;
5573 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005574
5575 if (needsRefresh) {
5576 refreshDrawableState();
5577 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005578 dispatchSetPressed(pressed);
5579 }
5580
5581 /**
5582 * Dispatch setPressed to all of this View's children.
5583 *
5584 * @see #setPressed(boolean)
5585 *
5586 * @param pressed The new pressed state
5587 */
5588 protected void dispatchSetPressed(boolean pressed) {
5589 }
5590
5591 /**
5592 * Indicates whether the view is currently in pressed state. Unless
5593 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5594 * the pressed state.
5595 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005596 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005597 * @see #isClickable()
5598 * @see #setClickable(boolean)
5599 *
5600 * @return true if the view is currently pressed, false otherwise
5601 */
5602 public boolean isPressed() {
5603 return (mPrivateFlags & PRESSED) == PRESSED;
5604 }
5605
5606 /**
5607 * Indicates whether this view will save its state (that is,
5608 * whether its {@link #onSaveInstanceState} method will be called).
5609 *
5610 * @return Returns true if the view state saving is enabled, else false.
5611 *
5612 * @see #setSaveEnabled(boolean)
5613 * @attr ref android.R.styleable#View_saveEnabled
5614 */
5615 public boolean isSaveEnabled() {
5616 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5617 }
5618
5619 /**
5620 * Controls whether the saving of this view's state is
5621 * enabled (that is, whether its {@link #onSaveInstanceState} method
5622 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005623 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005624 * for its state to be saved. This flag can only disable the
5625 * saving of this view; any child views may still have their state saved.
5626 *
5627 * @param enabled Set to false to <em>disable</em> state saving, or true
5628 * (the default) to allow it.
5629 *
5630 * @see #isSaveEnabled()
5631 * @see #setId(int)
5632 * @see #onSaveInstanceState()
5633 * @attr ref android.R.styleable#View_saveEnabled
5634 */
5635 public void setSaveEnabled(boolean enabled) {
5636 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5637 }
5638
Jeff Brown85a31762010-09-01 17:01:00 -07005639 /**
5640 * Gets whether the framework should discard touches when the view's
5641 * window is obscured by another visible window.
5642 * Refer to the {@link View} security documentation for more details.
5643 *
5644 * @return True if touch filtering is enabled.
5645 *
5646 * @see #setFilterTouchesWhenObscured(boolean)
5647 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5648 */
5649 @ViewDebug.ExportedProperty
5650 public boolean getFilterTouchesWhenObscured() {
5651 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5652 }
5653
5654 /**
5655 * Sets whether the framework should discard touches when the view's
5656 * window is obscured by another visible window.
5657 * Refer to the {@link View} security documentation for more details.
5658 *
5659 * @param enabled True if touch filtering should be enabled.
5660 *
5661 * @see #getFilterTouchesWhenObscured
5662 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5663 */
5664 public void setFilterTouchesWhenObscured(boolean enabled) {
5665 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5666 FILTER_TOUCHES_WHEN_OBSCURED);
5667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005668
5669 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005670 * Indicates whether the entire hierarchy under this view will save its
5671 * state when a state saving traversal occurs from its parent. The default
5672 * is true; if false, these views will not be saved unless
5673 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5674 *
5675 * @return Returns true if the view state saving from parent is enabled, else false.
5676 *
5677 * @see #setSaveFromParentEnabled(boolean)
5678 */
5679 public boolean isSaveFromParentEnabled() {
5680 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5681 }
5682
5683 /**
5684 * Controls whether the entire hierarchy under this view will save its
5685 * state when a state saving traversal occurs from its parent. The default
5686 * is true; if false, these views will not be saved unless
5687 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5688 *
5689 * @param enabled Set to false to <em>disable</em> state saving, or true
5690 * (the default) to allow it.
5691 *
5692 * @see #isSaveFromParentEnabled()
5693 * @see #setId(int)
5694 * @see #onSaveInstanceState()
5695 */
5696 public void setSaveFromParentEnabled(boolean enabled) {
5697 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5698 }
5699
5700
5701 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005702 * Returns whether this View is able to take focus.
5703 *
5704 * @return True if this view can take focus, or false otherwise.
5705 * @attr ref android.R.styleable#View_focusable
5706 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005707 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005708 public final boolean isFocusable() {
5709 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5710 }
5711
5712 /**
5713 * When a view is focusable, it may not want to take focus when in touch mode.
5714 * For example, a button would like focus when the user is navigating via a D-pad
5715 * so that the user can click on it, but once the user starts touching the screen,
5716 * the button shouldn't take focus
5717 * @return Whether the view is focusable in touch mode.
5718 * @attr ref android.R.styleable#View_focusableInTouchMode
5719 */
5720 @ViewDebug.ExportedProperty
5721 public final boolean isFocusableInTouchMode() {
5722 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5723 }
5724
5725 /**
5726 * Find the nearest view in the specified direction that can take focus.
5727 * This does not actually give focus to that view.
5728 *
5729 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5730 *
5731 * @return The nearest focusable in the specified direction, or null if none
5732 * can be found.
5733 */
5734 public View focusSearch(int direction) {
5735 if (mParent != null) {
5736 return mParent.focusSearch(this, direction);
5737 } else {
5738 return null;
5739 }
5740 }
5741
5742 /**
5743 * This method is the last chance for the focused view and its ancestors to
5744 * respond to an arrow key. This is called when the focused view did not
5745 * consume the key internally, nor could the view system find a new view in
5746 * the requested direction to give focus to.
5747 *
5748 * @param focused The currently focused view.
5749 * @param direction The direction focus wants to move. One of FOCUS_UP,
5750 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5751 * @return True if the this view consumed this unhandled move.
5752 */
5753 public boolean dispatchUnhandledMove(View focused, int direction) {
5754 return false;
5755 }
5756
5757 /**
5758 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005759 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005760 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005761 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5762 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005763 * @return The user specified next view, or null if there is none.
5764 */
5765 View findUserSetNextFocus(View root, int direction) {
5766 switch (direction) {
5767 case FOCUS_LEFT:
5768 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005769 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005770 case FOCUS_RIGHT:
5771 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005772 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005773 case FOCUS_UP:
5774 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005775 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005776 case FOCUS_DOWN:
5777 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005778 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005779 case FOCUS_FORWARD:
5780 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005781 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005782 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005783 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005784 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005785 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005786 @Override
5787 public boolean apply(View t) {
5788 return t.mNextFocusForwardId == id;
5789 }
5790 });
5791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005792 }
5793 return null;
5794 }
5795
Jeff Brown4dfbec22011-08-15 14:55:37 -07005796 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5797 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5798 @Override
5799 public boolean apply(View t) {
5800 return t.mID == childViewId;
5801 }
5802 });
5803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005804 if (result == null) {
5805 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5806 + "by user for id " + childViewId);
5807 }
5808 return result;
5809 }
5810
5811 /**
5812 * Find and return all focusable views that are descendants of this view,
5813 * possibly including this view if it is focusable itself.
5814 *
5815 * @param direction The direction of the focus
5816 * @return A list of focusable views
5817 */
5818 public ArrayList<View> getFocusables(int direction) {
5819 ArrayList<View> result = new ArrayList<View>(24);
5820 addFocusables(result, direction);
5821 return result;
5822 }
5823
5824 /**
5825 * Add any focusable views that are descendants of this view (possibly
5826 * including this view if it is focusable itself) to views. If we are in touch mode,
5827 * only add views that are also focusable in touch mode.
5828 *
5829 * @param views Focusable views found so far
5830 * @param direction The direction of the focus
5831 */
5832 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005833 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5834 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005835
svetoslavganov75986cf2009-05-14 22:28:01 -07005836 /**
5837 * Adds any focusable views that are descendants of this view (possibly
5838 * including this view if it is focusable itself) to views. This method
5839 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07005840 * only views focusable in touch mode if we are in touch mode or
5841 * only views that can take accessibility focus if accessibility is enabeld
5842 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07005843 *
5844 * @param views Focusable views found so far or null if all we are interested is
5845 * the number of focusables.
5846 * @param direction The direction of the focus.
5847 * @param focusableMode The type of focusables to be added.
5848 *
5849 * @see #FOCUSABLES_ALL
5850 * @see #FOCUSABLES_TOUCH_MODE
5851 */
5852 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07005853 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005854 return;
5855 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07005856 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
5857 if (AccessibilityManager.getInstance(mContext).isEnabled()
5858 && includeForAccessibility()) {
5859 views.add(this);
5860 return;
5861 }
5862 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005863 if (!isFocusable()) {
5864 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07005865 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005866 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
5867 && isInTouchMode() && !isFocusableInTouchMode()) {
5868 return;
5869 }
5870 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005871 }
5872
5873 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005874 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005875 * The search is performed by either the text that the View renders or the content
5876 * description that describes the view for accessibility purposes and the view does
5877 * not render or both. Clients can specify how the search is to be performed via
5878 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5879 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005880 *
5881 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005882 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005883 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005884 * @see #FIND_VIEWS_WITH_TEXT
5885 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5886 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005887 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005888 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005889 if (getAccessibilityNodeProvider() != null) {
5890 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5891 outViews.add(this);
5892 }
5893 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5894 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005895 String searchedLowerCase = searched.toString().toLowerCase();
5896 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5897 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5898 outViews.add(this);
5899 }
5900 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005901 }
5902
5903 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005904 * Find and return all touchable views that are descendants of this view,
5905 * possibly including this view if it is touchable itself.
5906 *
5907 * @return A list of touchable views
5908 */
5909 public ArrayList<View> getTouchables() {
5910 ArrayList<View> result = new ArrayList<View>();
5911 addTouchables(result);
5912 return result;
5913 }
5914
5915 /**
5916 * Add any touchable views that are descendants of this view (possibly
5917 * including this view if it is touchable itself) to views.
5918 *
5919 * @param views Touchable views found so far
5920 */
5921 public void addTouchables(ArrayList<View> views) {
5922 final int viewFlags = mViewFlags;
5923
5924 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5925 && (viewFlags & ENABLED_MASK) == ENABLED) {
5926 views.add(this);
5927 }
5928 }
5929
5930 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07005931 * Returns whether this View is accessibility focused.
5932 *
5933 * @return True if this View is accessibility focused.
5934 */
5935 boolean isAccessibilityFocused() {
5936 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
5937 }
5938
5939 /**
5940 * Call this to try to give accessibility focus to this view.
5941 *
5942 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
5943 * returns false or the view is no visible or the view already has accessibility
5944 * focus.
5945 *
5946 * See also {@link #focusSearch(int)}, which is what you call to say that you
5947 * have focus, and you want your parent to look for the next one.
5948 *
5949 * @return Whether this view actually took accessibility focus.
5950 *
5951 * @hide
5952 */
5953 public boolean requestAccessibilityFocus() {
5954 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
5955 return false;
5956 }
5957 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5958 return false;
5959 }
5960 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
5961 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
5962 ViewRootImpl viewRootImpl = getViewRootImpl();
5963 if (viewRootImpl != null) {
5964 viewRootImpl.setAccessibilityFocusedHost(this);
5965 }
5966 invalidate();
5967 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
5968 notifyAccessibilityStateChanged();
5969 // Try to give input focus to this view - not a descendant.
5970 requestFocusNoSearch(View.FOCUS_DOWN, null);
5971 return true;
5972 }
5973 return false;
5974 }
5975
5976 /**
5977 * Call this to try to clear accessibility focus of this view.
5978 *
5979 * See also {@link #focusSearch(int)}, which is what you call to say that you
5980 * have focus, and you want your parent to look for the next one.
5981 *
5982 * @hide
5983 */
5984 public void clearAccessibilityFocus() {
5985 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
5986 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
5987 ViewRootImpl viewRootImpl = getViewRootImpl();
5988 if (viewRootImpl != null) {
5989 viewRootImpl.setAccessibilityFocusedHost(null);
5990 }
5991 invalidate();
5992 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
5993 notifyAccessibilityStateChanged();
5994 // Try to move accessibility focus to the input focus.
5995 View rootView = getRootView();
5996 if (rootView != null) {
5997 View inputFocus = rootView.findFocus();
5998 if (inputFocus != null) {
5999 inputFocus.requestAccessibilityFocus();
6000 }
6001 }
6002 }
6003 }
6004
6005 /**
6006 * Find the best view to take accessibility focus from a hover.
6007 * This function finds the deepest actionable view and if that
6008 * fails ask the parent to take accessibility focus from hover.
6009 *
6010 * @param x The X hovered location in this view coorditantes.
6011 * @param y The Y hovered location in this view coorditantes.
6012 * @return Whether the request was handled.
6013 *
6014 * @hide
6015 */
6016 public boolean requestAccessibilityFocusFromHover(float x, float y) {
6017 if (onRequestAccessibilityFocusFromHover(x, y)) {
6018 return true;
6019 }
6020 ViewParent parent = mParent;
6021 if (parent instanceof View) {
6022 View parentView = (View) parent;
6023
6024 float[] position = mAttachInfo.mTmpTransformLocation;
6025 position[0] = x;
6026 position[1] = y;
6027
6028 // Compensate for the transformation of the current matrix.
6029 if (!hasIdentityMatrix()) {
6030 getMatrix().mapPoints(position);
6031 }
6032
6033 // Compensate for the parent scroll and the offset
6034 // of this view stop from the parent top.
6035 position[0] += mLeft - parentView.mScrollX;
6036 position[1] += mTop - parentView.mScrollY;
6037
6038 return parentView.requestAccessibilityFocusFromHover(position[0], position[1]);
6039 }
6040 return false;
6041 }
6042
6043 /**
6044 * Requests to give this View focus from hover.
6045 *
6046 * @param x The X hovered location in this view coorditantes.
6047 * @param y The Y hovered location in this view coorditantes.
6048 * @return Whether the request was handled.
6049 *
6050 * @hide
6051 */
6052 public boolean onRequestAccessibilityFocusFromHover(float x, float y) {
6053 if (includeForAccessibility()
6054 && (isActionableForAccessibility() || hasListenersForAccessibility())) {
6055 return requestAccessibilityFocus();
6056 }
6057 return false;
6058 }
6059
6060 /**
6061 * Clears accessibility focus without calling any callback methods
6062 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6063 * is used for clearing accessibility focus when giving this focus to
6064 * another view.
6065 */
6066 void clearAccessibilityFocusNoCallbacks() {
6067 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6068 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6069 invalidate();
6070 }
6071 }
6072
6073 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006074 * Call this to try to give focus to a specific view or to one of its
6075 * descendants.
6076 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006077 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6078 * false), or if it is focusable and it is not focusable in touch mode
6079 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006080 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006081 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006082 * have focus, and you want your parent to look for the next one.
6083 *
6084 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6085 * {@link #FOCUS_DOWN} and <code>null</code>.
6086 *
6087 * @return Whether this view or one of its descendants actually took focus.
6088 */
6089 public final boolean requestFocus() {
6090 return requestFocus(View.FOCUS_DOWN);
6091 }
6092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006093 /**
6094 * Call this to try to give focus to a specific view or to one of its
6095 * descendants and give it a hint about what direction focus is heading.
6096 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006097 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6098 * false), or if it is focusable and it is not focusable in touch mode
6099 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006100 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006101 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006102 * have focus, and you want your parent to look for the next one.
6103 *
6104 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6105 * <code>null</code> set for the previously focused rectangle.
6106 *
6107 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6108 * @return Whether this view or one of its descendants actually took focus.
6109 */
6110 public final boolean requestFocus(int direction) {
6111 return requestFocus(direction, null);
6112 }
6113
6114 /**
6115 * Call this to try to give focus to a specific view or to one of its descendants
6116 * and give it hints about the direction and a specific rectangle that the focus
6117 * is coming from. The rectangle can help give larger views a finer grained hint
6118 * about where focus is coming from, and therefore, where to show selection, or
6119 * forward focus change internally.
6120 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006121 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6122 * false), or if it is focusable and it is not focusable in touch mode
6123 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006124 *
6125 * A View will not take focus if it is not visible.
6126 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006127 * A View will not take focus if one of its parents has
6128 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6129 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006130 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006131 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006132 * have focus, and you want your parent to look for the next one.
6133 *
6134 * You may wish to override this method if your custom {@link View} has an internal
6135 * {@link View} that it wishes to forward the request to.
6136 *
6137 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6138 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6139 * to give a finer grained hint about where focus is coming from. May be null
6140 * if there is no hint.
6141 * @return Whether this view or one of its descendants actually took focus.
6142 */
6143 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006144 return requestFocusNoSearch(direction, previouslyFocusedRect);
6145 }
6146
6147 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006148 // need to be focusable
6149 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6150 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6151 return false;
6152 }
6153
6154 // need to be focusable in touch mode if in touch mode
6155 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006156 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6157 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006158 }
6159
6160 // need to not have any parents blocking us
6161 if (hasAncestorThatBlocksDescendantFocus()) {
6162 return false;
6163 }
6164
6165 handleFocusGainInternal(direction, previouslyFocusedRect);
6166 return true;
6167 }
6168
6169 /**
6170 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6171 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6172 * touch mode to request focus when they are touched.
6173 *
6174 * @return Whether this view or one of its descendants actually took focus.
6175 *
6176 * @see #isInTouchMode()
6177 *
6178 */
6179 public final boolean requestFocusFromTouch() {
6180 // Leave touch mode if we need to
6181 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006182 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006183 if (viewRoot != null) {
6184 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006185 }
6186 }
6187 return requestFocus(View.FOCUS_DOWN);
6188 }
6189
6190 /**
6191 * @return Whether any ancestor of this view blocks descendant focus.
6192 */
6193 private boolean hasAncestorThatBlocksDescendantFocus() {
6194 ViewParent ancestor = mParent;
6195 while (ancestor instanceof ViewGroup) {
6196 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6197 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6198 return true;
6199 } else {
6200 ancestor = vgAncestor.getParent();
6201 }
6202 }
6203 return false;
6204 }
6205
6206 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006207 * Gets the mode for determining whether this View is important for accessibility
6208 * which is if it fires accessibility events and if it is reported to
6209 * accessibility services that query the screen.
6210 *
6211 * @return The mode for determining whether a View is important for accessibility.
6212 *
6213 * @attr ref android.R.styleable#View_importantForAccessibility
6214 *
6215 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6216 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6217 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6218 */
6219 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6220 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO,
6221 to = "IMPORTANT_FOR_ACCESSIBILITY_AUTO"),
6222 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES,
6223 to = "IMPORTANT_FOR_ACCESSIBILITY_YES"),
6224 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO,
6225 to = "IMPORTANT_FOR_ACCESSIBILITY_NO")
6226 })
6227 public int getImportantForAccessibility() {
6228 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6229 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6230 }
6231
6232 /**
6233 * Sets how to determine whether this view is important for accessibility
6234 * which is if it fires accessibility events and if it is reported to
6235 * accessibility services that query the screen.
6236 *
6237 * @param mode How to determine whether this view is important for accessibility.
6238 *
6239 * @attr ref android.R.styleable#View_importantForAccessibility
6240 *
6241 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6242 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6243 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6244 */
6245 public void setImportantForAccessibility(int mode) {
6246 if (mode != getImportantForAccessibility()) {
6247 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6248 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6249 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6250 notifyAccessibilityStateChanged();
6251 }
6252 }
6253
6254 /**
6255 * Gets whether this view should be exposed for accessibility.
6256 *
6257 * @return Whether the view is exposed for accessibility.
6258 *
6259 * @hide
6260 */
6261 public boolean isImportantForAccessibility() {
6262 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6263 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6264 switch (mode) {
6265 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6266 return true;
6267 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6268 return false;
6269 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6270 return isActionableForAccessibility() || hasListenersForAccessibility();
6271 default:
6272 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6273 + mode);
6274 }
6275 }
6276
6277 /**
6278 * Gets the parent for accessibility purposes. Note that the parent for
6279 * accessibility is not necessary the immediate parent. It is the first
6280 * predecessor that is important for accessibility.
6281 *
6282 * @return The parent for accessibility purposes.
6283 */
6284 public ViewParent getParentForAccessibility() {
6285 if (mParent instanceof View) {
6286 View parentView = (View) mParent;
6287 if (parentView.includeForAccessibility()) {
6288 return mParent;
6289 } else {
6290 return mParent.getParentForAccessibility();
6291 }
6292 }
6293 return null;
6294 }
6295
6296 /**
6297 * Adds the children of a given View for accessibility. Since some Views are
6298 * not important for accessibility the children for accessibility are not
6299 * necessarily direct children of the riew, rather they are the first level of
6300 * descendants important for accessibility.
6301 *
6302 * @param children The list of children for accessibility.
6303 */
6304 public void addChildrenForAccessibility(ArrayList<View> children) {
6305 if (includeForAccessibility()) {
6306 children.add(this);
6307 }
6308 }
6309
6310 /**
6311 * Whether to regard this view for accessibility. A view is regarded for
6312 * accessibility if it is important for accessibility or the querying
6313 * accessibility service has explicitly requested that view not
6314 * important for accessibility are regarded.
6315 *
6316 * @return Whether to regard the view for accessibility.
6317 */
6318 boolean includeForAccessibility() {
6319 if (mAttachInfo != null) {
6320 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov749e7962012-04-19 17:13:46 -07006321 return isImportantForAccessibility() && isDisplayedOnScreen();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006322 } else {
Svetoslav Ganov749e7962012-04-19 17:13:46 -07006323 return isDisplayedOnScreen();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006324 }
6325 }
6326 return false;
6327 }
6328
6329 /**
6330 * Returns whether the View is considered actionable from
6331 * accessibility perspective. Such view are important for
6332 * accessiiblity.
6333 *
6334 * @return True if the view is actionable for accessibility.
6335 */
6336 private boolean isActionableForAccessibility() {
6337 return (isClickable() || isLongClickable() || isFocusable());
6338 }
6339
6340 /**
6341 * Returns whether the View has registered callbacks wich makes it
6342 * important for accessiiblity.
6343 *
6344 * @return True if the view is actionable for accessibility.
6345 */
6346 private boolean hasListenersForAccessibility() {
6347 ListenerInfo info = getListenerInfo();
6348 return mTouchDelegate != null || info.mOnKeyListener != null
6349 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6350 || info.mOnHoverListener != null || info.mOnDragListener != null;
6351 }
6352
6353 /**
6354 * Notifies accessibility services that some view's important for
6355 * accessibility state has changed. Note that such notifications
6356 * are made at most once every
6357 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6358 * to avoid unnecessary load to the system. Also once a view has
6359 * made a notifucation this method is a NOP until the notification has
6360 * been sent to clients.
6361 *
6362 * @hide
6363 *
6364 * TODO: Makse sure this method is called for any view state change
6365 * that is interesting for accessilility purposes.
6366 */
6367 public void notifyAccessibilityStateChanged() {
6368 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6369 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6370 if (mParent != null) {
6371 mParent.childAccessibilityStateChanged(this);
6372 }
6373 }
6374 }
6375
6376 /**
6377 * Reset the state indicating the this view has requested clients
6378 * interested in its accessiblity state to be notified.
6379 *
6380 * @hide
6381 */
6382 public void resetAccessibilityStateChanged() {
6383 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6384 }
6385
6386 /**
6387 * Performs the specified accessibility action on the view. For
6388 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
6389 *
6390 * @param action The action to perform.
6391 * @return Whether the action was performed.
6392 */
6393 public boolean performAccessibilityAction(int action) {
6394 switch (action) {
6395 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006396 if (isClickable()) {
6397 performClick();
6398 }
6399 } break;
6400 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6401 if (isLongClickable()) {
6402 performLongClick();
6403 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006404 } break;
6405 case AccessibilityNodeInfo.ACTION_FOCUS: {
6406 if (!hasFocus()) {
6407 // Get out of touch mode since accessibility
6408 // wants to move focus around.
6409 getViewRootImpl().ensureTouchMode(false);
6410 return requestFocus();
6411 }
6412 } break;
6413 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6414 if (hasFocus()) {
6415 clearFocus();
6416 return !isFocused();
6417 }
6418 } break;
6419 case AccessibilityNodeInfo.ACTION_SELECT: {
6420 if (!isSelected()) {
6421 setSelected(true);
6422 return isSelected();
6423 }
6424 } break;
6425 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6426 if (isSelected()) {
6427 setSelected(false);
6428 return !isSelected();
6429 }
6430 } break;
6431 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
6432 if (!isAccessibilityFocused()) {
6433 return requestAccessibilityFocus();
6434 }
6435 } break;
6436 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6437 if (isAccessibilityFocused()) {
6438 clearAccessibilityFocus();
6439 return true;
6440 }
6441 } break;
6442 }
6443 return false;
6444 }
6445
6446 /**
Romain Guya440b002010-02-24 15:57:54 -08006447 * @hide
6448 */
6449 public void dispatchStartTemporaryDetach() {
6450 onStartTemporaryDetach();
6451 }
6452
6453 /**
6454 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006455 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6456 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006457 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006458 */
6459 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006460 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006461 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006462 }
6463
6464 /**
6465 * @hide
6466 */
6467 public void dispatchFinishTemporaryDetach() {
6468 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006469 }
Romain Guy8506ab42009-06-11 17:35:47 -07006470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006471 /**
6472 * Called after {@link #onStartTemporaryDetach} when the container is done
6473 * changing the view.
6474 */
6475 public void onFinishTemporaryDetach() {
6476 }
Romain Guy8506ab42009-06-11 17:35:47 -07006477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006478 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006479 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6480 * for this view's window. Returns null if the view is not currently attached
6481 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006482 * just use the standard high-level event callbacks like
6483 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006484 */
6485 public KeyEvent.DispatcherState getKeyDispatcherState() {
6486 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6487 }
Joe Malin32736f02011-01-19 16:14:20 -08006488
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006489 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006490 * Dispatch a key event before it is processed by any input method
6491 * associated with the view hierarchy. This can be used to intercept
6492 * key events in special situations before the IME consumes them; a
6493 * typical example would be handling the BACK key to update the application's
6494 * UI instead of allowing the IME to see it and close itself.
6495 *
6496 * @param event The key event to be dispatched.
6497 * @return True if the event was handled, false otherwise.
6498 */
6499 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6500 return onKeyPreIme(event.getKeyCode(), event);
6501 }
6502
6503 /**
6504 * Dispatch a key event to the next view on the focus path. This path runs
6505 * from the top of the view tree down to the currently focused view. If this
6506 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6507 * the next node down the focus path. This method also fires any key
6508 * listeners.
6509 *
6510 * @param event The key event to be dispatched.
6511 * @return True if the event was handled, false otherwise.
6512 */
6513 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006514 if (mInputEventConsistencyVerifier != null) {
6515 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006517
Jeff Brown21bc5c92011-02-28 18:27:14 -08006518 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006519 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006520 ListenerInfo li = mListenerInfo;
6521 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6522 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006523 return true;
6524 }
6525
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006526 if (event.dispatch(this, mAttachInfo != null
6527 ? mAttachInfo.mKeyDispatchState : null, this)) {
6528 return true;
6529 }
6530
6531 if (mInputEventConsistencyVerifier != null) {
6532 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6533 }
6534 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006535 }
6536
6537 /**
6538 * Dispatches a key shortcut event.
6539 *
6540 * @param event The key event to be dispatched.
6541 * @return True if the event was handled by the view, false otherwise.
6542 */
6543 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6544 return onKeyShortcut(event.getKeyCode(), event);
6545 }
6546
6547 /**
6548 * Pass the touch screen motion event down to the target view, or this
6549 * view if it is the target.
6550 *
6551 * @param event The motion event to be dispatched.
6552 * @return True if the event was handled by the view, false otherwise.
6553 */
6554 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006555 if (mInputEventConsistencyVerifier != null) {
6556 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6557 }
6558
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006559 if (onFilterTouchEventForSecurity(event)) {
6560 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006561 ListenerInfo li = mListenerInfo;
6562 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6563 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006564 return true;
6565 }
6566
6567 if (onTouchEvent(event)) {
6568 return true;
6569 }
Jeff Brown85a31762010-09-01 17:01:00 -07006570 }
6571
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006572 if (mInputEventConsistencyVerifier != null) {
6573 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006574 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006575 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006576 }
6577
6578 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006579 * Filter the touch event to apply security policies.
6580 *
6581 * @param event The motion event to be filtered.
6582 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006583 *
Jeff Brown85a31762010-09-01 17:01:00 -07006584 * @see #getFilterTouchesWhenObscured
6585 */
6586 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006587 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006588 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6589 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6590 // Window is obscured, drop this touch.
6591 return false;
6592 }
6593 return true;
6594 }
6595
6596 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006597 * Pass a trackball motion event down to the focused view.
6598 *
6599 * @param event The motion event to be dispatched.
6600 * @return True if the event was handled by the view, false otherwise.
6601 */
6602 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006603 if (mInputEventConsistencyVerifier != null) {
6604 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6605 }
6606
Romain Guy02ccac62011-06-24 13:20:23 -07006607 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006608 }
6609
6610 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006611 * Dispatch a generic motion event.
6612 * <p>
6613 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6614 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006615 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006616 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006617 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006618 *
6619 * @param event The motion event to be dispatched.
6620 * @return True if the event was handled by the view, false otherwise.
6621 */
6622 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006623 if (mInputEventConsistencyVerifier != null) {
6624 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6625 }
6626
Jeff Browna032cc02011-03-07 16:56:21 -08006627 final int source = event.getSource();
6628 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6629 final int action = event.getAction();
6630 if (action == MotionEvent.ACTION_HOVER_ENTER
6631 || action == MotionEvent.ACTION_HOVER_MOVE
6632 || action == MotionEvent.ACTION_HOVER_EXIT) {
6633 if (dispatchHoverEvent(event)) {
6634 return true;
6635 }
6636 } else if (dispatchGenericPointerEvent(event)) {
6637 return true;
6638 }
6639 } else if (dispatchGenericFocusedEvent(event)) {
6640 return true;
6641 }
6642
Jeff Brown10b62902011-06-20 16:40:37 -07006643 if (dispatchGenericMotionEventInternal(event)) {
6644 return true;
6645 }
6646
6647 if (mInputEventConsistencyVerifier != null) {
6648 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6649 }
6650 return false;
6651 }
6652
6653 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07006654 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006655 ListenerInfo li = mListenerInfo;
6656 if (li != null && li.mOnGenericMotionListener != null
6657 && (mViewFlags & ENABLED_MASK) == ENABLED
6658 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006659 return true;
6660 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006661
6662 if (onGenericMotionEvent(event)) {
6663 return true;
6664 }
6665
6666 if (mInputEventConsistencyVerifier != null) {
6667 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6668 }
6669 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006670 }
6671
6672 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006673 * Dispatch a hover event.
6674 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07006675 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07006676 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006677 * </p>
6678 *
6679 * @param event The motion event to be dispatched.
6680 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006681 */
6682 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07006683 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006684 ListenerInfo li = mListenerInfo;
6685 if (li != null && li.mOnHoverListener != null
6686 && (mViewFlags & ENABLED_MASK) == ENABLED
6687 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07006688 return true;
6689 }
6690
Jeff Browna032cc02011-03-07 16:56:21 -08006691 return onHoverEvent(event);
6692 }
6693
6694 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006695 * Returns true if the view has a child to which it has recently sent
6696 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
6697 * it does not have a hovered child, then it must be the innermost hovered view.
6698 * @hide
6699 */
6700 protected boolean hasHoveredChild() {
6701 return false;
6702 }
6703
6704 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006705 * Dispatch a generic motion event to the view under the first pointer.
6706 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006707 * Do not call this method directly.
6708 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006709 * </p>
6710 *
6711 * @param event The motion event to be dispatched.
6712 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006713 */
6714 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
6715 return false;
6716 }
6717
6718 /**
6719 * Dispatch a generic motion event to the currently focused view.
6720 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006721 * Do not call this method directly.
6722 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006723 * </p>
6724 *
6725 * @param event The motion event to be dispatched.
6726 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006727 */
6728 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
6729 return false;
6730 }
6731
6732 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006733 * Dispatch a pointer event.
6734 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006735 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
6736 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
6737 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08006738 * and should not be expected to handle other pointing device features.
6739 * </p>
6740 *
6741 * @param event The motion event to be dispatched.
6742 * @return True if the event was handled by the view, false otherwise.
6743 * @hide
6744 */
6745 public final boolean dispatchPointerEvent(MotionEvent event) {
6746 if (event.isTouchEvent()) {
6747 return dispatchTouchEvent(event);
6748 } else {
6749 return dispatchGenericMotionEvent(event);
6750 }
6751 }
6752
6753 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006754 * Called when the window containing this view gains or loses window focus.
6755 * ViewGroups should override to route to their children.
6756 *
6757 * @param hasFocus True if the window containing this view now has focus,
6758 * false otherwise.
6759 */
6760 public void dispatchWindowFocusChanged(boolean hasFocus) {
6761 onWindowFocusChanged(hasFocus);
6762 }
6763
6764 /**
6765 * Called when the window containing this view gains or loses focus. Note
6766 * that this is separate from view focus: to receive key events, both
6767 * your view and its window must have focus. If a window is displayed
6768 * on top of yours that takes input focus, then your own window will lose
6769 * focus but the view focus will remain unchanged.
6770 *
6771 * @param hasWindowFocus True if the window containing this view now has
6772 * focus, false otherwise.
6773 */
6774 public void onWindowFocusChanged(boolean hasWindowFocus) {
6775 InputMethodManager imm = InputMethodManager.peekInstance();
6776 if (!hasWindowFocus) {
6777 if (isPressed()) {
6778 setPressed(false);
6779 }
6780 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6781 imm.focusOut(this);
6782 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006783 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08006784 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07006785 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006786 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6787 imm.focusIn(this);
6788 }
6789 refreshDrawableState();
6790 }
6791
6792 /**
6793 * Returns true if this view is in a window that currently has window focus.
6794 * Note that this is not the same as the view itself having focus.
6795 *
6796 * @return True if this view is in a window that currently has window focus.
6797 */
6798 public boolean hasWindowFocus() {
6799 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
6800 }
6801
6802 /**
Adam Powell326d8082009-12-09 15:10:07 -08006803 * Dispatch a view visibility change down the view hierarchy.
6804 * ViewGroups should override to route to their children.
6805 * @param changedView The view whose visibility changed. Could be 'this' or
6806 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006807 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6808 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006809 */
6810 protected void dispatchVisibilityChanged(View changedView, int visibility) {
6811 onVisibilityChanged(changedView, visibility);
6812 }
6813
6814 /**
6815 * Called when the visibility of the view or an ancestor of the view is changed.
6816 * @param changedView The view whose visibility changed. Could be 'this' or
6817 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006818 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6819 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006820 */
6821 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006822 if (visibility == VISIBLE) {
6823 if (mAttachInfo != null) {
6824 initialAwakenScrollBars();
6825 } else {
6826 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
6827 }
6828 }
Adam Powell326d8082009-12-09 15:10:07 -08006829 }
6830
6831 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08006832 * Dispatch a hint about whether this view is displayed. For instance, when
6833 * a View moves out of the screen, it might receives a display hint indicating
6834 * the view is not displayed. Applications should not <em>rely</em> on this hint
6835 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006836 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006837 * @param hint A hint about whether or not this view is displayed:
6838 * {@link #VISIBLE} or {@link #INVISIBLE}.
6839 */
6840 public void dispatchDisplayHint(int hint) {
6841 onDisplayHint(hint);
6842 }
6843
6844 /**
6845 * Gives this view a hint about whether is displayed or not. For instance, when
6846 * a View moves out of the screen, it might receives a display hint indicating
6847 * the view is not displayed. Applications should not <em>rely</em> on this hint
6848 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006849 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006850 * @param hint A hint about whether or not this view is displayed:
6851 * {@link #VISIBLE} or {@link #INVISIBLE}.
6852 */
6853 protected void onDisplayHint(int hint) {
6854 }
6855
6856 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006857 * Dispatch a window visibility change down the view hierarchy.
6858 * ViewGroups should override to route to their children.
6859 *
6860 * @param visibility The new visibility of the window.
6861 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006862 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006863 */
6864 public void dispatchWindowVisibilityChanged(int visibility) {
6865 onWindowVisibilityChanged(visibility);
6866 }
6867
6868 /**
6869 * Called when the window containing has change its visibility
6870 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
6871 * that this tells you whether or not your window is being made visible
6872 * to the window manager; this does <em>not</em> tell you whether or not
6873 * your window is obscured by other windows on the screen, even if it
6874 * is itself visible.
6875 *
6876 * @param visibility The new visibility of the window.
6877 */
6878 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006879 if (visibility == VISIBLE) {
6880 initialAwakenScrollBars();
6881 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006882 }
6883
6884 /**
6885 * Returns the current visibility of the window this view is attached to
6886 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
6887 *
6888 * @return Returns the current visibility of the view's window.
6889 */
6890 public int getWindowVisibility() {
6891 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
6892 }
6893
6894 /**
6895 * Retrieve the overall visible display size in which the window this view is
6896 * attached to has been positioned in. This takes into account screen
6897 * decorations above the window, for both cases where the window itself
6898 * is being position inside of them or the window is being placed under
6899 * then and covered insets are used for the window to position its content
6900 * inside. In effect, this tells you the available area where content can
6901 * be placed and remain visible to users.
6902 *
6903 * <p>This function requires an IPC back to the window manager to retrieve
6904 * the requested information, so should not be used in performance critical
6905 * code like drawing.
6906 *
6907 * @param outRect Filled in with the visible display frame. If the view
6908 * is not attached to a window, this is simply the raw display size.
6909 */
6910 public void getWindowVisibleDisplayFrame(Rect outRect) {
6911 if (mAttachInfo != null) {
6912 try {
6913 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
6914 } catch (RemoteException e) {
6915 return;
6916 }
6917 // XXX This is really broken, and probably all needs to be done
6918 // in the window manager, and we need to know more about whether
6919 // we want the area behind or in front of the IME.
6920 final Rect insets = mAttachInfo.mVisibleInsets;
6921 outRect.left += insets.left;
6922 outRect.top += insets.top;
6923 outRect.right -= insets.right;
6924 outRect.bottom -= insets.bottom;
6925 return;
6926 }
6927 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006928 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006929 }
6930
6931 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006932 * Dispatch a notification about a resource configuration change down
6933 * the view hierarchy.
6934 * ViewGroups should override to route to their children.
6935 *
6936 * @param newConfig The new resource configuration.
6937 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006938 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006939 */
6940 public void dispatchConfigurationChanged(Configuration newConfig) {
6941 onConfigurationChanged(newConfig);
6942 }
6943
6944 /**
6945 * Called when the current configuration of the resources being used
6946 * by the application have changed. You can use this to decide when
6947 * to reload resources that can changed based on orientation and other
6948 * configuration characterstics. You only need to use this if you are
6949 * not relying on the normal {@link android.app.Activity} mechanism of
6950 * recreating the activity instance upon a configuration change.
6951 *
6952 * @param newConfig The new resource configuration.
6953 */
6954 protected void onConfigurationChanged(Configuration newConfig) {
6955 }
6956
6957 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006958 * Private function to aggregate all per-view attributes in to the view
6959 * root.
6960 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006961 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6962 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006963 }
6964
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006965 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6966 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08006967 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006968 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006969 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006970 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006971 ListenerInfo li = mListenerInfo;
6972 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006973 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006974 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006975 }
6976 }
6977
6978 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006979 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006980 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006981 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6982 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006983 ai.mRecomputeGlobalAttributes = true;
6984 }
6985 }
6986 }
6987
6988 /**
6989 * Returns whether the device is currently in touch mode. Touch mode is entered
6990 * once the user begins interacting with the device by touch, and affects various
6991 * things like whether focus is always visible to the user.
6992 *
6993 * @return Whether the device is in touch mode.
6994 */
6995 @ViewDebug.ExportedProperty
6996 public boolean isInTouchMode() {
6997 if (mAttachInfo != null) {
6998 return mAttachInfo.mInTouchMode;
6999 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007000 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007001 }
7002 }
7003
7004 /**
7005 * Returns the context the view is running in, through which it can
7006 * access the current theme, resources, etc.
7007 *
7008 * @return The view's Context.
7009 */
7010 @ViewDebug.CapturedViewProperty
7011 public final Context getContext() {
7012 return mContext;
7013 }
7014
7015 /**
7016 * Handle a key event before it is processed by any input method
7017 * associated with the view hierarchy. This can be used to intercept
7018 * key events in special situations before the IME consumes them; a
7019 * typical example would be handling the BACK key to update the application's
7020 * UI instead of allowing the IME to see it and close itself.
7021 *
7022 * @param keyCode The value in event.getKeyCode().
7023 * @param event Description of the key event.
7024 * @return If you handled the event, return true. If you want to allow the
7025 * event to be handled by the next receiver, return false.
7026 */
7027 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7028 return false;
7029 }
7030
7031 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007032 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7033 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007034 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7035 * is released, if the view is enabled and clickable.
7036 *
7037 * @param keyCode A key code that represents the button pressed, from
7038 * {@link android.view.KeyEvent}.
7039 * @param event The KeyEvent object that defines the button action.
7040 */
7041 public boolean onKeyDown(int keyCode, KeyEvent event) {
7042 boolean result = false;
7043
7044 switch (keyCode) {
7045 case KeyEvent.KEYCODE_DPAD_CENTER:
7046 case KeyEvent.KEYCODE_ENTER: {
7047 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7048 return true;
7049 }
7050 // Long clickable items don't necessarily have to be clickable
7051 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7052 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7053 (event.getRepeatCount() == 0)) {
7054 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007055 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007056 return true;
7057 }
7058 break;
7059 }
7060 }
7061 return result;
7062 }
7063
7064 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007065 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7066 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7067 * the event).
7068 */
7069 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7070 return false;
7071 }
7072
7073 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007074 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7075 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007076 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7077 * {@link KeyEvent#KEYCODE_ENTER} is released.
7078 *
7079 * @param keyCode A key code that represents the button pressed, from
7080 * {@link android.view.KeyEvent}.
7081 * @param event The KeyEvent object that defines the button action.
7082 */
7083 public boolean onKeyUp(int keyCode, KeyEvent event) {
7084 boolean result = false;
7085
7086 switch (keyCode) {
7087 case KeyEvent.KEYCODE_DPAD_CENTER:
7088 case KeyEvent.KEYCODE_ENTER: {
7089 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7090 return true;
7091 }
7092 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7093 setPressed(false);
7094
7095 if (!mHasPerformedLongPress) {
7096 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007097 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007098
7099 result = performClick();
7100 }
7101 }
7102 break;
7103 }
7104 }
7105 return result;
7106 }
7107
7108 /**
7109 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7110 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7111 * the event).
7112 *
7113 * @param keyCode A key code that represents the button pressed, from
7114 * {@link android.view.KeyEvent}.
7115 * @param repeatCount The number of times the action was made.
7116 * @param event The KeyEvent object that defines the button action.
7117 */
7118 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7119 return false;
7120 }
7121
7122 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007123 * Called on the focused view when a key shortcut event is not handled.
7124 * Override this method to implement local key shortcuts for the View.
7125 * Key shortcuts can also be implemented by setting the
7126 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007127 *
7128 * @param keyCode The value in event.getKeyCode().
7129 * @param event Description of the key event.
7130 * @return If you handled the event, return true. If you want to allow the
7131 * event to be handled by the next receiver, return false.
7132 */
7133 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7134 return false;
7135 }
7136
7137 /**
7138 * Check whether the called view is a text editor, in which case it
7139 * would make sense to automatically display a soft input window for
7140 * it. Subclasses should override this if they implement
7141 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007142 * a call on that method would return a non-null InputConnection, and
7143 * they are really a first-class editor that the user would normally
7144 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007145 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007146 * <p>The default implementation always returns false. This does
7147 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7148 * will not be called or the user can not otherwise perform edits on your
7149 * view; it is just a hint to the system that this is not the primary
7150 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007151 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007152 * @return Returns true if this view is a text editor, else false.
7153 */
7154 public boolean onCheckIsTextEditor() {
7155 return false;
7156 }
Romain Guy8506ab42009-06-11 17:35:47 -07007157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007158 /**
7159 * Create a new InputConnection for an InputMethod to interact
7160 * with the view. The default implementation returns null, since it doesn't
7161 * support input methods. You can override this to implement such support.
7162 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007163 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007164 * <p>When implementing this, you probably also want to implement
7165 * {@link #onCheckIsTextEditor()} to indicate you will return a
7166 * non-null InputConnection.
7167 *
7168 * @param outAttrs Fill in with attribute information about the connection.
7169 */
7170 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7171 return null;
7172 }
7173
7174 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007175 * Called by the {@link android.view.inputmethod.InputMethodManager}
7176 * when a view who is not the current
7177 * input connection target is trying to make a call on the manager. The
7178 * default implementation returns false; you can override this to return
7179 * true for certain views if you are performing InputConnection proxying
7180 * to them.
7181 * @param view The View that is making the InputMethodManager call.
7182 * @return Return true to allow the call, false to reject.
7183 */
7184 public boolean checkInputConnectionProxy(View view) {
7185 return false;
7186 }
Romain Guy8506ab42009-06-11 17:35:47 -07007187
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007188 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007189 * Show the context menu for this view. It is not safe to hold on to the
7190 * menu after returning from this method.
7191 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007192 * You should normally not overload this method. Overload
7193 * {@link #onCreateContextMenu(ContextMenu)} or define an
7194 * {@link OnCreateContextMenuListener} to add items to the context menu.
7195 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007196 * @param menu The context menu to populate
7197 */
7198 public void createContextMenu(ContextMenu menu) {
7199 ContextMenuInfo menuInfo = getContextMenuInfo();
7200
7201 // Sets the current menu info so all items added to menu will have
7202 // my extra info set.
7203 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7204
7205 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007206 ListenerInfo li = mListenerInfo;
7207 if (li != null && li.mOnCreateContextMenuListener != null) {
7208 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007209 }
7210
7211 // Clear the extra information so subsequent items that aren't mine don't
7212 // have my extra info.
7213 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7214
7215 if (mParent != null) {
7216 mParent.createContextMenu(menu);
7217 }
7218 }
7219
7220 /**
7221 * Views should implement this if they have extra information to associate
7222 * with the context menu. The return result is supplied as a parameter to
7223 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7224 * callback.
7225 *
7226 * @return Extra information about the item for which the context menu
7227 * should be shown. This information will vary across different
7228 * subclasses of View.
7229 */
7230 protected ContextMenuInfo getContextMenuInfo() {
7231 return null;
7232 }
7233
7234 /**
7235 * Views should implement this if the view itself is going to add items to
7236 * the context menu.
7237 *
7238 * @param menu the context menu to populate
7239 */
7240 protected void onCreateContextMenu(ContextMenu menu) {
7241 }
7242
7243 /**
7244 * Implement this method to handle trackball motion events. The
7245 * <em>relative</em> movement of the trackball since the last event
7246 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7247 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7248 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7249 * they will often be fractional values, representing the more fine-grained
7250 * movement information available from a trackball).
7251 *
7252 * @param event The motion event.
7253 * @return True if the event was handled, false otherwise.
7254 */
7255 public boolean onTrackballEvent(MotionEvent event) {
7256 return false;
7257 }
7258
7259 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007260 * Implement this method to handle generic motion events.
7261 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007262 * Generic motion events describe joystick movements, mouse hovers, track pad
7263 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007264 * {@link MotionEvent#getSource() source} of the motion event specifies
7265 * the class of input that was received. Implementations of this method
7266 * must examine the bits in the source before processing the event.
7267 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007268 * </p><p>
7269 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7270 * are delivered to the view under the pointer. All other generic motion events are
7271 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007272 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007273 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007274 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007275 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7276 * // process the joystick movement...
7277 * return true;
7278 * }
7279 * }
7280 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7281 * switch (event.getAction()) {
7282 * case MotionEvent.ACTION_HOVER_MOVE:
7283 * // process the mouse hover movement...
7284 * return true;
7285 * case MotionEvent.ACTION_SCROLL:
7286 * // process the scroll wheel movement...
7287 * return true;
7288 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007289 * }
7290 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007291 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007292 *
7293 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007294 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007295 */
7296 public boolean onGenericMotionEvent(MotionEvent event) {
7297 return false;
7298 }
7299
7300 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007301 * Implement this method to handle hover events.
7302 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007303 * This method is called whenever a pointer is hovering into, over, or out of the
7304 * bounds of a view and the view is not currently being touched.
7305 * Hover events are represented as pointer events with action
7306 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7307 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7308 * </p>
7309 * <ul>
7310 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7311 * when the pointer enters the bounds of the view.</li>
7312 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7313 * when the pointer has already entered the bounds of the view and has moved.</li>
7314 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7315 * when the pointer has exited the bounds of the view or when the pointer is
7316 * about to go down due to a button click, tap, or similar user action that
7317 * causes the view to be touched.</li>
7318 * </ul>
7319 * <p>
7320 * The view should implement this method to return true to indicate that it is
7321 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007322 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007323 * The default implementation calls {@link #setHovered} to update the hovered state
7324 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007325 * is enabled and is clickable. The default implementation also sends hover
7326 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007327 * </p>
7328 *
7329 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007330 * @return True if the view handled the hover event.
7331 *
7332 * @see #isHovered
7333 * @see #setHovered
7334 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007335 */
7336 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007337 // The root view may receive hover (or touch) events that are outside the bounds of
7338 // the window. This code ensures that we only send accessibility events for
7339 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007340 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007341 if (!mSendingHoverAccessibilityEvents) {
7342 if ((action == MotionEvent.ACTION_HOVER_ENTER
7343 || action == MotionEvent.ACTION_HOVER_MOVE)
7344 && !hasHoveredChild()
7345 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007346 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007347 mSendingHoverAccessibilityEvents = true;
7348 requestAccessibilityFocusFromHover((int) event.getX(), (int) event.getY());
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007349 }
7350 } else {
7351 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007352 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007353 && !pointInView(event.getX(), event.getY()))) {
7354 mSendingHoverAccessibilityEvents = false;
7355 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007356 // If the window does not have input focus we take away accessibility
7357 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007358 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007359 getViewRootImpl().setAccessibilityFocusedHost(null);
7360 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007361 }
Jeff Browna1b24182011-07-28 13:38:24 -07007362 }
7363
Jeff Brown87b7f802011-06-21 18:35:45 -07007364 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007365 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007366 case MotionEvent.ACTION_HOVER_ENTER:
7367 setHovered(true);
7368 break;
7369 case MotionEvent.ACTION_HOVER_EXIT:
7370 setHovered(false);
7371 break;
7372 }
Jeff Browna1b24182011-07-28 13:38:24 -07007373
7374 // Dispatch the event to onGenericMotionEvent before returning true.
7375 // This is to provide compatibility with existing applications that
7376 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7377 // break because of the new default handling for hoverable views
7378 // in onHoverEvent.
7379 // Note that onGenericMotionEvent will be called by default when
7380 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7381 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007382 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007383 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007384
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007385 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007386 }
7387
7388 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007389 * Returns true if the view should handle {@link #onHoverEvent}
7390 * by calling {@link #setHovered} to change its hovered state.
7391 *
7392 * @return True if the view is hoverable.
7393 */
7394 private boolean isHoverable() {
7395 final int viewFlags = mViewFlags;
7396 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7397 return false;
7398 }
7399
7400 return (viewFlags & CLICKABLE) == CLICKABLE
7401 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7402 }
7403
7404 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007405 * Returns true if the view is currently hovered.
7406 *
7407 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007408 *
7409 * @see #setHovered
7410 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007411 */
Jeff Brown10b62902011-06-20 16:40:37 -07007412 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007413 public boolean isHovered() {
7414 return (mPrivateFlags & HOVERED) != 0;
7415 }
7416
7417 /**
7418 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007419 * <p>
7420 * Calling this method also changes the drawable state of the view. This
7421 * enables the view to react to hover by using different drawable resources
7422 * to change its appearance.
7423 * </p><p>
7424 * The {@link #onHoverChanged} method is called when the hovered state changes.
7425 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007426 *
7427 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007428 *
7429 * @see #isHovered
7430 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007431 */
7432 public void setHovered(boolean hovered) {
7433 if (hovered) {
7434 if ((mPrivateFlags & HOVERED) == 0) {
7435 mPrivateFlags |= HOVERED;
7436 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007437 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007438 }
7439 } else {
7440 if ((mPrivateFlags & HOVERED) != 0) {
7441 mPrivateFlags &= ~HOVERED;
7442 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007443 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007444 }
7445 }
7446 }
7447
7448 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007449 * Implement this method to handle hover state changes.
7450 * <p>
7451 * This method is called whenever the hover state changes as a result of a
7452 * call to {@link #setHovered}.
7453 * </p>
7454 *
7455 * @param hovered The current hover state, as returned by {@link #isHovered}.
7456 *
7457 * @see #isHovered
7458 * @see #setHovered
7459 */
7460 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007461 }
7462
7463 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007464 * Implement this method to handle touch screen motion events.
7465 *
7466 * @param event The motion event.
7467 * @return True if the event was handled, false otherwise.
7468 */
7469 public boolean onTouchEvent(MotionEvent event) {
7470 final int viewFlags = mViewFlags;
7471
7472 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007473 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007474 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007476 // A disabled view that is clickable still consumes the touch
7477 // events, it just doesn't respond to them.
7478 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7479 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7480 }
7481
7482 if (mTouchDelegate != null) {
7483 if (mTouchDelegate.onTouchEvent(event)) {
7484 return true;
7485 }
7486 }
7487
7488 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7489 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7490 switch (event.getAction()) {
7491 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007492 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7493 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007494 // take focus if we don't have it already and we should in
7495 // touch mode.
7496 boolean focusTaken = false;
7497 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7498 focusTaken = requestFocus();
7499 }
7500
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007501 if (prepressed) {
7502 // The button is being released before we actually
7503 // showed it as pressed. Make it show the pressed
7504 // state now (before scheduling the click) to ensure
7505 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007506 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007507 }
Joe Malin32736f02011-01-19 16:14:20 -08007508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007509 if (!mHasPerformedLongPress) {
7510 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007511 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007512
7513 // Only perform take click actions if we were in the pressed state
7514 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007515 // Use a Runnable and post this rather than calling
7516 // performClick directly. This lets other visual state
7517 // of the view update before click actions start.
7518 if (mPerformClick == null) {
7519 mPerformClick = new PerformClick();
7520 }
7521 if (!post(mPerformClick)) {
7522 performClick();
7523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007524 }
7525 }
7526
7527 if (mUnsetPressedState == null) {
7528 mUnsetPressedState = new UnsetPressedState();
7529 }
7530
Adam Powelle14579b2009-12-16 18:39:52 -08007531 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007532 postDelayed(mUnsetPressedState,
7533 ViewConfiguration.getPressedStateDuration());
7534 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007535 // If the post failed, unpress right now
7536 mUnsetPressedState.run();
7537 }
Adam Powelle14579b2009-12-16 18:39:52 -08007538 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007539 }
7540 break;
7541
7542 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007543 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007544
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007545 if (performButtonActionOnTouchDown(event)) {
7546 break;
7547 }
7548
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007549 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007550 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007551
7552 // For views inside a scrolling container, delay the pressed feedback for
7553 // a short period in case this is a scroll.
7554 if (isInScrollingContainer) {
7555 mPrivateFlags |= PREPRESSED;
7556 if (mPendingCheckForTap == null) {
7557 mPendingCheckForTap = new CheckForTap();
7558 }
7559 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7560 } else {
7561 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007562 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007563 checkForLongClick(0);
7564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007565 break;
7566
7567 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08007568 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08007569 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007570 break;
7571
7572 case MotionEvent.ACTION_MOVE:
7573 final int x = (int) event.getX();
7574 final int y = (int) event.getY();
7575
7576 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007577 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007578 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007579 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007580 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007581 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007582 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007583
Adam Powell4d6f0662012-02-21 15:11:11 -08007584 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007586 }
7587 break;
7588 }
7589 return true;
7590 }
7591
7592 return false;
7593 }
7594
7595 /**
Adam Powell10298662011-08-14 18:26:30 -07007596 * @hide
7597 */
7598 public boolean isInScrollingContainer() {
7599 ViewParent p = getParent();
7600 while (p != null && p instanceof ViewGroup) {
7601 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7602 return true;
7603 }
7604 p = p.getParent();
7605 }
7606 return false;
7607 }
7608
7609 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007610 * Remove the longpress detection timer.
7611 */
7612 private void removeLongPressCallback() {
7613 if (mPendingCheckForLongPress != null) {
7614 removeCallbacks(mPendingCheckForLongPress);
7615 }
7616 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007617
7618 /**
7619 * Remove the pending click action
7620 */
7621 private void removePerformClickCallback() {
7622 if (mPerformClick != null) {
7623 removeCallbacks(mPerformClick);
7624 }
7625 }
7626
Adam Powelle14579b2009-12-16 18:39:52 -08007627 /**
Romain Guya440b002010-02-24 15:57:54 -08007628 * Remove the prepress detection timer.
7629 */
7630 private void removeUnsetPressCallback() {
7631 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7632 setPressed(false);
7633 removeCallbacks(mUnsetPressedState);
7634 }
7635 }
7636
7637 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007638 * Remove the tap detection timer.
7639 */
7640 private void removeTapCallback() {
7641 if (mPendingCheckForTap != null) {
7642 mPrivateFlags &= ~PREPRESSED;
7643 removeCallbacks(mPendingCheckForTap);
7644 }
7645 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007646
7647 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007648 * Cancels a pending long press. Your subclass can use this if you
7649 * want the context menu to come up if the user presses and holds
7650 * at the same place, but you don't want it to come up if they press
7651 * and then move around enough to cause scrolling.
7652 */
7653 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05007654 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08007655
7656 /*
7657 * The prepressed state handled by the tap callback is a display
7658 * construct, but the tap callback will post a long press callback
7659 * less its own timeout. Remove it here.
7660 */
7661 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007662 }
7663
7664 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07007665 * Remove the pending callback for sending a
7666 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
7667 */
7668 private void removeSendViewScrolledAccessibilityEventCallback() {
7669 if (mSendViewScrolledAccessibilityEvent != null) {
7670 removeCallbacks(mSendViewScrolledAccessibilityEvent);
7671 }
7672 }
7673
7674 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007675 * Sets the TouchDelegate for this View.
7676 */
7677 public void setTouchDelegate(TouchDelegate delegate) {
7678 mTouchDelegate = delegate;
7679 }
7680
7681 /**
7682 * Gets the TouchDelegate for this View.
7683 */
7684 public TouchDelegate getTouchDelegate() {
7685 return mTouchDelegate;
7686 }
7687
7688 /**
7689 * Set flags controlling behavior of this view.
7690 *
7691 * @param flags Constant indicating the value which should be set
7692 * @param mask Constant indicating the bit range that should be changed
7693 */
7694 void setFlags(int flags, int mask) {
7695 int old = mViewFlags;
7696 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
7697
7698 int changed = mViewFlags ^ old;
7699 if (changed == 0) {
7700 return;
7701 }
7702 int privateFlags = mPrivateFlags;
7703
7704 /* Check if the FOCUSABLE bit has changed */
7705 if (((changed & FOCUSABLE_MASK) != 0) &&
7706 ((privateFlags & HAS_BOUNDS) !=0)) {
7707 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
7708 && ((privateFlags & FOCUSED) != 0)) {
7709 /* Give up focus if we are no longer focusable */
7710 clearFocus();
7711 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
7712 && ((privateFlags & FOCUSED) == 0)) {
7713 /*
7714 * Tell the view system that we are now available to take focus
7715 * if no one else already has it.
7716 */
7717 if (mParent != null) mParent.focusableViewAvailable(this);
7718 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007719 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7720 notifyAccessibilityStateChanged();
7721 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007722 }
7723
7724 if ((flags & VISIBILITY_MASK) == VISIBLE) {
7725 if ((changed & VISIBILITY_MASK) != 0) {
7726 /*
Chet Haase4324ead2011-08-24 21:31:03 -07007727 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07007728 * it was not visible. Marking it drawn ensures that the invalidation will
7729 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007730 */
Chet Haaseaceafe62011-08-26 15:44:33 -07007731 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07007732 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007733
7734 needGlobalAttributesUpdate(true);
7735
7736 // a view becoming visible is worth notifying the parent
7737 // about in case nothing has focus. even if this specific view
7738 // isn't focusable, it may contain something that is, so let
7739 // the root view try to give this focus if nothing else does.
7740 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
7741 mParent.focusableViewAvailable(this);
7742 }
7743 }
7744 }
7745
7746 /* Check if the GONE bit has changed */
7747 if ((changed & GONE) != 0) {
7748 needGlobalAttributesUpdate(false);
7749 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007750
Romain Guyecd80ee2009-12-03 17:13:02 -08007751 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
7752 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007753 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08007754 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07007755 if (mParent instanceof View) {
7756 // GONE views noop invalidation, so invalidate the parent
7757 ((View) mParent).invalidate(true);
7758 }
7759 // Mark the view drawn to ensure that it gets invalidated properly the next
7760 // time it is visible and gets invalidated
7761 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007762 }
7763 if (mAttachInfo != null) {
7764 mAttachInfo.mViewVisibilityChanged = true;
7765 }
7766 }
7767
7768 /* Check if the VISIBLE bit has changed */
7769 if ((changed & INVISIBLE) != 0) {
7770 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07007771 /*
7772 * If this view is becoming invisible, set the DRAWN flag so that
7773 * the next invalidate() will not be skipped.
7774 */
7775 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007776
7777 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007778 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007779 if (getRootView() != this) {
7780 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007781 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007782 }
7783 }
7784 if (mAttachInfo != null) {
7785 mAttachInfo.mViewVisibilityChanged = true;
7786 }
7787 }
7788
Adam Powell326d8082009-12-09 15:10:07 -08007789 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07007790 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08007791 ((ViewGroup) mParent).onChildVisibilityChanged(this,
7792 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08007793 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07007794 } else if (mParent != null) {
7795 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07007796 }
Adam Powell326d8082009-12-09 15:10:07 -08007797 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
7798 }
7799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007800 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
7801 destroyDrawingCache();
7802 }
7803
7804 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
7805 destroyDrawingCache();
7806 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007807 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007808 }
7809
7810 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
7811 destroyDrawingCache();
7812 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7813 }
7814
7815 if ((changed & DRAW_MASK) != 0) {
7816 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07007817 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007818 mPrivateFlags &= ~SKIP_DRAW;
7819 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
7820 } else {
7821 mPrivateFlags |= SKIP_DRAW;
7822 }
7823 } else {
7824 mPrivateFlags &= ~SKIP_DRAW;
7825 }
7826 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08007827 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007828 }
7829
7830 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08007831 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007832 mParent.recomputeViewAttributes(this);
7833 }
7834 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007835
7836 if (AccessibilityManager.getInstance(mContext).isEnabled()
7837 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
7838 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
7839 notifyAccessibilityStateChanged();
7840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007841 }
7842
7843 /**
7844 * Change the view's z order in the tree, so it's on top of other sibling
7845 * views
7846 */
7847 public void bringToFront() {
7848 if (mParent != null) {
7849 mParent.bringChildToFront(this);
7850 }
7851 }
7852
7853 /**
7854 * This is called in response to an internal scroll in this view (i.e., the
7855 * view scrolled its own contents). This is typically as a result of
7856 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
7857 * called.
7858 *
7859 * @param l Current horizontal scroll origin.
7860 * @param t Current vertical scroll origin.
7861 * @param oldl Previous horizontal scroll origin.
7862 * @param oldt Previous vertical scroll origin.
7863 */
7864 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07007865 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7866 postSendViewScrolledAccessibilityEventCallback();
7867 }
7868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007869 mBackgroundSizeChanged = true;
7870
7871 final AttachInfo ai = mAttachInfo;
7872 if (ai != null) {
7873 ai.mViewScrollChanged = true;
7874 }
7875 }
7876
7877 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007878 * Interface definition for a callback to be invoked when the layout bounds of a view
7879 * changes due to layout processing.
7880 */
7881 public interface OnLayoutChangeListener {
7882 /**
7883 * Called when the focus state of a view has changed.
7884 *
7885 * @param v The view whose state has changed.
7886 * @param left The new value of the view's left property.
7887 * @param top The new value of the view's top property.
7888 * @param right The new value of the view's right property.
7889 * @param bottom The new value of the view's bottom property.
7890 * @param oldLeft The previous value of the view's left property.
7891 * @param oldTop The previous value of the view's top property.
7892 * @param oldRight The previous value of the view's right property.
7893 * @param oldBottom The previous value of the view's bottom property.
7894 */
7895 void onLayoutChange(View v, int left, int top, int right, int bottom,
7896 int oldLeft, int oldTop, int oldRight, int oldBottom);
7897 }
7898
7899 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007900 * This is called during layout when the size of this view has changed. If
7901 * you were just added to the view hierarchy, you're called with the old
7902 * values of 0.
7903 *
7904 * @param w Current width of this view.
7905 * @param h Current height of this view.
7906 * @param oldw Old width of this view.
7907 * @param oldh Old height of this view.
7908 */
7909 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
7910 }
7911
7912 /**
7913 * Called by draw to draw the child views. This may be overridden
7914 * by derived classes to gain control just before its children are drawn
7915 * (but after its own view has been drawn).
7916 * @param canvas the canvas on which to draw the view
7917 */
7918 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007920 }
7921
7922 /**
7923 * Gets the parent of this view. Note that the parent is a
7924 * ViewParent and not necessarily a View.
7925 *
7926 * @return Parent of this view.
7927 */
7928 public final ViewParent getParent() {
7929 return mParent;
7930 }
7931
7932 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007933 * Set the horizontal scrolled position of your view. This will cause a call to
7934 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7935 * invalidated.
7936 * @param value the x position to scroll to
7937 */
7938 public void setScrollX(int value) {
7939 scrollTo(value, mScrollY);
7940 }
7941
7942 /**
7943 * Set the vertical scrolled position of your view. This will cause a call to
7944 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7945 * invalidated.
7946 * @param value the y position to scroll to
7947 */
7948 public void setScrollY(int value) {
7949 scrollTo(mScrollX, value);
7950 }
7951
7952 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007953 * Return the scrolled left position of this view. This is the left edge of
7954 * the displayed part of your view. You do not need to draw any pixels
7955 * farther left, since those are outside of the frame of your view on
7956 * screen.
7957 *
7958 * @return The left edge of the displayed part of your view, in pixels.
7959 */
7960 public final int getScrollX() {
7961 return mScrollX;
7962 }
7963
7964 /**
7965 * Return the scrolled top position of this view. This is the top edge of
7966 * the displayed part of your view. You do not need to draw any pixels above
7967 * it, since those are outside of the frame of your view on screen.
7968 *
7969 * @return The top edge of the displayed part of your view, in pixels.
7970 */
7971 public final int getScrollY() {
7972 return mScrollY;
7973 }
7974
7975 /**
7976 * Return the width of the your view.
7977 *
7978 * @return The width of your view, in pixels.
7979 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007980 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007981 public final int getWidth() {
7982 return mRight - mLeft;
7983 }
7984
7985 /**
7986 * Return the height of your view.
7987 *
7988 * @return The height of your view, in pixels.
7989 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007990 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007991 public final int getHeight() {
7992 return mBottom - mTop;
7993 }
7994
7995 /**
7996 * Return the visible drawing bounds of your view. Fills in the output
7997 * rectangle with the values from getScrollX(), getScrollY(),
7998 * getWidth(), and getHeight().
7999 *
8000 * @param outRect The (scrolled) drawing bounds of the view.
8001 */
8002 public void getDrawingRect(Rect outRect) {
8003 outRect.left = mScrollX;
8004 outRect.top = mScrollY;
8005 outRect.right = mScrollX + (mRight - mLeft);
8006 outRect.bottom = mScrollY + (mBottom - mTop);
8007 }
8008
8009 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008010 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8011 * raw width component (that is the result is masked by
8012 * {@link #MEASURED_SIZE_MASK}).
8013 *
8014 * @return The raw measured width of this view.
8015 */
8016 public final int getMeasuredWidth() {
8017 return mMeasuredWidth & MEASURED_SIZE_MASK;
8018 }
8019
8020 /**
8021 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008022 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008023 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008024 * This should be used during measurement and layout calculations only. Use
8025 * {@link #getWidth()} to see how wide a view is after layout.
8026 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008027 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008028 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008029 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008030 return mMeasuredWidth;
8031 }
8032
8033 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008034 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8035 * raw width component (that is the result is masked by
8036 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008037 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008038 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008039 */
8040 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008041 return mMeasuredHeight & MEASURED_SIZE_MASK;
8042 }
8043
8044 /**
8045 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008046 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008047 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8048 * This should be used during measurement and layout calculations only. Use
8049 * {@link #getHeight()} to see how wide a view is after layout.
8050 *
8051 * @return The measured width of this view as a bit mask.
8052 */
8053 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008054 return mMeasuredHeight;
8055 }
8056
8057 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008058 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8059 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8060 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8061 * and the height component is at the shifted bits
8062 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8063 */
8064 public final int getMeasuredState() {
8065 return (mMeasuredWidth&MEASURED_STATE_MASK)
8066 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8067 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8068 }
8069
8070 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008071 * The transform matrix of this view, which is calculated based on the current
8072 * roation, scale, and pivot properties.
8073 *
8074 * @see #getRotation()
8075 * @see #getScaleX()
8076 * @see #getScaleY()
8077 * @see #getPivotX()
8078 * @see #getPivotY()
8079 * @return The current transform matrix for the view
8080 */
8081 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008082 if (mTransformationInfo != null) {
8083 updateMatrix();
8084 return mTransformationInfo.mMatrix;
8085 }
8086 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008087 }
8088
8089 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008090 * Utility function to determine if the value is far enough away from zero to be
8091 * considered non-zero.
8092 * @param value A floating point value to check for zero-ness
8093 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8094 */
8095 private static boolean nonzero(float value) {
8096 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8097 }
8098
8099 /**
Jeff Brown86671742010-09-30 20:00:15 -07008100 * Returns true if the transform matrix is the identity matrix.
8101 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008102 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008103 * @return True if the transform matrix is the identity matrix, false otherwise.
8104 */
Jeff Brown86671742010-09-30 20:00:15 -07008105 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008106 if (mTransformationInfo != null) {
8107 updateMatrix();
8108 return mTransformationInfo.mMatrixIsIdentity;
8109 }
8110 return true;
8111 }
8112
8113 void ensureTransformationInfo() {
8114 if (mTransformationInfo == null) {
8115 mTransformationInfo = new TransformationInfo();
8116 }
Jeff Brown86671742010-09-30 20:00:15 -07008117 }
8118
8119 /**
8120 * Recomputes the transform matrix if necessary.
8121 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008122 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008123 final TransformationInfo info = mTransformationInfo;
8124 if (info == null) {
8125 return;
8126 }
8127 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008128 // transform-related properties have changed since the last time someone
8129 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008130
8131 // Figure out if we need to update the pivot point
8132 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008133 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8134 info.mPrevWidth = mRight - mLeft;
8135 info.mPrevHeight = mBottom - mTop;
8136 info.mPivotX = info.mPrevWidth / 2f;
8137 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008138 }
8139 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008140 info.mMatrix.reset();
8141 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8142 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8143 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8144 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008145 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008146 if (info.mCamera == null) {
8147 info.mCamera = new Camera();
8148 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008149 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008150 info.mCamera.save();
8151 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8152 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8153 info.mCamera.getMatrix(info.matrix3D);
8154 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8155 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8156 info.mPivotY + info.mTranslationY);
8157 info.mMatrix.postConcat(info.matrix3D);
8158 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008159 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008160 info.mMatrixDirty = false;
8161 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8162 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008163 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008164 }
8165
8166 /**
8167 * Utility method to retrieve the inverse of the current mMatrix property.
8168 * We cache the matrix to avoid recalculating it when transform properties
8169 * have not changed.
8170 *
8171 * @return The inverse of the current matrix of this view.
8172 */
Jeff Brown86671742010-09-30 20:00:15 -07008173 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008174 final TransformationInfo info = mTransformationInfo;
8175 if (info != null) {
8176 updateMatrix();
8177 if (info.mInverseMatrixDirty) {
8178 if (info.mInverseMatrix == null) {
8179 info.mInverseMatrix = new Matrix();
8180 }
8181 info.mMatrix.invert(info.mInverseMatrix);
8182 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008183 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008184 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008185 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008186 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008187 }
8188
8189 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008190 * Gets the distance along the Z axis from the camera to this view.
8191 *
8192 * @see #setCameraDistance(float)
8193 *
8194 * @return The distance along the Z axis.
8195 */
8196 public float getCameraDistance() {
8197 ensureTransformationInfo();
8198 final float dpi = mResources.getDisplayMetrics().densityDpi;
8199 final TransformationInfo info = mTransformationInfo;
8200 if (info.mCamera == null) {
8201 info.mCamera = new Camera();
8202 info.matrix3D = new Matrix();
8203 }
8204 return -(info.mCamera.getLocationZ() * dpi);
8205 }
8206
8207 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008208 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8209 * views are drawn) from the camera to this view. The camera's distance
8210 * affects 3D transformations, for instance rotations around the X and Y
8211 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008212 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008213 * use a camera distance that's greater than the height (X axis rotation) or
8214 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008215 *
Romain Guya5364ee2011-02-24 14:46:04 -08008216 * <p>The distance of the camera from the view plane can have an affect on the
8217 * perspective distortion of the view when it is rotated around the x or y axis.
8218 * For example, a large distance will result in a large viewing angle, and there
8219 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008220 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008221 * also result in some drawing artifacts if the rotated view ends up partially
8222 * behind the camera (which is why the recommendation is to use a distance at
8223 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008224 *
Romain Guya5364ee2011-02-24 14:46:04 -08008225 * <p>The distance is expressed in "depth pixels." The default distance depends
8226 * on the screen density. For instance, on a medium density display, the
8227 * default distance is 1280. On a high density display, the default distance
8228 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008229 *
Romain Guya5364ee2011-02-24 14:46:04 -08008230 * <p>If you want to specify a distance that leads to visually consistent
8231 * results across various densities, use the following formula:</p>
8232 * <pre>
8233 * float scale = context.getResources().getDisplayMetrics().density;
8234 * view.setCameraDistance(distance * scale);
8235 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008236 *
Romain Guya5364ee2011-02-24 14:46:04 -08008237 * <p>The density scale factor of a high density display is 1.5,
8238 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008239 *
Romain Guya5364ee2011-02-24 14:46:04 -08008240 * @param distance The distance in "depth pixels", if negative the opposite
8241 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008242 *
8243 * @see #setRotationX(float)
8244 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008245 */
8246 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008247 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008248
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008249 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008250 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008251 final TransformationInfo info = mTransformationInfo;
8252 if (info.mCamera == null) {
8253 info.mCamera = new Camera();
8254 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008255 }
8256
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008257 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8258 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008259
Chet Haase9d1992d2012-03-13 11:03:25 -07008260 invalidateViewProperty(false, false);
Chet Haasea1cff502012-02-21 13:43:44 -08008261 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008262 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008263 }
Romain Guya5364ee2011-02-24 14:46:04 -08008264 }
8265
8266 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008267 * The degrees that the view is rotated around the pivot point.
8268 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008269 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008270 * @see #getPivotX()
8271 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008272 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008273 * @return The degrees of rotation.
8274 */
Chet Haasea5531132012-02-02 13:41:44 -08008275 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008276 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008277 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008278 }
8279
8280 /**
Chet Haase897247b2010-09-09 14:54:47 -07008281 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8282 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008283 *
8284 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008285 *
8286 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008287 * @see #getPivotX()
8288 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008289 * @see #setRotationX(float)
8290 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008291 *
8292 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008293 */
8294 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008295 ensureTransformationInfo();
8296 final TransformationInfo info = mTransformationInfo;
8297 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008298 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008299 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008300 info.mRotation = rotation;
8301 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008302 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008303 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8304 mDisplayList.setRotation(rotation);
8305 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008306 }
8307 }
8308
8309 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008310 * The degrees that the view is rotated around the vertical axis through the pivot point.
8311 *
8312 * @see #getPivotX()
8313 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008314 * @see #setRotationY(float)
8315 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008316 * @return The degrees of Y rotation.
8317 */
Chet Haasea5531132012-02-02 13:41:44 -08008318 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008319 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008320 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008321 }
8322
8323 /**
Chet Haase897247b2010-09-09 14:54:47 -07008324 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8325 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8326 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008327 *
Romain Guya5364ee2011-02-24 14:46:04 -08008328 * When rotating large views, it is recommended to adjust the camera distance
8329 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008330 *
8331 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008332 *
8333 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008334 * @see #getPivotX()
8335 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008336 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008337 * @see #setRotationX(float)
8338 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008339 *
8340 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008341 */
8342 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008343 ensureTransformationInfo();
8344 final TransformationInfo info = mTransformationInfo;
8345 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008346 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008347 info.mRotationY = rotationY;
8348 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008349 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008350 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8351 mDisplayList.setRotationY(rotationY);
8352 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008353 }
8354 }
8355
8356 /**
8357 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8358 *
8359 * @see #getPivotX()
8360 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008361 * @see #setRotationX(float)
8362 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008363 * @return The degrees of X rotation.
8364 */
Chet Haasea5531132012-02-02 13:41:44 -08008365 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008366 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008367 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008368 }
8369
8370 /**
Chet Haase897247b2010-09-09 14:54:47 -07008371 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8372 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8373 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008374 *
Romain Guya5364ee2011-02-24 14:46:04 -08008375 * When rotating large views, it is recommended to adjust the camera distance
8376 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008377 *
8378 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008379 *
8380 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008381 * @see #getPivotX()
8382 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008383 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008384 * @see #setRotationY(float)
8385 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008386 *
8387 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008388 */
8389 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008390 ensureTransformationInfo();
8391 final TransformationInfo info = mTransformationInfo;
8392 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008393 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008394 info.mRotationX = rotationX;
8395 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008396 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008397 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8398 mDisplayList.setRotationX(rotationX);
8399 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008400 }
8401 }
8402
8403 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008404 * The amount that the view is scaled in x around the pivot point, as a proportion of
8405 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8406 *
Joe Onorato93162322010-09-16 15:42:01 -04008407 * <p>By default, this is 1.0f.
8408 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008409 * @see #getPivotX()
8410 * @see #getPivotY()
8411 * @return The scaling factor.
8412 */
Chet Haasea5531132012-02-02 13:41:44 -08008413 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008414 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008415 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008416 }
8417
8418 /**
8419 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8420 * the view's unscaled width. A value of 1 means that no scaling is applied.
8421 *
8422 * @param scaleX The scaling factor.
8423 * @see #getPivotX()
8424 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008425 *
8426 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008427 */
8428 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008429 ensureTransformationInfo();
8430 final TransformationInfo info = mTransformationInfo;
8431 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008432 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008433 info.mScaleX = scaleX;
8434 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008435 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008436 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8437 mDisplayList.setScaleX(scaleX);
8438 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008439 }
8440 }
8441
8442 /**
8443 * The amount that the view is scaled in y around the pivot point, as a proportion of
8444 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8445 *
Joe Onorato93162322010-09-16 15:42:01 -04008446 * <p>By default, this is 1.0f.
8447 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008448 * @see #getPivotX()
8449 * @see #getPivotY()
8450 * @return The scaling factor.
8451 */
Chet Haasea5531132012-02-02 13:41:44 -08008452 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008453 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008454 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008455 }
8456
8457 /**
8458 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8459 * the view's unscaled width. A value of 1 means that no scaling is applied.
8460 *
8461 * @param scaleY The scaling factor.
8462 * @see #getPivotX()
8463 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008464 *
8465 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008466 */
8467 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008468 ensureTransformationInfo();
8469 final TransformationInfo info = mTransformationInfo;
8470 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008471 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008472 info.mScaleY = scaleY;
8473 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008474 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008475 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8476 mDisplayList.setScaleY(scaleY);
8477 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008478 }
8479 }
8480
8481 /**
8482 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8483 * and {@link #setScaleX(float) scaled}.
8484 *
8485 * @see #getRotation()
8486 * @see #getScaleX()
8487 * @see #getScaleY()
8488 * @see #getPivotY()
8489 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008490 *
8491 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008492 */
Chet Haasea5531132012-02-02 13:41:44 -08008493 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008494 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008495 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008496 }
8497
8498 /**
8499 * Sets the x location of the point around which the view is
8500 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008501 * By default, the pivot point is centered on the object.
8502 * Setting this property disables this behavior and causes the view to use only the
8503 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008504 *
8505 * @param pivotX The x location of the pivot point.
8506 * @see #getRotation()
8507 * @see #getScaleX()
8508 * @see #getScaleY()
8509 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008510 *
8511 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008512 */
8513 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008514 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008515 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008516 final TransformationInfo info = mTransformationInfo;
8517 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008518 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008519 info.mPivotX = pivotX;
8520 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008521 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008522 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8523 mDisplayList.setPivotX(pivotX);
8524 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008525 }
8526 }
8527
8528 /**
8529 * The y location of the point around which the view is {@link #setRotation(float) rotated}
8530 * and {@link #setScaleY(float) scaled}.
8531 *
8532 * @see #getRotation()
8533 * @see #getScaleX()
8534 * @see #getScaleY()
8535 * @see #getPivotY()
8536 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008537 *
8538 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008539 */
Chet Haasea5531132012-02-02 13:41:44 -08008540 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008541 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008542 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008543 }
8544
8545 /**
8546 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07008547 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
8548 * Setting this property disables this behavior and causes the view to use only the
8549 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008550 *
8551 * @param pivotY The y location of the pivot point.
8552 * @see #getRotation()
8553 * @see #getScaleX()
8554 * @see #getScaleY()
8555 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008556 *
8557 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008558 */
8559 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008560 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008561 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008562 final TransformationInfo info = mTransformationInfo;
8563 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008564 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008565 info.mPivotY = pivotY;
8566 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008567 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008568 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8569 mDisplayList.setPivotY(pivotY);
8570 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008571 }
8572 }
8573
8574 /**
8575 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
8576 * completely transparent and 1 means the view is completely opaque.
8577 *
Joe Onorato93162322010-09-16 15:42:01 -04008578 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07008579 * @return The opacity of the view.
8580 */
Chet Haasea5531132012-02-02 13:41:44 -08008581 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008582 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008583 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008584 }
8585
8586 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07008587 * Returns whether this View has content which overlaps. This function, intended to be
8588 * overridden by specific View types, is an optimization when alpha is set on a view. If
8589 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8590 * and then composited it into place, which can be expensive. If the view has no overlapping
8591 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8592 * An example of overlapping rendering is a TextView with a background image, such as a
8593 * Button. An example of non-overlapping rendering is a TextView with no background, or
8594 * an ImageView with only the foreground image. The default implementation returns true;
8595 * subclasses should override if they have cases which can be optimized.
8596 *
8597 * @return true if the content in this view might overlap, false otherwise.
8598 */
8599 public boolean hasOverlappingRendering() {
8600 return true;
8601 }
8602
8603 /**
Romain Guy171c5922011-01-06 10:04:23 -08008604 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8605 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008606 *
Romain Guy171c5922011-01-06 10:04:23 -08008607 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8608 * responsible for applying the opacity itself. Otherwise, calling this method is
8609 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008610 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008611 *
Chet Haasea5531132012-02-02 13:41:44 -08008612 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8613 * performance implications. It is generally best to use the alpha property sparingly and
8614 * transiently, as in the case of fading animations.</p>
8615 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008616 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08008617 *
Joe Malin32736f02011-01-19 16:14:20 -08008618 * @see #setLayerType(int, android.graphics.Paint)
8619 *
Chet Haase73066682010-11-29 15:55:32 -08008620 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07008621 */
8622 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008623 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008624 if (mTransformationInfo.mAlpha != alpha) {
8625 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008626 if (onSetAlpha((int) (alpha * 255))) {
8627 mPrivateFlags |= ALPHA_SET;
8628 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07008629 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008630 invalidate(true);
8631 } else {
8632 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07008633 invalidateViewProperty(true, false);
Chet Haasea1cff502012-02-21 13:43:44 -08008634 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8635 mDisplayList.setAlpha(alpha);
8636 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008637 }
Chet Haaseed032702010-10-01 14:05:54 -07008638 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008639 }
8640
8641 /**
Chet Haasea00f3862011-02-22 06:34:40 -08008642 * Faster version of setAlpha() which performs the same steps except there are
8643 * no calls to invalidate(). The caller of this function should perform proper invalidation
8644 * on the parent and this object. The return value indicates whether the subclass handles
8645 * alpha (the return value for onSetAlpha()).
8646 *
8647 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008648 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
8649 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08008650 */
8651 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008652 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008653 if (mTransformationInfo.mAlpha != alpha) {
8654 mTransformationInfo.mAlpha = alpha;
8655 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
8656 if (subclassHandlesAlpha) {
8657 mPrivateFlags |= ALPHA_SET;
8658 return true;
8659 } else {
8660 mPrivateFlags &= ~ALPHA_SET;
Chet Haasea1cff502012-02-21 13:43:44 -08008661 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8662 mDisplayList.setAlpha(alpha);
8663 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008664 }
Chet Haasea00f3862011-02-22 06:34:40 -08008665 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008666 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08008667 }
8668
8669 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008670 * Top position of this view relative to its parent.
8671 *
8672 * @return The top of this view, in pixels.
8673 */
8674 @ViewDebug.CapturedViewProperty
8675 public final int getTop() {
8676 return mTop;
8677 }
8678
8679 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008680 * Sets the top position of this view relative to its parent. This method is meant to be called
8681 * by the layout system and should not generally be called otherwise, because the property
8682 * may be changed at any time by the layout.
8683 *
8684 * @param top The top of this view, in pixels.
8685 */
8686 public final void setTop(int top) {
8687 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07008688 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008689 final boolean matrixIsIdentity = mTransformationInfo == null
8690 || mTransformationInfo.mMatrixIsIdentity;
8691 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008692 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008693 int minTop;
8694 int yLoc;
8695 if (top < mTop) {
8696 minTop = top;
8697 yLoc = top - mTop;
8698 } else {
8699 minTop = mTop;
8700 yLoc = 0;
8701 }
Chet Haasee9140a72011-02-16 16:23:29 -08008702 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008703 }
8704 } else {
8705 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008706 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008707 }
8708
Chet Haaseed032702010-10-01 14:05:54 -07008709 int width = mRight - mLeft;
8710 int oldHeight = mBottom - mTop;
8711
Chet Haase21cd1382010-09-01 17:42:29 -07008712 mTop = top;
Chet Haasea1cff502012-02-21 13:43:44 -08008713 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8714 mDisplayList.setTop(mTop);
8715 }
Chet Haase21cd1382010-09-01 17:42:29 -07008716
Chet Haaseed032702010-10-01 14:05:54 -07008717 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8718
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008719 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008720 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8721 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008722 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008723 }
Chet Haase21cd1382010-09-01 17:42:29 -07008724 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008725 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008726 }
Chet Haase55dbb652010-12-21 20:15:08 -08008727 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008728 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008729 }
8730 }
8731
8732 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008733 * Bottom position of this view relative to its parent.
8734 *
8735 * @return The bottom of this view, in pixels.
8736 */
8737 @ViewDebug.CapturedViewProperty
8738 public final int getBottom() {
8739 return mBottom;
8740 }
8741
8742 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08008743 * True if this view has changed since the last time being drawn.
8744 *
8745 * @return The dirty state of this view.
8746 */
8747 public boolean isDirty() {
8748 return (mPrivateFlags & DIRTY_MASK) != 0;
8749 }
8750
8751 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008752 * Sets the bottom position of this view relative to its parent. This method is meant to be
8753 * called by the layout system and should not generally be called otherwise, because the
8754 * property may be changed at any time by the layout.
8755 *
8756 * @param bottom The bottom of this view, in pixels.
8757 */
8758 public final void setBottom(int bottom) {
8759 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07008760 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008761 final boolean matrixIsIdentity = mTransformationInfo == null
8762 || mTransformationInfo.mMatrixIsIdentity;
8763 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008764 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008765 int maxBottom;
8766 if (bottom < mBottom) {
8767 maxBottom = mBottom;
8768 } else {
8769 maxBottom = bottom;
8770 }
Chet Haasee9140a72011-02-16 16:23:29 -08008771 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008772 }
8773 } else {
8774 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008775 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008776 }
8777
Chet Haaseed032702010-10-01 14:05:54 -07008778 int width = mRight - mLeft;
8779 int oldHeight = mBottom - mTop;
8780
Chet Haase21cd1382010-09-01 17:42:29 -07008781 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -08008782 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8783 mDisplayList.setBottom(mBottom);
8784 }
Chet Haase21cd1382010-09-01 17:42:29 -07008785
Chet Haaseed032702010-10-01 14:05:54 -07008786 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8787
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008788 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008789 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8790 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008791 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008792 }
Chet Haase21cd1382010-09-01 17:42:29 -07008793 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008794 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008795 }
Chet Haase55dbb652010-12-21 20:15:08 -08008796 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008797 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008798 }
8799 }
8800
8801 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008802 * Left position of this view relative to its parent.
8803 *
8804 * @return The left edge of this view, in pixels.
8805 */
8806 @ViewDebug.CapturedViewProperty
8807 public final int getLeft() {
8808 return mLeft;
8809 }
8810
8811 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008812 * Sets the left position of this view relative to its parent. This method is meant to be called
8813 * by the layout system and should not generally be called otherwise, because the property
8814 * may be changed at any time by the layout.
8815 *
8816 * @param left The bottom of this view, in pixels.
8817 */
8818 public final void setLeft(int left) {
8819 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07008820 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008821 final boolean matrixIsIdentity = mTransformationInfo == null
8822 || mTransformationInfo.mMatrixIsIdentity;
8823 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008824 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008825 int minLeft;
8826 int xLoc;
8827 if (left < mLeft) {
8828 minLeft = left;
8829 xLoc = left - mLeft;
8830 } else {
8831 minLeft = mLeft;
8832 xLoc = 0;
8833 }
Chet Haasee9140a72011-02-16 16:23:29 -08008834 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008835 }
8836 } else {
8837 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008838 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008839 }
8840
Chet Haaseed032702010-10-01 14:05:54 -07008841 int oldWidth = mRight - mLeft;
8842 int height = mBottom - mTop;
8843
Chet Haase21cd1382010-09-01 17:42:29 -07008844 mLeft = left;
Chet Haasea1cff502012-02-21 13:43:44 -08008845 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8846 mDisplayList.setLeft(left);
8847 }
Chet Haase21cd1382010-09-01 17:42:29 -07008848
Chet Haaseed032702010-10-01 14:05:54 -07008849 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8850
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008851 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008852 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8853 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008854 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008855 }
Chet Haase21cd1382010-09-01 17:42:29 -07008856 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008857 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008858 }
Chet Haase55dbb652010-12-21 20:15:08 -08008859 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008860 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008861 if (USE_DISPLAY_LIST_PROPERTIES) {
8862
8863 }
Chet Haase21cd1382010-09-01 17:42:29 -07008864 }
8865 }
8866
8867 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008868 * Right position of this view relative to its parent.
8869 *
8870 * @return The right edge of this view, in pixels.
8871 */
8872 @ViewDebug.CapturedViewProperty
8873 public final int getRight() {
8874 return mRight;
8875 }
8876
8877 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008878 * Sets the right position of this view relative to its parent. This method is meant to be called
8879 * by the layout system and should not generally be called otherwise, because the property
8880 * may be changed at any time by the layout.
8881 *
8882 * @param right The bottom of this view, in pixels.
8883 */
8884 public final void setRight(int right) {
8885 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07008886 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008887 final boolean matrixIsIdentity = mTransformationInfo == null
8888 || mTransformationInfo.mMatrixIsIdentity;
8889 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008890 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008891 int maxRight;
8892 if (right < mRight) {
8893 maxRight = mRight;
8894 } else {
8895 maxRight = right;
8896 }
Chet Haasee9140a72011-02-16 16:23:29 -08008897 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008898 }
8899 } else {
8900 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008901 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008902 }
8903
Chet Haaseed032702010-10-01 14:05:54 -07008904 int oldWidth = mRight - mLeft;
8905 int height = mBottom - mTop;
8906
Chet Haase21cd1382010-09-01 17:42:29 -07008907 mRight = right;
Chet Haasea1cff502012-02-21 13:43:44 -08008908 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8909 mDisplayList.setRight(mRight);
8910 }
Chet Haase21cd1382010-09-01 17:42:29 -07008911
Chet Haaseed032702010-10-01 14:05:54 -07008912 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8913
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008914 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008915 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8916 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008917 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008918 }
Chet Haase21cd1382010-09-01 17:42:29 -07008919 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008920 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008921 }
Chet Haase55dbb652010-12-21 20:15:08 -08008922 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008923 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008924 }
8925 }
8926
8927 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008928 * The visual x position of this view, in pixels. This is equivalent to the
8929 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08008930 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07008931 *
Chet Haasedf030d22010-07-30 17:22:38 -07008932 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008933 */
Chet Haasea5531132012-02-02 13:41:44 -08008934 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008935 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008936 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008937 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008938
Chet Haasedf030d22010-07-30 17:22:38 -07008939 /**
8940 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
8941 * {@link #setTranslationX(float) translationX} property to be the difference between
8942 * the x value passed in and the current {@link #getLeft() left} property.
8943 *
8944 * @param x The visual x position of this view, in pixels.
8945 */
8946 public void setX(float x) {
8947 setTranslationX(x - mLeft);
8948 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008949
Chet Haasedf030d22010-07-30 17:22:38 -07008950 /**
8951 * The visual y position of this view, in pixels. This is equivalent to the
8952 * {@link #setTranslationY(float) translationY} property plus the current
8953 * {@link #getTop() top} property.
8954 *
8955 * @return The visual y position of this view, in pixels.
8956 */
Chet Haasea5531132012-02-02 13:41:44 -08008957 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008958 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008959 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008960 }
8961
8962 /**
8963 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
8964 * {@link #setTranslationY(float) translationY} property to be the difference between
8965 * the y value passed in and the current {@link #getTop() top} property.
8966 *
8967 * @param y The visual y position of this view, in pixels.
8968 */
8969 public void setY(float y) {
8970 setTranslationY(y - mTop);
8971 }
8972
8973
8974 /**
8975 * The horizontal location of this view relative to its {@link #getLeft() left} position.
8976 * This position is post-layout, in addition to wherever the object's
8977 * layout placed it.
8978 *
8979 * @return The horizontal position of this view relative to its left position, in pixels.
8980 */
Chet Haasea5531132012-02-02 13:41:44 -08008981 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008982 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008983 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07008984 }
8985
8986 /**
8987 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
8988 * This effectively positions the object post-layout, in addition to wherever the object's
8989 * layout placed it.
8990 *
8991 * @param translationX The horizontal position of this view relative to its left position,
8992 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008993 *
8994 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07008995 */
8996 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008997 ensureTransformationInfo();
8998 final TransformationInfo info = mTransformationInfo;
8999 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009000 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009001 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009002 info.mTranslationX = translationX;
9003 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009004 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08009005 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9006 mDisplayList.setTranslationX(translationX);
9007 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009008 }
9009 }
9010
9011 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009012 * The horizontal location of this view relative to its {@link #getTop() top} position.
9013 * This position is post-layout, in addition to wherever the object's
9014 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009015 *
Chet Haasedf030d22010-07-30 17:22:38 -07009016 * @return The vertical position of this view relative to its top position,
9017 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009018 */
Chet Haasea5531132012-02-02 13:41:44 -08009019 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009020 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009021 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009022 }
9023
9024 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009025 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9026 * This effectively positions the object post-layout, in addition to wherever the object's
9027 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009028 *
Chet Haasedf030d22010-07-30 17:22:38 -07009029 * @param translationY The vertical position of this view relative to its top position,
9030 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009031 *
9032 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009033 */
Chet Haasedf030d22010-07-30 17:22:38 -07009034 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009035 ensureTransformationInfo();
9036 final TransformationInfo info = mTransformationInfo;
9037 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009038 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009039 info.mTranslationY = translationY;
9040 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009041 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08009042 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9043 mDisplayList.setTranslationY(translationY);
9044 }
Chet Haasedf030d22010-07-30 17:22:38 -07009045 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009046 }
9047
9048 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009049 * Hit rectangle in parent's coordinates
9050 *
9051 * @param outRect The hit rectangle of the view.
9052 */
9053 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009054 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009055 final TransformationInfo info = mTransformationInfo;
9056 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009057 outRect.set(mLeft, mTop, mRight, mBottom);
9058 } else {
9059 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009060 tmpRect.set(-info.mPivotX, -info.mPivotY,
9061 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9062 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009063 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9064 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009065 }
9066 }
9067
9068 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009069 * Determines whether the given point, in local coordinates is inside the view.
9070 */
9071 /*package*/ final boolean pointInView(float localX, float localY) {
9072 return localX >= 0 && localX < (mRight - mLeft)
9073 && localY >= 0 && localY < (mBottom - mTop);
9074 }
9075
9076 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009077 * Utility method to determine whether the given point, in local coordinates,
9078 * is inside the view, where the area of the view is expanded by the slop factor.
9079 * This method is called while processing touch-move events to determine if the event
9080 * is still within the view.
9081 */
9082 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009083 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009084 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009085 }
9086
9087 /**
9088 * When a view has focus and the user navigates away from it, the next view is searched for
9089 * starting from the rectangle filled in by this method.
9090 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009091 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9092 * of the view. However, if your view maintains some idea of internal selection,
9093 * such as a cursor, or a selected row or column, you should override this method and
9094 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009095 *
9096 * @param r The rectangle to fill in, in this view's coordinates.
9097 */
9098 public void getFocusedRect(Rect r) {
9099 getDrawingRect(r);
9100 }
9101
9102 /**
9103 * If some part of this view is not clipped by any of its parents, then
9104 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009105 * coordinates (without taking possible View rotations into account), offset
9106 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9107 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009108 *
9109 * @param r If true is returned, r holds the global coordinates of the
9110 * visible portion of this view.
9111 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9112 * between this view and its root. globalOffet may be null.
9113 * @return true if r is non-empty (i.e. part of the view is visible at the
9114 * root level.
9115 */
9116 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9117 int width = mRight - mLeft;
9118 int height = mBottom - mTop;
9119 if (width > 0 && height > 0) {
9120 r.set(0, 0, width, height);
9121 if (globalOffset != null) {
9122 globalOffset.set(-mScrollX, -mScrollY);
9123 }
9124 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9125 }
9126 return false;
9127 }
9128
9129 public final boolean getGlobalVisibleRect(Rect r) {
9130 return getGlobalVisibleRect(r, null);
9131 }
9132
9133 public final boolean getLocalVisibleRect(Rect r) {
9134 Point offset = new Point();
9135 if (getGlobalVisibleRect(r, offset)) {
9136 r.offset(-offset.x, -offset.y); // make r local
9137 return true;
9138 }
9139 return false;
9140 }
9141
9142 /**
9143 * Offset this view's vertical location by the specified number of pixels.
9144 *
9145 * @param offset the number of pixels to offset the view by
9146 */
9147 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009148 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009149 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009150 final boolean matrixIsIdentity = mTransformationInfo == null
9151 || mTransformationInfo.mMatrixIsIdentity;
9152 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009153 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9154 invalidateViewProperty(false, false);
9155 } else {
9156 final ViewParent p = mParent;
9157 if (p != null && mAttachInfo != null) {
9158 final Rect r = mAttachInfo.mTmpInvalRect;
9159 int minTop;
9160 int maxBottom;
9161 int yLoc;
9162 if (offset < 0) {
9163 minTop = mTop + offset;
9164 maxBottom = mBottom;
9165 yLoc = offset;
9166 } else {
9167 minTop = mTop;
9168 maxBottom = mBottom + offset;
9169 yLoc = 0;
9170 }
9171 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9172 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009173 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009174 }
9175 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009176 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009177 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009178
Chet Haasec3aa3612010-06-17 08:50:37 -07009179 mTop += offset;
9180 mBottom += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08009181 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9182 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009183 invalidateViewProperty(false, false);
9184 } else {
9185 if (!matrixIsIdentity) {
9186 invalidateViewProperty(false, true);
9187 }
9188 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009189 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009191 }
9192
9193 /**
9194 * Offset this view's horizontal location by the specified amount of pixels.
9195 *
9196 * @param offset the numer of pixels to offset the view by
9197 */
9198 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009199 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009200 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009201 final boolean matrixIsIdentity = mTransformationInfo == null
9202 || mTransformationInfo.mMatrixIsIdentity;
9203 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009204 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9205 invalidateViewProperty(false, false);
9206 } else {
9207 final ViewParent p = mParent;
9208 if (p != null && mAttachInfo != null) {
9209 final Rect r = mAttachInfo.mTmpInvalRect;
9210 int minLeft;
9211 int maxRight;
9212 if (offset < 0) {
9213 minLeft = mLeft + offset;
9214 maxRight = mRight;
9215 } else {
9216 minLeft = mLeft;
9217 maxRight = mRight + offset;
9218 }
9219 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9220 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009221 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009222 }
9223 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009224 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009225 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009226
Chet Haasec3aa3612010-06-17 08:50:37 -07009227 mLeft += offset;
9228 mRight += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08009229 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9230 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009231 invalidateViewProperty(false, false);
9232 } else {
9233 if (!matrixIsIdentity) {
9234 invalidateViewProperty(false, true);
9235 }
9236 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009237 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009239 }
9240
9241 /**
9242 * Get the LayoutParams associated with this view. All views should have
9243 * layout parameters. These supply parameters to the <i>parent</i> of this
9244 * view specifying how it should be arranged. There are many subclasses of
9245 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9246 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009247 *
9248 * This method may return null if this View is not attached to a parent
9249 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9250 * was not invoked successfully. When a View is attached to a parent
9251 * ViewGroup, this method must not return null.
9252 *
9253 * @return The LayoutParams associated with this view, or null if no
9254 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009255 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009256 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009257 public ViewGroup.LayoutParams getLayoutParams() {
9258 return mLayoutParams;
9259 }
9260
9261 /**
9262 * Set the layout parameters associated with this view. These supply
9263 * parameters to the <i>parent</i> of this view specifying how it should be
9264 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9265 * correspond to the different subclasses of ViewGroup that are responsible
9266 * for arranging their children.
9267 *
Romain Guy01c174b2011-02-22 11:51:06 -08009268 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009269 */
9270 public void setLayoutParams(ViewGroup.LayoutParams params) {
9271 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009272 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009273 }
9274 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009275 if (mParent instanceof ViewGroup) {
9276 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9277 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009278 requestLayout();
9279 }
9280
9281 /**
9282 * Set the scrolled position of your view. This will cause a call to
9283 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9284 * invalidated.
9285 * @param x the x position to scroll to
9286 * @param y the y position to scroll to
9287 */
9288 public void scrollTo(int x, int y) {
9289 if (mScrollX != x || mScrollY != y) {
9290 int oldX = mScrollX;
9291 int oldY = mScrollY;
9292 mScrollX = x;
9293 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009294 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009295 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009296 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009297 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009298 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009299 }
9300 }
9301
9302 /**
9303 * Move the scrolled position of your view. This will cause a call to
9304 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9305 * invalidated.
9306 * @param x the amount of pixels to scroll by horizontally
9307 * @param y the amount of pixels to scroll by vertically
9308 */
9309 public void scrollBy(int x, int y) {
9310 scrollTo(mScrollX + x, mScrollY + y);
9311 }
9312
9313 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009314 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9315 * animation to fade the scrollbars out after a default delay. If a subclass
9316 * provides animated scrolling, the start delay should equal the duration
9317 * of the scrolling animation.</p>
9318 *
9319 * <p>The animation starts only if at least one of the scrollbars is
9320 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9321 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9322 * this method returns true, and false otherwise. If the animation is
9323 * started, this method calls {@link #invalidate()}; in that case the
9324 * caller should not call {@link #invalidate()}.</p>
9325 *
9326 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009327 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009328 *
9329 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9330 * and {@link #scrollTo(int, int)}.</p>
9331 *
9332 * @return true if the animation is played, false otherwise
9333 *
9334 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009335 * @see #scrollBy(int, int)
9336 * @see #scrollTo(int, int)
9337 * @see #isHorizontalScrollBarEnabled()
9338 * @see #isVerticalScrollBarEnabled()
9339 * @see #setHorizontalScrollBarEnabled(boolean)
9340 * @see #setVerticalScrollBarEnabled(boolean)
9341 */
9342 protected boolean awakenScrollBars() {
9343 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009344 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009345 }
9346
9347 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009348 * Trigger the scrollbars to draw.
9349 * This method differs from awakenScrollBars() only in its default duration.
9350 * initialAwakenScrollBars() will show the scroll bars for longer than
9351 * usual to give the user more of a chance to notice them.
9352 *
9353 * @return true if the animation is played, false otherwise.
9354 */
9355 private boolean initialAwakenScrollBars() {
9356 return mScrollCache != null &&
9357 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9358 }
9359
9360 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009361 * <p>
9362 * Trigger the scrollbars to draw. When invoked this method starts an
9363 * animation to fade the scrollbars out after a fixed delay. If a subclass
9364 * provides animated scrolling, the start delay should equal the duration of
9365 * the scrolling animation.
9366 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009367 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009368 * <p>
9369 * The animation starts only if at least one of the scrollbars is enabled,
9370 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9371 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9372 * this method returns true, and false otherwise. If the animation is
9373 * started, this method calls {@link #invalidate()}; in that case the caller
9374 * should not call {@link #invalidate()}.
9375 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009376 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009377 * <p>
9378 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009379 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009380 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009381 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009382 * @param startDelay the delay, in milliseconds, after which the animation
9383 * should start; when the delay is 0, the animation starts
9384 * immediately
9385 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009386 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009387 * @see #scrollBy(int, int)
9388 * @see #scrollTo(int, int)
9389 * @see #isHorizontalScrollBarEnabled()
9390 * @see #isVerticalScrollBarEnabled()
9391 * @see #setHorizontalScrollBarEnabled(boolean)
9392 * @see #setVerticalScrollBarEnabled(boolean)
9393 */
9394 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009395 return awakenScrollBars(startDelay, true);
9396 }
Joe Malin32736f02011-01-19 16:14:20 -08009397
Mike Cleron290947b2009-09-29 18:34:32 -07009398 /**
9399 * <p>
9400 * Trigger the scrollbars to draw. When invoked this method starts an
9401 * animation to fade the scrollbars out after a fixed delay. If a subclass
9402 * provides animated scrolling, the start delay should equal the duration of
9403 * the scrolling animation.
9404 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009405 *
Mike Cleron290947b2009-09-29 18:34:32 -07009406 * <p>
9407 * The animation starts only if at least one of the scrollbars is enabled,
9408 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9409 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9410 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009411 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009412 * is set to true; in that case the caller
9413 * should not call {@link #invalidate()}.
9414 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009415 *
Mike Cleron290947b2009-09-29 18:34:32 -07009416 * <p>
9417 * This method should be invoked everytime a subclass directly updates the
9418 * scroll parameters.
9419 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009420 *
Mike Cleron290947b2009-09-29 18:34:32 -07009421 * @param startDelay the delay, in milliseconds, after which the animation
9422 * should start; when the delay is 0, the animation starts
9423 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009424 *
Mike Cleron290947b2009-09-29 18:34:32 -07009425 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009426 *
Mike Cleron290947b2009-09-29 18:34:32 -07009427 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009428 *
Mike Cleron290947b2009-09-29 18:34:32 -07009429 * @see #scrollBy(int, int)
9430 * @see #scrollTo(int, int)
9431 * @see #isHorizontalScrollBarEnabled()
9432 * @see #isVerticalScrollBarEnabled()
9433 * @see #setHorizontalScrollBarEnabled(boolean)
9434 * @see #setVerticalScrollBarEnabled(boolean)
9435 */
9436 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009437 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009438
Mike Cleronf116bf82009-09-27 19:14:12 -07009439 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9440 return false;
9441 }
9442
9443 if (scrollCache.scrollBar == null) {
9444 scrollCache.scrollBar = new ScrollBarDrawable();
9445 }
9446
9447 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9448
Mike Cleron290947b2009-09-29 18:34:32 -07009449 if (invalidate) {
9450 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009451 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009452 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009453
9454 if (scrollCache.state == ScrollabilityCache.OFF) {
9455 // FIXME: this is copied from WindowManagerService.
9456 // We should get this value from the system when it
9457 // is possible to do so.
9458 final int KEY_REPEAT_FIRST_DELAY = 750;
9459 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9460 }
9461
9462 // Tell mScrollCache when we should start fading. This may
9463 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009464 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009465 scrollCache.fadeStartTime = fadeStartTime;
9466 scrollCache.state = ScrollabilityCache.ON;
9467
9468 // Schedule our fader to run, unscheduling any old ones first
9469 if (mAttachInfo != null) {
9470 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9471 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9472 }
9473
9474 return true;
9475 }
9476
9477 return false;
9478 }
9479
9480 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009481 * Do not invalidate views which are not visible and which are not running an animation. They
9482 * will not get drawn and they should not set dirty flags as if they will be drawn
9483 */
9484 private boolean skipInvalidate() {
9485 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9486 (!(mParent instanceof ViewGroup) ||
9487 !((ViewGroup) mParent).isViewTransitioning(this));
9488 }
9489 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009490 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009491 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
9492 * in the future. This must be called from a UI thread. To call from a non-UI
9493 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009494 *
9495 * WARNING: This method is destructive to dirty.
9496 * @param dirty the rectangle representing the bounds of the dirty region
9497 */
9498 public void invalidate(Rect dirty) {
9499 if (ViewDebug.TRACE_HIERARCHY) {
9500 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9501 }
9502
Chet Haaseaceafe62011-08-26 15:44:33 -07009503 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009504 return;
9505 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009506 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009507 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9508 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009509 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009510 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009511 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009512 final ViewParent p = mParent;
9513 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009514 //noinspection PointlessBooleanExpression,ConstantConditions
9515 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9516 if (p != null && ai != null && ai.mHardwareAccelerated) {
9517 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009518 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009519 p.invalidateChild(this, null);
9520 return;
9521 }
Romain Guyaf636eb2010-12-09 17:47:21 -08009522 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009523 if (p != null && ai != null) {
9524 final int scrollX = mScrollX;
9525 final int scrollY = mScrollY;
9526 final Rect r = ai.mTmpInvalRect;
9527 r.set(dirty.left - scrollX, dirty.top - scrollY,
9528 dirty.right - scrollX, dirty.bottom - scrollY);
9529 mParent.invalidateChild(this, r);
9530 }
9531 }
9532 }
9533
9534 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009535 * 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 -08009536 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009537 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
9538 * will be called at some point in the future. This must be called from
9539 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009540 * @param l the left position of the dirty region
9541 * @param t the top position of the dirty region
9542 * @param r the right position of the dirty region
9543 * @param b the bottom position of the dirty region
9544 */
9545 public void invalidate(int l, int t, int r, int b) {
9546 if (ViewDebug.TRACE_HIERARCHY) {
9547 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9548 }
9549
Chet Haaseaceafe62011-08-26 15:44:33 -07009550 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009551 return;
9552 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009553 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009554 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9555 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009556 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009557 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009558 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009559 final ViewParent p = mParent;
9560 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009561 //noinspection PointlessBooleanExpression,ConstantConditions
9562 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9563 if (p != null && ai != null && ai.mHardwareAccelerated) {
9564 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009565 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009566 p.invalidateChild(this, null);
9567 return;
9568 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009570 if (p != null && ai != null && l < r && t < b) {
9571 final int scrollX = mScrollX;
9572 final int scrollY = mScrollY;
9573 final Rect tmpr = ai.mTmpInvalRect;
9574 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
9575 p.invalidateChild(this, tmpr);
9576 }
9577 }
9578 }
9579
9580 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009581 * Invalidate the whole view. If the view is visible,
9582 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
9583 * the future. This must be called from a UI thread. To call from a non-UI thread,
9584 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009585 */
9586 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07009587 invalidate(true);
9588 }
Joe Malin32736f02011-01-19 16:14:20 -08009589
Chet Haaseed032702010-10-01 14:05:54 -07009590 /**
9591 * This is where the invalidate() work actually happens. A full invalidate()
9592 * causes the drawing cache to be invalidated, but this function can be called with
9593 * invalidateCache set to false to skip that invalidation step for cases that do not
9594 * need it (for example, a component that remains at the same dimensions with the same
9595 * content).
9596 *
9597 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9598 * well. This is usually true for a full invalidate, but may be set to false if the
9599 * View's contents or dimensions have not changed.
9600 */
Romain Guy849d0a32011-02-01 17:20:48 -08009601 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009602 if (ViewDebug.TRACE_HIERARCHY) {
9603 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9604 }
9605
Chet Haaseaceafe62011-08-26 15:44:33 -07009606 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009607 return;
9608 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009609 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08009610 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08009611 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
9612 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07009613 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07009614 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07009615 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009616 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07009617 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009619 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07009620 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08009621 //noinspection PointlessBooleanExpression,ConstantConditions
9622 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9623 if (p != null && ai != null && ai.mHardwareAccelerated) {
9624 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009625 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009626 p.invalidateChild(this, null);
9627 return;
9628 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009629 }
Michael Jurkaebefea42010-11-15 16:04:01 -08009630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009631 if (p != null && ai != null) {
9632 final Rect r = ai.mTmpInvalRect;
9633 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9634 // Don't call invalidate -- we don't want to internally scroll
9635 // our own bounds
9636 p.invalidateChild(this, r);
9637 }
9638 }
9639 }
9640
9641 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07009642 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
9643 * set any flags or handle all of the cases handled by the default invalidation methods.
9644 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
9645 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
9646 * walk up the hierarchy, transforming the dirty rect as necessary.
9647 *
9648 * The method also handles normal invalidation logic if display list properties are not
9649 * being used in this view. The invalidateParent and forceRedraw flags are used by that
9650 * backup approach, to handle these cases used in the various property-setting methods.
9651 *
9652 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
9653 * are not being used in this view
9654 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
9655 * list properties are not being used in this view
9656 */
9657 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
9658 if (!USE_DISPLAY_LIST_PROPERTIES || mDisplayList == null ||
9659 (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
9660 if (invalidateParent) {
9661 invalidateParentCaches();
9662 }
9663 if (forceRedraw) {
9664 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
9665 }
9666 invalidate(false);
9667 } else {
9668 final AttachInfo ai = mAttachInfo;
9669 final ViewParent p = mParent;
9670 if (p != null && ai != null) {
9671 final Rect r = ai.mTmpInvalRect;
9672 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9673 if (mParent instanceof ViewGroup) {
9674 ((ViewGroup) mParent).invalidateChildFast(this, r);
9675 } else {
9676 mParent.invalidateChild(this, r);
9677 }
9678 }
9679 }
9680 }
9681
9682 /**
9683 * Utility method to transform a given Rect by the current matrix of this view.
9684 */
9685 void transformRect(final Rect rect) {
9686 if (!getMatrix().isIdentity()) {
9687 RectF boundingRect = mAttachInfo.mTmpTransformRect;
9688 boundingRect.set(rect);
9689 getMatrix().mapRect(boundingRect);
9690 rect.set((int) (boundingRect.left - 0.5f),
9691 (int) (boundingRect.top - 0.5f),
9692 (int) (boundingRect.right + 0.5f),
9693 (int) (boundingRect.bottom + 0.5f));
9694 }
9695 }
9696
9697 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08009698 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08009699 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9700 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08009701 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
9702 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08009703 *
9704 * @hide
9705 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08009706 protected void invalidateParentCaches() {
9707 if (mParent instanceof View) {
9708 ((View) mParent).mPrivateFlags |= INVALIDATED;
9709 }
9710 }
Joe Malin32736f02011-01-19 16:14:20 -08009711
Romain Guy0fd89bf2011-01-26 15:41:30 -08009712 /**
9713 * Used to indicate that the parent of this view should be invalidated. This functionality
9714 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9715 * which is necessary when various parent-managed properties of the view change, such as
9716 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
9717 * an invalidation event to the parent.
9718 *
9719 * @hide
9720 */
9721 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08009722 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08009723 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08009724 }
9725 }
9726
9727 /**
Romain Guy24443ea2009-05-11 11:56:30 -07009728 * Indicates whether this View is opaque. An opaque View guarantees that it will
9729 * draw all the pixels overlapping its bounds using a fully opaque color.
9730 *
9731 * Subclasses of View should override this method whenever possible to indicate
9732 * whether an instance is opaque. Opaque Views are treated in a special way by
9733 * the View hierarchy, possibly allowing it to perform optimizations during
9734 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07009735 *
Romain Guy24443ea2009-05-11 11:56:30 -07009736 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07009737 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009738 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07009739 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07009740 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009741 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
9742 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07009743 }
9744
Adam Powell20232d02010-12-08 21:08:53 -08009745 /**
9746 * @hide
9747 */
9748 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07009749 // Opaque if:
9750 // - Has a background
9751 // - Background is opaque
9752 // - Doesn't have scrollbars or scrollbars are inside overlay
9753
Philip Milne6c8ea062012-04-03 17:38:43 -07009754 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -07009755 mPrivateFlags |= OPAQUE_BACKGROUND;
9756 } else {
9757 mPrivateFlags &= ~OPAQUE_BACKGROUND;
9758 }
9759
9760 final int flags = mViewFlags;
9761 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
9762 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
9763 mPrivateFlags |= OPAQUE_SCROLLBARS;
9764 } else {
9765 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
9766 }
9767 }
9768
9769 /**
9770 * @hide
9771 */
9772 protected boolean hasOpaqueScrollbars() {
9773 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07009774 }
9775
9776 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009777 * @return A handler associated with the thread running the View. This
9778 * handler can be used to pump events in the UI events queue.
9779 */
9780 public Handler getHandler() {
9781 if (mAttachInfo != null) {
9782 return mAttachInfo.mHandler;
9783 }
9784 return null;
9785 }
9786
9787 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08009788 * Gets the view root associated with the View.
9789 * @return The view root, or null if none.
9790 * @hide
9791 */
9792 public ViewRootImpl getViewRootImpl() {
9793 if (mAttachInfo != null) {
9794 return mAttachInfo.mViewRootImpl;
9795 }
9796 return null;
9797 }
9798
9799 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009800 * <p>Causes the Runnable to be added to the message queue.
9801 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009802 *
Romain Guye63a4f32011-08-11 11:33:31 -07009803 * <p>This method can be invoked from outside of the UI thread
9804 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009805 *
9806 * @param action The Runnable that will be executed.
9807 *
9808 * @return Returns true if the Runnable was successfully placed in to the
9809 * message queue. Returns false on failure, usually because the
9810 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009811 *
9812 * @see #postDelayed
9813 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009814 */
9815 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009816 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009817 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009818 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009819 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009820 // Assume that post will succeed later
9821 ViewRootImpl.getRunQueue().post(action);
9822 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009823 }
9824
9825 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009826 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009827 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07009828 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009829 *
Romain Guye63a4f32011-08-11 11:33:31 -07009830 * <p>This method can be invoked from outside of the UI thread
9831 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009832 *
9833 * @param action The Runnable that will be executed.
9834 * @param delayMillis The delay (in milliseconds) until the Runnable
9835 * will be executed.
9836 *
9837 * @return true if the Runnable was successfully placed in to the
9838 * message queue. Returns false on failure, usually because the
9839 * looper processing the message queue is exiting. Note that a
9840 * result of true does not mean the Runnable will be processed --
9841 * if the looper is quit before the delivery time of the message
9842 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009843 *
9844 * @see #post
9845 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009846 */
9847 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009848 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009849 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009850 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009851 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009852 // Assume that post will succeed later
9853 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9854 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009855 }
9856
9857 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009858 * <p>Causes the Runnable to execute on the next animation time step.
9859 * The runnable will be run on the user interface thread.</p>
9860 *
9861 * <p>This method can be invoked from outside of the UI thread
9862 * only when this View is attached to a window.</p>
9863 *
9864 * @param action The Runnable that will be executed.
9865 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009866 * @see #postOnAnimationDelayed
9867 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009868 */
9869 public void postOnAnimation(Runnable action) {
9870 final AttachInfo attachInfo = mAttachInfo;
9871 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009872 attachInfo.mViewRootImpl.mChoreographer.postCallback(
9873 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009874 } else {
9875 // Assume that post will succeed later
9876 ViewRootImpl.getRunQueue().post(action);
9877 }
9878 }
9879
9880 /**
9881 * <p>Causes the Runnable to execute on the next animation time step,
9882 * after the specified amount of time elapses.
9883 * The runnable will be run on the user interface thread.</p>
9884 *
9885 * <p>This method can be invoked from outside of the UI thread
9886 * only when this View is attached to a window.</p>
9887 *
9888 * @param action The Runnable that will be executed.
9889 * @param delayMillis The delay (in milliseconds) until the Runnable
9890 * will be executed.
9891 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009892 * @see #postOnAnimation
9893 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009894 */
9895 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
9896 final AttachInfo attachInfo = mAttachInfo;
9897 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009898 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
9899 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009900 } else {
9901 // Assume that post will succeed later
9902 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9903 }
9904 }
9905
9906 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009907 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009908 *
Romain Guye63a4f32011-08-11 11:33:31 -07009909 * <p>This method can be invoked from outside of the UI thread
9910 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009911 *
9912 * @param action The Runnable to remove from the message handling queue
9913 *
9914 * @return true if this view could ask the Handler to remove the Runnable,
9915 * false otherwise. When the returned value is true, the Runnable
9916 * may or may not have been actually removed from the message queue
9917 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009918 *
9919 * @see #post
9920 * @see #postDelayed
9921 * @see #postOnAnimation
9922 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009923 */
9924 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -08009925 if (action != null) {
9926 final AttachInfo attachInfo = mAttachInfo;
9927 if (attachInfo != null) {
9928 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009929 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
9930 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -08009931 } else {
9932 // Assume that post will succeed later
9933 ViewRootImpl.getRunQueue().removeCallbacks(action);
9934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009936 return true;
9937 }
9938
9939 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009940 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
9941 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009942 *
Romain Guye63a4f32011-08-11 11:33:31 -07009943 * <p>This method can be invoked from outside of the UI thread
9944 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009945 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009946 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009947 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009948 */
9949 public void postInvalidate() {
9950 postInvalidateDelayed(0);
9951 }
9952
9953 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009954 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9955 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009956 *
Romain Guye63a4f32011-08-11 11:33:31 -07009957 * <p>This method can be invoked from outside of the UI thread
9958 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009959 *
9960 * @param left The left coordinate of the rectangle to invalidate.
9961 * @param top The top coordinate of the rectangle to invalidate.
9962 * @param right The right coordinate of the rectangle to invalidate.
9963 * @param bottom The bottom coordinate of the rectangle to invalidate.
9964 *
9965 * @see #invalidate(int, int, int, int)
9966 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009967 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009968 */
9969 public void postInvalidate(int left, int top, int right, int bottom) {
9970 postInvalidateDelayed(0, left, top, right, bottom);
9971 }
9972
9973 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009974 * <p>Cause an invalidate to happen on a subsequent cycle through the event
9975 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009976 *
Romain Guye63a4f32011-08-11 11:33:31 -07009977 * <p>This method can be invoked from outside of the UI thread
9978 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009979 *
9980 * @param delayMilliseconds the duration in milliseconds to delay the
9981 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009982 *
9983 * @see #invalidate()
9984 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009985 */
9986 public void postInvalidateDelayed(long delayMilliseconds) {
9987 // We try only with the AttachInfo because there's no point in invalidating
9988 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009989 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009990 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009991 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009992 }
9993 }
9994
9995 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009996 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9997 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009998 *
Romain Guye63a4f32011-08-11 11:33:31 -07009999 * <p>This method can be invoked from outside of the UI thread
10000 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010001 *
10002 * @param delayMilliseconds the duration in milliseconds to delay the
10003 * invalidation by
10004 * @param left The left coordinate of the rectangle to invalidate.
10005 * @param top The top coordinate of the rectangle to invalidate.
10006 * @param right The right coordinate of the rectangle to invalidate.
10007 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010008 *
10009 * @see #invalidate(int, int, int, int)
10010 * @see #invalidate(Rect)
10011 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010012 */
10013 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10014 int right, int bottom) {
10015
10016 // We try only with the AttachInfo because there's no point in invalidating
10017 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010018 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010019 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010020 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10021 info.target = this;
10022 info.left = left;
10023 info.top = top;
10024 info.right = right;
10025 info.bottom = bottom;
10026
Jeff Browna175a5b2012-02-15 19:18:31 -080010027 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010028 }
10029 }
10030
10031 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010032 * <p>Cause an invalidate to happen on the next animation time step, typically the
10033 * next display frame.</p>
10034 *
10035 * <p>This method can be invoked from outside of the UI thread
10036 * only when this View is attached to a window.</p>
10037 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010038 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010039 */
10040 public void postInvalidateOnAnimation() {
10041 // We try only with the AttachInfo because there's no point in invalidating
10042 // if we are not attached to our window
10043 final AttachInfo attachInfo = mAttachInfo;
10044 if (attachInfo != null) {
10045 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10046 }
10047 }
10048
10049 /**
10050 * <p>Cause an invalidate of the specified area to happen on the next animation
10051 * time step, typically the next display frame.</p>
10052 *
10053 * <p>This method can be invoked from outside of the UI thread
10054 * only when this View is attached to a window.</p>
10055 *
10056 * @param left The left coordinate of the rectangle to invalidate.
10057 * @param top The top coordinate of the rectangle to invalidate.
10058 * @param right The right coordinate of the rectangle to invalidate.
10059 * @param bottom The bottom coordinate of the rectangle to invalidate.
10060 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010061 * @see #invalidate(int, int, int, int)
10062 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010063 */
10064 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10065 // We try only with the AttachInfo because there's no point in invalidating
10066 // if we are not attached to our window
10067 final AttachInfo attachInfo = mAttachInfo;
10068 if (attachInfo != null) {
10069 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10070 info.target = this;
10071 info.left = left;
10072 info.top = top;
10073 info.right = right;
10074 info.bottom = bottom;
10075
10076 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10077 }
10078 }
10079
10080 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010081 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10082 * This event is sent at most once every
10083 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10084 */
10085 private void postSendViewScrolledAccessibilityEventCallback() {
10086 if (mSendViewScrolledAccessibilityEvent == null) {
10087 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10088 }
10089 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10090 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10091 postDelayed(mSendViewScrolledAccessibilityEvent,
10092 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10093 }
10094 }
10095
10096 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010097 * Called by a parent to request that a child update its values for mScrollX
10098 * and mScrollY if necessary. This will typically be done if the child is
10099 * animating a scroll using a {@link android.widget.Scroller Scroller}
10100 * object.
10101 */
10102 public void computeScroll() {
10103 }
10104
10105 /**
10106 * <p>Indicate whether the horizontal edges are faded when the view is
10107 * scrolled horizontally.</p>
10108 *
10109 * @return true if the horizontal edges should are faded on scroll, false
10110 * otherwise
10111 *
10112 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010113 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010114 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010115 */
10116 public boolean isHorizontalFadingEdgeEnabled() {
10117 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10118 }
10119
10120 /**
10121 * <p>Define whether the horizontal edges should be faded when this view
10122 * is scrolled horizontally.</p>
10123 *
10124 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10125 * be faded when the view is scrolled
10126 * horizontally
10127 *
10128 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010129 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010130 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010131 */
10132 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10133 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10134 if (horizontalFadingEdgeEnabled) {
10135 initScrollCache();
10136 }
10137
10138 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10139 }
10140 }
10141
10142 /**
10143 * <p>Indicate whether the vertical edges are faded when the view is
10144 * scrolled horizontally.</p>
10145 *
10146 * @return true if the vertical edges should are faded on scroll, false
10147 * otherwise
10148 *
10149 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010150 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010151 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010152 */
10153 public boolean isVerticalFadingEdgeEnabled() {
10154 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10155 }
10156
10157 /**
10158 * <p>Define whether the vertical edges should be faded when this view
10159 * is scrolled vertically.</p>
10160 *
10161 * @param verticalFadingEdgeEnabled true if the vertical edges should
10162 * be faded when the view is scrolled
10163 * vertically
10164 *
10165 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010166 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010167 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010168 */
10169 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10170 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10171 if (verticalFadingEdgeEnabled) {
10172 initScrollCache();
10173 }
10174
10175 mViewFlags ^= FADING_EDGE_VERTICAL;
10176 }
10177 }
10178
10179 /**
10180 * Returns the strength, or intensity, of the top faded edge. The strength is
10181 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10182 * returns 0.0 or 1.0 but no value in between.
10183 *
10184 * Subclasses should override this method to provide a smoother fade transition
10185 * when scrolling occurs.
10186 *
10187 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10188 */
10189 protected float getTopFadingEdgeStrength() {
10190 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10191 }
10192
10193 /**
10194 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10195 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10196 * returns 0.0 or 1.0 but no value in between.
10197 *
10198 * Subclasses should override this method to provide a smoother fade transition
10199 * when scrolling occurs.
10200 *
10201 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10202 */
10203 protected float getBottomFadingEdgeStrength() {
10204 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10205 computeVerticalScrollRange() ? 1.0f : 0.0f;
10206 }
10207
10208 /**
10209 * Returns the strength, or intensity, of the left faded edge. The strength is
10210 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10211 * returns 0.0 or 1.0 but no value in between.
10212 *
10213 * Subclasses should override this method to provide a smoother fade transition
10214 * when scrolling occurs.
10215 *
10216 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10217 */
10218 protected float getLeftFadingEdgeStrength() {
10219 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10220 }
10221
10222 /**
10223 * Returns the strength, or intensity, of the right faded edge. The strength is
10224 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10225 * returns 0.0 or 1.0 but no value in between.
10226 *
10227 * Subclasses should override this method to provide a smoother fade transition
10228 * when scrolling occurs.
10229 *
10230 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10231 */
10232 protected float getRightFadingEdgeStrength() {
10233 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10234 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10235 }
10236
10237 /**
10238 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10239 * scrollbar is not drawn by default.</p>
10240 *
10241 * @return true if the horizontal scrollbar should be painted, false
10242 * otherwise
10243 *
10244 * @see #setHorizontalScrollBarEnabled(boolean)
10245 */
10246 public boolean isHorizontalScrollBarEnabled() {
10247 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10248 }
10249
10250 /**
10251 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10252 * scrollbar is not drawn by default.</p>
10253 *
10254 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10255 * be painted
10256 *
10257 * @see #isHorizontalScrollBarEnabled()
10258 */
10259 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10260 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10261 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010262 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010263 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010264 }
10265 }
10266
10267 /**
10268 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10269 * scrollbar is not drawn by default.</p>
10270 *
10271 * @return true if the vertical scrollbar should be painted, false
10272 * otherwise
10273 *
10274 * @see #setVerticalScrollBarEnabled(boolean)
10275 */
10276 public boolean isVerticalScrollBarEnabled() {
10277 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10278 }
10279
10280 /**
10281 * <p>Define whether the vertical scrollbar should be drawn or not. The
10282 * scrollbar is not drawn by default.</p>
10283 *
10284 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10285 * be painted
10286 *
10287 * @see #isVerticalScrollBarEnabled()
10288 */
10289 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10290 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10291 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010292 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010293 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010294 }
10295 }
10296
Adam Powell20232d02010-12-08 21:08:53 -080010297 /**
10298 * @hide
10299 */
10300 protected void recomputePadding() {
10301 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010302 }
Joe Malin32736f02011-01-19 16:14:20 -080010303
Mike Cleronfe81d382009-09-28 14:22:16 -070010304 /**
10305 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010306 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010307 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010308 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010309 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010310 */
10311 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10312 initScrollCache();
10313 final ScrollabilityCache scrollabilityCache = mScrollCache;
10314 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010315 if (fadeScrollbars) {
10316 scrollabilityCache.state = ScrollabilityCache.OFF;
10317 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010318 scrollabilityCache.state = ScrollabilityCache.ON;
10319 }
10320 }
Joe Malin32736f02011-01-19 16:14:20 -080010321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010322 /**
Joe Malin32736f02011-01-19 16:14:20 -080010323 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010324 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010325 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010326 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010327 *
10328 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010329 */
10330 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010331 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010332 }
Joe Malin32736f02011-01-19 16:14:20 -080010333
Mike Cleron52f0a642009-09-28 18:21:37 -070010334 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010335 *
10336 * Returns the delay before scrollbars fade.
10337 *
10338 * @return the delay before scrollbars fade
10339 *
10340 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10341 */
10342 public int getScrollBarDefaultDelayBeforeFade() {
10343 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10344 mScrollCache.scrollBarDefaultDelayBeforeFade;
10345 }
10346
10347 /**
10348 * Define the delay before scrollbars fade.
10349 *
10350 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10351 *
10352 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10353 */
10354 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10355 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10356 }
10357
10358 /**
10359 *
10360 * Returns the scrollbar fade duration.
10361 *
10362 * @return the scrollbar fade duration
10363 *
10364 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10365 */
10366 public int getScrollBarFadeDuration() {
10367 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10368 mScrollCache.scrollBarFadeDuration;
10369 }
10370
10371 /**
10372 * Define the scrollbar fade duration.
10373 *
10374 * @param scrollBarFadeDuration - the scrollbar fade duration
10375 *
10376 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10377 */
10378 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10379 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10380 }
10381
10382 /**
10383 *
10384 * Returns the scrollbar size.
10385 *
10386 * @return the scrollbar size
10387 *
10388 * @attr ref android.R.styleable#View_scrollbarSize
10389 */
10390 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010391 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010392 mScrollCache.scrollBarSize;
10393 }
10394
10395 /**
10396 * Define the scrollbar size.
10397 *
10398 * @param scrollBarSize - the scrollbar size
10399 *
10400 * @attr ref android.R.styleable#View_scrollbarSize
10401 */
10402 public void setScrollBarSize(int scrollBarSize) {
10403 getScrollCache().scrollBarSize = scrollBarSize;
10404 }
10405
10406 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010407 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10408 * inset. When inset, they add to the padding of the view. And the scrollbars
10409 * can be drawn inside the padding area or on the edge of the view. For example,
10410 * if a view has a background drawable and you want to draw the scrollbars
10411 * inside the padding specified by the drawable, you can use
10412 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10413 * appear at the edge of the view, ignoring the padding, then you can use
10414 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10415 * @param style the style of the scrollbars. Should be one of
10416 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10417 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10418 * @see #SCROLLBARS_INSIDE_OVERLAY
10419 * @see #SCROLLBARS_INSIDE_INSET
10420 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10421 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010422 *
10423 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010424 */
10425 public void setScrollBarStyle(int style) {
10426 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10427 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010428 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010429 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010430 }
10431 }
10432
10433 /**
10434 * <p>Returns the current scrollbar style.</p>
10435 * @return the current scrollbar style
10436 * @see #SCROLLBARS_INSIDE_OVERLAY
10437 * @see #SCROLLBARS_INSIDE_INSET
10438 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10439 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010440 *
10441 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010442 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010443 @ViewDebug.ExportedProperty(mapping = {
10444 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10445 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10446 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10447 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10448 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010449 public int getScrollBarStyle() {
10450 return mViewFlags & SCROLLBARS_STYLE_MASK;
10451 }
10452
10453 /**
10454 * <p>Compute the horizontal range that the horizontal scrollbar
10455 * represents.</p>
10456 *
10457 * <p>The range is expressed in arbitrary units that must be the same as the
10458 * units used by {@link #computeHorizontalScrollExtent()} and
10459 * {@link #computeHorizontalScrollOffset()}.</p>
10460 *
10461 * <p>The default range is the drawing width of this view.</p>
10462 *
10463 * @return the total horizontal range represented by the horizontal
10464 * scrollbar
10465 *
10466 * @see #computeHorizontalScrollExtent()
10467 * @see #computeHorizontalScrollOffset()
10468 * @see android.widget.ScrollBarDrawable
10469 */
10470 protected int computeHorizontalScrollRange() {
10471 return getWidth();
10472 }
10473
10474 /**
10475 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10476 * within the horizontal range. This value is used to compute the position
10477 * of the thumb within the scrollbar's track.</p>
10478 *
10479 * <p>The range is expressed in arbitrary units that must be the same as the
10480 * units used by {@link #computeHorizontalScrollRange()} and
10481 * {@link #computeHorizontalScrollExtent()}.</p>
10482 *
10483 * <p>The default offset is the scroll offset of this view.</p>
10484 *
10485 * @return the horizontal offset of the scrollbar's thumb
10486 *
10487 * @see #computeHorizontalScrollRange()
10488 * @see #computeHorizontalScrollExtent()
10489 * @see android.widget.ScrollBarDrawable
10490 */
10491 protected int computeHorizontalScrollOffset() {
10492 return mScrollX;
10493 }
10494
10495 /**
10496 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10497 * within the horizontal range. This value is used to compute the length
10498 * of the thumb within the scrollbar's track.</p>
10499 *
10500 * <p>The range is expressed in arbitrary units that must be the same as the
10501 * units used by {@link #computeHorizontalScrollRange()} and
10502 * {@link #computeHorizontalScrollOffset()}.</p>
10503 *
10504 * <p>The default extent is the drawing width of this view.</p>
10505 *
10506 * @return the horizontal extent of the scrollbar's thumb
10507 *
10508 * @see #computeHorizontalScrollRange()
10509 * @see #computeHorizontalScrollOffset()
10510 * @see android.widget.ScrollBarDrawable
10511 */
10512 protected int computeHorizontalScrollExtent() {
10513 return getWidth();
10514 }
10515
10516 /**
10517 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
10518 *
10519 * <p>The range is expressed in arbitrary units that must be the same as the
10520 * units used by {@link #computeVerticalScrollExtent()} and
10521 * {@link #computeVerticalScrollOffset()}.</p>
10522 *
10523 * @return the total vertical range represented by the vertical scrollbar
10524 *
10525 * <p>The default range is the drawing height of this view.</p>
10526 *
10527 * @see #computeVerticalScrollExtent()
10528 * @see #computeVerticalScrollOffset()
10529 * @see android.widget.ScrollBarDrawable
10530 */
10531 protected int computeVerticalScrollRange() {
10532 return getHeight();
10533 }
10534
10535 /**
10536 * <p>Compute the vertical offset of the vertical scrollbar's thumb
10537 * within the horizontal range. This value is used to compute the position
10538 * of the thumb within the scrollbar's track.</p>
10539 *
10540 * <p>The range is expressed in arbitrary units that must be the same as the
10541 * units used by {@link #computeVerticalScrollRange()} and
10542 * {@link #computeVerticalScrollExtent()}.</p>
10543 *
10544 * <p>The default offset is the scroll offset of this view.</p>
10545 *
10546 * @return the vertical offset of the scrollbar's thumb
10547 *
10548 * @see #computeVerticalScrollRange()
10549 * @see #computeVerticalScrollExtent()
10550 * @see android.widget.ScrollBarDrawable
10551 */
10552 protected int computeVerticalScrollOffset() {
10553 return mScrollY;
10554 }
10555
10556 /**
10557 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
10558 * within the vertical range. This value is used to compute the length
10559 * of the thumb within the scrollbar's track.</p>
10560 *
10561 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080010562 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010563 * {@link #computeVerticalScrollOffset()}.</p>
10564 *
10565 * <p>The default extent is the drawing height of this view.</p>
10566 *
10567 * @return the vertical extent of the scrollbar's thumb
10568 *
10569 * @see #computeVerticalScrollRange()
10570 * @see #computeVerticalScrollOffset()
10571 * @see android.widget.ScrollBarDrawable
10572 */
10573 protected int computeVerticalScrollExtent() {
10574 return getHeight();
10575 }
10576
10577 /**
Adam Powell69159442011-06-13 17:53:06 -070010578 * Check if this view can be scrolled horizontally in a certain direction.
10579 *
10580 * @param direction Negative to check scrolling left, positive to check scrolling right.
10581 * @return true if this view can be scrolled in the specified direction, false otherwise.
10582 */
10583 public boolean canScrollHorizontally(int direction) {
10584 final int offset = computeHorizontalScrollOffset();
10585 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
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 /**
10595 * Check if this view can be scrolled vertically in a certain direction.
10596 *
10597 * @param direction Negative to check scrolling up, positive to check scrolling down.
10598 * @return true if this view can be scrolled in the specified direction, false otherwise.
10599 */
10600 public boolean canScrollVertically(int direction) {
10601 final int offset = computeVerticalScrollOffset();
10602 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
10603 if (range == 0) return false;
10604 if (direction < 0) {
10605 return offset > 0;
10606 } else {
10607 return offset < range - 1;
10608 }
10609 }
10610
10611 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010612 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
10613 * scrollbars are painted only if they have been awakened first.</p>
10614 *
10615 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080010616 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010617 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010618 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080010619 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010620 // scrollbars are drawn only when the animation is running
10621 final ScrollabilityCache cache = mScrollCache;
10622 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080010623
Mike Cleronf116bf82009-09-27 19:14:12 -070010624 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080010625
Mike Cleronf116bf82009-09-27 19:14:12 -070010626 if (state == ScrollabilityCache.OFF) {
10627 return;
10628 }
Joe Malin32736f02011-01-19 16:14:20 -080010629
Mike Cleronf116bf82009-09-27 19:14:12 -070010630 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080010631
Mike Cleronf116bf82009-09-27 19:14:12 -070010632 if (state == ScrollabilityCache.FADING) {
10633 // We're fading -- get our fade interpolation
10634 if (cache.interpolatorValues == null) {
10635 cache.interpolatorValues = new float[1];
10636 }
Joe Malin32736f02011-01-19 16:14:20 -080010637
Mike Cleronf116bf82009-09-27 19:14:12 -070010638 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080010639
Mike Cleronf116bf82009-09-27 19:14:12 -070010640 // Stops the animation if we're done
10641 if (cache.scrollBarInterpolator.timeToValues(values) ==
10642 Interpolator.Result.FREEZE_END) {
10643 cache.state = ScrollabilityCache.OFF;
10644 } else {
10645 cache.scrollBar.setAlpha(Math.round(values[0]));
10646 }
Joe Malin32736f02011-01-19 16:14:20 -080010647
10648 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070010649 // drawing. We only want this when we're fading so that
10650 // we prevent excessive redraws
10651 invalidate = true;
10652 } else {
10653 // We're just on -- but we may have been fading before so
10654 // reset alpha
10655 cache.scrollBar.setAlpha(255);
10656 }
10657
Joe Malin32736f02011-01-19 16:14:20 -080010658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010659 final int viewFlags = mViewFlags;
10660
10661 final boolean drawHorizontalScrollBar =
10662 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10663 final boolean drawVerticalScrollBar =
10664 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
10665 && !isVerticalScrollBarHidden();
10666
10667 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
10668 final int width = mRight - mLeft;
10669 final int height = mBottom - mTop;
10670
10671 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010672
Mike Reede8853fc2009-09-04 14:01:48 -040010673 final int scrollX = mScrollX;
10674 final int scrollY = mScrollY;
10675 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
10676
Mike Cleronf116bf82009-09-27 19:14:12 -070010677 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080010678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010679 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010680 int size = scrollBar.getSize(false);
10681 if (size <= 0) {
10682 size = cache.scrollBarSize;
10683 }
10684
Mike Cleronf116bf82009-09-27 19:14:12 -070010685 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040010686 computeHorizontalScrollOffset(),
10687 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040010688 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070010689 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080010690 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070010691 left = scrollX + (mPaddingLeft & inside);
10692 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
10693 bottom = top + size;
10694 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
10695 if (invalidate) {
10696 invalidate(left, top, right, bottom);
10697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010698 }
10699
10700 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010701 int size = scrollBar.getSize(true);
10702 if (size <= 0) {
10703 size = cache.scrollBarSize;
10704 }
10705
Mike Reede8853fc2009-09-04 14:01:48 -040010706 scrollBar.setParameters(computeVerticalScrollRange(),
10707 computeVerticalScrollOffset(),
10708 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080010709 switch (mVerticalScrollbarPosition) {
10710 default:
10711 case SCROLLBAR_POSITION_DEFAULT:
10712 case SCROLLBAR_POSITION_RIGHT:
10713 left = scrollX + width - size - (mUserPaddingRight & inside);
10714 break;
10715 case SCROLLBAR_POSITION_LEFT:
10716 left = scrollX + (mUserPaddingLeft & inside);
10717 break;
10718 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010719 top = scrollY + (mPaddingTop & inside);
10720 right = left + size;
10721 bottom = scrollY + height - (mUserPaddingBottom & inside);
10722 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
10723 if (invalidate) {
10724 invalidate(left, top, right, bottom);
10725 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010726 }
10727 }
10728 }
10729 }
Romain Guy8506ab42009-06-11 17:35:47 -070010730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010731 /**
Romain Guy8506ab42009-06-11 17:35:47 -070010732 * 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 -080010733 * FastScroller is visible.
10734 * @return whether to temporarily hide the vertical scrollbar
10735 * @hide
10736 */
10737 protected boolean isVerticalScrollBarHidden() {
10738 return false;
10739 }
10740
10741 /**
10742 * <p>Draw the horizontal scrollbar if
10743 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
10744 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010745 * @param canvas the canvas on which to draw the scrollbar
10746 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010747 *
10748 * @see #isHorizontalScrollBarEnabled()
10749 * @see #computeHorizontalScrollRange()
10750 * @see #computeHorizontalScrollExtent()
10751 * @see #computeHorizontalScrollOffset()
10752 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070010753 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010754 */
Romain Guy8fb95422010-08-17 18:38:51 -070010755 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
10756 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010757 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010758 scrollBar.draw(canvas);
10759 }
Mike Reede8853fc2009-09-04 14:01:48 -040010760
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010761 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010762 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
10763 * returns true.</p>
10764 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010765 * @param canvas the canvas on which to draw the scrollbar
10766 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010767 *
10768 * @see #isVerticalScrollBarEnabled()
10769 * @see #computeVerticalScrollRange()
10770 * @see #computeVerticalScrollExtent()
10771 * @see #computeVerticalScrollOffset()
10772 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040010773 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010774 */
Romain Guy8fb95422010-08-17 18:38:51 -070010775 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
10776 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040010777 scrollBar.setBounds(l, t, r, b);
10778 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010779 }
10780
10781 /**
10782 * Implement this to do your drawing.
10783 *
10784 * @param canvas the canvas on which the background will be drawn
10785 */
10786 protected void onDraw(Canvas canvas) {
10787 }
10788
10789 /*
10790 * Caller is responsible for calling requestLayout if necessary.
10791 * (This allows addViewInLayout to not request a new layout.)
10792 */
10793 void assignParent(ViewParent parent) {
10794 if (mParent == null) {
10795 mParent = parent;
10796 } else if (parent == null) {
10797 mParent = null;
10798 } else {
10799 throw new RuntimeException("view " + this + " being added, but"
10800 + " it already has a parent");
10801 }
10802 }
10803
10804 /**
10805 * This is called when the view is attached to a window. At this point it
10806 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010807 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
10808 * however it may be called any time before the first onDraw -- including
10809 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010810 *
10811 * @see #onDetachedFromWindow()
10812 */
10813 protected void onAttachedToWindow() {
10814 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
10815 mParent.requestTransparentRegion(this);
10816 }
Adam Powell8568c3a2010-04-19 14:26:11 -070010817 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
10818 initialAwakenScrollBars();
10819 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
10820 }
Chet Haasea9b61ac2010-12-20 07:40:25 -080010821 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070010822 // Order is important here: LayoutDirection MUST be resolved before Padding
10823 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010824 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010825 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010826 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070010827 resolveTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070010828 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070010829 if (isFocused()) {
10830 InputMethodManager imm = InputMethodManager.peekInstance();
10831 imm.focusIn(this);
10832 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010833 }
Cibu Johny86666632010-02-22 13:01:02 -080010834
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010835 /**
Romain Guybb9908b2012-03-08 11:14:07 -080010836 * @see #onScreenStateChanged(int)
10837 */
10838 void dispatchScreenStateChanged(int screenState) {
10839 onScreenStateChanged(screenState);
10840 }
10841
10842 /**
10843 * This method is called whenever the state of the screen this view is
10844 * attached to changes. A state change will usually occurs when the screen
10845 * turns on or off (whether it happens automatically or the user does it
10846 * manually.)
10847 *
10848 * @param screenState The new state of the screen. Can be either
10849 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
10850 */
10851 public void onScreenStateChanged(int screenState) {
10852 }
10853
10854 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010855 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
10856 */
10857 private boolean hasRtlSupport() {
10858 return mContext.getApplicationInfo().hasRtlSupport();
10859 }
10860
10861 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010862 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
10863 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010864 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010865 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010866 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010867 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010868 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010869
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010870 if (hasRtlSupport()) {
10871 // Set resolved depending on layout direction
10872 switch (getLayoutDirection()) {
10873 case LAYOUT_DIRECTION_INHERIT:
10874 // If this is root view, no need to look at parent's layout dir.
10875 if (canResolveLayoutDirection()) {
10876 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010877
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010878 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
10879 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10880 }
10881 } else {
10882 // Nothing to do, LTR by default
10883 }
10884 break;
10885 case LAYOUT_DIRECTION_RTL:
10886 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10887 break;
10888 case LAYOUT_DIRECTION_LOCALE:
10889 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010890 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10891 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010892 break;
10893 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010894 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010895 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010896 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010897
10898 // Set to resolved
10899 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010900 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010901 // Resolve padding
10902 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010903 }
10904
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010905 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010906 * Called when layout direction has been resolved.
10907 *
10908 * The default implementation does nothing.
10909 */
10910 public void onResolvedLayoutDirectionChanged() {
10911 }
10912
10913 /**
10914 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010915 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010916 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010917 // If the user specified the absolute padding (either with android:padding or
10918 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
10919 // use the default padding or the padding from the background drawable
10920 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010921 int resolvedLayoutDirection = getResolvedLayoutDirection();
10922 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010923 case LAYOUT_DIRECTION_RTL:
10924 // Start user padding override Right user padding. Otherwise, if Right user
10925 // padding is not defined, use the default Right padding. If Right user padding
10926 // is defined, just use it.
10927 if (mUserPaddingStart >= 0) {
10928 mUserPaddingRight = mUserPaddingStart;
10929 } else if (mUserPaddingRight < 0) {
10930 mUserPaddingRight = mPaddingRight;
10931 }
10932 if (mUserPaddingEnd >= 0) {
10933 mUserPaddingLeft = mUserPaddingEnd;
10934 } else if (mUserPaddingLeft < 0) {
10935 mUserPaddingLeft = mPaddingLeft;
10936 }
10937 break;
10938 case LAYOUT_DIRECTION_LTR:
10939 default:
10940 // Start user padding override Left user padding. Otherwise, if Left user
10941 // padding is not defined, use the default left padding. If Left user padding
10942 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010943 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010944 mUserPaddingLeft = mUserPaddingStart;
10945 } else if (mUserPaddingLeft < 0) {
10946 mUserPaddingLeft = mPaddingLeft;
10947 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010948 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010949 mUserPaddingRight = mUserPaddingEnd;
10950 } else if (mUserPaddingRight < 0) {
10951 mUserPaddingRight = mPaddingRight;
10952 }
10953 }
10954
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010955 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
10956
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010957 if(isPaddingRelative()) {
10958 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
10959 } else {
10960 recomputePadding();
10961 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010962 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010963 }
10964
10965 /**
10966 * Resolve padding depending on the layout direction. Subclasses that care about
10967 * padding resolution should override this method. The default implementation does
10968 * nothing.
10969 *
10970 * @param layoutDirection the direction of the layout
10971 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080010972 * @see {@link #LAYOUT_DIRECTION_LTR}
10973 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010974 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010975 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010976 }
10977
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010978 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010979 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010980 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010981 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010982 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010983 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010984 switch (getLayoutDirection()) {
10985 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010986 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010987 default:
10988 return true;
10989 }
10990 }
10991
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010992 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010993 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
10994 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010995 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010996 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010997 // Reset the current resolved bits
10998 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010999 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011000 // Reset also the text direction
11001 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011002 }
11003
11004 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011005 * Called during reset of resolved layout direction.
11006 *
11007 * Subclasses need to override this method to clear cached information that depends on the
11008 * resolved layout direction, or to inform child views that inherit their layout direction.
11009 *
11010 * The default implementation does nothing.
11011 */
11012 public void onResolvedLayoutDirectionReset() {
11013 }
11014
11015 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011016 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011017 *
11018 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011019 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011020 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011021 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011022 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011023 }
11024
11025 /**
11026 * This is called when the view is detached from a window. At this point it
11027 * no longer has a surface for drawing.
11028 *
11029 * @see #onAttachedToWindow()
11030 */
11031 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011032 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011033
Romain Guya440b002010-02-24 15:57:54 -080011034 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011035 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011036 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011037 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011039 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011040
Romain Guya998dff2012-03-23 18:58:36 -070011041 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011042
11043 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011044 if (mDisplayList != null) {
11045 mAttachInfo.mViewRootImpl.invalidateDisplayList(mDisplayList);
11046 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011047 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011048 } else {
11049 if (mDisplayList != null) {
11050 // Should never happen
11051 mDisplayList.invalidate();
11052 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011053 }
11054
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011055 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011056
11057 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011058 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011059 resetAccessibilityStateChanged();
11060 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011061 }
11062
11063 /**
11064 * @return The number of times this view has been attached to a window
11065 */
11066 protected int getWindowAttachCount() {
11067 return mWindowAttachCount;
11068 }
11069
11070 /**
11071 * Retrieve a unique token identifying the window this view is attached to.
11072 * @return Return the window's token for use in
11073 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11074 */
11075 public IBinder getWindowToken() {
11076 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11077 }
11078
11079 /**
11080 * Retrieve a unique token identifying the top-level "real" window of
11081 * the window that this view is attached to. That is, this is like
11082 * {@link #getWindowToken}, except if the window this view in is a panel
11083 * window (attached to another containing window), then the token of
11084 * the containing window is returned instead.
11085 *
11086 * @return Returns the associated window token, either
11087 * {@link #getWindowToken()} or the containing window's token.
11088 */
11089 public IBinder getApplicationWindowToken() {
11090 AttachInfo ai = mAttachInfo;
11091 if (ai != null) {
11092 IBinder appWindowToken = ai.mPanelParentWindowToken;
11093 if (appWindowToken == null) {
11094 appWindowToken = ai.mWindowToken;
11095 }
11096 return appWindowToken;
11097 }
11098 return null;
11099 }
11100
11101 /**
11102 * Retrieve private session object this view hierarchy is using to
11103 * communicate with the window manager.
11104 * @return the session object to communicate with the window manager
11105 */
11106 /*package*/ IWindowSession getWindowSession() {
11107 return mAttachInfo != null ? mAttachInfo.mSession : null;
11108 }
11109
11110 /**
11111 * @param info the {@link android.view.View.AttachInfo} to associated with
11112 * this view
11113 */
11114 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11115 //System.out.println("Attached! " + this);
11116 mAttachInfo = info;
11117 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011118 // We will need to evaluate the drawable state at least once.
11119 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011120 if (mFloatingTreeObserver != null) {
11121 info.mTreeObserver.merge(mFloatingTreeObserver);
11122 mFloatingTreeObserver = null;
11123 }
11124 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11125 mAttachInfo.mScrollContainers.add(this);
11126 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11127 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011128 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011129 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011130
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011131 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011132 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011133 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011134 if (listeners != null && listeners.size() > 0) {
11135 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11136 // perform the dispatching. The iterator is a safe guard against listeners that
11137 // could mutate the list by calling the various add/remove methods. This prevents
11138 // the array from being modified while we iterate it.
11139 for (OnAttachStateChangeListener listener : listeners) {
11140 listener.onViewAttachedToWindow(this);
11141 }
11142 }
11143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011144 int vis = info.mWindowVisibility;
11145 if (vis != GONE) {
11146 onWindowVisibilityChanged(vis);
11147 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011148 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11149 // If nobody has evaluated the drawable state yet, then do it now.
11150 refreshDrawableState();
11151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011152 }
11153
11154 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011155 AttachInfo info = mAttachInfo;
11156 if (info != null) {
11157 int vis = info.mWindowVisibility;
11158 if (vis != GONE) {
11159 onWindowVisibilityChanged(GONE);
11160 }
11161 }
11162
11163 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011164
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011165 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011166 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011167 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011168 if (listeners != null && listeners.size() > 0) {
11169 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11170 // perform the dispatching. The iterator is a safe guard against listeners that
11171 // could mutate the list by calling the various add/remove methods. This prevents
11172 // the array from being modified while we iterate it.
11173 for (OnAttachStateChangeListener listener : listeners) {
11174 listener.onViewDetachedFromWindow(this);
11175 }
11176 }
11177
Romain Guy01d5edc2011-01-28 11:28:53 -080011178 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011179 mAttachInfo.mScrollContainers.remove(this);
11180 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11181 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011183 mAttachInfo = null;
11184 }
11185
11186 /**
11187 * Store this view hierarchy's frozen state into the given container.
11188 *
11189 * @param container The SparseArray in which to save the view's state.
11190 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011191 * @see #restoreHierarchyState(android.util.SparseArray)
11192 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11193 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011194 */
11195 public void saveHierarchyState(SparseArray<Parcelable> container) {
11196 dispatchSaveInstanceState(container);
11197 }
11198
11199 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011200 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11201 * this view and its children. May be overridden to modify how freezing happens to a
11202 * 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 -080011203 *
11204 * @param container The SparseArray in which to save the view's state.
11205 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011206 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11207 * @see #saveHierarchyState(android.util.SparseArray)
11208 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011209 */
11210 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11211 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11212 mPrivateFlags &= ~SAVE_STATE_CALLED;
11213 Parcelable state = onSaveInstanceState();
11214 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11215 throw new IllegalStateException(
11216 "Derived class did not call super.onSaveInstanceState()");
11217 }
11218 if (state != null) {
11219 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11220 // + ": " + state);
11221 container.put(mID, state);
11222 }
11223 }
11224 }
11225
11226 /**
11227 * Hook allowing a view to generate a representation of its internal state
11228 * that can later be used to create a new instance with that same state.
11229 * This state should only contain information that is not persistent or can
11230 * not be reconstructed later. For example, you will never store your
11231 * current position on screen because that will be computed again when a
11232 * new instance of the view is placed in its view hierarchy.
11233 * <p>
11234 * Some examples of things you may store here: the current cursor position
11235 * in a text view (but usually not the text itself since that is stored in a
11236 * content provider or other persistent storage), the currently selected
11237 * item in a list view.
11238 *
11239 * @return Returns a Parcelable object containing the view's current dynamic
11240 * state, or null if there is nothing interesting to save. The
11241 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011242 * @see #onRestoreInstanceState(android.os.Parcelable)
11243 * @see #saveHierarchyState(android.util.SparseArray)
11244 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011245 * @see #setSaveEnabled(boolean)
11246 */
11247 protected Parcelable onSaveInstanceState() {
11248 mPrivateFlags |= SAVE_STATE_CALLED;
11249 return BaseSavedState.EMPTY_STATE;
11250 }
11251
11252 /**
11253 * Restore this view hierarchy's frozen state from the given container.
11254 *
11255 * @param container The SparseArray which holds previously frozen states.
11256 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011257 * @see #saveHierarchyState(android.util.SparseArray)
11258 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11259 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011260 */
11261 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11262 dispatchRestoreInstanceState(container);
11263 }
11264
11265 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011266 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11267 * state for this view and its children. May be overridden to modify how restoring
11268 * happens to a view's children; for example, some views may want to not store state
11269 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011270 *
11271 * @param container The SparseArray which holds previously saved state.
11272 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011273 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11274 * @see #restoreHierarchyState(android.util.SparseArray)
11275 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011276 */
11277 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11278 if (mID != NO_ID) {
11279 Parcelable state = container.get(mID);
11280 if (state != null) {
11281 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11282 // + ": " + state);
11283 mPrivateFlags &= ~SAVE_STATE_CALLED;
11284 onRestoreInstanceState(state);
11285 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11286 throw new IllegalStateException(
11287 "Derived class did not call super.onRestoreInstanceState()");
11288 }
11289 }
11290 }
11291 }
11292
11293 /**
11294 * Hook allowing a view to re-apply a representation of its internal state that had previously
11295 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11296 * null state.
11297 *
11298 * @param state The frozen state that had previously been returned by
11299 * {@link #onSaveInstanceState}.
11300 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011301 * @see #onSaveInstanceState()
11302 * @see #restoreHierarchyState(android.util.SparseArray)
11303 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011304 */
11305 protected void onRestoreInstanceState(Parcelable state) {
11306 mPrivateFlags |= SAVE_STATE_CALLED;
11307 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011308 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11309 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011310 + "when two views of different type have the same id in the same hierarchy. "
11311 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011312 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011313 }
11314 }
11315
11316 /**
11317 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11318 *
11319 * @return the drawing start time in milliseconds
11320 */
11321 public long getDrawingTime() {
11322 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11323 }
11324
11325 /**
11326 * <p>Enables or disables the duplication of the parent's state into this view. When
11327 * duplication is enabled, this view gets its drawable state from its parent rather
11328 * than from its own internal properties.</p>
11329 *
11330 * <p>Note: in the current implementation, setting this property to true after the
11331 * view was added to a ViewGroup might have no effect at all. This property should
11332 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11333 *
11334 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11335 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011336 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011337 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11338 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011339 *
11340 * @param enabled True to enable duplication of the parent's drawable state, false
11341 * to disable it.
11342 *
11343 * @see #getDrawableState()
11344 * @see #isDuplicateParentStateEnabled()
11345 */
11346 public void setDuplicateParentStateEnabled(boolean enabled) {
11347 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11348 }
11349
11350 /**
11351 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11352 *
11353 * @return True if this view's drawable state is duplicated from the parent,
11354 * false otherwise
11355 *
11356 * @see #getDrawableState()
11357 * @see #setDuplicateParentStateEnabled(boolean)
11358 */
11359 public boolean isDuplicateParentStateEnabled() {
11360 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11361 }
11362
11363 /**
Romain Guy171c5922011-01-06 10:04:23 -080011364 * <p>Specifies the type of layer backing this view. The layer can be
11365 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11366 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011367 *
Romain Guy171c5922011-01-06 10:04:23 -080011368 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11369 * instance that controls how the layer is composed on screen. The following
11370 * properties of the paint are taken into account when composing the layer:</p>
11371 * <ul>
11372 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11373 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11374 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11375 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011376 *
Romain Guy171c5922011-01-06 10:04:23 -080011377 * <p>If this view has an alpha value set to < 1.0 by calling
11378 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11379 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11380 * equivalent to setting a hardware layer on this view and providing a paint with
11381 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011382 *
Romain Guy171c5922011-01-06 10:04:23 -080011383 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11384 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11385 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011386 *
Romain Guy171c5922011-01-06 10:04:23 -080011387 * @param layerType The ype of layer to use with this view, must be one of
11388 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11389 * {@link #LAYER_TYPE_HARDWARE}
11390 * @param paint The paint used to compose the layer. This argument is optional
11391 * and can be null. It is ignored when the layer type is
11392 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011393 *
11394 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011395 * @see #LAYER_TYPE_NONE
11396 * @see #LAYER_TYPE_SOFTWARE
11397 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011398 * @see #setAlpha(float)
11399 *
Romain Guy171c5922011-01-06 10:04:23 -080011400 * @attr ref android.R.styleable#View_layerType
11401 */
11402 public void setLayerType(int layerType, Paint paint) {
11403 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011404 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011405 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11406 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011407
Romain Guyd6cd5722011-01-17 14:42:41 -080011408 if (layerType == mLayerType) {
11409 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11410 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011411 invalidateParentCaches();
11412 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011413 }
11414 return;
11415 }
Romain Guy171c5922011-01-06 10:04:23 -080011416
11417 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011418 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011419 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011420 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011421 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011422 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011423 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011424 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011425 default:
11426 break;
Romain Guy171c5922011-01-06 10:04:23 -080011427 }
11428
11429 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011430 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11431 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11432 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011433
Romain Guy0fd89bf2011-01-26 15:41:30 -080011434 invalidateParentCaches();
11435 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011436 }
11437
11438 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011439 * Indicates whether this view has a static layer. A view with layer type
11440 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11441 * dynamic.
11442 */
11443 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011444 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011445 }
11446
11447 /**
Romain Guy171c5922011-01-06 10:04:23 -080011448 * Indicates what type of layer is currently associated with this view. By default
11449 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11450 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11451 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011452 *
Romain Guy171c5922011-01-06 10:04:23 -080011453 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11454 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011455 *
11456 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011457 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011458 * @see #LAYER_TYPE_NONE
11459 * @see #LAYER_TYPE_SOFTWARE
11460 * @see #LAYER_TYPE_HARDWARE
11461 */
11462 public int getLayerType() {
11463 return mLayerType;
11464 }
Joe Malin32736f02011-01-19 16:14:20 -080011465
Romain Guy6c319ca2011-01-11 14:29:25 -080011466 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011467 * Forces this view's layer to be created and this view to be rendered
11468 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11469 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011470 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011471 * This method can for instance be used to render a view into its layer before
11472 * starting an animation. If this view is complex, rendering into the layer
11473 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011474 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011475 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011476 *
11477 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080011478 */
11479 public void buildLayer() {
11480 if (mLayerType == LAYER_TYPE_NONE) return;
11481
11482 if (mAttachInfo == null) {
11483 throw new IllegalStateException("This view must be attached to a window first");
11484 }
11485
11486 switch (mLayerType) {
11487 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080011488 if (mAttachInfo.mHardwareRenderer != null &&
11489 mAttachInfo.mHardwareRenderer.isEnabled() &&
11490 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011491 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080011492 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011493 break;
11494 case LAYER_TYPE_SOFTWARE:
11495 buildDrawingCache(true);
11496 break;
11497 }
11498 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011499
Romain Guy9c4b79a2011-11-10 19:23:58 -080011500 // Make sure the HardwareRenderer.validate() was invoked before calling this method
11501 void flushLayer() {
11502 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
11503 mHardwareLayer.flush();
11504 }
11505 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011506
11507 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080011508 * <p>Returns a hardware layer that can be used to draw this view again
11509 * without executing its draw method.</p>
11510 *
11511 * @return A HardwareLayer ready to render, or null if an error occurred.
11512 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080011513 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070011514 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
11515 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080011516 return null;
11517 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011518
Romain Guy9c4b79a2011-11-10 19:23:58 -080011519 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080011520
11521 final int width = mRight - mLeft;
11522 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080011523
Romain Guy6c319ca2011-01-11 14:29:25 -080011524 if (width == 0 || height == 0) {
11525 return null;
11526 }
11527
11528 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
11529 if (mHardwareLayer == null) {
11530 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
11531 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070011532 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011533 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
11534 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070011535 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011536 }
11537
Romain Guy5cd5c3f2011-10-17 17:10:02 -070011538 // The layer is not valid if the underlying GPU resources cannot be allocated
11539 if (!mHardwareLayer.isValid()) {
11540 return null;
11541 }
11542
Chet Haasea1cff502012-02-21 13:43:44 -080011543 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080011544 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080011545 }
11546
11547 return mHardwareLayer;
11548 }
Romain Guy171c5922011-01-06 10:04:23 -080011549
Romain Guy589b0bb2011-10-10 13:57:47 -070011550 /**
11551 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070011552 *
Romain Guy589b0bb2011-10-10 13:57:47 -070011553 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070011554 *
11555 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070011556 * @see #LAYER_TYPE_HARDWARE
11557 */
Romain Guya998dff2012-03-23 18:58:36 -070011558 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070011559 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011560 AttachInfo info = mAttachInfo;
11561 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070011562 info.mHardwareRenderer.isEnabled() &&
11563 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011564 mHardwareLayer.destroy();
11565 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080011566
Romain Guy9c4b79a2011-11-10 19:23:58 -080011567 invalidate(true);
11568 invalidateParentCaches();
11569 }
Romain Guy65b345f2011-07-27 18:51:50 -070011570 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070011571 }
Romain Guy65b345f2011-07-27 18:51:50 -070011572 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070011573 }
11574
Romain Guy171c5922011-01-06 10:04:23 -080011575 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080011576 * Destroys all hardware rendering resources. This method is invoked
11577 * when the system needs to reclaim resources. Upon execution of this
11578 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070011579 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011580 * Note: you <strong>must</strong> call
11581 * <code>super.destroyHardwareResources()</code> when overriding
11582 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070011583 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011584 * @hide
11585 */
11586 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070011587 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011588 }
11589
11590 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011591 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
11592 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
11593 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
11594 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
11595 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
11596 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011597 *
Romain Guy171c5922011-01-06 10:04:23 -080011598 * <p>Enabling the drawing cache is similar to
11599 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080011600 * acceleration is turned off. When hardware acceleration is turned on, enabling the
11601 * drawing cache has no effect on rendering because the system uses a different mechanism
11602 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
11603 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
11604 * for information on how to enable software and hardware layers.</p>
11605 *
11606 * <p>This API can be used to manually generate
11607 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
11608 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011609 *
11610 * @param enabled true to enable the drawing cache, false otherwise
11611 *
11612 * @see #isDrawingCacheEnabled()
11613 * @see #getDrawingCache()
11614 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080011615 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011616 */
11617 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011618 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011619 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
11620 }
11621
11622 /**
11623 * <p>Indicates whether the drawing cache is enabled for this view.</p>
11624 *
11625 * @return true if the drawing cache is enabled
11626 *
11627 * @see #setDrawingCacheEnabled(boolean)
11628 * @see #getDrawingCache()
11629 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011630 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011631 public boolean isDrawingCacheEnabled() {
11632 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
11633 }
11634
11635 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080011636 * Debugging utility which recursively outputs the dirty state of a view and its
11637 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080011638 *
Chet Haasedaf98e92011-01-10 14:10:36 -080011639 * @hide
11640 */
Romain Guy676b1732011-02-14 14:45:33 -080011641 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080011642 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
11643 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
11644 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
11645 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
11646 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
11647 if (clear) {
11648 mPrivateFlags &= clearMask;
11649 }
11650 if (this instanceof ViewGroup) {
11651 ViewGroup parent = (ViewGroup) this;
11652 final int count = parent.getChildCount();
11653 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080011654 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080011655 child.outputDirtyFlags(indent + " ", clear, clearMask);
11656 }
11657 }
11658 }
11659
11660 /**
11661 * This method is used by ViewGroup to cause its children to restore or recreate their
11662 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
11663 * to recreate its own display list, which would happen if it went through the normal
11664 * draw/dispatchDraw mechanisms.
11665 *
11666 * @hide
11667 */
11668 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080011669
11670 /**
11671 * A view that is not attached or hardware accelerated cannot create a display list.
11672 * This method checks these conditions and returns the appropriate result.
11673 *
11674 * @return true if view has the ability to create a display list, false otherwise.
11675 *
11676 * @hide
11677 */
11678 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080011679 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080011680 }
Joe Malin32736f02011-01-19 16:14:20 -080011681
Chet Haasedaf98e92011-01-10 14:10:36 -080011682 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080011683 * @return The HardwareRenderer associated with that view or null if hardware rendering
11684 * is not supported or this this has not been attached to a window.
11685 *
11686 * @hide
11687 */
11688 public HardwareRenderer getHardwareRenderer() {
11689 if (mAttachInfo != null) {
11690 return mAttachInfo.mHardwareRenderer;
11691 }
11692 return null;
11693 }
11694
11695 /**
Chet Haasea1cff502012-02-21 13:43:44 -080011696 * Returns a DisplayList. If the incoming displayList is null, one will be created.
11697 * Otherwise, the same display list will be returned (after having been rendered into
11698 * along the way, depending on the invalidation state of the view).
11699 *
11700 * @param displayList The previous version of this displayList, could be null.
11701 * @param isLayer Whether the requester of the display list is a layer. If so,
11702 * the view will avoid creating a layer inside the resulting display list.
11703 * @return A new or reused DisplayList object.
11704 */
11705 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
11706 if (!canHaveDisplayList()) {
11707 return null;
11708 }
11709
11710 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
11711 displayList == null || !displayList.isValid() ||
11712 (!isLayer && mRecreateDisplayList))) {
11713 // Don't need to recreate the display list, just need to tell our
11714 // children to restore/recreate theirs
11715 if (displayList != null && displayList.isValid() &&
11716 !isLayer && !mRecreateDisplayList) {
11717 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11718 mPrivateFlags &= ~DIRTY_MASK;
11719 dispatchGetDisplayList();
11720
11721 return displayList;
11722 }
11723
11724 if (!isLayer) {
11725 // If we got here, we're recreating it. Mark it as such to ensure that
11726 // we copy in child display lists into ours in drawChild()
11727 mRecreateDisplayList = true;
11728 }
11729 if (displayList == null) {
11730 final String name = getClass().getSimpleName();
11731 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
11732 // If we're creating a new display list, make sure our parent gets invalidated
11733 // since they will need to recreate their display list to account for this
11734 // new child display list.
11735 invalidateParentCaches();
11736 }
11737
11738 boolean caching = false;
11739 final HardwareCanvas canvas = displayList.start();
11740 int restoreCount = 0;
11741 int width = mRight - mLeft;
11742 int height = mBottom - mTop;
11743
11744 try {
11745 canvas.setViewport(width, height);
11746 // The dirty rect should always be null for a display list
11747 canvas.onPreDraw(null);
11748 int layerType = (
11749 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
11750 getLayerType() : LAYER_TYPE_NONE;
Chet Haaseb85967b2012-03-26 14:37:51 -070011751 if (!isLayer && layerType != LAYER_TYPE_NONE && USE_DISPLAY_LIST_PROPERTIES) {
11752 if (layerType == LAYER_TYPE_HARDWARE) {
11753 final HardwareLayer layer = getHardwareLayer();
11754 if (layer != null && layer.isValid()) {
11755 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
11756 } else {
11757 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
11758 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
11759 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11760 }
11761 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080011762 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070011763 buildDrawingCache(true);
11764 Bitmap cache = getDrawingCache(true);
11765 if (cache != null) {
11766 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
11767 caching = true;
11768 }
Chet Haasea1cff502012-02-21 13:43:44 -080011769 }
Chet Haasea1cff502012-02-21 13:43:44 -080011770 } else {
11771
11772 computeScroll();
11773
11774 if (!USE_DISPLAY_LIST_PROPERTIES) {
11775 restoreCount = canvas.save();
11776 }
11777 canvas.translate(-mScrollX, -mScrollY);
11778 if (!isLayer) {
11779 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11780 mPrivateFlags &= ~DIRTY_MASK;
11781 }
11782
11783 // Fast path for layouts with no backgrounds
11784 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11785 dispatchDraw(canvas);
11786 } else {
11787 draw(canvas);
11788 }
11789 }
11790 } finally {
11791 if (USE_DISPLAY_LIST_PROPERTIES) {
11792 canvas.restoreToCount(restoreCount);
11793 }
11794 canvas.onPostDraw();
11795
11796 displayList.end();
11797 if (USE_DISPLAY_LIST_PROPERTIES) {
11798 displayList.setCaching(caching);
11799 }
11800 if (isLayer && USE_DISPLAY_LIST_PROPERTIES) {
11801 displayList.setLeftTopRightBottom(0, 0, width, height);
11802 } else {
11803 setDisplayListProperties(displayList);
11804 }
11805 }
11806 } else if (!isLayer) {
11807 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11808 mPrivateFlags &= ~DIRTY_MASK;
11809 }
11810
11811 return displayList;
11812 }
11813
11814 /**
11815 * Get the DisplayList for the HardwareLayer
11816 *
11817 * @param layer The HardwareLayer whose DisplayList we want
11818 * @return A DisplayList fopr the specified HardwareLayer
11819 */
11820 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
11821 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
11822 layer.setDisplayList(displayList);
11823 return displayList;
11824 }
11825
11826
11827 /**
Romain Guyb051e892010-09-28 19:09:36 -070011828 * <p>Returns a display list that can be used to draw this view again
11829 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011830 *
Romain Guyb051e892010-09-28 19:09:36 -070011831 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080011832 *
11833 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070011834 */
Chet Haasedaf98e92011-01-10 14:10:36 -080011835 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080011836 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070011837 return mDisplayList;
11838 }
11839
11840 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011841 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011842 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011843 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011844 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011845 * @see #getDrawingCache(boolean)
11846 */
11847 public Bitmap getDrawingCache() {
11848 return getDrawingCache(false);
11849 }
11850
11851 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011852 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
11853 * is null when caching is disabled. If caching is enabled and the cache is not ready,
11854 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
11855 * draw from the cache when the cache is enabled. To benefit from the cache, you must
11856 * request the drawing cache by calling this method and draw it on screen if the
11857 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011858 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011859 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11860 * this method will create a bitmap of the same size as this view. Because this bitmap
11861 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11862 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11863 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11864 * size than the view. This implies that your application must be able to handle this
11865 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011866 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011867 * @param autoScale Indicates whether the generated bitmap should be scaled based on
11868 * the current density of the screen when the application is in compatibility
11869 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011870 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011871 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011872 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011873 * @see #setDrawingCacheEnabled(boolean)
11874 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070011875 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011876 * @see #destroyDrawingCache()
11877 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011878 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011879 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
11880 return null;
11881 }
11882 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011883 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011884 }
Romain Guy02890fd2010-08-06 17:58:44 -070011885 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011886 }
11887
11888 /**
11889 * <p>Frees the resources used by the drawing cache. If you call
11890 * {@link #buildDrawingCache()} manually without calling
11891 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11892 * should cleanup the cache with this method afterwards.</p>
11893 *
11894 * @see #setDrawingCacheEnabled(boolean)
11895 * @see #buildDrawingCache()
11896 * @see #getDrawingCache()
11897 */
11898 public void destroyDrawingCache() {
11899 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011900 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011901 mDrawingCache = null;
11902 }
Romain Guyfbd8f692009-06-26 14:51:58 -070011903 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011904 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070011905 mUnscaledDrawingCache = null;
11906 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011907 }
11908
11909 /**
11910 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070011911 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011912 * view will always be drawn on top of a solid color.
11913 *
11914 * @param color The background color to use for the drawing cache's bitmap
11915 *
11916 * @see #setDrawingCacheEnabled(boolean)
11917 * @see #buildDrawingCache()
11918 * @see #getDrawingCache()
11919 */
11920 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080011921 if (color != mDrawingCacheBackgroundColor) {
11922 mDrawingCacheBackgroundColor = color;
11923 mPrivateFlags &= ~DRAWING_CACHE_VALID;
11924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011925 }
11926
11927 /**
11928 * @see #setDrawingCacheBackgroundColor(int)
11929 *
11930 * @return The background color to used for the drawing cache's bitmap
11931 */
11932 public int getDrawingCacheBackgroundColor() {
11933 return mDrawingCacheBackgroundColor;
11934 }
11935
11936 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011937 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011938 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011939 * @see #buildDrawingCache(boolean)
11940 */
11941 public void buildDrawingCache() {
11942 buildDrawingCache(false);
11943 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080011944
Romain Guyfbd8f692009-06-26 14:51:58 -070011945 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011946 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
11947 *
11948 * <p>If you call {@link #buildDrawingCache()} manually without calling
11949 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11950 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011951 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011952 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11953 * this method will create a bitmap of the same size as this view. Because this bitmap
11954 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11955 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11956 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11957 * size than the view. This implies that your application must be able to handle this
11958 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011959 *
Romain Guy0d9275e2010-10-26 14:22:30 -070011960 * <p>You should avoid calling this method when hardware acceleration is enabled. If
11961 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080011962 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070011963 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011964 *
11965 * @see #getDrawingCache()
11966 * @see #destroyDrawingCache()
11967 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011968 public void buildDrawingCache(boolean autoScale) {
11969 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070011970 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011971 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011972
11973 if (ViewDebug.TRACE_HIERARCHY) {
11974 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
11975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011976
Romain Guy8506ab42009-06-11 17:35:47 -070011977 int width = mRight - mLeft;
11978 int height = mBottom - mTop;
11979
11980 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070011981 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070011982
Romain Guye1123222009-06-29 14:24:56 -070011983 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011984 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
11985 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070011986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011987
11988 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070011989 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080011990 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011991
11992 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070011993 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080011994 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011995 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
11996 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080011997 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011998 return;
11999 }
12000
12001 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012002 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012003
12004 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012005 Bitmap.Config quality;
12006 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012007 // Never pick ARGB_4444 because it looks awful
12008 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012009 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12010 case DRAWING_CACHE_QUALITY_AUTO:
12011 quality = Bitmap.Config.ARGB_8888;
12012 break;
12013 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012014 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012015 break;
12016 case DRAWING_CACHE_QUALITY_HIGH:
12017 quality = Bitmap.Config.ARGB_8888;
12018 break;
12019 default:
12020 quality = Bitmap.Config.ARGB_8888;
12021 break;
12022 }
12023 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012024 // Optimization for translucent windows
12025 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012026 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012027 }
12028
12029 // Try to cleanup memory
12030 if (bitmap != null) bitmap.recycle();
12031
12032 try {
12033 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012034 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012035 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012036 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012037 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012038 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012039 }
Adam Powell26153a32010-11-08 15:22:27 -080012040 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012041 } catch (OutOfMemoryError e) {
12042 // If there is not enough memory to create the bitmap cache, just
12043 // ignore the issue as bitmap caches are not required to draw the
12044 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012045 if (autoScale) {
12046 mDrawingCache = null;
12047 } else {
12048 mUnscaledDrawingCache = null;
12049 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012050 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012051 return;
12052 }
12053
12054 clear = drawingCacheBackgroundColor != 0;
12055 }
12056
12057 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012058 if (attachInfo != null) {
12059 canvas = attachInfo.mCanvas;
12060 if (canvas == null) {
12061 canvas = new Canvas();
12062 }
12063 canvas.setBitmap(bitmap);
12064 // Temporarily clobber the cached Canvas in case one of our children
12065 // is also using a drawing cache. Without this, the children would
12066 // steal the canvas by attaching their own bitmap to it and bad, bad
12067 // thing would happen (invisible views, corrupted drawings, etc.)
12068 attachInfo.mCanvas = null;
12069 } else {
12070 // This case should hopefully never or seldom happen
12071 canvas = new Canvas(bitmap);
12072 }
12073
12074 if (clear) {
12075 bitmap.eraseColor(drawingCacheBackgroundColor);
12076 }
12077
12078 computeScroll();
12079 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012080
Romain Guye1123222009-06-29 14:24:56 -070012081 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012082 final float scale = attachInfo.mApplicationScale;
12083 canvas.scale(scale, scale);
12084 }
Joe Malin32736f02011-01-19 16:14:20 -080012085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012086 canvas.translate(-mScrollX, -mScrollY);
12087
Romain Guy5bcdff42009-05-14 21:27:18 -070012088 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012089 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12090 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012091 mPrivateFlags |= DRAWING_CACHE_VALID;
12092 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012093
12094 // Fast path for layouts with no backgrounds
12095 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12096 if (ViewDebug.TRACE_HIERARCHY) {
12097 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12098 }
Romain Guy5bcdff42009-05-14 21:27:18 -070012099 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012100 dispatchDraw(canvas);
12101 } else {
12102 draw(canvas);
12103 }
12104
12105 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012106 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012107
12108 if (attachInfo != null) {
12109 // Restore the cached Canvas for our siblings
12110 attachInfo.mCanvas = canvas;
12111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012112 }
12113 }
12114
12115 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012116 * Create a snapshot of the view into a bitmap. We should probably make
12117 * some form of this public, but should think about the API.
12118 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012119 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012120 int width = mRight - mLeft;
12121 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012122
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012123 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012124 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012125 width = (int) ((width * scale) + 0.5f);
12126 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012127
Romain Guy8c11e312009-09-14 15:15:30 -070012128 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012129 if (bitmap == null) {
12130 throw new OutOfMemoryError();
12131 }
12132
Romain Guyc529d8d2011-09-06 15:01:39 -070012133 Resources resources = getResources();
12134 if (resources != null) {
12135 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12136 }
Joe Malin32736f02011-01-19 16:14:20 -080012137
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012138 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012139 if (attachInfo != null) {
12140 canvas = attachInfo.mCanvas;
12141 if (canvas == null) {
12142 canvas = new Canvas();
12143 }
12144 canvas.setBitmap(bitmap);
12145 // Temporarily clobber the cached Canvas in case one of our children
12146 // is also using a drawing cache. Without this, the children would
12147 // steal the canvas by attaching their own bitmap to it and bad, bad
12148 // things would happen (invisible views, corrupted drawings, etc.)
12149 attachInfo.mCanvas = null;
12150 } else {
12151 // This case should hopefully never or seldom happen
12152 canvas = new Canvas(bitmap);
12153 }
12154
Romain Guy5bcdff42009-05-14 21:27:18 -070012155 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012156 bitmap.eraseColor(backgroundColor);
12157 }
12158
12159 computeScroll();
12160 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012161 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012162 canvas.translate(-mScrollX, -mScrollY);
12163
Romain Guy5bcdff42009-05-14 21:27:18 -070012164 // Temporarily remove the dirty mask
12165 int flags = mPrivateFlags;
12166 mPrivateFlags &= ~DIRTY_MASK;
12167
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012168 // Fast path for layouts with no backgrounds
12169 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12170 dispatchDraw(canvas);
12171 } else {
12172 draw(canvas);
12173 }
12174
Romain Guy5bcdff42009-05-14 21:27:18 -070012175 mPrivateFlags = flags;
12176
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012177 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012178 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012179
12180 if (attachInfo != null) {
12181 // Restore the cached Canvas for our siblings
12182 attachInfo.mCanvas = canvas;
12183 }
Romain Guy8506ab42009-06-11 17:35:47 -070012184
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012185 return bitmap;
12186 }
12187
12188 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012189 * Indicates whether this View is currently in edit mode. A View is usually
12190 * in edit mode when displayed within a developer tool. For instance, if
12191 * this View is being drawn by a visual user interface builder, this method
12192 * should return true.
12193 *
12194 * Subclasses should check the return value of this method to provide
12195 * different behaviors if their normal behavior might interfere with the
12196 * host environment. For instance: the class spawns a thread in its
12197 * constructor, the drawing code relies on device-specific features, etc.
12198 *
12199 * This method is usually checked in the drawing code of custom widgets.
12200 *
12201 * @return True if this View is in edit mode, false otherwise.
12202 */
12203 public boolean isInEditMode() {
12204 return false;
12205 }
12206
12207 /**
12208 * If the View draws content inside its padding and enables fading edges,
12209 * it needs to support padding offsets. Padding offsets are added to the
12210 * fading edges to extend the length of the fade so that it covers pixels
12211 * drawn inside the padding.
12212 *
12213 * Subclasses of this class should override this method if they need
12214 * to draw content inside the padding.
12215 *
12216 * @return True if padding offset must be applied, false otherwise.
12217 *
12218 * @see #getLeftPaddingOffset()
12219 * @see #getRightPaddingOffset()
12220 * @see #getTopPaddingOffset()
12221 * @see #getBottomPaddingOffset()
12222 *
12223 * @since CURRENT
12224 */
12225 protected boolean isPaddingOffsetRequired() {
12226 return false;
12227 }
12228
12229 /**
12230 * Amount by which to extend the left fading region. Called only when
12231 * {@link #isPaddingOffsetRequired()} returns true.
12232 *
12233 * @return The left padding offset in pixels.
12234 *
12235 * @see #isPaddingOffsetRequired()
12236 *
12237 * @since CURRENT
12238 */
12239 protected int getLeftPaddingOffset() {
12240 return 0;
12241 }
12242
12243 /**
12244 * Amount by which to extend the right fading region. Called only when
12245 * {@link #isPaddingOffsetRequired()} returns true.
12246 *
12247 * @return The right padding offset in pixels.
12248 *
12249 * @see #isPaddingOffsetRequired()
12250 *
12251 * @since CURRENT
12252 */
12253 protected int getRightPaddingOffset() {
12254 return 0;
12255 }
12256
12257 /**
12258 * Amount by which to extend the top fading region. Called only when
12259 * {@link #isPaddingOffsetRequired()} returns true.
12260 *
12261 * @return The top padding offset in pixels.
12262 *
12263 * @see #isPaddingOffsetRequired()
12264 *
12265 * @since CURRENT
12266 */
12267 protected int getTopPaddingOffset() {
12268 return 0;
12269 }
12270
12271 /**
12272 * Amount by which to extend the bottom fading region. Called only when
12273 * {@link #isPaddingOffsetRequired()} returns true.
12274 *
12275 * @return The bottom padding offset in pixels.
12276 *
12277 * @see #isPaddingOffsetRequired()
12278 *
12279 * @since CURRENT
12280 */
12281 protected int getBottomPaddingOffset() {
12282 return 0;
12283 }
12284
12285 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012286 * @hide
12287 * @param offsetRequired
12288 */
12289 protected int getFadeTop(boolean offsetRequired) {
12290 int top = mPaddingTop;
12291 if (offsetRequired) top += getTopPaddingOffset();
12292 return top;
12293 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012294
Romain Guyf2fc4602011-07-19 15:20:03 -070012295 /**
12296 * @hide
12297 * @param offsetRequired
12298 */
12299 protected int getFadeHeight(boolean offsetRequired) {
12300 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012301 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012302 return mBottom - mTop - mPaddingBottom - padding;
12303 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012304
Romain Guyf2fc4602011-07-19 15:20:03 -070012305 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012306 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012307 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012308 *
Romain Guy2bffd262010-09-12 17:40:02 -070012309 * <p>Even if this method returns true, it does not mean that every call
12310 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12311 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012312 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012313 * window is hardware accelerated,
12314 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12315 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012316 *
Romain Guy2bffd262010-09-12 17:40:02 -070012317 * @return True if the view is attached to a window and the window is
12318 * hardware accelerated; false in any other case.
12319 */
12320 public boolean isHardwareAccelerated() {
12321 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12322 }
Joe Malin32736f02011-01-19 16:14:20 -080012323
Romain Guy2bffd262010-09-12 17:40:02 -070012324 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012325 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12326 * case of an active Animation being run on the view.
12327 */
12328 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12329 Animation a, boolean scalingRequired) {
12330 Transformation invalidationTransform;
12331 final int flags = parent.mGroupFlags;
12332 final boolean initialized = a.isInitialized();
12333 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012334 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012335 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
12336 onAnimationStart();
12337 }
12338
12339 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12340 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12341 if (parent.mInvalidationTransformation == null) {
12342 parent.mInvalidationTransformation = new Transformation();
12343 }
12344 invalidationTransform = parent.mInvalidationTransformation;
12345 a.getTransformation(drawingTime, invalidationTransform, 1f);
12346 } else {
12347 invalidationTransform = parent.mChildTransformation;
12348 }
12349 if (more) {
12350 if (!a.willChangeBounds()) {
12351 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12352 parent.FLAG_OPTIMIZE_INVALIDATE) {
12353 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12354 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12355 // The child need to draw an animation, potentially offscreen, so
12356 // make sure we do not cancel invalidate requests
12357 parent.mPrivateFlags |= DRAW_ANIMATION;
12358 parent.invalidate(mLeft, mTop, mRight, mBottom);
12359 }
12360 } else {
12361 if (parent.mInvalidateRegion == null) {
12362 parent.mInvalidateRegion = new RectF();
12363 }
12364 final RectF region = parent.mInvalidateRegion;
12365 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12366 invalidationTransform);
12367
12368 // The child need to draw an animation, potentially offscreen, so
12369 // make sure we do not cancel invalidate requests
12370 parent.mPrivateFlags |= DRAW_ANIMATION;
12371
12372 final int left = mLeft + (int) region.left;
12373 final int top = mTop + (int) region.top;
12374 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12375 top + (int) (region.height() + .5f));
12376 }
12377 }
12378 return more;
12379 }
12380
Chet Haasea1cff502012-02-21 13:43:44 -080012381 void setDisplayListProperties() {
12382 setDisplayListProperties(mDisplayList);
12383 }
12384
12385 /**
12386 * This method is called by getDisplayList() when a display list is created or re-rendered.
12387 * It sets or resets the current value of all properties on that display list (resetting is
12388 * necessary when a display list is being re-created, because we need to make sure that
12389 * previously-set transform values
12390 */
12391 void setDisplayListProperties(DisplayList displayList) {
12392 if (USE_DISPLAY_LIST_PROPERTIES && displayList != null) {
12393 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012394 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012395 if (mParent instanceof ViewGroup) {
12396 displayList.setClipChildren(
12397 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12398 }
Chet Haase9420abd2012-03-29 16:28:32 -070012399 float alpha = 1;
12400 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12401 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12402 ViewGroup parentVG = (ViewGroup) mParent;
12403 final boolean hasTransform =
12404 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12405 if (hasTransform) {
12406 Transformation transform = parentVG.mChildTransformation;
12407 final int transformType = parentVG.mChildTransformation.getTransformationType();
12408 if (transformType != Transformation.TYPE_IDENTITY) {
12409 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12410 alpha = transform.getAlpha();
12411 }
12412 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12413 displayList.setStaticMatrix(transform.getMatrix());
12414 }
12415 }
12416 }
Chet Haasea1cff502012-02-21 13:43:44 -080012417 }
12418 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012419 alpha *= mTransformationInfo.mAlpha;
12420 if (alpha < 1) {
12421 final int multipliedAlpha = (int) (255 * alpha);
12422 if (onSetAlpha(multipliedAlpha)) {
12423 alpha = 1;
12424 }
12425 }
12426 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012427 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12428 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12429 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12430 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012431 if (mTransformationInfo.mCamera == null) {
12432 mTransformationInfo.mCamera = new Camera();
12433 mTransformationInfo.matrix3D = new Matrix();
12434 }
12435 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012436 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12437 displayList.setPivotX(getPivotX());
12438 displayList.setPivotY(getPivotY());
12439 }
Chet Haase9420abd2012-03-29 16:28:32 -070012440 } else if (alpha < 1) {
12441 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012442 }
12443 }
12444 }
12445
Chet Haasebcca79a2012-02-14 08:45:14 -080012446 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012447 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12448 * This draw() method is an implementation detail and is not intended to be overridden or
12449 * to be called from anywhere else other than ViewGroup.drawChild().
12450 */
12451 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haasea1cff502012-02-21 13:43:44 -080012452 boolean useDisplayListProperties = USE_DISPLAY_LIST_PROPERTIES && mAttachInfo != null &&
12453 mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012454 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012455 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012456 final int flags = parent.mGroupFlags;
12457
Chet Haasea1cff502012-02-21 13:43:44 -080012458 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012459 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012460 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012461 }
12462
12463 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012464 boolean concatMatrix = false;
12465
12466 boolean scalingRequired = false;
12467 boolean caching;
12468 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
12469
12470 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012471 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12472 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012473 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012474 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012475 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12476 } else {
12477 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12478 }
12479
Chet Haasebcca79a2012-02-14 08:45:14 -080012480 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080012481 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012482 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080012483 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080012484 transformToApply = parent.mChildTransformation;
Chet Haase9420abd2012-03-29 16:28:32 -070012485 } else if (!useDisplayListProperties &&
12486 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012487 final boolean hasTransform =
12488 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080012489 if (hasTransform) {
12490 final int transformType = parent.mChildTransformation.getTransformationType();
12491 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
12492 parent.mChildTransformation : null;
12493 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
12494 }
12495 }
12496
12497 concatMatrix |= !childHasIdentityMatrix;
12498
12499 // Sets the flag as early as possible to allow draw() implementations
12500 // to call invalidate() successfully when doing animations
12501 mPrivateFlags |= DRAWN;
12502
Chet Haasebcca79a2012-02-14 08:45:14 -080012503 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080012504 (mPrivateFlags & DRAW_ANIMATION) == 0) {
12505 return more;
12506 }
12507
12508 if (hardwareAccelerated) {
12509 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
12510 // retain the flag's value temporarily in the mRecreateDisplayList flag
12511 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
12512 mPrivateFlags &= ~INVALIDATED;
12513 }
12514
12515 computeScroll();
12516
12517 final int sx = mScrollX;
12518 final int sy = mScrollY;
12519
12520 DisplayList displayList = null;
12521 Bitmap cache = null;
12522 boolean hasDisplayList = false;
12523 if (caching) {
12524 if (!hardwareAccelerated) {
12525 if (layerType != LAYER_TYPE_NONE) {
12526 layerType = LAYER_TYPE_SOFTWARE;
12527 buildDrawingCache(true);
12528 }
12529 cache = getDrawingCache(true);
12530 } else {
12531 switch (layerType) {
12532 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070012533 if (useDisplayListProperties) {
12534 hasDisplayList = canHaveDisplayList();
12535 } else {
12536 buildDrawingCache(true);
12537 cache = getDrawingCache(true);
12538 }
Chet Haase64a48c12012-02-13 16:33:29 -080012539 break;
Chet Haasea1cff502012-02-21 13:43:44 -080012540 case LAYER_TYPE_HARDWARE:
12541 if (useDisplayListProperties) {
12542 hasDisplayList = canHaveDisplayList();
12543 }
12544 break;
Chet Haase64a48c12012-02-13 16:33:29 -080012545 case LAYER_TYPE_NONE:
12546 // Delay getting the display list until animation-driven alpha values are
12547 // set up and possibly passed on to the view
12548 hasDisplayList = canHaveDisplayList();
12549 break;
12550 }
12551 }
12552 }
Chet Haasea1cff502012-02-21 13:43:44 -080012553 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070012554 if (useDisplayListProperties) {
12555 displayList = getDisplayList();
12556 if (!displayList.isValid()) {
12557 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12558 // to getDisplayList(), the display list will be marked invalid and we should not
12559 // try to use it again.
12560 displayList = null;
12561 hasDisplayList = false;
12562 useDisplayListProperties = false;
12563 }
12564 }
Chet Haase64a48c12012-02-13 16:33:29 -080012565
12566 final boolean hasNoCache = cache == null || hasDisplayList;
12567 final boolean offsetForScroll = cache == null && !hasDisplayList &&
12568 layerType != LAYER_TYPE_HARDWARE;
12569
Chet Haasea1cff502012-02-21 13:43:44 -080012570 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070012571 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012572 restoreTo = canvas.save();
12573 }
Chet Haase64a48c12012-02-13 16:33:29 -080012574 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012575 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080012576 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080012577 if (!useDisplayListProperties) {
12578 canvas.translate(mLeft, mTop);
12579 }
Chet Haase64a48c12012-02-13 16:33:29 -080012580 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080012581 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070012582 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080012583 restoreTo = canvas.save();
12584 }
Chet Haase64a48c12012-02-13 16:33:29 -080012585 // mAttachInfo cannot be null, otherwise scalingRequired == false
12586 final float scale = 1.0f / mAttachInfo.mApplicationScale;
12587 canvas.scale(scale, scale);
12588 }
12589 }
12590
Chet Haasea1cff502012-02-21 13:43:44 -080012591 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012592 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080012593 if (transformToApply != null || !childHasIdentityMatrix) {
12594 int transX = 0;
12595 int transY = 0;
12596
12597 if (offsetForScroll) {
12598 transX = -sx;
12599 transY = -sy;
12600 }
12601
12602 if (transformToApply != null) {
12603 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070012604 if (useDisplayListProperties) {
12605 displayList.setAnimationMatrix(transformToApply.getMatrix());
12606 } else {
12607 // Undo the scroll translation, apply the transformation matrix,
12608 // then redo the scroll translate to get the correct result.
12609 canvas.translate(-transX, -transY);
12610 canvas.concat(transformToApply.getMatrix());
12611 canvas.translate(transX, transY);
12612 }
Chet Haasea1cff502012-02-21 13:43:44 -080012613 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012614 }
12615
12616 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012617 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012618 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080012619 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012620 }
12621 }
12622
Chet Haasea1cff502012-02-21 13:43:44 -080012623 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012624 canvas.translate(-transX, -transY);
12625 canvas.concat(getMatrix());
12626 canvas.translate(transX, transY);
12627 }
12628 }
12629
Chet Haase9420abd2012-03-29 16:28:32 -070012630 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080012631 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012632 if (hasNoCache) {
12633 final int multipliedAlpha = (int) (255 * alpha);
12634 if (!onSetAlpha(multipliedAlpha)) {
12635 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080012636 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080012637 layerType != LAYER_TYPE_NONE) {
12638 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
12639 }
Chet Haase9420abd2012-03-29 16:28:32 -070012640 if (useDisplayListProperties) {
12641 displayList.setAlpha(alpha * getAlpha());
12642 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070012643 final int scrollX = hasDisplayList ? 0 : sx;
12644 final int scrollY = hasDisplayList ? 0 : sy;
12645 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
12646 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080012647 }
12648 } else {
12649 // Alpha is handled by the child directly, clobber the layer's alpha
12650 mPrivateFlags |= ALPHA_SET;
12651 }
12652 }
12653 }
12654 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12655 onSetAlpha(255);
12656 mPrivateFlags &= ~ALPHA_SET;
12657 }
12658
Chet Haasea1cff502012-02-21 13:43:44 -080012659 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
12660 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012661 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012662 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080012663 } else {
12664 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012665 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080012666 } else {
12667 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
12668 }
12669 }
12670 }
12671
Chet Haase9420abd2012-03-29 16:28:32 -070012672 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080012673 displayList = getDisplayList();
12674 if (!displayList.isValid()) {
12675 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12676 // to getDisplayList(), the display list will be marked invalid and we should not
12677 // try to use it again.
12678 displayList = null;
12679 hasDisplayList = false;
12680 }
12681 }
12682
12683 if (hasNoCache) {
12684 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080012685 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012686 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080012687 if (layer != null && layer.isValid()) {
12688 mLayerPaint.setAlpha((int) (alpha * 255));
12689 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
12690 layerRendered = true;
12691 } else {
12692 final int scrollX = hasDisplayList ? 0 : sx;
12693 final int scrollY = hasDisplayList ? 0 : sy;
12694 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080012695 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080012696 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12697 }
12698 }
12699
12700 if (!layerRendered) {
12701 if (!hasDisplayList) {
12702 // Fast path for layouts with no backgrounds
12703 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12704 if (ViewDebug.TRACE_HIERARCHY) {
12705 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
12706 }
12707 mPrivateFlags &= ~DIRTY_MASK;
12708 dispatchDraw(canvas);
12709 } else {
12710 draw(canvas);
12711 }
12712 } else {
12713 mPrivateFlags &= ~DIRTY_MASK;
Chet Haasebcca79a2012-02-14 08:45:14 -080012714 ((HardwareCanvas) canvas).drawDisplayList(displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -080012715 mRight - mLeft, mBottom - mTop, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080012716 }
12717 }
12718 } else if (cache != null) {
12719 mPrivateFlags &= ~DIRTY_MASK;
12720 Paint cachePaint;
12721
12722 if (layerType == LAYER_TYPE_NONE) {
12723 cachePaint = parent.mCachePaint;
12724 if (cachePaint == null) {
12725 cachePaint = new Paint();
12726 cachePaint.setDither(false);
12727 parent.mCachePaint = cachePaint;
12728 }
Chet Haase9420abd2012-03-29 16:28:32 -070012729 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012730 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080012731 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
12732 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012733 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080012734 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080012735 }
12736 } else {
12737 cachePaint = mLayerPaint;
12738 cachePaint.setAlpha((int) (alpha * 255));
12739 }
12740 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
12741 }
12742
Chet Haasea1cff502012-02-21 13:43:44 -080012743 if (restoreTo >= 0) {
12744 canvas.restoreToCount(restoreTo);
12745 }
Chet Haase64a48c12012-02-13 16:33:29 -080012746
12747 if (a != null && !more) {
12748 if (!hardwareAccelerated && !a.getFillAfter()) {
12749 onSetAlpha(255);
12750 }
12751 parent.finishAnimatingView(this, a);
12752 }
12753
12754 if (more && hardwareAccelerated) {
12755 // invalidation is the trigger to recreate display lists, so if we're using
12756 // display lists to render, force an invalidate to allow the animation to
12757 // continue drawing another frame
12758 parent.invalidate(true);
12759 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12760 // alpha animations should cause the child to recreate its display list
12761 invalidate(true);
12762 }
12763 }
12764
12765 mRecreateDisplayList = false;
12766
12767 return more;
12768 }
12769
12770 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012771 * Manually render this view (and all of its children) to the given Canvas.
12772 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070012773 * called. When implementing a view, implement
12774 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
12775 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012776 *
12777 * @param canvas The Canvas to which the View is rendered.
12778 */
12779 public void draw(Canvas canvas) {
12780 if (ViewDebug.TRACE_HIERARCHY) {
12781 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12782 }
12783
Romain Guy5bcdff42009-05-14 21:27:18 -070012784 final int privateFlags = mPrivateFlags;
12785 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
12786 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
12787 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070012788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012789 /*
12790 * Draw traversal performs several drawing steps which must be executed
12791 * in the appropriate order:
12792 *
12793 * 1. Draw the background
12794 * 2. If necessary, save the canvas' layers to prepare for fading
12795 * 3. Draw view's content
12796 * 4. Draw children
12797 * 5. If necessary, draw the fading edges and restore layers
12798 * 6. Draw decorations (scrollbars for instance)
12799 */
12800
12801 // Step 1, draw the background, if needed
12802 int saveCount;
12803
Romain Guy24443ea2009-05-11 11:56:30 -070012804 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012805 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070012806 if (background != null) {
12807 final int scrollX = mScrollX;
12808 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012809
Romain Guy24443ea2009-05-11 11:56:30 -070012810 if (mBackgroundSizeChanged) {
12811 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
12812 mBackgroundSizeChanged = false;
12813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012814
Romain Guy24443ea2009-05-11 11:56:30 -070012815 if ((scrollX | scrollY) == 0) {
12816 background.draw(canvas);
12817 } else {
12818 canvas.translate(scrollX, scrollY);
12819 background.draw(canvas);
12820 canvas.translate(-scrollX, -scrollY);
12821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012822 }
12823 }
12824
12825 // skip step 2 & 5 if possible (common case)
12826 final int viewFlags = mViewFlags;
12827 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
12828 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
12829 if (!verticalEdges && !horizontalEdges) {
12830 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012831 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012832
12833 // Step 4, draw the children
12834 dispatchDraw(canvas);
12835
12836 // Step 6, draw decorations (scrollbars)
12837 onDrawScrollBars(canvas);
12838
12839 // we're done...
12840 return;
12841 }
12842
12843 /*
12844 * Here we do the full fledged routine...
12845 * (this is an uncommon case where speed matters less,
12846 * this is why we repeat some of the tests that have been
12847 * done above)
12848 */
12849
12850 boolean drawTop = false;
12851 boolean drawBottom = false;
12852 boolean drawLeft = false;
12853 boolean drawRight = false;
12854
12855 float topFadeStrength = 0.0f;
12856 float bottomFadeStrength = 0.0f;
12857 float leftFadeStrength = 0.0f;
12858 float rightFadeStrength = 0.0f;
12859
12860 // Step 2, save the canvas' layers
12861 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012862
12863 final boolean offsetRequired = isPaddingOffsetRequired();
12864 if (offsetRequired) {
12865 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012866 }
12867
12868 int left = mScrollX + paddingLeft;
12869 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070012870 int top = mScrollY + getFadeTop(offsetRequired);
12871 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012872
12873 if (offsetRequired) {
12874 right += getRightPaddingOffset();
12875 bottom += getBottomPaddingOffset();
12876 }
12877
12878 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070012879 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012880 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012881
12882 // clip the fade length if top and bottom fades overlap
12883 // overlapping fades produce odd-looking artifacts
12884 if (verticalEdges && (top + length > bottom - length)) {
12885 length = (bottom - top) / 2;
12886 }
12887
12888 // also clip horizontal fades if necessary
12889 if (horizontalEdges && (left + length > right - length)) {
12890 length = (right - left) / 2;
12891 }
12892
12893 if (verticalEdges) {
12894 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012895 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012896 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012897 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012898 }
12899
12900 if (horizontalEdges) {
12901 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012902 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012903 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012904 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012905 }
12906
12907 saveCount = canvas.getSaveCount();
12908
12909 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070012910 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012911 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
12912
12913 if (drawTop) {
12914 canvas.saveLayer(left, top, right, top + length, null, flags);
12915 }
12916
12917 if (drawBottom) {
12918 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
12919 }
12920
12921 if (drawLeft) {
12922 canvas.saveLayer(left, top, left + length, bottom, null, flags);
12923 }
12924
12925 if (drawRight) {
12926 canvas.saveLayer(right - length, top, right, bottom, null, flags);
12927 }
12928 } else {
12929 scrollabilityCache.setFadeColor(solidColor);
12930 }
12931
12932 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012933 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012934
12935 // Step 4, draw the children
12936 dispatchDraw(canvas);
12937
12938 // Step 5, draw the fade effect and restore layers
12939 final Paint p = scrollabilityCache.paint;
12940 final Matrix matrix = scrollabilityCache.matrix;
12941 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012942
12943 if (drawTop) {
12944 matrix.setScale(1, fadeHeight * topFadeStrength);
12945 matrix.postTranslate(left, top);
12946 fade.setLocalMatrix(matrix);
12947 canvas.drawRect(left, top, right, top + length, p);
12948 }
12949
12950 if (drawBottom) {
12951 matrix.setScale(1, fadeHeight * bottomFadeStrength);
12952 matrix.postRotate(180);
12953 matrix.postTranslate(left, bottom);
12954 fade.setLocalMatrix(matrix);
12955 canvas.drawRect(left, bottom - length, right, bottom, p);
12956 }
12957
12958 if (drawLeft) {
12959 matrix.setScale(1, fadeHeight * leftFadeStrength);
12960 matrix.postRotate(-90);
12961 matrix.postTranslate(left, top);
12962 fade.setLocalMatrix(matrix);
12963 canvas.drawRect(left, top, left + length, bottom, p);
12964 }
12965
12966 if (drawRight) {
12967 matrix.setScale(1, fadeHeight * rightFadeStrength);
12968 matrix.postRotate(90);
12969 matrix.postTranslate(right, top);
12970 fade.setLocalMatrix(matrix);
12971 canvas.drawRect(right - length, top, right, bottom, p);
12972 }
12973
12974 canvas.restoreToCount(saveCount);
12975
12976 // Step 6, draw decorations (scrollbars)
12977 onDrawScrollBars(canvas);
12978 }
12979
12980 /**
12981 * Override this if your view is known to always be drawn on top of a solid color background,
12982 * and needs to draw fading edges. Returning a non-zero color enables the view system to
12983 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
12984 * should be set to 0xFF.
12985 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012986 * @see #setVerticalFadingEdgeEnabled(boolean)
12987 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012988 *
12989 * @return The known solid color background for this view, or 0 if the color may vary
12990 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012991 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012992 public int getSolidColor() {
12993 return 0;
12994 }
12995
12996 /**
12997 * Build a human readable string representation of the specified view flags.
12998 *
12999 * @param flags the view flags to convert to a string
13000 * @return a String representing the supplied flags
13001 */
13002 private static String printFlags(int flags) {
13003 String output = "";
13004 int numFlags = 0;
13005 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13006 output += "TAKES_FOCUS";
13007 numFlags++;
13008 }
13009
13010 switch (flags & VISIBILITY_MASK) {
13011 case INVISIBLE:
13012 if (numFlags > 0) {
13013 output += " ";
13014 }
13015 output += "INVISIBLE";
13016 // USELESS HERE numFlags++;
13017 break;
13018 case GONE:
13019 if (numFlags > 0) {
13020 output += " ";
13021 }
13022 output += "GONE";
13023 // USELESS HERE numFlags++;
13024 break;
13025 default:
13026 break;
13027 }
13028 return output;
13029 }
13030
13031 /**
13032 * Build a human readable string representation of the specified private
13033 * view flags.
13034 *
13035 * @param privateFlags the private view flags to convert to a string
13036 * @return a String representing the supplied flags
13037 */
13038 private static String printPrivateFlags(int privateFlags) {
13039 String output = "";
13040 int numFlags = 0;
13041
13042 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13043 output += "WANTS_FOCUS";
13044 numFlags++;
13045 }
13046
13047 if ((privateFlags & FOCUSED) == FOCUSED) {
13048 if (numFlags > 0) {
13049 output += " ";
13050 }
13051 output += "FOCUSED";
13052 numFlags++;
13053 }
13054
13055 if ((privateFlags & SELECTED) == SELECTED) {
13056 if (numFlags > 0) {
13057 output += " ";
13058 }
13059 output += "SELECTED";
13060 numFlags++;
13061 }
13062
13063 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13064 if (numFlags > 0) {
13065 output += " ";
13066 }
13067 output += "IS_ROOT_NAMESPACE";
13068 numFlags++;
13069 }
13070
13071 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13072 if (numFlags > 0) {
13073 output += " ";
13074 }
13075 output += "HAS_BOUNDS";
13076 numFlags++;
13077 }
13078
13079 if ((privateFlags & DRAWN) == DRAWN) {
13080 if (numFlags > 0) {
13081 output += " ";
13082 }
13083 output += "DRAWN";
13084 // USELESS HERE numFlags++;
13085 }
13086 return output;
13087 }
13088
13089 /**
13090 * <p>Indicates whether or not this view's layout will be requested during
13091 * the next hierarchy layout pass.</p>
13092 *
13093 * @return true if the layout will be forced during next layout pass
13094 */
13095 public boolean isLayoutRequested() {
13096 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13097 }
13098
13099 /**
13100 * Assign a size and position to a view and all of its
13101 * descendants
13102 *
13103 * <p>This is the second phase of the layout mechanism.
13104 * (The first is measuring). In this phase, each parent calls
13105 * layout on all of its children to position them.
13106 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013107 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013108 *
Chet Haase9c087442011-01-12 16:20:16 -080013109 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013110 * Derived classes with children should override
13111 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013112 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013113 *
13114 * @param l Left position, relative to parent
13115 * @param t Top position, relative to parent
13116 * @param r Right position, relative to parent
13117 * @param b Bottom position, relative to parent
13118 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013119 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013120 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013121 int oldL = mLeft;
13122 int oldT = mTop;
13123 int oldB = mBottom;
13124 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013125 boolean changed = setFrame(l, t, r, b);
13126 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
13127 if (ViewDebug.TRACE_HIERARCHY) {
13128 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
13129 }
13130
13131 onLayout(changed, l, t, r, b);
13132 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013133
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013134 ListenerInfo li = mListenerInfo;
13135 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013136 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013137 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013138 int numListeners = listenersCopy.size();
13139 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013140 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013141 }
13142 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013143 }
13144 mPrivateFlags &= ~FORCE_LAYOUT;
13145 }
13146
13147 /**
13148 * Called from layout when this view should
13149 * assign a size and position to each of its children.
13150 *
13151 * Derived classes with children should override
13152 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013153 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013154 * @param changed This is a new size or position for this view
13155 * @param left Left position, relative to parent
13156 * @param top Top position, relative to parent
13157 * @param right Right position, relative to parent
13158 * @param bottom Bottom position, relative to parent
13159 */
13160 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13161 }
13162
13163 /**
13164 * Assign a size and position to this view.
13165 *
13166 * This is called from layout.
13167 *
13168 * @param left Left position, relative to parent
13169 * @param top Top position, relative to parent
13170 * @param right Right position, relative to parent
13171 * @param bottom Bottom position, relative to parent
13172 * @return true if the new size and position are different than the
13173 * previous ones
13174 * {@hide}
13175 */
13176 protected boolean setFrame(int left, int top, int right, int bottom) {
13177 boolean changed = false;
13178
13179 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013180 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013181 + right + "," + bottom + ")");
13182 }
13183
13184 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13185 changed = true;
13186
13187 // Remember our drawn bit
13188 int drawn = mPrivateFlags & DRAWN;
13189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013190 int oldWidth = mRight - mLeft;
13191 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013192 int newWidth = right - left;
13193 int newHeight = bottom - top;
13194 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13195
13196 // Invalidate our old position
13197 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013198
13199 mLeft = left;
13200 mTop = top;
13201 mRight = right;
13202 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -080013203 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
13204 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13205 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013206
13207 mPrivateFlags |= HAS_BOUNDS;
13208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013209
Chet Haase75755e22011-07-18 17:48:25 -070013210 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013211 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13212 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013213 if (mTransformationInfo != null) {
13214 mTransformationInfo.mMatrixDirty = true;
13215 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013217 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13218 }
13219
13220 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13221 // If we are visible, force the DRAWN bit to on so that
13222 // this invalidate will go through (at least to our parent).
13223 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013224 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013225 // the DRAWN bit.
13226 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013227 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013228 // parent display list may need to be recreated based on a change in the bounds
13229 // of any child
13230 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013231 }
13232
13233 // Reset drawn bit to original value (invalidate turns it off)
13234 mPrivateFlags |= drawn;
13235
13236 mBackgroundSizeChanged = true;
13237 }
13238 return changed;
13239 }
13240
13241 /**
13242 * Finalize inflating a view from XML. This is called as the last phase
13243 * of inflation, after all child views have been added.
13244 *
13245 * <p>Even if the subclass overrides onFinishInflate, they should always be
13246 * sure to call the super method, so that we get called.
13247 */
13248 protected void onFinishInflate() {
13249 }
13250
13251 /**
13252 * Returns the resources associated with this view.
13253 *
13254 * @return Resources object.
13255 */
13256 public Resources getResources() {
13257 return mResources;
13258 }
13259
13260 /**
13261 * Invalidates the specified Drawable.
13262 *
13263 * @param drawable the drawable to invalidate
13264 */
13265 public void invalidateDrawable(Drawable drawable) {
13266 if (verifyDrawable(drawable)) {
13267 final Rect dirty = drawable.getBounds();
13268 final int scrollX = mScrollX;
13269 final int scrollY = mScrollY;
13270
13271 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13272 dirty.right + scrollX, dirty.bottom + scrollY);
13273 }
13274 }
13275
13276 /**
13277 * Schedules an action on a drawable to occur at a specified time.
13278 *
13279 * @param who the recipient of the action
13280 * @param what the action to run on the drawable
13281 * @param when the time at which the action must occur. Uses the
13282 * {@link SystemClock#uptimeMillis} timebase.
13283 */
13284 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013285 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013286 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013287 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013288 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13289 Choreographer.CALLBACK_ANIMATION, what, who,
13290 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013291 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013292 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013293 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013294 }
13295 }
13296
13297 /**
13298 * Cancels a scheduled action on a drawable.
13299 *
13300 * @param who the recipient of the action
13301 * @param what the action to cancel
13302 */
13303 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013304 if (verifyDrawable(who) && what != null) {
13305 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013306 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13307 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013308 } else {
13309 ViewRootImpl.getRunQueue().removeCallbacks(what);
13310 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013311 }
13312 }
13313
13314 /**
13315 * Unschedule any events associated with the given Drawable. This can be
13316 * used when selecting a new Drawable into a view, so that the previous
13317 * one is completely unscheduled.
13318 *
13319 * @param who The Drawable to unschedule.
13320 *
13321 * @see #drawableStateChanged
13322 */
13323 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013324 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013325 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13326 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013327 }
13328 }
13329
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013330 /**
13331 * Return the layout direction of a given Drawable.
13332 *
13333 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013334 */
13335 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013336 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013337 }
13338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013339 /**
13340 * If your view subclass is displaying its own Drawable objects, it should
13341 * override this function and return true for any Drawable it is
13342 * displaying. This allows animations for those drawables to be
13343 * scheduled.
13344 *
13345 * <p>Be sure to call through to the super class when overriding this
13346 * function.
13347 *
13348 * @param who The Drawable to verify. Return true if it is one you are
13349 * displaying, else return the result of calling through to the
13350 * super class.
13351 *
13352 * @return boolean If true than the Drawable is being displayed in the
13353 * view; else false and it is not allowed to animate.
13354 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013355 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13356 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013357 */
13358 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013359 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013360 }
13361
13362 /**
13363 * This function is called whenever the state of the view changes in such
13364 * a way that it impacts the state of drawables being shown.
13365 *
13366 * <p>Be sure to call through to the superclass when overriding this
13367 * function.
13368 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013369 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013370 */
13371 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013372 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013373 if (d != null && d.isStateful()) {
13374 d.setState(getDrawableState());
13375 }
13376 }
13377
13378 /**
13379 * Call this to force a view to update its drawable state. This will cause
13380 * drawableStateChanged to be called on this view. Views that are interested
13381 * in the new state should call getDrawableState.
13382 *
13383 * @see #drawableStateChanged
13384 * @see #getDrawableState
13385 */
13386 public void refreshDrawableState() {
13387 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13388 drawableStateChanged();
13389
13390 ViewParent parent = mParent;
13391 if (parent != null) {
13392 parent.childDrawableStateChanged(this);
13393 }
13394 }
13395
13396 /**
13397 * Return an array of resource IDs of the drawable states representing the
13398 * current state of the view.
13399 *
13400 * @return The current drawable state
13401 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013402 * @see Drawable#setState(int[])
13403 * @see #drawableStateChanged()
13404 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013405 */
13406 public final int[] getDrawableState() {
13407 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13408 return mDrawableState;
13409 } else {
13410 mDrawableState = onCreateDrawableState(0);
13411 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13412 return mDrawableState;
13413 }
13414 }
13415
13416 /**
13417 * Generate the new {@link android.graphics.drawable.Drawable} state for
13418 * this view. This is called by the view
13419 * system when the cached Drawable state is determined to be invalid. To
13420 * retrieve the current state, you should use {@link #getDrawableState}.
13421 *
13422 * @param extraSpace if non-zero, this is the number of extra entries you
13423 * would like in the returned array in which you can place your own
13424 * states.
13425 *
13426 * @return Returns an array holding the current {@link Drawable} state of
13427 * the view.
13428 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013429 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013430 */
13431 protected int[] onCreateDrawableState(int extraSpace) {
13432 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13433 mParent instanceof View) {
13434 return ((View) mParent).onCreateDrawableState(extraSpace);
13435 }
13436
13437 int[] drawableState;
13438
13439 int privateFlags = mPrivateFlags;
13440
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013441 int viewStateIndex = 0;
13442 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13443 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13444 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013445 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013446 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13447 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013448 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13449 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013450 // This is set if HW acceleration is requested, even if the current
13451 // process doesn't allow it. This is just to allow app preview
13452 // windows to better match their app.
13453 viewStateIndex |= VIEW_STATE_ACCELERATED;
13454 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013455 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013456
Christopher Tate3d4bf172011-03-28 16:16:46 -070013457 final int privateFlags2 = mPrivateFlags2;
13458 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13459 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013461 drawableState = VIEW_STATE_SETS[viewStateIndex];
13462
13463 //noinspection ConstantIfStatement
13464 if (false) {
13465 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13466 Log.i("View", toString()
13467 + " pressed=" + ((privateFlags & PRESSED) != 0)
13468 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13469 + " fo=" + hasFocus()
13470 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013471 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013472 + ": " + Arrays.toString(drawableState));
13473 }
13474
13475 if (extraSpace == 0) {
13476 return drawableState;
13477 }
13478
13479 final int[] fullState;
13480 if (drawableState != null) {
13481 fullState = new int[drawableState.length + extraSpace];
13482 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
13483 } else {
13484 fullState = new int[extraSpace];
13485 }
13486
13487 return fullState;
13488 }
13489
13490 /**
13491 * Merge your own state values in <var>additionalState</var> into the base
13492 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013493 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013494 *
13495 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013496 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013497 * own additional state values.
13498 *
13499 * @param additionalState The additional state values you would like
13500 * added to <var>baseState</var>; this array is not modified.
13501 *
13502 * @return As a convenience, the <var>baseState</var> array you originally
13503 * passed into the function is returned.
13504 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013505 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013506 */
13507 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
13508 final int N = baseState.length;
13509 int i = N - 1;
13510 while (i >= 0 && baseState[i] == 0) {
13511 i--;
13512 }
13513 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
13514 return baseState;
13515 }
13516
13517 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070013518 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
13519 * on all Drawable objects associated with this view.
13520 */
13521 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013522 if (mBackground != null) {
13523 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070013524 }
13525 }
13526
13527 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013528 * Sets the background color for this view.
13529 * @param color the color of the background
13530 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013531 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013532 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013533 if (mBackground instanceof ColorDrawable) {
13534 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070013535 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070013536 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070013537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013538 }
13539
13540 /**
13541 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070013542 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013543 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070013544 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013545 * @attr ref android.R.styleable#View_background
13546 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013547 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013548 public void setBackgroundResource(int resid) {
13549 if (resid != 0 && resid == mBackgroundResource) {
13550 return;
13551 }
13552
13553 Drawable d= null;
13554 if (resid != 0) {
13555 d = mResources.getDrawable(resid);
13556 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013557 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013558
13559 mBackgroundResource = resid;
13560 }
13561
13562 /**
13563 * Set the background to a given Drawable, or remove the background. If the
13564 * background has padding, this View's padding is set to the background's
13565 * padding. However, when a background is removed, this View's padding isn't
13566 * touched. If setting the padding is desired, please use
13567 * {@link #setPadding(int, int, int, int)}.
13568 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013569 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013570 * background
13571 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013572 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070013573 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070013574 setBackgroundDrawable(background);
13575 }
13576
13577 /**
13578 * @deprecated use {@link #setBackground(Drawable)} instead
13579 */
13580 @Deprecated
13581 public void setBackgroundDrawable(Drawable background) {
13582 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070013583 return;
13584 }
13585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013586 boolean requestLayout = false;
13587
13588 mBackgroundResource = 0;
13589
13590 /*
13591 * Regardless of whether we're setting a new background or not, we want
13592 * to clear the previous drawable.
13593 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013594 if (mBackground != null) {
13595 mBackground.setCallback(null);
13596 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013597 }
13598
Philip Milne6c8ea062012-04-03 17:38:43 -070013599 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013600 Rect padding = sThreadLocal.get();
13601 if (padding == null) {
13602 padding = new Rect();
13603 sThreadLocal.set(padding);
13604 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013605 if (background.getPadding(padding)) {
13606 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013607 case LAYOUT_DIRECTION_RTL:
13608 setPadding(padding.right, padding.top, padding.left, padding.bottom);
13609 break;
13610 case LAYOUT_DIRECTION_LTR:
13611 default:
13612 setPadding(padding.left, padding.top, padding.right, padding.bottom);
13613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013614 }
13615
13616 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
13617 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070013618 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
13619 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013620 requestLayout = true;
13621 }
13622
Philip Milne6c8ea062012-04-03 17:38:43 -070013623 background.setCallback(this);
13624 if (background.isStateful()) {
13625 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013626 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013627 background.setVisible(getVisibility() == VISIBLE, false);
13628 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013629
13630 if ((mPrivateFlags & SKIP_DRAW) != 0) {
13631 mPrivateFlags &= ~SKIP_DRAW;
13632 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
13633 requestLayout = true;
13634 }
13635 } else {
13636 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070013637 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013638
13639 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
13640 /*
13641 * This view ONLY drew the background before and we're removing
13642 * the background, so now it won't draw anything
13643 * (hence we SKIP_DRAW)
13644 */
13645 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
13646 mPrivateFlags |= SKIP_DRAW;
13647 }
13648
13649 /*
13650 * When the background is set, we try to apply its padding to this
13651 * View. When the background is removed, we don't touch this View's
13652 * padding. This is noted in the Javadocs. Hence, we don't need to
13653 * requestLayout(), the invalidate() below is sufficient.
13654 */
13655
13656 // The old background's minimum size could have affected this
13657 // View's layout, so let's requestLayout
13658 requestLayout = true;
13659 }
13660
Romain Guy8f1344f52009-05-15 16:03:59 -070013661 computeOpaqueFlags();
13662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013663 if (requestLayout) {
13664 requestLayout();
13665 }
13666
13667 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013668 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013669 }
13670
13671 /**
13672 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070013673 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013674 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070013675 *
13676 * @see #setBackground(Drawable)
13677 *
13678 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013679 */
13680 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013681 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013682 }
13683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013684 /**
13685 * Sets the padding. The view may add on the space required to display
13686 * the scrollbars, depending on the style and visibility of the scrollbars.
13687 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
13688 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
13689 * from the values set in this call.
13690 *
13691 * @attr ref android.R.styleable#View_padding
13692 * @attr ref android.R.styleable#View_paddingBottom
13693 * @attr ref android.R.styleable#View_paddingLeft
13694 * @attr ref android.R.styleable#View_paddingRight
13695 * @attr ref android.R.styleable#View_paddingTop
13696 * @param left the left padding in pixels
13697 * @param top the top padding in pixels
13698 * @param right the right padding in pixels
13699 * @param bottom the bottom padding in pixels
13700 */
13701 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013702 mUserPaddingStart = -1;
13703 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013704 mUserPaddingRelative = false;
13705
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013706 internalSetPadding(left, top, right, bottom);
13707 }
13708
13709 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080013710 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013711 mUserPaddingRight = right;
13712 mUserPaddingBottom = bottom;
13713
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013714 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013715 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070013716
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013717 // Common case is there are no scroll bars.
13718 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013719 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080013720 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013721 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080013722 switch (mVerticalScrollbarPosition) {
13723 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013724 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
13725 left += offset;
13726 } else {
13727 right += offset;
13728 }
13729 break;
Adam Powell20232d02010-12-08 21:08:53 -080013730 case SCROLLBAR_POSITION_RIGHT:
13731 right += offset;
13732 break;
13733 case SCROLLBAR_POSITION_LEFT:
13734 left += offset;
13735 break;
13736 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013737 }
Adam Powell20232d02010-12-08 21:08:53 -080013738 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013739 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
13740 ? 0 : getHorizontalScrollbarHeight();
13741 }
13742 }
Romain Guy8506ab42009-06-11 17:35:47 -070013743
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013744 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013745 changed = true;
13746 mPaddingLeft = left;
13747 }
13748 if (mPaddingTop != top) {
13749 changed = true;
13750 mPaddingTop = top;
13751 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013752 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013753 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013754 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013755 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013756 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013757 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013758 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013759 }
13760
13761 if (changed) {
13762 requestLayout();
13763 }
13764 }
13765
13766 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013767 * Sets the relative padding. The view may add on the space required to display
13768 * the scrollbars, depending on the style and visibility of the scrollbars.
13769 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
13770 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
13771 * from the values set in this call.
13772 *
13773 * @attr ref android.R.styleable#View_padding
13774 * @attr ref android.R.styleable#View_paddingBottom
13775 * @attr ref android.R.styleable#View_paddingStart
13776 * @attr ref android.R.styleable#View_paddingEnd
13777 * @attr ref android.R.styleable#View_paddingTop
13778 * @param start the start padding in pixels
13779 * @param top the top padding in pixels
13780 * @param end the end padding in pixels
13781 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013782 */
13783 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013784 mUserPaddingStart = start;
13785 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013786 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013787
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013788 switch(getResolvedLayoutDirection()) {
13789 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013790 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013791 break;
13792 case LAYOUT_DIRECTION_LTR:
13793 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013794 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013795 }
13796 }
13797
13798 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013799 * Returns the top padding of this view.
13800 *
13801 * @return the top padding in pixels
13802 */
13803 public int getPaddingTop() {
13804 return mPaddingTop;
13805 }
13806
13807 /**
13808 * Returns the bottom padding of this view. If there are inset and enabled
13809 * scrollbars, this value may include the space required to display the
13810 * scrollbars as well.
13811 *
13812 * @return the bottom padding in pixels
13813 */
13814 public int getPaddingBottom() {
13815 return mPaddingBottom;
13816 }
13817
13818 /**
13819 * Returns the left padding of this view. If there are inset and enabled
13820 * scrollbars, this value may include the space required to display the
13821 * scrollbars as well.
13822 *
13823 * @return the left padding in pixels
13824 */
13825 public int getPaddingLeft() {
13826 return mPaddingLeft;
13827 }
13828
13829 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013830 * Returns the start padding of this view depending on its resolved layout direction.
13831 * If there are inset and enabled scrollbars, this value may include the space
13832 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013833 *
13834 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013835 */
13836 public int getPaddingStart() {
13837 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13838 mPaddingRight : mPaddingLeft;
13839 }
13840
13841 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013842 * Returns the right padding of this view. If there are inset and enabled
13843 * scrollbars, this value may include the space required to display the
13844 * scrollbars as well.
13845 *
13846 * @return the right padding in pixels
13847 */
13848 public int getPaddingRight() {
13849 return mPaddingRight;
13850 }
13851
13852 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013853 * Returns the end padding of this view depending on its resolved layout direction.
13854 * If there are inset and enabled scrollbars, this value may include the space
13855 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013856 *
13857 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013858 */
13859 public int getPaddingEnd() {
13860 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13861 mPaddingLeft : mPaddingRight;
13862 }
13863
13864 /**
13865 * Return if the padding as been set thru relative values
13866 * {@link #setPaddingRelative(int, int, int, int)} or thru
13867 * @attr ref android.R.styleable#View_paddingStart or
13868 * @attr ref android.R.styleable#View_paddingEnd
13869 *
13870 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013871 */
13872 public boolean isPaddingRelative() {
13873 return mUserPaddingRelative;
13874 }
13875
13876 /**
Philip Milne1557fd72012-04-04 23:41:34 -070013877 * @hide
13878 */
13879 public Insets getLayoutInsets() {
13880 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070013881 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070013882 }
13883 return mLayoutInsets;
13884 }
13885
13886 /**
13887 * @hide
13888 */
13889 public void setLayoutInsets(Insets layoutInsets) {
13890 mLayoutInsets = layoutInsets;
13891 }
13892
13893 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013894 * Changes the selection state of this view. A view can be selected or not.
13895 * Note that selection is not the same as focus. Views are typically
13896 * selected in the context of an AdapterView like ListView or GridView;
13897 * the selected view is the view that is highlighted.
13898 *
13899 * @param selected true if the view must be selected, false otherwise
13900 */
13901 public void setSelected(boolean selected) {
13902 if (((mPrivateFlags & SELECTED) != 0) != selected) {
13903 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070013904 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080013905 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013906 refreshDrawableState();
13907 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070013908 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
13909 notifyAccessibilityStateChanged();
13910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013911 }
13912 }
13913
13914 /**
13915 * Dispatch setSelected to all of this View's children.
13916 *
13917 * @see #setSelected(boolean)
13918 *
13919 * @param selected The new selected state
13920 */
13921 protected void dispatchSetSelected(boolean selected) {
13922 }
13923
13924 /**
13925 * Indicates the selection state of this view.
13926 *
13927 * @return true if the view is selected, false otherwise
13928 */
13929 @ViewDebug.ExportedProperty
13930 public boolean isSelected() {
13931 return (mPrivateFlags & SELECTED) != 0;
13932 }
13933
13934 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013935 * Changes the activated state of this view. A view can be activated or not.
13936 * Note that activation is not the same as selection. Selection is
13937 * a transient property, representing the view (hierarchy) the user is
13938 * currently interacting with. Activation is a longer-term state that the
13939 * user can move views in and out of. For example, in a list view with
13940 * single or multiple selection enabled, the views in the current selection
13941 * set are activated. (Um, yeah, we are deeply sorry about the terminology
13942 * here.) The activated state is propagated down to children of the view it
13943 * is set on.
13944 *
13945 * @param activated true if the view must be activated, false otherwise
13946 */
13947 public void setActivated(boolean activated) {
13948 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
13949 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013950 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013951 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070013952 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013953 }
13954 }
13955
13956 /**
13957 * Dispatch setActivated to all of this View's children.
13958 *
13959 * @see #setActivated(boolean)
13960 *
13961 * @param activated The new activated state
13962 */
13963 protected void dispatchSetActivated(boolean activated) {
13964 }
13965
13966 /**
13967 * Indicates the activation state of this view.
13968 *
13969 * @return true if the view is activated, false otherwise
13970 */
13971 @ViewDebug.ExportedProperty
13972 public boolean isActivated() {
13973 return (mPrivateFlags & ACTIVATED) != 0;
13974 }
13975
13976 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013977 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
13978 * observer can be used to get notifications when global events, like
13979 * layout, happen.
13980 *
13981 * The returned ViewTreeObserver observer is not guaranteed to remain
13982 * valid for the lifetime of this View. If the caller of this method keeps
13983 * a long-lived reference to ViewTreeObserver, it should always check for
13984 * the return value of {@link ViewTreeObserver#isAlive()}.
13985 *
13986 * @return The ViewTreeObserver for this view's hierarchy.
13987 */
13988 public ViewTreeObserver getViewTreeObserver() {
13989 if (mAttachInfo != null) {
13990 return mAttachInfo.mTreeObserver;
13991 }
13992 if (mFloatingTreeObserver == null) {
13993 mFloatingTreeObserver = new ViewTreeObserver();
13994 }
13995 return mFloatingTreeObserver;
13996 }
13997
13998 /**
13999 * <p>Finds the topmost view in the current view hierarchy.</p>
14000 *
14001 * @return the topmost view containing this view
14002 */
14003 public View getRootView() {
14004 if (mAttachInfo != null) {
14005 final View v = mAttachInfo.mRootView;
14006 if (v != null) {
14007 return v;
14008 }
14009 }
Romain Guy8506ab42009-06-11 17:35:47 -070014010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014011 View parent = this;
14012
14013 while (parent.mParent != null && parent.mParent instanceof View) {
14014 parent = (View) parent.mParent;
14015 }
14016
14017 return parent;
14018 }
14019
14020 /**
14021 * <p>Computes the coordinates of this view on the screen. 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 getLocationOnScreen(int[] location) {
14028 getLocationInWindow(location);
14029
14030 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014031 if (info != null) {
14032 location[0] += info.mWindowLeft;
14033 location[1] += info.mWindowTop;
14034 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014035 }
14036
14037 /**
14038 * <p>Computes the coordinates of this view in its window. The argument
14039 * must be an array of two integers. After the method returns, the array
14040 * contains the x and y location in that order.</p>
14041 *
14042 * @param location an array of two integers in which to hold the coordinates
14043 */
14044 public void getLocationInWindow(int[] location) {
14045 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014046 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014047 }
14048
Gilles Debunne6583ce52011-12-06 18:09:02 -080014049 if (mAttachInfo == null) {
14050 // When the view is not attached to a window, this method does not make sense
14051 location[0] = location[1] = 0;
14052 return;
14053 }
14054
Gilles Debunnecea45132011-11-24 02:19:27 +010014055 float[] position = mAttachInfo.mTmpTransformLocation;
14056 position[0] = position[1] = 0.0f;
14057
14058 if (!hasIdentityMatrix()) {
14059 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014061
Gilles Debunnecea45132011-11-24 02:19:27 +010014062 position[0] += mLeft;
14063 position[1] += mTop;
14064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014065 ViewParent viewParent = mParent;
14066 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014067 final View view = (View) viewParent;
14068
14069 position[0] -= view.mScrollX;
14070 position[1] -= view.mScrollY;
14071
14072 if (!view.hasIdentityMatrix()) {
14073 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014074 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014075
14076 position[0] += view.mLeft;
14077 position[1] += view.mTop;
14078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014079 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014080 }
Romain Guy8506ab42009-06-11 17:35:47 -070014081
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014082 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014083 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014084 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14085 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014086 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014087
14088 location[0] = (int) (position[0] + 0.5f);
14089 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014090 }
14091
14092 /**
14093 * {@hide}
14094 * @param id the id of the view to be found
14095 * @return the view of the specified id, null if cannot be found
14096 */
14097 protected View findViewTraversal(int id) {
14098 if (id == mID) {
14099 return this;
14100 }
14101 return null;
14102 }
14103
14104 /**
14105 * {@hide}
14106 * @param tag the tag of the view to be found
14107 * @return the view of specified tag, null if cannot be found
14108 */
14109 protected View findViewWithTagTraversal(Object tag) {
14110 if (tag != null && tag.equals(mTag)) {
14111 return this;
14112 }
14113 return null;
14114 }
14115
14116 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014117 * {@hide}
14118 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014119 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014120 * @return The first view that matches the predicate or null.
14121 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014122 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014123 if (predicate.apply(this)) {
14124 return this;
14125 }
14126 return null;
14127 }
14128
14129 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014130 * Look for a child view with the given id. If this view has the given
14131 * id, return this view.
14132 *
14133 * @param id The id to search for.
14134 * @return The view that has the given id in the hierarchy or null
14135 */
14136 public final View findViewById(int id) {
14137 if (id < 0) {
14138 return null;
14139 }
14140 return findViewTraversal(id);
14141 }
14142
14143 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014144 * Finds a view by its unuque and stable accessibility id.
14145 *
14146 * @param accessibilityId The searched accessibility id.
14147 * @return The found view.
14148 */
14149 final View findViewByAccessibilityId(int accessibilityId) {
14150 if (accessibilityId < 0) {
14151 return null;
14152 }
14153 return findViewByAccessibilityIdTraversal(accessibilityId);
14154 }
14155
14156 /**
14157 * Performs the traversal to find a view by its unuque and stable accessibility id.
14158 *
14159 * <strong>Note:</strong>This method does not stop at the root namespace
14160 * boundary since the user can touch the screen at an arbitrary location
14161 * potentially crossing the root namespace bounday which will send an
14162 * accessibility event to accessibility services and they should be able
14163 * to obtain the event source. Also accessibility ids are guaranteed to be
14164 * unique in the window.
14165 *
14166 * @param accessibilityId The accessibility id.
14167 * @return The found view.
14168 */
14169 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14170 if (getAccessibilityViewId() == accessibilityId) {
14171 return this;
14172 }
14173 return null;
14174 }
14175
14176 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014177 * Look for a child view with the given tag. If this view has the given
14178 * tag, return this view.
14179 *
14180 * @param tag The tag to search for, using "tag.equals(getTag())".
14181 * @return The View that has the given tag in the hierarchy or null
14182 */
14183 public final View findViewWithTag(Object tag) {
14184 if (tag == null) {
14185 return null;
14186 }
14187 return findViewWithTagTraversal(tag);
14188 }
14189
14190 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014191 * {@hide}
14192 * Look for a child view that matches the specified predicate.
14193 * If this view matches the predicate, return this view.
14194 *
14195 * @param predicate The predicate to evaluate.
14196 * @return The first view that matches the predicate or null.
14197 */
14198 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014199 return findViewByPredicateTraversal(predicate, null);
14200 }
14201
14202 /**
14203 * {@hide}
14204 * Look for a child view that matches the specified predicate,
14205 * starting with the specified view and its descendents and then
14206 * recusively searching the ancestors and siblings of that view
14207 * until this view is reached.
14208 *
14209 * This method is useful in cases where the predicate does not match
14210 * a single unique view (perhaps multiple views use the same id)
14211 * and we are trying to find the view that is "closest" in scope to the
14212 * starting view.
14213 *
14214 * @param start The view to start from.
14215 * @param predicate The predicate to evaluate.
14216 * @return The first view that matches the predicate or null.
14217 */
14218 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14219 View childToSkip = null;
14220 for (;;) {
14221 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14222 if (view != null || start == this) {
14223 return view;
14224 }
14225
14226 ViewParent parent = start.getParent();
14227 if (parent == null || !(parent instanceof View)) {
14228 return null;
14229 }
14230
14231 childToSkip = start;
14232 start = (View) parent;
14233 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014234 }
14235
14236 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014237 * Sets the identifier for this view. The identifier does not have to be
14238 * unique in this view's hierarchy. The identifier should be a positive
14239 * number.
14240 *
14241 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014242 * @see #getId()
14243 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014244 *
14245 * @param id a number used to identify the view
14246 *
14247 * @attr ref android.R.styleable#View_id
14248 */
14249 public void setId(int id) {
14250 mID = id;
14251 }
14252
14253 /**
14254 * {@hide}
14255 *
14256 * @param isRoot true if the view belongs to the root namespace, false
14257 * otherwise
14258 */
14259 public void setIsRootNamespace(boolean isRoot) {
14260 if (isRoot) {
14261 mPrivateFlags |= IS_ROOT_NAMESPACE;
14262 } else {
14263 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14264 }
14265 }
14266
14267 /**
14268 * {@hide}
14269 *
14270 * @return true if the view belongs to the root namespace, false otherwise
14271 */
14272 public boolean isRootNamespace() {
14273 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14274 }
14275
14276 /**
14277 * Returns this view's identifier.
14278 *
14279 * @return a positive integer used to identify the view or {@link #NO_ID}
14280 * if the view has no ID
14281 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014282 * @see #setId(int)
14283 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014284 * @attr ref android.R.styleable#View_id
14285 */
14286 @ViewDebug.CapturedViewProperty
14287 public int getId() {
14288 return mID;
14289 }
14290
14291 /**
14292 * Returns this view's tag.
14293 *
14294 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014295 *
14296 * @see #setTag(Object)
14297 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014298 */
14299 @ViewDebug.ExportedProperty
14300 public Object getTag() {
14301 return mTag;
14302 }
14303
14304 /**
14305 * Sets the tag associated with this view. A tag can be used to mark
14306 * a view in its hierarchy and does not have to be unique within the
14307 * hierarchy. Tags can also be used to store data within a view without
14308 * resorting to another data structure.
14309 *
14310 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014311 *
14312 * @see #getTag()
14313 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014314 */
14315 public void setTag(final Object tag) {
14316 mTag = tag;
14317 }
14318
14319 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014320 * Returns the tag associated with this view and the specified key.
14321 *
14322 * @param key The key identifying the tag
14323 *
14324 * @return the Object stored in this view as a tag
14325 *
14326 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014327 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014328 */
14329 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014330 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014331 return null;
14332 }
14333
14334 /**
14335 * Sets a tag associated with this view and a key. A tag can be used
14336 * to mark a view in its hierarchy and does not have to be unique within
14337 * the hierarchy. Tags can also be used to store data within a view
14338 * without resorting to another data structure.
14339 *
14340 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014341 * application to ensure it is unique (see the <a
14342 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14343 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014344 * the Android framework or not associated with any package will cause
14345 * an {@link IllegalArgumentException} to be thrown.
14346 *
14347 * @param key The key identifying the tag
14348 * @param tag An Object to tag the view with
14349 *
14350 * @throws IllegalArgumentException If they specified key is not valid
14351 *
14352 * @see #setTag(Object)
14353 * @see #getTag(int)
14354 */
14355 public void setTag(int key, final Object tag) {
14356 // If the package id is 0x00 or 0x01, it's either an undefined package
14357 // or a framework id
14358 if ((key >>> 24) < 2) {
14359 throw new IllegalArgumentException("The key must be an application-specific "
14360 + "resource id.");
14361 }
14362
Adam Powell2b2f6d62011-09-23 11:15:39 -070014363 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014364 }
14365
14366 /**
14367 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14368 * framework id.
14369 *
14370 * @hide
14371 */
14372 public void setTagInternal(int key, Object tag) {
14373 if ((key >>> 24) != 0x1) {
14374 throw new IllegalArgumentException("The key must be a framework-specific "
14375 + "resource id.");
14376 }
14377
Adam Powell2b2f6d62011-09-23 11:15:39 -070014378 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014379 }
14380
Adam Powell2b2f6d62011-09-23 11:15:39 -070014381 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014382 if (mKeyedTags == null) {
14383 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014384 }
14385
Adam Powell7db82ac2011-09-22 19:44:04 -070014386 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014387 }
14388
14389 /**
Romain Guy13922e02009-05-12 17:56:14 -070014390 * @param consistency The type of consistency. See ViewDebug for more information.
14391 *
14392 * @hide
14393 */
14394 protected boolean dispatchConsistencyCheck(int consistency) {
14395 return onConsistencyCheck(consistency);
14396 }
14397
14398 /**
14399 * Method that subclasses should implement to check their consistency. The type of
14400 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070014401 *
Romain Guy13922e02009-05-12 17:56:14 -070014402 * @param consistency The type of consistency. See ViewDebug for more information.
14403 *
14404 * @throws IllegalStateException if the view is in an inconsistent state.
14405 *
14406 * @hide
14407 */
14408 protected boolean onConsistencyCheck(int consistency) {
14409 boolean result = true;
14410
14411 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
14412 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
14413
14414 if (checkLayout) {
14415 if (getParent() == null) {
14416 result = false;
14417 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14418 "View " + this + " does not have a parent.");
14419 }
14420
14421 if (mAttachInfo == null) {
14422 result = false;
14423 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14424 "View " + this + " is not attached to a window.");
14425 }
14426 }
14427
14428 if (checkDrawing) {
14429 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
14430 // from their draw() method
14431
14432 if ((mPrivateFlags & DRAWN) != DRAWN &&
14433 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
14434 result = false;
14435 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14436 "View " + this + " was invalidated but its drawing cache is valid.");
14437 }
14438 }
14439
14440 return result;
14441 }
14442
14443 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014444 * Prints information about this view in the log output, with the tag
14445 * {@link #VIEW_LOG_TAG}.
14446 *
14447 * @hide
14448 */
14449 public void debug() {
14450 debug(0);
14451 }
14452
14453 /**
14454 * Prints information about this view in the log output, with the tag
14455 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14456 * indentation defined by the <code>depth</code>.
14457 *
14458 * @param depth the indentation level
14459 *
14460 * @hide
14461 */
14462 protected void debug(int depth) {
14463 String output = debugIndent(depth - 1);
14464
14465 output += "+ " + this;
14466 int id = getId();
14467 if (id != -1) {
14468 output += " (id=" + id + ")";
14469 }
14470 Object tag = getTag();
14471 if (tag != null) {
14472 output += " (tag=" + tag + ")";
14473 }
14474 Log.d(VIEW_LOG_TAG, output);
14475
14476 if ((mPrivateFlags & FOCUSED) != 0) {
14477 output = debugIndent(depth) + " FOCUSED";
14478 Log.d(VIEW_LOG_TAG, output);
14479 }
14480
14481 output = debugIndent(depth);
14482 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14483 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14484 + "} ";
14485 Log.d(VIEW_LOG_TAG, output);
14486
14487 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14488 || mPaddingBottom != 0) {
14489 output = debugIndent(depth);
14490 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14491 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14492 Log.d(VIEW_LOG_TAG, output);
14493 }
14494
14495 output = debugIndent(depth);
14496 output += "mMeasureWidth=" + mMeasuredWidth +
14497 " mMeasureHeight=" + mMeasuredHeight;
14498 Log.d(VIEW_LOG_TAG, output);
14499
14500 output = debugIndent(depth);
14501 if (mLayoutParams == null) {
14502 output += "BAD! no layout params";
14503 } else {
14504 output = mLayoutParams.debug(output);
14505 }
14506 Log.d(VIEW_LOG_TAG, output);
14507
14508 output = debugIndent(depth);
14509 output += "flags={";
14510 output += View.printFlags(mViewFlags);
14511 output += "}";
14512 Log.d(VIEW_LOG_TAG, output);
14513
14514 output = debugIndent(depth);
14515 output += "privateFlags={";
14516 output += View.printPrivateFlags(mPrivateFlags);
14517 output += "}";
14518 Log.d(VIEW_LOG_TAG, output);
14519 }
14520
14521 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014522 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014523 *
14524 * @param depth the indentation level
14525 * @return a String containing (depth * 2 + 3) * 2 white spaces
14526 *
14527 * @hide
14528 */
14529 protected static String debugIndent(int depth) {
14530 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
14531 for (int i = 0; i < (depth * 2) + 3; i++) {
14532 spaces.append(' ').append(' ');
14533 }
14534 return spaces.toString();
14535 }
14536
14537 /**
14538 * <p>Return the offset of the widget's text baseline from the widget's top
14539 * boundary. If this widget does not support baseline alignment, this
14540 * method returns -1. </p>
14541 *
14542 * @return the offset of the baseline within the widget's bounds or -1
14543 * if baseline alignment is not supported
14544 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070014545 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014546 public int getBaseline() {
14547 return -1;
14548 }
14549
14550 /**
14551 * Call this when something has changed which has invalidated the
14552 * layout of this view. This will schedule a layout pass of the view
14553 * tree.
14554 */
14555 public void requestLayout() {
14556 if (ViewDebug.TRACE_HIERARCHY) {
14557 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
14558 }
14559
14560 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014561 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014562
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070014563 if (mLayoutParams != null) {
14564 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
14565 }
14566
14567 if (mParent != null && !mParent.isLayoutRequested()) {
14568 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014569 }
14570 }
14571
14572 /**
14573 * Forces this view to be laid out during the next layout pass.
14574 * This method does not call requestLayout() or forceLayout()
14575 * on the parent.
14576 */
14577 public void forceLayout() {
14578 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014579 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014580 }
14581
14582 /**
14583 * <p>
14584 * This is called to find out how big a view should be. The parent
14585 * supplies constraint information in the width and height parameters.
14586 * </p>
14587 *
14588 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080014589 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014590 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014591 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014592 * </p>
14593 *
14594 *
14595 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14596 * parent
14597 * @param heightMeasureSpec Vertical space requirements as imposed by the
14598 * parent
14599 *
14600 * @see #onMeasure(int, int)
14601 */
14602 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14603 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14604 widthMeasureSpec != mOldWidthMeasureSpec ||
14605 heightMeasureSpec != mOldHeightMeasureSpec) {
14606
14607 // first clears the measured dimension flag
14608 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14609
14610 if (ViewDebug.TRACE_HIERARCHY) {
14611 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
14612 }
14613
14614 // measure ourselves, this should set the measured dimension flag back
14615 onMeasure(widthMeasureSpec, heightMeasureSpec);
14616
14617 // flag not set, setMeasuredDimension() was not invoked, we raise
14618 // an exception to warn the developer
14619 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14620 throw new IllegalStateException("onMeasure() did not set the"
14621 + " measured dimension by calling"
14622 + " setMeasuredDimension()");
14623 }
14624
14625 mPrivateFlags |= LAYOUT_REQUIRED;
14626 }
14627
14628 mOldWidthMeasureSpec = widthMeasureSpec;
14629 mOldHeightMeasureSpec = heightMeasureSpec;
14630 }
14631
14632 /**
14633 * <p>
14634 * Measure the view and its content to determine the measured width and the
14635 * measured height. This method is invoked by {@link #measure(int, int)} and
14636 * should be overriden by subclasses to provide accurate and efficient
14637 * measurement of their contents.
14638 * </p>
14639 *
14640 * <p>
14641 * <strong>CONTRACT:</strong> When overriding this method, you
14642 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14643 * measured width and height of this view. Failure to do so will trigger an
14644 * <code>IllegalStateException</code>, thrown by
14645 * {@link #measure(int, int)}. Calling the superclass'
14646 * {@link #onMeasure(int, int)} is a valid use.
14647 * </p>
14648 *
14649 * <p>
14650 * The base class implementation of measure defaults to the background size,
14651 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14652 * override {@link #onMeasure(int, int)} to provide better measurements of
14653 * their content.
14654 * </p>
14655 *
14656 * <p>
14657 * If this method is overridden, it is the subclass's responsibility to make
14658 * sure the measured height and width are at least the view's minimum height
14659 * and width ({@link #getSuggestedMinimumHeight()} and
14660 * {@link #getSuggestedMinimumWidth()}).
14661 * </p>
14662 *
14663 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
14664 * The requirements are encoded with
14665 * {@link android.view.View.MeasureSpec}.
14666 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
14667 * The requirements are encoded with
14668 * {@link android.view.View.MeasureSpec}.
14669 *
14670 * @see #getMeasuredWidth()
14671 * @see #getMeasuredHeight()
14672 * @see #setMeasuredDimension(int, int)
14673 * @see #getSuggestedMinimumHeight()
14674 * @see #getSuggestedMinimumWidth()
14675 * @see android.view.View.MeasureSpec#getMode(int)
14676 * @see android.view.View.MeasureSpec#getSize(int)
14677 */
14678 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
14679 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
14680 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
14681 }
14682
14683 /**
14684 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
14685 * measured width and measured height. Failing to do so will trigger an
14686 * exception at measurement time.</p>
14687 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080014688 * @param measuredWidth The measured width of this view. May be a complex
14689 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14690 * {@link #MEASURED_STATE_TOO_SMALL}.
14691 * @param measuredHeight The measured height of this view. May be a complex
14692 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14693 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014694 */
14695 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
14696 mMeasuredWidth = measuredWidth;
14697 mMeasuredHeight = measuredHeight;
14698
14699 mPrivateFlags |= MEASURED_DIMENSION_SET;
14700 }
14701
14702 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080014703 * Merge two states as returned by {@link #getMeasuredState()}.
14704 * @param curState The current state as returned from a view or the result
14705 * of combining multiple views.
14706 * @param newState The new view state to combine.
14707 * @return Returns a new integer reflecting the combination of the two
14708 * states.
14709 */
14710 public static int combineMeasuredStates(int curState, int newState) {
14711 return curState | newState;
14712 }
14713
14714 /**
14715 * Version of {@link #resolveSizeAndState(int, int, int)}
14716 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
14717 */
14718 public static int resolveSize(int size, int measureSpec) {
14719 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
14720 }
14721
14722 /**
14723 * Utility to reconcile a desired size and state, with constraints imposed
14724 * by a MeasureSpec. Will take the desired size, unless a different size
14725 * is imposed by the constraints. The returned value is a compound integer,
14726 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
14727 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
14728 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014729 *
14730 * @param size How big the view wants to be
14731 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080014732 * @return Size information bit mask as defined by
14733 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014734 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080014735 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014736 int result = size;
14737 int specMode = MeasureSpec.getMode(measureSpec);
14738 int specSize = MeasureSpec.getSize(measureSpec);
14739 switch (specMode) {
14740 case MeasureSpec.UNSPECIFIED:
14741 result = size;
14742 break;
14743 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080014744 if (specSize < size) {
14745 result = specSize | MEASURED_STATE_TOO_SMALL;
14746 } else {
14747 result = size;
14748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014749 break;
14750 case MeasureSpec.EXACTLY:
14751 result = specSize;
14752 break;
14753 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080014754 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014755 }
14756
14757 /**
14758 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070014759 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014760 * by the MeasureSpec.
14761 *
14762 * @param size Default size for this view
14763 * @param measureSpec Constraints imposed by the parent
14764 * @return The size this view should be.
14765 */
14766 public static int getDefaultSize(int size, int measureSpec) {
14767 int result = size;
14768 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070014769 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014770
14771 switch (specMode) {
14772 case MeasureSpec.UNSPECIFIED:
14773 result = size;
14774 break;
14775 case MeasureSpec.AT_MOST:
14776 case MeasureSpec.EXACTLY:
14777 result = specSize;
14778 break;
14779 }
14780 return result;
14781 }
14782
14783 /**
14784 * Returns the suggested minimum height that the view should use. This
14785 * returns the maximum of the view's minimum height
14786 * and the background's minimum height
14787 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
14788 * <p>
14789 * When being used in {@link #onMeasure(int, int)}, the caller should still
14790 * ensure the returned height is within the requirements of the parent.
14791 *
14792 * @return The suggested minimum height of the view.
14793 */
14794 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014795 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014797 }
14798
14799 /**
14800 * Returns the suggested minimum width that the view should use. This
14801 * returns the maximum of the view's minimum width)
14802 * and the background's minimum width
14803 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
14804 * <p>
14805 * When being used in {@link #onMeasure(int, int)}, the caller should still
14806 * ensure the returned width is within the requirements of the parent.
14807 *
14808 * @return The suggested minimum width of the view.
14809 */
14810 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014811 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
14812 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014813
Philip Milne6c8ea062012-04-03 17:38:43 -070014814 /**
14815 * Returns the minimum height of the view.
14816 *
14817 * @return the minimum height the view will try to be.
14818 *
14819 * @see #setMinimumHeight(int)
14820 *
14821 * @attr ref android.R.styleable#View_minHeight
14822 */
14823 public int getMinimumHeight() {
14824 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014825 }
14826
14827 /**
14828 * Sets the minimum height of the view. It is not guaranteed the view will
14829 * be able to achieve this minimum height (for example, if its parent layout
14830 * constrains it with less available height).
14831 *
14832 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014833 *
14834 * @see #getMinimumHeight()
14835 *
14836 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014837 */
14838 public void setMinimumHeight(int minHeight) {
14839 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070014840 requestLayout();
14841 }
14842
14843 /**
14844 * Returns the minimum width of the view.
14845 *
14846 * @return the minimum width the view will try to be.
14847 *
14848 * @see #setMinimumWidth(int)
14849 *
14850 * @attr ref android.R.styleable#View_minWidth
14851 */
14852 public int getMinimumWidth() {
14853 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014854 }
14855
14856 /**
14857 * Sets the minimum width of the view. It is not guaranteed the view will
14858 * be able to achieve this minimum width (for example, if its parent layout
14859 * constrains it with less available width).
14860 *
14861 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014862 *
14863 * @see #getMinimumWidth()
14864 *
14865 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014866 */
14867 public void setMinimumWidth(int minWidth) {
14868 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070014869 requestLayout();
14870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014871 }
14872
14873 /**
14874 * Get the animation currently associated with this view.
14875 *
14876 * @return The animation that is currently playing or
14877 * scheduled to play for this view.
14878 */
14879 public Animation getAnimation() {
14880 return mCurrentAnimation;
14881 }
14882
14883 /**
14884 * Start the specified animation now.
14885 *
14886 * @param animation the animation to start now
14887 */
14888 public void startAnimation(Animation animation) {
14889 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
14890 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014891 invalidateParentCaches();
14892 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014893 }
14894
14895 /**
14896 * Cancels any animations for this view.
14897 */
14898 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080014899 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080014900 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080014901 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014902 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014903 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014904 }
14905
14906 /**
14907 * Sets the next animation to play for this view.
14908 * If you want the animation to play immediately, use
14909 * startAnimation. This method provides allows fine-grained
14910 * control over the start time and invalidation, but you
14911 * must make sure that 1) the animation has a start time set, and
14912 * 2) the view will be invalidated when the animation is supposed to
14913 * start.
14914 *
14915 * @param animation The next animation, or null.
14916 */
14917 public void setAnimation(Animation animation) {
14918 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070014919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014920 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070014921 // If the screen is off assume the animation start time is now instead of
14922 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
14923 // would cause the animation to start when the screen turns back on
14924 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
14925 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
14926 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
14927 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014928 animation.reset();
14929 }
14930 }
14931
14932 /**
14933 * Invoked by a parent ViewGroup to notify the start of the animation
14934 * currently associated with this view. If you override this method,
14935 * always call super.onAnimationStart();
14936 *
14937 * @see #setAnimation(android.view.animation.Animation)
14938 * @see #getAnimation()
14939 */
14940 protected void onAnimationStart() {
14941 mPrivateFlags |= ANIMATION_STARTED;
14942 }
14943
14944 /**
14945 * Invoked by a parent ViewGroup to notify the end of the animation
14946 * currently associated with this view. If you override this method,
14947 * always call super.onAnimationEnd();
14948 *
14949 * @see #setAnimation(android.view.animation.Animation)
14950 * @see #getAnimation()
14951 */
14952 protected void onAnimationEnd() {
14953 mPrivateFlags &= ~ANIMATION_STARTED;
14954 }
14955
14956 /**
14957 * Invoked if there is a Transform that involves alpha. Subclass that can
14958 * draw themselves with the specified alpha should return true, and then
14959 * respect that alpha when their onDraw() is called. If this returns false
14960 * then the view may be redirected to draw into an offscreen buffer to
14961 * fulfill the request, which will look fine, but may be slower than if the
14962 * subclass handles it internally. The default implementation returns false.
14963 *
14964 * @param alpha The alpha (0..255) to apply to the view's drawing
14965 * @return true if the view can draw with the specified alpha.
14966 */
14967 protected boolean onSetAlpha(int alpha) {
14968 return false;
14969 }
14970
14971 /**
14972 * This is used by the RootView to perform an optimization when
14973 * the view hierarchy contains one or several SurfaceView.
14974 * SurfaceView is always considered transparent, but its children are not,
14975 * therefore all View objects remove themselves from the global transparent
14976 * region (passed as a parameter to this function).
14977 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014978 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014979 *
14980 * @return Returns true if the effective visibility of the view at this
14981 * point is opaque, regardless of the transparent region; returns false
14982 * if it is possible for underlying windows to be seen behind the view.
14983 *
14984 * {@hide}
14985 */
14986 public boolean gatherTransparentRegion(Region region) {
14987 final AttachInfo attachInfo = mAttachInfo;
14988 if (region != null && attachInfo != null) {
14989 final int pflags = mPrivateFlags;
14990 if ((pflags & SKIP_DRAW) == 0) {
14991 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
14992 // remove it from the transparent region.
14993 final int[] location = attachInfo.mTransparentLocation;
14994 getLocationInWindow(location);
14995 region.op(location[0], location[1], location[0] + mRight - mLeft,
14996 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070014997 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014998 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
14999 // exists, so we remove the background drawable's non-transparent
15000 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015001 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015002 }
15003 }
15004 return true;
15005 }
15006
15007 /**
15008 * Play a sound effect for this view.
15009 *
15010 * <p>The framework will play sound effects for some built in actions, such as
15011 * clicking, but you may wish to play these effects in your widget,
15012 * for instance, for internal navigation.
15013 *
15014 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15015 * {@link #isSoundEffectsEnabled()} is true.
15016 *
15017 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15018 */
15019 public void playSoundEffect(int soundConstant) {
15020 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15021 return;
15022 }
15023 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15024 }
15025
15026 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015027 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015028 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015029 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015030 *
15031 * <p>The framework will provide haptic feedback for some built in actions,
15032 * such as long presses, but you may wish to provide feedback for your
15033 * own widget.
15034 *
15035 * <p>The feedback will only be performed if
15036 * {@link #isHapticFeedbackEnabled()} is true.
15037 *
15038 * @param feedbackConstant One of the constants defined in
15039 * {@link HapticFeedbackConstants}
15040 */
15041 public boolean performHapticFeedback(int feedbackConstant) {
15042 return performHapticFeedback(feedbackConstant, 0);
15043 }
15044
15045 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015046 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015047 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015048 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015049 *
15050 * @param feedbackConstant One of the constants defined in
15051 * {@link HapticFeedbackConstants}
15052 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15053 */
15054 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15055 if (mAttachInfo == null) {
15056 return false;
15057 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015058 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015059 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015060 && !isHapticFeedbackEnabled()) {
15061 return false;
15062 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015063 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15064 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015065 }
15066
15067 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015068 * Request that the visibility of the status bar or other screen/window
15069 * decorations be changed.
15070 *
15071 * <p>This method is used to put the over device UI into temporary modes
15072 * where the user's attention is focused more on the application content,
15073 * by dimming or hiding surrounding system affordances. This is typically
15074 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15075 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15076 * to be placed behind the action bar (and with these flags other system
15077 * affordances) so that smooth transitions between hiding and showing them
15078 * can be done.
15079 *
15080 * <p>Two representative examples of the use of system UI visibility is
15081 * implementing a content browsing application (like a magazine reader)
15082 * and a video playing application.
15083 *
15084 * <p>The first code shows a typical implementation of a View in a content
15085 * browsing application. In this implementation, the application goes
15086 * into a content-oriented mode by hiding the status bar and action bar,
15087 * and putting the navigation elements into lights out mode. The user can
15088 * then interact with content while in this mode. Such an application should
15089 * provide an easy way for the user to toggle out of the mode (such as to
15090 * check information in the status bar or access notifications). In the
15091 * implementation here, this is done simply by tapping on the content.
15092 *
15093 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15094 * content}
15095 *
15096 * <p>This second code sample shows a typical implementation of a View
15097 * in a video playing application. In this situation, while the video is
15098 * playing the application would like to go into a complete full-screen mode,
15099 * to use as much of the display as possible for the video. When in this state
15100 * the user can not interact with the application; the system intercepts
15101 * touching on the screen to pop the UI out of full screen mode.
15102 *
15103 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15104 * content}
15105 *
15106 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15107 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15108 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15109 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015110 */
15111 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015112 if (visibility != mSystemUiVisibility) {
15113 mSystemUiVisibility = visibility;
15114 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15115 mParent.recomputeViewAttributes(this);
15116 }
Joe Onorato664644d2011-01-23 17:53:23 -080015117 }
15118 }
15119
15120 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015121 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15122 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15123 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15124 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15125 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015126 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015127 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015128 return mSystemUiVisibility;
15129 }
15130
Scott Mainec6331b2011-05-24 16:55:56 -070015131 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015132 * Returns the current system UI visibility that is currently set for
15133 * the entire window. This is the combination of the
15134 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15135 * views in the window.
15136 */
15137 public int getWindowSystemUiVisibility() {
15138 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15139 }
15140
15141 /**
15142 * Override to find out when the window's requested system UI visibility
15143 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15144 * This is different from the callbacks recieved through
15145 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15146 * in that this is only telling you about the local request of the window,
15147 * not the actual values applied by the system.
15148 */
15149 public void onWindowSystemUiVisibilityChanged(int visible) {
15150 }
15151
15152 /**
15153 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15154 * the view hierarchy.
15155 */
15156 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15157 onWindowSystemUiVisibilityChanged(visible);
15158 }
15159
15160 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015161 * Set a listener to receive callbacks when the visibility of the system bar changes.
15162 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15163 */
Joe Onorato664644d2011-01-23 17:53:23 -080015164 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015165 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015166 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15167 mParent.recomputeViewAttributes(this);
15168 }
15169 }
15170
15171 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015172 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15173 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015174 */
15175 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015176 ListenerInfo li = mListenerInfo;
15177 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15178 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015179 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015180 }
15181 }
15182
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015183 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
15184 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15185 if (val != mSystemUiVisibility) {
15186 setSystemUiVisibility(val);
15187 }
15188 }
15189
Joe Onorato664644d2011-01-23 17:53:23 -080015190 /**
Joe Malin32736f02011-01-19 16:14:20 -080015191 * Creates an image that the system displays during the drag and drop
15192 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15193 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15194 * appearance as the given View. The default also positions the center of the drag shadow
15195 * directly under the touch point. If no View is provided (the constructor with no parameters
15196 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15197 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15198 * default is an invisible drag shadow.
15199 * <p>
15200 * You are not required to use the View you provide to the constructor as the basis of the
15201 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15202 * anything you want as the drag shadow.
15203 * </p>
15204 * <p>
15205 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15206 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15207 * size and position of the drag shadow. It uses this data to construct a
15208 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15209 * so that your application can draw the shadow image in the Canvas.
15210 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015211 *
15212 * <div class="special reference">
15213 * <h3>Developer Guides</h3>
15214 * <p>For a guide to implementing drag and drop features, read the
15215 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15216 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015217 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015218 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015219 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015220
15221 /**
Joe Malin32736f02011-01-19 16:14:20 -080015222 * Constructs a shadow image builder based on a View. By default, the resulting drag
15223 * shadow will have the same appearance and dimensions as the View, with the touch point
15224 * over the center of the View.
15225 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015226 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015227 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015228 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015229 }
15230
Christopher Tate17ed60c2011-01-18 12:50:26 -080015231 /**
15232 * Construct a shadow builder object with no associated View. This
15233 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15234 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15235 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015236 * reference to any View object. If they are not overridden, then the result is an
15237 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015238 */
15239 public DragShadowBuilder() {
15240 mView = new WeakReference<View>(null);
15241 }
15242
15243 /**
15244 * Returns the View object that had been passed to the
15245 * {@link #View.DragShadowBuilder(View)}
15246 * constructor. If that View parameter was {@code null} or if the
15247 * {@link #View.DragShadowBuilder()}
15248 * constructor was used to instantiate the builder object, this method will return
15249 * null.
15250 *
15251 * @return The View object associate with this builder object.
15252 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015253 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015254 final public View getView() {
15255 return mView.get();
15256 }
15257
Christopher Tate2c095f32010-10-04 14:13:40 -070015258 /**
Joe Malin32736f02011-01-19 16:14:20 -080015259 * Provides the metrics for the shadow image. These include the dimensions of
15260 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015261 * be centered under the touch location while dragging.
15262 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015263 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015264 * same as the dimensions of the View itself and centers the shadow under
15265 * the touch point.
15266 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015267 *
Joe Malin32736f02011-01-19 16:14:20 -080015268 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15269 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15270 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15271 * image.
15272 *
15273 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15274 * shadow image that should be underneath the touch point during the drag and drop
15275 * operation. Your application must set {@link android.graphics.Point#x} to the
15276 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015277 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015278 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015279 final View view = mView.get();
15280 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015281 shadowSize.set(view.getWidth(), view.getHeight());
15282 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015283 } else {
15284 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15285 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015286 }
15287
15288 /**
Joe Malin32736f02011-01-19 16:14:20 -080015289 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15290 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015291 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015292 *
Joe Malin32736f02011-01-19 16:14:20 -080015293 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015294 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015295 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015296 final View view = mView.get();
15297 if (view != null) {
15298 view.draw(canvas);
15299 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015300 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015301 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015302 }
15303 }
15304
15305 /**
Joe Malin32736f02011-01-19 16:14:20 -080015306 * Starts a drag and drop operation. When your application calls this method, it passes a
15307 * {@link android.view.View.DragShadowBuilder} object to the system. The
15308 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15309 * to get metrics for the drag shadow, and then calls the object's
15310 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15311 * <p>
15312 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15313 * drag events to all the View objects in your application that are currently visible. It does
15314 * this either by calling the View object's drag listener (an implementation of
15315 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15316 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15317 * Both are passed a {@link android.view.DragEvent} object that has a
15318 * {@link android.view.DragEvent#getAction()} value of
15319 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15320 * </p>
15321 * <p>
15322 * Your application can invoke startDrag() on any attached View object. The View object does not
15323 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15324 * be related to the View the user selected for dragging.
15325 * </p>
15326 * @param data A {@link android.content.ClipData} object pointing to the data to be
15327 * transferred by the drag and drop operation.
15328 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15329 * drag shadow.
15330 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15331 * drop operation. This Object is put into every DragEvent object sent by the system during the
15332 * current drag.
15333 * <p>
15334 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15335 * to the target Views. For example, it can contain flags that differentiate between a
15336 * a copy operation and a move operation.
15337 * </p>
15338 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15339 * so the parameter should be set to 0.
15340 * @return {@code true} if the method completes successfully, or
15341 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15342 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015343 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015344 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015345 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015346 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015347 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015348 }
15349 boolean okay = false;
15350
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015351 Point shadowSize = new Point();
15352 Point shadowTouchPoint = new Point();
15353 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015354
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015355 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15356 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15357 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015358 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015359
Chris Tatea32dcf72010-10-14 12:13:50 -070015360 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015361 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15362 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015363 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015364 Surface surface = new Surface();
15365 try {
15366 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015367 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015368 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015369 + " surface=" + surface);
15370 if (token != null) {
15371 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015372 try {
Chris Tate6b391282010-10-14 15:48:59 -070015373 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015374 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015375 } finally {
15376 surface.unlockCanvasAndPost(canvas);
15377 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015378
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015379 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015380
15381 // Cache the local state object for delivery with DragEvents
15382 root.setLocalDragState(myLocalState);
15383
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015384 // repurpose 'shadowSize' for the last touch point
15385 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015386
Christopher Tatea53146c2010-09-07 11:57:52 -070015387 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015388 shadowSize.x, shadowSize.y,
15389 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015390 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015391
15392 // Off and running! Release our local surface instance; the drag
15393 // shadow surface is now managed by the system process.
15394 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015395 }
15396 } catch (Exception e) {
15397 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15398 surface.destroy();
15399 }
15400
15401 return okay;
15402 }
15403
Christopher Tatea53146c2010-09-07 11:57:52 -070015404 /**
Joe Malin32736f02011-01-19 16:14:20 -080015405 * Handles drag events sent by the system following a call to
15406 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15407 *<p>
15408 * When the system calls this method, it passes a
15409 * {@link android.view.DragEvent} object. A call to
15410 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15411 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15412 * operation.
15413 * @param event The {@link android.view.DragEvent} sent by the system.
15414 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15415 * in DragEvent, indicating the type of drag event represented by this object.
15416 * @return {@code true} if the method was successful, otherwise {@code false}.
15417 * <p>
15418 * The method should return {@code true} in response to an action type of
15419 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15420 * operation.
15421 * </p>
15422 * <p>
15423 * The method should also return {@code true} in response to an action type of
15424 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15425 * {@code false} if it didn't.
15426 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015427 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015428 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015429 return false;
15430 }
15431
15432 /**
Joe Malin32736f02011-01-19 16:14:20 -080015433 * Detects if this View is enabled and has a drag event listener.
15434 * If both are true, then it calls the drag event listener with the
15435 * {@link android.view.DragEvent} it received. If the drag event listener returns
15436 * {@code true}, then dispatchDragEvent() returns {@code true}.
15437 * <p>
15438 * For all other cases, the method calls the
15439 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15440 * method and returns its result.
15441 * </p>
15442 * <p>
15443 * This ensures that a drag event is always consumed, even if the View does not have a drag
15444 * event listener. However, if the View has a listener and the listener returns true, then
15445 * onDragEvent() is not called.
15446 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015447 */
15448 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015449 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015450 ListenerInfo li = mListenerInfo;
15451 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15452 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015453 return true;
15454 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015455 return onDragEvent(event);
15456 }
15457
Christopher Tate3d4bf172011-03-28 16:16:46 -070015458 boolean canAcceptDrag() {
15459 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15460 }
15461
Christopher Tatea53146c2010-09-07 11:57:52 -070015462 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015463 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15464 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015465 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015466 */
15467 public void onCloseSystemDialogs(String reason) {
15468 }
Joe Malin32736f02011-01-19 16:14:20 -080015469
Dianne Hackbornffa42482009-09-23 22:20:11 -070015470 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015471 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015472 * update a Region being computed for
15473 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015474 * that any non-transparent parts of the Drawable are removed from the
15475 * given transparent region.
15476 *
15477 * @param dr The Drawable whose transparency is to be applied to the region.
15478 * @param region A Region holding the current transparency information,
15479 * where any parts of the region that are set are considered to be
15480 * transparent. On return, this region will be modified to have the
15481 * transparency information reduced by the corresponding parts of the
15482 * Drawable that are not transparent.
15483 * {@hide}
15484 */
15485 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15486 if (DBG) {
15487 Log.i("View", "Getting transparent region for: " + this);
15488 }
15489 final Region r = dr.getTransparentRegion();
15490 final Rect db = dr.getBounds();
15491 final AttachInfo attachInfo = mAttachInfo;
15492 if (r != null && attachInfo != null) {
15493 final int w = getRight()-getLeft();
15494 final int h = getBottom()-getTop();
15495 if (db.left > 0) {
15496 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15497 r.op(0, 0, db.left, h, Region.Op.UNION);
15498 }
15499 if (db.right < w) {
15500 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15501 r.op(db.right, 0, w, h, Region.Op.UNION);
15502 }
15503 if (db.top > 0) {
15504 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15505 r.op(0, 0, w, db.top, Region.Op.UNION);
15506 }
15507 if (db.bottom < h) {
15508 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15509 r.op(0, db.bottom, w, h, Region.Op.UNION);
15510 }
15511 final int[] location = attachInfo.mTransparentLocation;
15512 getLocationInWindow(location);
15513 r.translate(location[0], location[1]);
15514 region.op(r, Region.Op.INTERSECT);
15515 } else {
15516 region.op(db, Region.Op.DIFFERENCE);
15517 }
15518 }
15519
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015520 private void checkForLongClick(int delayOffset) {
15521 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
15522 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015523
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015524 if (mPendingCheckForLongPress == null) {
15525 mPendingCheckForLongPress = new CheckForLongPress();
15526 }
15527 mPendingCheckForLongPress.rememberWindowAttachCount();
15528 postDelayed(mPendingCheckForLongPress,
15529 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015531 }
15532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015533 /**
15534 * Inflate a view from an XML resource. This convenience method wraps the {@link
15535 * LayoutInflater} class, which provides a full range of options for view inflation.
15536 *
15537 * @param context The Context object for your activity or application.
15538 * @param resource The resource ID to inflate
15539 * @param root A view group that will be the parent. Used to properly inflate the
15540 * layout_* parameters.
15541 * @see LayoutInflater
15542 */
15543 public static View inflate(Context context, int resource, ViewGroup root) {
15544 LayoutInflater factory = LayoutInflater.from(context);
15545 return factory.inflate(resource, root);
15546 }
Romain Guy33e72ae2010-07-17 12:40:29 -070015547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015548 /**
Adam Powell637d3372010-08-25 14:37:03 -070015549 * Scroll the view with standard behavior for scrolling beyond the normal
15550 * content boundaries. Views that call this method should override
15551 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
15552 * results of an over-scroll operation.
15553 *
15554 * Views can use this method to handle any touch or fling-based scrolling.
15555 *
15556 * @param deltaX Change in X in pixels
15557 * @param deltaY Change in Y in pixels
15558 * @param scrollX Current X scroll value in pixels before applying deltaX
15559 * @param scrollY Current Y scroll value in pixels before applying deltaY
15560 * @param scrollRangeX Maximum content scroll range along the X axis
15561 * @param scrollRangeY Maximum content scroll range along the Y axis
15562 * @param maxOverScrollX Number of pixels to overscroll by in either direction
15563 * along the X axis.
15564 * @param maxOverScrollY Number of pixels to overscroll by in either direction
15565 * along the Y axis.
15566 * @param isTouchEvent true if this scroll operation is the result of a touch event.
15567 * @return true if scrolling was clamped to an over-scroll boundary along either
15568 * axis, false otherwise.
15569 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070015570 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070015571 protected boolean overScrollBy(int deltaX, int deltaY,
15572 int scrollX, int scrollY,
15573 int scrollRangeX, int scrollRangeY,
15574 int maxOverScrollX, int maxOverScrollY,
15575 boolean isTouchEvent) {
15576 final int overScrollMode = mOverScrollMode;
15577 final boolean canScrollHorizontal =
15578 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
15579 final boolean canScrollVertical =
15580 computeVerticalScrollRange() > computeVerticalScrollExtent();
15581 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
15582 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
15583 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
15584 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15585
15586 int newScrollX = scrollX + deltaX;
15587 if (!overScrollHorizontal) {
15588 maxOverScrollX = 0;
15589 }
15590
15591 int newScrollY = scrollY + deltaY;
15592 if (!overScrollVertical) {
15593 maxOverScrollY = 0;
15594 }
15595
15596 // Clamp values if at the limits and record
15597 final int left = -maxOverScrollX;
15598 final int right = maxOverScrollX + scrollRangeX;
15599 final int top = -maxOverScrollY;
15600 final int bottom = maxOverScrollY + scrollRangeY;
15601
15602 boolean clampedX = false;
15603 if (newScrollX > right) {
15604 newScrollX = right;
15605 clampedX = true;
15606 } else if (newScrollX < left) {
15607 newScrollX = left;
15608 clampedX = true;
15609 }
15610
15611 boolean clampedY = false;
15612 if (newScrollY > bottom) {
15613 newScrollY = bottom;
15614 clampedY = true;
15615 } else if (newScrollY < top) {
15616 newScrollY = top;
15617 clampedY = true;
15618 }
15619
15620 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
15621
15622 return clampedX || clampedY;
15623 }
15624
15625 /**
15626 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
15627 * respond to the results of an over-scroll operation.
15628 *
15629 * @param scrollX New X scroll value in pixels
15630 * @param scrollY New Y scroll value in pixels
15631 * @param clampedX True if scrollX was clamped to an over-scroll boundary
15632 * @param clampedY True if scrollY was clamped to an over-scroll boundary
15633 */
15634 protected void onOverScrolled(int scrollX, int scrollY,
15635 boolean clampedX, boolean clampedY) {
15636 // Intentionally empty.
15637 }
15638
15639 /**
15640 * Returns the over-scroll mode for this view. The result will be
15641 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15642 * (allow over-scrolling only if the view content is larger than the container),
15643 * or {@link #OVER_SCROLL_NEVER}.
15644 *
15645 * @return This view's over-scroll mode.
15646 */
15647 public int getOverScrollMode() {
15648 return mOverScrollMode;
15649 }
15650
15651 /**
15652 * Set the over-scroll mode for this view. Valid over-scroll modes are
15653 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15654 * (allow over-scrolling only if the view content is larger than the container),
15655 * or {@link #OVER_SCROLL_NEVER}.
15656 *
15657 * Setting the over-scroll mode of a view will have an effect only if the
15658 * view is capable of scrolling.
15659 *
15660 * @param overScrollMode The new over-scroll mode for this view.
15661 */
15662 public void setOverScrollMode(int overScrollMode) {
15663 if (overScrollMode != OVER_SCROLL_ALWAYS &&
15664 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
15665 overScrollMode != OVER_SCROLL_NEVER) {
15666 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
15667 }
15668 mOverScrollMode = overScrollMode;
15669 }
15670
15671 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015672 * Gets a scale factor that determines the distance the view should scroll
15673 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
15674 * @return The vertical scroll scale factor.
15675 * @hide
15676 */
15677 protected float getVerticalScrollFactor() {
15678 if (mVerticalScrollFactor == 0) {
15679 TypedValue outValue = new TypedValue();
15680 if (!mContext.getTheme().resolveAttribute(
15681 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
15682 throw new IllegalStateException(
15683 "Expected theme to define listPreferredItemHeight.");
15684 }
15685 mVerticalScrollFactor = outValue.getDimension(
15686 mContext.getResources().getDisplayMetrics());
15687 }
15688 return mVerticalScrollFactor;
15689 }
15690
15691 /**
15692 * Gets a scale factor that determines the distance the view should scroll
15693 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
15694 * @return The horizontal scroll scale factor.
15695 * @hide
15696 */
15697 protected float getHorizontalScrollFactor() {
15698 // TODO: Should use something else.
15699 return getVerticalScrollFactor();
15700 }
15701
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015702 /**
15703 * Return the value specifying the text direction or policy that was set with
15704 * {@link #setTextDirection(int)}.
15705 *
15706 * @return the defined text direction. It can be one of:
15707 *
15708 * {@link #TEXT_DIRECTION_INHERIT},
15709 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15710 * {@link #TEXT_DIRECTION_ANY_RTL},
15711 * {@link #TEXT_DIRECTION_LTR},
15712 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015713 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015714 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015715 @ViewDebug.ExportedProperty(category = "text", mapping = {
15716 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
15717 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
15718 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
15719 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
15720 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
15721 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
15722 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015723 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015724 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015725 }
15726
15727 /**
15728 * Set the text direction.
15729 *
15730 * @param textDirection the direction to set. Should be one of:
15731 *
15732 * {@link #TEXT_DIRECTION_INHERIT},
15733 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15734 * {@link #TEXT_DIRECTION_ANY_RTL},
15735 * {@link #TEXT_DIRECTION_LTR},
15736 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015737 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015738 */
15739 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015740 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015741 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015742 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015743 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015744 // Set the new text direction
15745 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015746 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015747 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015748 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015749 }
15750 }
15751
15752 /**
15753 * Return the resolved text direction.
15754 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015755 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
15756 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
15757 * up the parent chain of the view. if there is no parent, then it will return the default
15758 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
15759 *
15760 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015761 *
Doug Feltcb3791202011-07-07 11:57:48 -070015762 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15763 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015764 * {@link #TEXT_DIRECTION_LTR},
15765 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015766 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015767 */
15768 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070015769 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015770 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015771 resolveTextDirection();
15772 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015773 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015774 }
15775
15776 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015777 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
15778 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015779 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015780 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015781 // Reset any previous text direction resolution
15782 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
15783
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015784 if (hasRtlSupport()) {
15785 // Set resolved text direction flag depending on text direction flag
15786 final int textDirection = getTextDirection();
15787 switch(textDirection) {
15788 case TEXT_DIRECTION_INHERIT:
15789 if (canResolveTextDirection()) {
15790 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015791
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015792 // Set current resolved direction to the same value as the parent's one
15793 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
15794 switch (parentResolvedDirection) {
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 mPrivateFlags2 |=
15801 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15802 break;
15803 default:
15804 // Default resolved direction is "first strong" heuristic
15805 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
15806 }
15807 } else {
15808 // We cannot do the resolution if there is no parent, so use the default one
15809 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015810 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015811 break;
15812 case TEXT_DIRECTION_FIRST_STRONG:
15813 case TEXT_DIRECTION_ANY_RTL:
15814 case TEXT_DIRECTION_LTR:
15815 case TEXT_DIRECTION_RTL:
15816 case TEXT_DIRECTION_LOCALE:
15817 // Resolved direction is the same as text direction
15818 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15819 break;
15820 default:
15821 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015822 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015823 }
15824 } else {
15825 // Default resolved direction is "first strong" heuristic
15826 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015827 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015828
15829 // Set to resolved
15830 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015831 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015832 }
15833
15834 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015835 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015836 * resolution should override this method.
15837 *
15838 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015839 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015840 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015841 }
15842
15843 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015844 * Check if text direction resolution can be done.
15845 *
15846 * @return true if text direction resolution can be done otherwise return false.
15847 */
15848 public boolean canResolveTextDirection() {
15849 switch (getTextDirection()) {
15850 case TEXT_DIRECTION_INHERIT:
15851 return (mParent != null) && (mParent instanceof ViewGroup);
15852 default:
15853 return true;
15854 }
15855 }
15856
15857 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015858 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015859 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015860 * reset is done.
15861 */
15862 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015863 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015864 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015865 }
15866
15867 /**
15868 * Called when text direction is reset. Subclasses that care about text direction reset should
15869 * override this method and do a reset of the text direction of their children. The default
15870 * implementation does nothing.
15871 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015872 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015873 }
15874
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015875 /**
15876 * Return the value specifying the text alignment or policy that was set with
15877 * {@link #setTextAlignment(int)}.
15878 *
15879 * @return the defined text alignment. It can be one of:
15880 *
15881 * {@link #TEXT_ALIGNMENT_INHERIT},
15882 * {@link #TEXT_ALIGNMENT_GRAVITY},
15883 * {@link #TEXT_ALIGNMENT_CENTER},
15884 * {@link #TEXT_ALIGNMENT_TEXT_START},
15885 * {@link #TEXT_ALIGNMENT_TEXT_END},
15886 * {@link #TEXT_ALIGNMENT_VIEW_START},
15887 * {@link #TEXT_ALIGNMENT_VIEW_END}
15888 */
15889 @ViewDebug.ExportedProperty(category = "text", mapping = {
15890 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15891 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15892 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15893 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15894 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15895 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15896 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15897 })
15898 public int getTextAlignment() {
15899 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
15900 }
15901
15902 /**
15903 * Set the text alignment.
15904 *
15905 * @param textAlignment The text alignment to set. Should be one of
15906 *
15907 * {@link #TEXT_ALIGNMENT_INHERIT},
15908 * {@link #TEXT_ALIGNMENT_GRAVITY},
15909 * {@link #TEXT_ALIGNMENT_CENTER},
15910 * {@link #TEXT_ALIGNMENT_TEXT_START},
15911 * {@link #TEXT_ALIGNMENT_TEXT_END},
15912 * {@link #TEXT_ALIGNMENT_VIEW_START},
15913 * {@link #TEXT_ALIGNMENT_VIEW_END}
15914 *
15915 * @attr ref android.R.styleable#View_textAlignment
15916 */
15917 public void setTextAlignment(int textAlignment) {
15918 if (textAlignment != getTextAlignment()) {
15919 // Reset the current and resolved text alignment
15920 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
15921 resetResolvedTextAlignment();
15922 // Set the new text alignment
15923 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
15924 // Refresh
15925 requestLayout();
15926 invalidate(true);
15927 }
15928 }
15929
15930 /**
15931 * Return the resolved text alignment.
15932 *
15933 * The resolved text alignment. This needs resolution if the value is
15934 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
15935 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
15936 *
15937 * @return the resolved text alignment. Returns one of:
15938 *
15939 * {@link #TEXT_ALIGNMENT_GRAVITY},
15940 * {@link #TEXT_ALIGNMENT_CENTER},
15941 * {@link #TEXT_ALIGNMENT_TEXT_START},
15942 * {@link #TEXT_ALIGNMENT_TEXT_END},
15943 * {@link #TEXT_ALIGNMENT_VIEW_START},
15944 * {@link #TEXT_ALIGNMENT_VIEW_END}
15945 */
15946 @ViewDebug.ExportedProperty(category = "text", mapping = {
15947 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15948 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15949 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15950 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15951 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15952 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15953 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15954 })
15955 public int getResolvedTextAlignment() {
15956 // If text alignment is not resolved, then resolve it
15957 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
15958 resolveTextAlignment();
15959 }
15960 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
15961 }
15962
15963 /**
15964 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
15965 * resolution is done.
15966 */
15967 public void resolveTextAlignment() {
15968 // Reset any previous text alignment resolution
15969 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
15970
15971 if (hasRtlSupport()) {
15972 // Set resolved text alignment flag depending on text alignment flag
15973 final int textAlignment = getTextAlignment();
15974 switch (textAlignment) {
15975 case TEXT_ALIGNMENT_INHERIT:
15976 // Check if we can resolve the text alignment
15977 if (canResolveLayoutDirection() && mParent instanceof View) {
15978 View view = (View) mParent;
15979
15980 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
15981 switch (parentResolvedTextAlignment) {
15982 case TEXT_ALIGNMENT_GRAVITY:
15983 case TEXT_ALIGNMENT_TEXT_START:
15984 case TEXT_ALIGNMENT_TEXT_END:
15985 case TEXT_ALIGNMENT_CENTER:
15986 case TEXT_ALIGNMENT_VIEW_START:
15987 case TEXT_ALIGNMENT_VIEW_END:
15988 // Resolved text alignment is the same as the parent resolved
15989 // text alignment
15990 mPrivateFlags2 |=
15991 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
15992 break;
15993 default:
15994 // Use default resolved text alignment
15995 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15996 }
15997 }
15998 else {
15999 // We cannot do the resolution if there is no parent so use the default
16000 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16001 }
16002 break;
16003 case TEXT_ALIGNMENT_GRAVITY:
16004 case TEXT_ALIGNMENT_TEXT_START:
16005 case TEXT_ALIGNMENT_TEXT_END:
16006 case TEXT_ALIGNMENT_CENTER:
16007 case TEXT_ALIGNMENT_VIEW_START:
16008 case TEXT_ALIGNMENT_VIEW_END:
16009 // Resolved text alignment is the same as text alignment
16010 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16011 break;
16012 default:
16013 // Use default resolved text alignment
16014 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16015 }
16016 } else {
16017 // Use default resolved text alignment
16018 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16019 }
16020
16021 // Set the resolved
16022 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16023 onResolvedTextAlignmentChanged();
16024 }
16025
16026 /**
16027 * Check if text alignment resolution can be done.
16028 *
16029 * @return true if text alignment resolution can be done otherwise return false.
16030 */
16031 public boolean canResolveTextAlignment() {
16032 switch (getTextAlignment()) {
16033 case TEXT_DIRECTION_INHERIT:
16034 return (mParent != null);
16035 default:
16036 return true;
16037 }
16038 }
16039
16040 /**
16041 * Called when text alignment has been resolved. Subclasses that care about text alignment
16042 * resolution should override this method.
16043 *
16044 * The default implementation does nothing.
16045 */
16046 public void onResolvedTextAlignmentChanged() {
16047 }
16048
16049 /**
16050 * Reset resolved text alignment. Text alignment can be resolved with a call to
16051 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16052 * reset is done.
16053 */
16054 public void resetResolvedTextAlignment() {
16055 // Reset any previous text alignment resolution
16056 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16057 onResolvedTextAlignmentReset();
16058 }
16059
16060 /**
16061 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16062 * override this method and do a reset of the text alignment of their children. The default
16063 * implementation does nothing.
16064 */
16065 public void onResolvedTextAlignmentReset() {
16066 }
16067
Chet Haaseb39f0512011-05-24 14:36:40 -070016068 //
16069 // Properties
16070 //
16071 /**
16072 * A Property wrapper around the <code>alpha</code> functionality handled by the
16073 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16074 */
Chet Haased47f1532011-12-16 11:18:52 -080016075 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016076 @Override
16077 public void setValue(View object, float value) {
16078 object.setAlpha(value);
16079 }
16080
16081 @Override
16082 public Float get(View object) {
16083 return object.getAlpha();
16084 }
16085 };
16086
16087 /**
16088 * A Property wrapper around the <code>translationX</code> functionality handled by the
16089 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16090 */
Chet Haased47f1532011-12-16 11:18:52 -080016091 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016092 @Override
16093 public void setValue(View object, float value) {
16094 object.setTranslationX(value);
16095 }
16096
16097 @Override
16098 public Float get(View object) {
16099 return object.getTranslationX();
16100 }
16101 };
16102
16103 /**
16104 * A Property wrapper around the <code>translationY</code> functionality handled by the
16105 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16106 */
Chet Haased47f1532011-12-16 11:18:52 -080016107 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016108 @Override
16109 public void setValue(View object, float value) {
16110 object.setTranslationY(value);
16111 }
16112
16113 @Override
16114 public Float get(View object) {
16115 return object.getTranslationY();
16116 }
16117 };
16118
16119 /**
16120 * A Property wrapper around the <code>x</code> functionality handled by the
16121 * {@link View#setX(float)} and {@link View#getX()} methods.
16122 */
Chet Haased47f1532011-12-16 11:18:52 -080016123 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016124 @Override
16125 public void setValue(View object, float value) {
16126 object.setX(value);
16127 }
16128
16129 @Override
16130 public Float get(View object) {
16131 return object.getX();
16132 }
16133 };
16134
16135 /**
16136 * A Property wrapper around the <code>y</code> functionality handled by the
16137 * {@link View#setY(float)} and {@link View#getY()} methods.
16138 */
Chet Haased47f1532011-12-16 11:18:52 -080016139 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016140 @Override
16141 public void setValue(View object, float value) {
16142 object.setY(value);
16143 }
16144
16145 @Override
16146 public Float get(View object) {
16147 return object.getY();
16148 }
16149 };
16150
16151 /**
16152 * A Property wrapper around the <code>rotation</code> functionality handled by the
16153 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16154 */
Chet Haased47f1532011-12-16 11:18:52 -080016155 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016156 @Override
16157 public void setValue(View object, float value) {
16158 object.setRotation(value);
16159 }
16160
16161 @Override
16162 public Float get(View object) {
16163 return object.getRotation();
16164 }
16165 };
16166
16167 /**
16168 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16169 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16170 */
Chet Haased47f1532011-12-16 11:18:52 -080016171 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016172 @Override
16173 public void setValue(View object, float value) {
16174 object.setRotationX(value);
16175 }
16176
16177 @Override
16178 public Float get(View object) {
16179 return object.getRotationX();
16180 }
16181 };
16182
16183 /**
16184 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16185 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16186 */
Chet Haased47f1532011-12-16 11:18:52 -080016187 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016188 @Override
16189 public void setValue(View object, float value) {
16190 object.setRotationY(value);
16191 }
16192
16193 @Override
16194 public Float get(View object) {
16195 return object.getRotationY();
16196 }
16197 };
16198
16199 /**
16200 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16201 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16202 */
Chet Haased47f1532011-12-16 11:18:52 -080016203 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016204 @Override
16205 public void setValue(View object, float value) {
16206 object.setScaleX(value);
16207 }
16208
16209 @Override
16210 public Float get(View object) {
16211 return object.getScaleX();
16212 }
16213 };
16214
16215 /**
16216 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16217 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16218 */
Chet Haased47f1532011-12-16 11:18:52 -080016219 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016220 @Override
16221 public void setValue(View object, float value) {
16222 object.setScaleY(value);
16223 }
16224
16225 @Override
16226 public Float get(View object) {
16227 return object.getScaleY();
16228 }
16229 };
16230
Jeff Brown33bbfd22011-02-24 20:55:35 -080016231 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016232 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16233 * Each MeasureSpec represents a requirement for either the width or the height.
16234 * A MeasureSpec is comprised of a size and a mode. There are three possible
16235 * modes:
16236 * <dl>
16237 * <dt>UNSPECIFIED</dt>
16238 * <dd>
16239 * The parent has not imposed any constraint on the child. It can be whatever size
16240 * it wants.
16241 * </dd>
16242 *
16243 * <dt>EXACTLY</dt>
16244 * <dd>
16245 * The parent has determined an exact size for the child. The child is going to be
16246 * given those bounds regardless of how big it wants to be.
16247 * </dd>
16248 *
16249 * <dt>AT_MOST</dt>
16250 * <dd>
16251 * The child can be as large as it wants up to the specified size.
16252 * </dd>
16253 * </dl>
16254 *
16255 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16256 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16257 */
16258 public static class MeasureSpec {
16259 private static final int MODE_SHIFT = 30;
16260 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16261
16262 /**
16263 * Measure specification mode: The parent has not imposed any constraint
16264 * on the child. It can be whatever size it wants.
16265 */
16266 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16267
16268 /**
16269 * Measure specification mode: The parent has determined an exact size
16270 * for the child. The child is going to be given those bounds regardless
16271 * of how big it wants to be.
16272 */
16273 public static final int EXACTLY = 1 << MODE_SHIFT;
16274
16275 /**
16276 * Measure specification mode: The child can be as large as it wants up
16277 * to the specified size.
16278 */
16279 public static final int AT_MOST = 2 << MODE_SHIFT;
16280
16281 /**
16282 * Creates a measure specification based on the supplied size and mode.
16283 *
16284 * The mode must always be one of the following:
16285 * <ul>
16286 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16287 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16288 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16289 * </ul>
16290 *
16291 * @param size the size of the measure specification
16292 * @param mode the mode of the measure specification
16293 * @return the measure specification based on size and mode
16294 */
16295 public static int makeMeasureSpec(int size, int mode) {
16296 return size + mode;
16297 }
16298
16299 /**
16300 * Extracts the mode from the supplied measure specification.
16301 *
16302 * @param measureSpec the measure specification to extract the mode from
16303 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16304 * {@link android.view.View.MeasureSpec#AT_MOST} or
16305 * {@link android.view.View.MeasureSpec#EXACTLY}
16306 */
16307 public static int getMode(int measureSpec) {
16308 return (measureSpec & MODE_MASK);
16309 }
16310
16311 /**
16312 * Extracts the size from the supplied measure specification.
16313 *
16314 * @param measureSpec the measure specification to extract the size from
16315 * @return the size in pixels defined in the supplied measure specification
16316 */
16317 public static int getSize(int measureSpec) {
16318 return (measureSpec & ~MODE_MASK);
16319 }
16320
16321 /**
16322 * Returns a String representation of the specified measure
16323 * specification.
16324 *
16325 * @param measureSpec the measure specification to convert to a String
16326 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16327 */
16328 public static String toString(int measureSpec) {
16329 int mode = getMode(measureSpec);
16330 int size = getSize(measureSpec);
16331
16332 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16333
16334 if (mode == UNSPECIFIED)
16335 sb.append("UNSPECIFIED ");
16336 else if (mode == EXACTLY)
16337 sb.append("EXACTLY ");
16338 else if (mode == AT_MOST)
16339 sb.append("AT_MOST ");
16340 else
16341 sb.append(mode).append(" ");
16342
16343 sb.append(size);
16344 return sb.toString();
16345 }
16346 }
16347
16348 class CheckForLongPress implements Runnable {
16349
16350 private int mOriginalWindowAttachCount;
16351
16352 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016353 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016354 && mOriginalWindowAttachCount == mWindowAttachCount) {
16355 if (performLongClick()) {
16356 mHasPerformedLongPress = true;
16357 }
16358 }
16359 }
16360
16361 public void rememberWindowAttachCount() {
16362 mOriginalWindowAttachCount = mWindowAttachCount;
16363 }
16364 }
Joe Malin32736f02011-01-19 16:14:20 -080016365
Adam Powelle14579b2009-12-16 18:39:52 -080016366 private final class CheckForTap implements Runnable {
16367 public void run() {
16368 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016369 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016370 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016371 }
16372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016373
Adam Powella35d7682010-03-12 14:48:13 -080016374 private final class PerformClick implements Runnable {
16375 public void run() {
16376 performClick();
16377 }
16378 }
16379
Dianne Hackborn63042d62011-01-26 18:56:29 -080016380 /** @hide */
16381 public void hackTurnOffWindowResizeAnim(boolean off) {
16382 mAttachInfo.mTurnOffWindowResizeAnim = off;
16383 }
Joe Malin32736f02011-01-19 16:14:20 -080016384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016385 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016386 * This method returns a ViewPropertyAnimator object, which can be used to animate
16387 * specific properties on this View.
16388 *
16389 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16390 */
16391 public ViewPropertyAnimator animate() {
16392 if (mAnimator == null) {
16393 mAnimator = new ViewPropertyAnimator(this);
16394 }
16395 return mAnimator;
16396 }
16397
16398 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016399 * Interface definition for a callback to be invoked when a key event is
16400 * dispatched to this view. The callback will be invoked before the key
16401 * event is given to the view.
16402 */
16403 public interface OnKeyListener {
16404 /**
16405 * Called when a key is dispatched to a view. This allows listeners to
16406 * get a chance to respond before the target view.
16407 *
16408 * @param v The view the key has been dispatched to.
16409 * @param keyCode The code for the physical key that was pressed
16410 * @param event The KeyEvent object containing full information about
16411 * the event.
16412 * @return True if the listener has consumed the event, false otherwise.
16413 */
16414 boolean onKey(View v, int keyCode, KeyEvent event);
16415 }
16416
16417 /**
16418 * Interface definition for a callback to be invoked when a touch event is
16419 * dispatched to this view. The callback will be invoked before the touch
16420 * event is given to the view.
16421 */
16422 public interface OnTouchListener {
16423 /**
16424 * Called when a touch event is dispatched to a view. This allows listeners to
16425 * get a chance to respond before the target view.
16426 *
16427 * @param v The view the touch event has been dispatched to.
16428 * @param event The MotionEvent object containing full information about
16429 * the event.
16430 * @return True if the listener has consumed the event, false otherwise.
16431 */
16432 boolean onTouch(View v, MotionEvent event);
16433 }
16434
16435 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016436 * Interface definition for a callback to be invoked when a hover event is
16437 * dispatched to this view. The callback will be invoked before the hover
16438 * event is given to the view.
16439 */
16440 public interface OnHoverListener {
16441 /**
16442 * Called when a hover event is dispatched to a view. This allows listeners to
16443 * get a chance to respond before the target view.
16444 *
16445 * @param v The view the hover event has been dispatched to.
16446 * @param event The MotionEvent object containing full information about
16447 * the event.
16448 * @return True if the listener has consumed the event, false otherwise.
16449 */
16450 boolean onHover(View v, MotionEvent event);
16451 }
16452
16453 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016454 * Interface definition for a callback to be invoked when a generic motion event is
16455 * dispatched to this view. The callback will be invoked before the generic motion
16456 * event is given to the view.
16457 */
16458 public interface OnGenericMotionListener {
16459 /**
16460 * Called when a generic motion event is dispatched to a view. This allows listeners to
16461 * get a chance to respond before the target view.
16462 *
16463 * @param v The view the generic motion event has been dispatched to.
16464 * @param event The MotionEvent object containing full information about
16465 * the event.
16466 * @return True if the listener has consumed the event, false otherwise.
16467 */
16468 boolean onGenericMotion(View v, MotionEvent event);
16469 }
16470
16471 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016472 * Interface definition for a callback to be invoked when a view has been clicked and held.
16473 */
16474 public interface OnLongClickListener {
16475 /**
16476 * Called when a view has been clicked and held.
16477 *
16478 * @param v The view that was clicked and held.
16479 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016480 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016481 */
16482 boolean onLongClick(View v);
16483 }
16484
16485 /**
Chris Tate32affef2010-10-18 15:29:21 -070016486 * Interface definition for a callback to be invoked when a drag is being dispatched
16487 * to this view. The callback will be invoked before the hosting view's own
16488 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16489 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016490 *
16491 * <div class="special reference">
16492 * <h3>Developer Guides</h3>
16493 * <p>For a guide to implementing drag and drop features, read the
16494 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16495 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016496 */
16497 public interface OnDragListener {
16498 /**
16499 * Called when a drag event is dispatched to a view. This allows listeners
16500 * to get a chance to override base View behavior.
16501 *
Joe Malin32736f02011-01-19 16:14:20 -080016502 * @param v The View that received the drag event.
16503 * @param event The {@link android.view.DragEvent} object for the drag event.
16504 * @return {@code true} if the drag event was handled successfully, or {@code false}
16505 * if the drag event was not handled. Note that {@code false} will trigger the View
16506 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070016507 */
16508 boolean onDrag(View v, DragEvent event);
16509 }
16510
16511 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016512 * Interface definition for a callback to be invoked when the focus state of
16513 * a view changed.
16514 */
16515 public interface OnFocusChangeListener {
16516 /**
16517 * Called when the focus state of a view has changed.
16518 *
16519 * @param v The view whose state has changed.
16520 * @param hasFocus The new focus state of v.
16521 */
16522 void onFocusChange(View v, boolean hasFocus);
16523 }
16524
16525 /**
16526 * Interface definition for a callback to be invoked when a view is clicked.
16527 */
16528 public interface OnClickListener {
16529 /**
16530 * Called when a view has been clicked.
16531 *
16532 * @param v The view that was clicked.
16533 */
16534 void onClick(View v);
16535 }
16536
16537 /**
16538 * Interface definition for a callback to be invoked when the context menu
16539 * for this view is being built.
16540 */
16541 public interface OnCreateContextMenuListener {
16542 /**
16543 * Called when the context menu for this view is being built. It is not
16544 * safe to hold onto the menu after this method returns.
16545 *
16546 * @param menu The context menu that is being built
16547 * @param v The view for which the context menu is being built
16548 * @param menuInfo Extra information about the item for which the
16549 * context menu should be shown. This information will vary
16550 * depending on the class of v.
16551 */
16552 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
16553 }
16554
Joe Onorato664644d2011-01-23 17:53:23 -080016555 /**
16556 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016557 * visibility. This reports <strong>global</strong> changes to the system UI
16558 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080016559 *
Philip Milne6c8ea062012-04-03 17:38:43 -070016560 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080016561 */
16562 public interface OnSystemUiVisibilityChangeListener {
16563 /**
16564 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070016565 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080016566 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040016567 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016568 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
16569 * <strong>global</strong> state of the UI visibility flags, not what your
16570 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080016571 */
16572 public void onSystemUiVisibilityChange(int visibility);
16573 }
16574
Adam Powell4afd62b2011-02-18 15:02:18 -080016575 /**
16576 * Interface definition for a callback to be invoked when this view is attached
16577 * or detached from its window.
16578 */
16579 public interface OnAttachStateChangeListener {
16580 /**
16581 * Called when the view is attached to a window.
16582 * @param v The view that was attached
16583 */
16584 public void onViewAttachedToWindow(View v);
16585 /**
16586 * Called when the view is detached from a window.
16587 * @param v The view that was detached
16588 */
16589 public void onViewDetachedFromWindow(View v);
16590 }
16591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016592 private final class UnsetPressedState implements Runnable {
16593 public void run() {
16594 setPressed(false);
16595 }
16596 }
16597
16598 /**
16599 * Base class for derived classes that want to save and restore their own
16600 * state in {@link android.view.View#onSaveInstanceState()}.
16601 */
16602 public static class BaseSavedState extends AbsSavedState {
16603 /**
16604 * Constructor used when reading from a parcel. Reads the state of the superclass.
16605 *
16606 * @param source
16607 */
16608 public BaseSavedState(Parcel source) {
16609 super(source);
16610 }
16611
16612 /**
16613 * Constructor called by derived classes when creating their SavedState objects
16614 *
16615 * @param superState The state of the superclass of this view
16616 */
16617 public BaseSavedState(Parcelable superState) {
16618 super(superState);
16619 }
16620
16621 public static final Parcelable.Creator<BaseSavedState> CREATOR =
16622 new Parcelable.Creator<BaseSavedState>() {
16623 public BaseSavedState createFromParcel(Parcel in) {
16624 return new BaseSavedState(in);
16625 }
16626
16627 public BaseSavedState[] newArray(int size) {
16628 return new BaseSavedState[size];
16629 }
16630 };
16631 }
16632
16633 /**
16634 * A set of information given to a view when it is attached to its parent
16635 * window.
16636 */
16637 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016638 interface Callbacks {
16639 void playSoundEffect(int effectId);
16640 boolean performHapticFeedback(int effectId, boolean always);
16641 }
16642
16643 /**
16644 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
16645 * to a Handler. This class contains the target (View) to invalidate and
16646 * the coordinates of the dirty rectangle.
16647 *
16648 * For performance purposes, this class also implements a pool of up to
16649 * POOL_LIMIT objects that get reused. This reduces memory allocations
16650 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016651 */
Romain Guyd928d682009-03-31 17:52:16 -070016652 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016653 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070016654 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
16655 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070016656 public InvalidateInfo newInstance() {
16657 return new InvalidateInfo();
16658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016659
Romain Guyd928d682009-03-31 17:52:16 -070016660 public void onAcquired(InvalidateInfo element) {
16661 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016662
Romain Guyd928d682009-03-31 17:52:16 -070016663 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070016664 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070016665 }
16666 }, POOL_LIMIT)
16667 );
16668
16669 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016670 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016671
16672 View target;
16673
16674 int left;
16675 int top;
16676 int right;
16677 int bottom;
16678
Romain Guyd928d682009-03-31 17:52:16 -070016679 public void setNextPoolable(InvalidateInfo element) {
16680 mNext = element;
16681 }
16682
16683 public InvalidateInfo getNextPoolable() {
16684 return mNext;
16685 }
16686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016687 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070016688 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016689 }
16690
16691 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070016692 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016693 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016694
16695 public boolean isPooled() {
16696 return mIsPooled;
16697 }
16698
16699 public void setPooled(boolean isPooled) {
16700 mIsPooled = isPooled;
16701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016702 }
16703
16704 final IWindowSession mSession;
16705
16706 final IWindow mWindow;
16707
16708 final IBinder mWindowToken;
16709
16710 final Callbacks mRootCallbacks;
16711
Romain Guy59a12ca2011-06-09 17:48:21 -070016712 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080016713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016714 /**
16715 * The top view of the hierarchy.
16716 */
16717 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070016718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016719 IBinder mPanelParentWindowToken;
16720 Surface mSurface;
16721
Romain Guyb051e892010-09-28 19:09:36 -070016722 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080016723 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070016724 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080016725
Romain Guy7e4e5612012-03-05 14:37:29 -080016726 boolean mScreenOn;
16727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016728 /**
Romain Guy8506ab42009-06-11 17:35:47 -070016729 * Scale factor used by the compatibility mode
16730 */
16731 float mApplicationScale;
16732
16733 /**
16734 * Indicates whether the application is in compatibility mode
16735 */
16736 boolean mScalingRequired;
16737
16738 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016739 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080016740 */
16741 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080016742
Dianne Hackborn63042d62011-01-26 18:56:29 -080016743 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016744 * Left position of this view's window
16745 */
16746 int mWindowLeft;
16747
16748 /**
16749 * Top position of this view's window
16750 */
16751 int mWindowTop;
16752
16753 /**
Adam Powell26153a32010-11-08 15:22:27 -080016754 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070016755 */
Adam Powell26153a32010-11-08 15:22:27 -080016756 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070016757
16758 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016759 * For windows that are full-screen but using insets to layout inside
16760 * of the screen decorations, these are the current insets for the
16761 * content of the window.
16762 */
16763 final Rect mContentInsets = new Rect();
16764
16765 /**
16766 * For windows that are full-screen but using insets to layout inside
16767 * of the screen decorations, these are the current insets for the
16768 * actual visible parts of the window.
16769 */
16770 final Rect mVisibleInsets = new Rect();
16771
16772 /**
16773 * The internal insets given by this window. This value is
16774 * supplied by the client (through
16775 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
16776 * be given to the window manager when changed to be used in laying
16777 * out windows behind it.
16778 */
16779 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
16780 = new ViewTreeObserver.InternalInsetsInfo();
16781
16782 /**
16783 * All views in the window's hierarchy that serve as scroll containers,
16784 * used to determine if the window can be resized or must be panned
16785 * to adjust for a soft input area.
16786 */
16787 final ArrayList<View> mScrollContainers = new ArrayList<View>();
16788
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070016789 final KeyEvent.DispatcherState mKeyDispatchState
16790 = new KeyEvent.DispatcherState();
16791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016792 /**
16793 * Indicates whether the view's window currently has the focus.
16794 */
16795 boolean mHasWindowFocus;
16796
16797 /**
16798 * The current visibility of the window.
16799 */
16800 int mWindowVisibility;
16801
16802 /**
16803 * Indicates the time at which drawing started to occur.
16804 */
16805 long mDrawingTime;
16806
16807 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070016808 * Indicates whether or not ignoring the DIRTY_MASK flags.
16809 */
16810 boolean mIgnoreDirtyState;
16811
16812 /**
Romain Guy02ccac62011-06-24 13:20:23 -070016813 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
16814 * to avoid clearing that flag prematurely.
16815 */
16816 boolean mSetIgnoreDirtyState = false;
16817
16818 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016819 * Indicates whether the view's window is currently in touch mode.
16820 */
16821 boolean mInTouchMode;
16822
16823 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016824 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016825 * the next time it performs a traversal
16826 */
16827 boolean mRecomputeGlobalAttributes;
16828
16829 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016830 * Always report new attributes at next traversal.
16831 */
16832 boolean mForceReportNewAttributes;
16833
16834 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016835 * Set during a traveral if any views want to keep the screen on.
16836 */
16837 boolean mKeepScreenOn;
16838
16839 /**
Joe Onorato664644d2011-01-23 17:53:23 -080016840 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
16841 */
16842 int mSystemUiVisibility;
16843
16844 /**
16845 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
16846 * attached.
16847 */
16848 boolean mHasSystemUiListeners;
16849
16850 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016851 * Set if the visibility of any views has changed.
16852 */
16853 boolean mViewVisibilityChanged;
16854
16855 /**
16856 * Set to true if a view has been scrolled.
16857 */
16858 boolean mViewScrollChanged;
16859
16860 /**
16861 * Global to the view hierarchy used as a temporary for dealing with
16862 * x/y points in the transparent region computations.
16863 */
16864 final int[] mTransparentLocation = new int[2];
16865
16866 /**
16867 * Global to the view hierarchy used as a temporary for dealing with
16868 * x/y points in the ViewGroup.invalidateChild implementation.
16869 */
16870 final int[] mInvalidateChildLocation = new int[2];
16871
Chet Haasec3aa3612010-06-17 08:50:37 -070016872
16873 /**
16874 * Global to the view hierarchy used as a temporary for dealing with
16875 * x/y location when view is transformed.
16876 */
16877 final float[] mTmpTransformLocation = new float[2];
16878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016879 /**
16880 * The view tree observer used to dispatch global events like
16881 * layout, pre-draw, touch mode change, etc.
16882 */
16883 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
16884
16885 /**
16886 * A Canvas used by the view hierarchy to perform bitmap caching.
16887 */
16888 Canvas mCanvas;
16889
16890 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080016891 * The view root impl.
16892 */
16893 final ViewRootImpl mViewRootImpl;
16894
16895 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016896 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016897 * handler can be used to pump events in the UI events queue.
16898 */
16899 final Handler mHandler;
16900
16901 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016902 * Temporary for use in computing invalidate rectangles while
16903 * calling up the hierarchy.
16904 */
16905 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070016906
16907 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070016908 * Temporary for use in computing hit areas with transformed views
16909 */
16910 final RectF mTmpTransformRect = new RectF();
16911
16912 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070016913 * Temporary list for use in collecting focusable descendents of a view.
16914 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070016915 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070016916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016917 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016918 * The id of the window for accessibility purposes.
16919 */
16920 int mAccessibilityWindowId = View.NO_ID;
16921
16922 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070016923 * Whether to ingore not exposed for accessibility Views when
16924 * reporting the view tree to accessibility services.
16925 */
16926 boolean mIncludeNotImportantViews;
16927
16928 /**
16929 * The drawable for highlighting accessibility focus.
16930 */
16931 Drawable mAccessibilityFocusDrawable;
16932
16933 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016934 * Creates a new set of attachment information with the specified
16935 * events handler and thread.
16936 *
16937 * @param handler the events handler the view must use
16938 */
16939 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080016940 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016941 mSession = session;
16942 mWindow = window;
16943 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080016944 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016945 mHandler = handler;
16946 mRootCallbacks = effectPlayer;
16947 }
16948 }
16949
16950 /**
16951 * <p>ScrollabilityCache holds various fields used by a View when scrolling
16952 * is supported. This avoids keeping too many unused fields in most
16953 * instances of View.</p>
16954 */
Mike Cleronf116bf82009-09-27 19:14:12 -070016955 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080016956
Mike Cleronf116bf82009-09-27 19:14:12 -070016957 /**
16958 * Scrollbars are not visible
16959 */
16960 public static final int OFF = 0;
16961
16962 /**
16963 * Scrollbars are visible
16964 */
16965 public static final int ON = 1;
16966
16967 /**
16968 * Scrollbars are fading away
16969 */
16970 public static final int FADING = 2;
16971
16972 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080016973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016974 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070016975 public int scrollBarDefaultDelayBeforeFade;
16976 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016977
16978 public int scrollBarSize;
16979 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070016980 public float[] interpolatorValues;
16981 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016982
16983 public final Paint paint;
16984 public final Matrix matrix;
16985 public Shader shader;
16986
Mike Cleronf116bf82009-09-27 19:14:12 -070016987 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
16988
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016989 private static final float[] OPAQUE = { 255 };
16990 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080016991
Mike Cleronf116bf82009-09-27 19:14:12 -070016992 /**
16993 * When fading should start. This time moves into the future every time
16994 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
16995 */
16996 public long fadeStartTime;
16997
16998
16999 /**
17000 * The current state of the scrollbars: ON, OFF, or FADING
17001 */
17002 public int state = OFF;
17003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017004 private int mLastColor;
17005
Mike Cleronf116bf82009-09-27 19:14:12 -070017006 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017007 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17008 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017009 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17010 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017011
17012 paint = new Paint();
17013 matrix = new Matrix();
17014 // use use a height of 1, and then wack the matrix each time we
17015 // actually use it.
17016 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017018 paint.setShader(shader);
17019 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017020 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017021 }
Romain Guy8506ab42009-06-11 17:35:47 -070017022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017023 public void setFadeColor(int color) {
17024 if (color != 0 && color != mLastColor) {
17025 mLastColor = color;
17026 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017027
Romain Guye55e1a72009-08-27 10:42:26 -070017028 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17029 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017031 paint.setShader(shader);
17032 // Restore the default transfer mode (src_over)
17033 paint.setXfermode(null);
17034 }
17035 }
Joe Malin32736f02011-01-19 16:14:20 -080017036
Mike Cleronf116bf82009-09-27 19:14:12 -070017037 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017038 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017039 if (now >= fadeStartTime) {
17040
17041 // the animation fades the scrollbars out by changing
17042 // the opacity (alpha) from fully opaque to fully
17043 // transparent
17044 int nextFrame = (int) now;
17045 int framesCount = 0;
17046
17047 Interpolator interpolator = scrollBarInterpolator;
17048
17049 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017050 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017051
17052 // End transparent
17053 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017054 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017055
17056 state = FADING;
17057
17058 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017059 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017060 }
17061 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017062 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017063
Svetoslav Ganova0156172011-06-26 17:55:44 -070017064 /**
17065 * Resuable callback for sending
17066 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17067 */
17068 private class SendViewScrolledAccessibilityEvent implements Runnable {
17069 public volatile boolean mIsPending;
17070
17071 public void run() {
17072 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17073 mIsPending = false;
17074 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017075 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017076
17077 /**
17078 * <p>
17079 * This class represents a delegate that can be registered in a {@link View}
17080 * to enhance accessibility support via composition rather via inheritance.
17081 * It is specifically targeted to widget developers that extend basic View
17082 * classes i.e. classes in package android.view, that would like their
17083 * applications to be backwards compatible.
17084 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017085 * <div class="special reference">
17086 * <h3>Developer Guides</h3>
17087 * <p>For more information about making applications accessible, read the
17088 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17089 * developer guide.</p>
17090 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017091 * <p>
17092 * A scenario in which a developer would like to use an accessibility delegate
17093 * is overriding a method introduced in a later API version then the minimal API
17094 * version supported by the application. For example, the method
17095 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17096 * in API version 4 when the accessibility APIs were first introduced. If a
17097 * developer would like his application to run on API version 4 devices (assuming
17098 * all other APIs used by the application are version 4 or lower) and take advantage
17099 * of this method, instead of overriding the method which would break the application's
17100 * backwards compatibility, he can override the corresponding method in this
17101 * delegate and register the delegate in the target View if the API version of
17102 * the system is high enough i.e. the API version is same or higher to the API
17103 * version that introduced
17104 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17105 * </p>
17106 * <p>
17107 * Here is an example implementation:
17108 * </p>
17109 * <code><pre><p>
17110 * if (Build.VERSION.SDK_INT >= 14) {
17111 * // If the API version is equal of higher than the version in
17112 * // which onInitializeAccessibilityNodeInfo was introduced we
17113 * // register a delegate with a customized implementation.
17114 * View view = findViewById(R.id.view_id);
17115 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17116 * public void onInitializeAccessibilityNodeInfo(View host,
17117 * AccessibilityNodeInfo info) {
17118 * // Let the default implementation populate the info.
17119 * super.onInitializeAccessibilityNodeInfo(host, info);
17120 * // Set some other information.
17121 * info.setEnabled(host.isEnabled());
17122 * }
17123 * });
17124 * }
17125 * </code></pre></p>
17126 * <p>
17127 * This delegate contains methods that correspond to the accessibility methods
17128 * in View. If a delegate has been specified the implementation in View hands
17129 * off handling to the corresponding method in this delegate. The default
17130 * implementation the delegate methods behaves exactly as the corresponding
17131 * method in View for the case of no accessibility delegate been set. Hence,
17132 * to customize the behavior of a View method, clients can override only the
17133 * corresponding delegate method without altering the behavior of the rest
17134 * accessibility related methods of the host view.
17135 * </p>
17136 */
17137 public static class AccessibilityDelegate {
17138
17139 /**
17140 * Sends an accessibility event of the given type. If accessibility is not
17141 * enabled this method has no effect.
17142 * <p>
17143 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17144 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17145 * been set.
17146 * </p>
17147 *
17148 * @param host The View hosting the delegate.
17149 * @param eventType The type of the event to send.
17150 *
17151 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17152 */
17153 public void sendAccessibilityEvent(View host, int eventType) {
17154 host.sendAccessibilityEventInternal(eventType);
17155 }
17156
17157 /**
17158 * Sends an accessibility event. This method behaves exactly as
17159 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17160 * empty {@link AccessibilityEvent} and does not perform a check whether
17161 * accessibility is enabled.
17162 * <p>
17163 * The default implementation behaves as
17164 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17165 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17166 * the case of no accessibility delegate been set.
17167 * </p>
17168 *
17169 * @param host The View hosting the delegate.
17170 * @param event The event to send.
17171 *
17172 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17173 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17174 */
17175 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17176 host.sendAccessibilityEventUncheckedInternal(event);
17177 }
17178
17179 /**
17180 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17181 * to its children for adding their text content to the event.
17182 * <p>
17183 * The default implementation behaves as
17184 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17185 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17186 * the case of no accessibility delegate been set.
17187 * </p>
17188 *
17189 * @param host The View hosting the delegate.
17190 * @param event The event.
17191 * @return True if the event population was completed.
17192 *
17193 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17194 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17195 */
17196 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17197 return host.dispatchPopulateAccessibilityEventInternal(event);
17198 }
17199
17200 /**
17201 * Gives a chance to the host View to populate the accessibility event with its
17202 * text content.
17203 * <p>
17204 * The default implementation behaves as
17205 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17206 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17207 * the case of no accessibility delegate been set.
17208 * </p>
17209 *
17210 * @param host The View hosting the delegate.
17211 * @param event The accessibility event which to populate.
17212 *
17213 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17214 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17215 */
17216 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17217 host.onPopulateAccessibilityEventInternal(event);
17218 }
17219
17220 /**
17221 * Initializes an {@link AccessibilityEvent} with information about the
17222 * the host View which is the event source.
17223 * <p>
17224 * The default implementation behaves as
17225 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17226 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17227 * the case of no accessibility delegate been set.
17228 * </p>
17229 *
17230 * @param host The View hosting the delegate.
17231 * @param event The event to initialize.
17232 *
17233 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17234 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17235 */
17236 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17237 host.onInitializeAccessibilityEventInternal(event);
17238 }
17239
17240 /**
17241 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17242 * <p>
17243 * The default implementation behaves as
17244 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17245 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17246 * the case of no accessibility delegate been set.
17247 * </p>
17248 *
17249 * @param host The View hosting the delegate.
17250 * @param info The instance to initialize.
17251 *
17252 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17253 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17254 */
17255 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17256 host.onInitializeAccessibilityNodeInfoInternal(info);
17257 }
17258
17259 /**
17260 * Called when a child of the host View has requested sending an
17261 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17262 * to augment the event.
17263 * <p>
17264 * The default implementation behaves as
17265 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17266 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17267 * the case of no accessibility delegate been set.
17268 * </p>
17269 *
17270 * @param host The View hosting the delegate.
17271 * @param child The child which requests sending the event.
17272 * @param event The event to be sent.
17273 * @return True if the event should be sent
17274 *
17275 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17276 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17277 */
17278 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17279 AccessibilityEvent event) {
17280 return host.onRequestSendAccessibilityEventInternal(child, event);
17281 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017282
17283 /**
17284 * Gets the provider for managing a virtual view hierarchy rooted at this View
17285 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17286 * that explore the window content.
17287 * <p>
17288 * The default implementation behaves as
17289 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17290 * the case of no accessibility delegate been set.
17291 * </p>
17292 *
17293 * @return The provider.
17294 *
17295 * @see AccessibilityNodeProvider
17296 */
17297 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17298 return null;
17299 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017301}