blob: 6c1f02d0da2d7a3124915b62db0b88b0bc35a5d2 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Christopher Tatea53146c2010-09-07 11:57:52 -070019import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070025import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.Canvas;
Mike Cleronf116bf82009-09-27 19:14:12 -070027import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.graphics.LinearGradient;
29import android.graphics.Matrix;
30import android.graphics.Paint;
31import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070032import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.graphics.PorterDuff;
34import android.graphics.PorterDuffXfermode;
35import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070036import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.graphics.Region;
38import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.graphics.drawable.ColorDrawable;
40import android.graphics.drawable.Drawable;
41import android.os.Handler;
42import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Parcel;
44import android.os.Parcelable;
45import android.os.RemoteException;
46import android.os.SystemClock;
Svetoslav Ganovea515ae2011-09-14 18:15:32 -070047import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070049import android.util.FloatProperty;
50import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070052import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070053import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070056import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080058import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.view.accessibility.AccessibilityEvent;
61import android.view.accessibility.AccessibilityEventSource;
62import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070063import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070064import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070066import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080067import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070068import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.view.inputmethod.InputConnection;
70import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.widget.ScrollBarDrawable;
72
Romain Guy1ef3fdb2011-09-09 15:30:30 -070073import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070074import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070075
Doug Feltcb3791202011-07-07 11:57:48 -070076import com.android.internal.R;
77import com.android.internal.util.Predicate;
78import com.android.internal.view.menu.MenuBuilder;
79
Christopher Tatea0374192010-10-05 13:06:41 -070080import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070081import java.lang.reflect.InvocationTargetException;
82import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import java.util.ArrayList;
84import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070085import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080086import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
88/**
89 * <p>
90 * This class represents the basic building block for user interface components. A View
91 * occupies a rectangular area on the screen and is responsible for drawing and
92 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070093 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
95 * are invisible containers that hold other Views (or other ViewGroups) and define
96 * their layout properties.
97 * </p>
98 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -070099 * <div class="special reference">
100 * <h3>Developer Guides</h3>
101 * <p>For information about using this class to develop your application's user interface,
102 * read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700104 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 * <a name="Using"></a>
106 * <h3>Using Views</h3>
107 * <p>
108 * All of the views in a window are arranged in a single tree. You can add views
109 * either from code or by specifying a tree of views in one or more XML layout
110 * files. There are many specialized subclasses of views that act as controls or
111 * are capable of displaying text, images, or other content.
112 * </p>
113 * <p>
114 * Once you have created a tree of views, there are typically a few types of
115 * common operations you may wish to perform:
116 * <ul>
117 * <li><strong>Set properties:</strong> for example setting the text of a
118 * {@link android.widget.TextView}. The available properties and the methods
119 * that set them will vary among the different subclasses of views. Note that
120 * properties that are known at build time can be set in the XML layout
121 * files.</li>
122 * <li><strong>Set focus:</strong> The framework will handled moving focus in
123 * response to user input. To force focus to a specific view, call
124 * {@link #requestFocus}.</li>
125 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
126 * that will be notified when something interesting happens to the view. For
127 * example, all views will let you set a listener to be notified when the view
128 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700129 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700130 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700131 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700133 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * </ul>
135 * </p>
136 * <p><em>
137 * Note: The Android framework is responsible for measuring, laying out and
138 * drawing views. You should not call methods that perform these actions on
139 * views yourself unless you are actually implementing a
140 * {@link android.view.ViewGroup}.
141 * </em></p>
142 *
143 * <a name="Lifecycle"></a>
144 * <h3>Implementing a Custom View</h3>
145 *
146 * <p>
147 * To implement a custom view, you will usually begin by providing overrides for
148 * some of the standard methods that the framework calls on all views. You do
149 * not need to override all of these methods. In fact, you can start by just
150 * overriding {@link #onDraw(android.graphics.Canvas)}.
151 * <table border="2" width="85%" align="center" cellpadding="5">
152 * <thead>
153 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
154 * </thead>
155 *
156 * <tbody>
157 * <tr>
158 * <td rowspan="2">Creation</td>
159 * <td>Constructors</td>
160 * <td>There is a form of the constructor that are called when the view
161 * is created from code and a form that is called when the view is
162 * inflated from a layout file. The second form should parse and apply
163 * any attributes defined in the layout file.
164 * </td>
165 * </tr>
166 * <tr>
167 * <td><code>{@link #onFinishInflate()}</code></td>
168 * <td>Called after a view and all of its children has been inflated
169 * from XML.</td>
170 * </tr>
171 *
172 * <tr>
173 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700174 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 * <td>Called to determine the size requirements for this view and all
176 * of its children.
177 * </td>
178 * </tr>
179 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700180 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <td>Called when this view should assign a size and position to all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700186 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * <td>Called when the size of this view has changed.
188 * </td>
189 * </tr>
190 *
191 * <tr>
192 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700193 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 * <td>Called when the view should render its content.
195 * </td>
196 * </tr>
197 *
198 * <tr>
199 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700200 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 * <td>Called when a new key event occurs.
202 * </td>
203 * </tr>
204 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700205 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 * <td>Called when a key up event occurs.
207 * </td>
208 * </tr>
209 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700210 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 * <td>Called when a trackball motion event occurs.
212 * </td>
213 * </tr>
214 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700215 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 * <td>Called when a touch screen motion event occurs.
217 * </td>
218 * </tr>
219 *
220 * <tr>
221 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700222 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 * <td>Called when the view gains or loses focus.
224 * </td>
225 * </tr>
226 *
227 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700228 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <td>Called when the window containing the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
234 * <td rowspan="3">Attaching</td>
235 * <td><code>{@link #onAttachedToWindow()}</code></td>
236 * <td>Called when the view is attached to a window.
237 * </td>
238 * </tr>
239 *
240 * <tr>
241 * <td><code>{@link #onDetachedFromWindow}</code></td>
242 * <td>Called when the view is detached from its window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700247 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 * <td>Called when the visibility of the window containing the view
249 * has changed.
250 * </td>
251 * </tr>
252 * </tbody>
253 *
254 * </table>
255 * </p>
256 *
257 * <a name="IDs"></a>
258 * <h3>IDs</h3>
259 * Views may have an integer id associated with them. These ids are typically
260 * assigned in the layout XML files, and are used to find specific views within
261 * the view tree. A common pattern is to:
262 * <ul>
263 * <li>Define a Button in the layout file and assign it a unique ID.
264 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700265 * &lt;Button
266 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 * android:layout_width="wrap_content"
268 * android:layout_height="wrap_content"
269 * android:text="@string/my_button_text"/&gt;
270 * </pre></li>
271 * <li>From the onCreate method of an Activity, find the Button
272 * <pre class="prettyprint">
273 * Button myButton = (Button) findViewById(R.id.my_button);
274 * </pre></li>
275 * </ul>
276 * <p>
277 * View IDs need not be unique throughout the tree, but it is good practice to
278 * ensure that they are at least unique within the part of the tree you are
279 * searching.
280 * </p>
281 *
282 * <a name="Position"></a>
283 * <h3>Position</h3>
284 * <p>
285 * The geometry of a view is that of a rectangle. A view has a location,
286 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
287 * two dimensions, expressed as a width and a height. The unit for location
288 * and dimensions is the pixel.
289 * </p>
290 *
291 * <p>
292 * It is possible to retrieve the location of a view by invoking the methods
293 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
294 * coordinate of the rectangle representing the view. The latter returns the
295 * top, or Y, coordinate of the rectangle representing the view. These methods
296 * both return the location of the view relative to its parent. For instance,
297 * when getLeft() returns 20, that means the view is located 20 pixels to the
298 * right of the left edge of its direct parent.
299 * </p>
300 *
301 * <p>
302 * In addition, several convenience methods are offered to avoid unnecessary
303 * computations, namely {@link #getRight()} and {@link #getBottom()}.
304 * These methods return the coordinates of the right and bottom edges of the
305 * rectangle representing the view. For instance, calling {@link #getRight()}
306 * is similar to the following computation: <code>getLeft() + getWidth()</code>
307 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
308 * </p>
309 *
310 * <a name="SizePaddingMargins"></a>
311 * <h3>Size, padding and margins</h3>
312 * <p>
313 * The size of a view is expressed with a width and a height. A view actually
314 * possess two pairs of width and height values.
315 * </p>
316 *
317 * <p>
318 * The first pair is known as <em>measured width</em> and
319 * <em>measured height</em>. These dimensions define how big a view wants to be
320 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
321 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
322 * and {@link #getMeasuredHeight()}.
323 * </p>
324 *
325 * <p>
326 * The second pair is simply known as <em>width</em> and <em>height</em>, or
327 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
328 * dimensions define the actual size of the view on screen, at drawing time and
329 * after layout. These values may, but do not have to, be different from the
330 * measured width and height. The width and height can be obtained by calling
331 * {@link #getWidth()} and {@link #getHeight()}.
332 * </p>
333 *
334 * <p>
335 * To measure its dimensions, a view takes into account its padding. The padding
336 * is expressed in pixels for the left, top, right and bottom parts of the view.
337 * Padding can be used to offset the content of the view by a specific amount of
338 * pixels. For instance, a left padding of 2 will push the view's content by
339 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700340 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
341 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
342 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
343 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 * </p>
345 *
346 * <p>
347 * Even though a view can define a padding, it does not provide any support for
348 * margins. However, view groups provide such a support. Refer to
349 * {@link android.view.ViewGroup} and
350 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
351 * </p>
352 *
353 * <a name="Layout"></a>
354 * <h3>Layout</h3>
355 * <p>
356 * Layout is a two pass process: a measure pass and a layout pass. The measuring
357 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
358 * of the view tree. Each view pushes dimension specifications down the tree
359 * during the recursion. At the end of the measure pass, every view has stored
360 * its measurements. The second pass happens in
361 * {@link #layout(int,int,int,int)} and is also top-down. During
362 * this pass each parent is responsible for positioning all of its children
363 * using the sizes computed in the measure pass.
364 * </p>
365 *
366 * <p>
367 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
368 * {@link #getMeasuredHeight()} values must be set, along with those for all of
369 * that view's descendants. A view's measured width and measured height values
370 * must respect the constraints imposed by the view's parents. This guarantees
371 * that at the end of the measure pass, all parents accept all of their
372 * children's measurements. A parent view may call measure() more than once on
373 * its children. For example, the parent may measure each child once with
374 * unspecified dimensions to find out how big they want to be, then call
375 * measure() on them again with actual numbers if the sum of all the children's
376 * unconstrained sizes is too big or too small.
377 * </p>
378 *
379 * <p>
380 * The measure pass uses two classes to communicate dimensions. The
381 * {@link MeasureSpec} class is used by views to tell their parents how they
382 * want to be measured and positioned. The base LayoutParams class just
383 * describes how big the view wants to be for both width and height. For each
384 * dimension, it can specify one of:
385 * <ul>
386 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800387 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 * (minus padding)
389 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
390 * enclose its content (plus padding).
391 * </ul>
392 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
393 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
394 * an X and Y value.
395 * </p>
396 *
397 * <p>
398 * MeasureSpecs are used to push requirements down the tree from parent to
399 * child. A MeasureSpec can be in one of three modes:
400 * <ul>
401 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
402 * of a child view. For example, a LinearLayout may call measure() on its child
403 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
404 * tall the child view wants to be given a width of 240 pixels.
405 * <li>EXACTLY: This is used by the parent to impose an exact size on the
406 * child. The child must use this size, and guarantee that all of its
407 * descendants will fit within this size.
408 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
409 * child. The child must gurantee that it and all of its descendants will fit
410 * within this size.
411 * </ul>
412 * </p>
413 *
414 * <p>
415 * To intiate a layout, call {@link #requestLayout}. This method is typically
416 * called by a view on itself when it believes that is can no longer fit within
417 * its current bounds.
418 * </p>
419 *
420 * <a name="Drawing"></a>
421 * <h3>Drawing</h3>
422 * <p>
423 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700424 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 * this means that parents will draw before (i.e., behind) their children, with
426 * siblings drawn in the order they appear in the tree.
427 * If you set a background drawable for a View, then the View will draw it for you
428 * before calling back to its <code>onDraw()</code> method.
429 * </p>
430 *
431 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700432 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 * </p>
434 *
435 * <p>
436 * To force a view to draw, call {@link #invalidate()}.
437 * </p>
438 *
439 * <a name="EventHandlingThreading"></a>
440 * <h3>Event Handling and Threading</h3>
441 * <p>
442 * The basic cycle of a view is as follows:
443 * <ol>
444 * <li>An event comes in and is dispatched to the appropriate view. The view
445 * handles the event and notifies any listeners.</li>
446 * <li>If in the course of processing the event, the view's bounds may need
447 * to be changed, the view will call {@link #requestLayout()}.</li>
448 * <li>Similarly, if in the course of processing the event the view's appearance
449 * may need to be changed, the view will call {@link #invalidate()}.</li>
450 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
451 * the framework will take care of measuring, laying out, and drawing the tree
452 * as appropriate.</li>
453 * </ol>
454 * </p>
455 *
456 * <p><em>Note: The entire view tree is single threaded. You must always be on
457 * the UI thread when calling any method on any view.</em>
458 * If you are doing work on other threads and want to update the state of a view
459 * from that thread, you should use a {@link Handler}.
460 * </p>
461 *
462 * <a name="FocusHandling"></a>
463 * <h3>Focus Handling</h3>
464 * <p>
465 * The framework will handle routine focus movement in response to user input.
466 * This includes changing the focus as views are removed or hidden, or as new
467 * views become available. Views indicate their willingness to take focus
468 * through the {@link #isFocusable} method. To change whether a view can take
469 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
470 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
471 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
472 * </p>
473 * <p>
474 * Focus movement is based on an algorithm which finds the nearest neighbor in a
475 * given direction. In rare cases, the default algorithm may not match the
476 * intended behavior of the developer. In these situations, you can provide
477 * explicit overrides by using these XML attributes in the layout file:
478 * <pre>
479 * nextFocusDown
480 * nextFocusLeft
481 * nextFocusRight
482 * nextFocusUp
483 * </pre>
484 * </p>
485 *
486 *
487 * <p>
488 * To get a particular view to take focus, call {@link #requestFocus()}.
489 * </p>
490 *
491 * <a name="TouchMode"></a>
492 * <h3>Touch Mode</h3>
493 * <p>
494 * When a user is navigating a user interface via directional keys such as a D-pad, it is
495 * necessary to give focus to actionable items such as buttons so the user can see
496 * what will take input. If the device has touch capabilities, however, and the user
497 * begins interacting with the interface by touching it, it is no longer necessary to
498 * always highlight, or give focus to, a particular view. This motivates a mode
499 * for interaction named 'touch mode'.
500 * </p>
501 * <p>
502 * For a touch capable device, once the user touches the screen, the device
503 * will enter touch mode. From this point onward, only views for which
504 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
505 * Other views that are touchable, like buttons, will not take focus when touched; they will
506 * only fire the on click listeners.
507 * </p>
508 * <p>
509 * Any time a user hits a directional key, such as a D-pad direction, the view device will
510 * exit touch mode, and find a view to take focus, so that the user may resume interacting
511 * with the user interface without touching the screen again.
512 * </p>
513 * <p>
514 * The touch mode state is maintained across {@link android.app.Activity}s. Call
515 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
516 * </p>
517 *
518 * <a name="Scrolling"></a>
519 * <h3>Scrolling</h3>
520 * <p>
521 * The framework provides basic support for views that wish to internally
522 * scroll their content. This includes keeping track of the X and Y scroll
523 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800524 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700525 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 * </p>
527 *
528 * <a name="Tags"></a>
529 * <h3>Tags</h3>
530 * <p>
531 * Unlike IDs, tags are not used to identify views. Tags are essentially an
532 * extra piece of information that can be associated with a view. They are most
533 * often used as a convenience to store data related to views in the views
534 * themselves rather than by putting them in a separate structure.
535 * </p>
536 *
537 * <a name="Animation"></a>
538 * <h3>Animation</h3>
539 * <p>
540 * You can attach an {@link Animation} object to a view using
541 * {@link #setAnimation(Animation)} or
542 * {@link #startAnimation(Animation)}. The animation can alter the scale,
543 * rotation, translation and alpha of a view over time. If the animation is
544 * attached to a view that has children, the animation will affect the entire
545 * subtree rooted by that node. When an animation is started, the framework will
546 * take care of redrawing the appropriate views until the animation completes.
547 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800548 * <p>
549 * Starting with Android 3.0, the preferred way of animating views is to use the
550 * {@link android.animation} package APIs.
551 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 *
Jeff Brown85a31762010-09-01 17:01:00 -0700553 * <a name="Security"></a>
554 * <h3>Security</h3>
555 * <p>
556 * Sometimes it is essential that an application be able to verify that an action
557 * is being performed with the full knowledge and consent of the user, such as
558 * granting a permission request, making a purchase or clicking on an advertisement.
559 * Unfortunately, a malicious application could try to spoof the user into
560 * performing these actions, unaware, by concealing the intended purpose of the view.
561 * As a remedy, the framework offers a touch filtering mechanism that can be used to
562 * improve the security of views that provide access to sensitive functionality.
563 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700564 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800565 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700566 * will discard touches that are received whenever the view's window is obscured by
567 * another visible window. As a result, the view will not receive touches whenever a
568 * toast, dialog or other window appears above the view's window.
569 * </p><p>
570 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700571 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
572 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700573 * </p>
574 *
Romain Guy171c5922011-01-06 10:04:23 -0800575 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700576 * @attr ref android.R.styleable#View_background
577 * @attr ref android.R.styleable#View_clickable
578 * @attr ref android.R.styleable#View_contentDescription
579 * @attr ref android.R.styleable#View_drawingCacheQuality
580 * @attr ref android.R.styleable#View_duplicateParentState
581 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700582 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700583 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700584 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700585 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700587 * @attr ref android.R.styleable#View_isScrollContainer
588 * @attr ref android.R.styleable#View_focusable
589 * @attr ref android.R.styleable#View_focusableInTouchMode
590 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
591 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800592 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700593 * @attr ref android.R.styleable#View_longClickable
594 * @attr ref android.R.styleable#View_minHeight
595 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 * @attr ref android.R.styleable#View_nextFocusDown
597 * @attr ref android.R.styleable#View_nextFocusLeft
598 * @attr ref android.R.styleable#View_nextFocusRight
599 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700600 * @attr ref android.R.styleable#View_onClick
601 * @attr ref android.R.styleable#View_padding
602 * @attr ref android.R.styleable#View_paddingBottom
603 * @attr ref android.R.styleable#View_paddingLeft
604 * @attr ref android.R.styleable#View_paddingRight
605 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800606 * @attr ref android.R.styleable#View_paddingStart
607 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700608 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800609 * @attr ref android.R.styleable#View_rotation
610 * @attr ref android.R.styleable#View_rotationX
611 * @attr ref android.R.styleable#View_rotationY
612 * @attr ref android.R.styleable#View_scaleX
613 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 * @attr ref android.R.styleable#View_scrollX
615 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700616 * @attr ref android.R.styleable#View_scrollbarSize
617 * @attr ref android.R.styleable#View_scrollbarStyle
618 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700619 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
620 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
622 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 * @attr ref android.R.styleable#View_scrollbarThumbVertical
624 * @attr ref android.R.styleable#View_scrollbarTrackVertical
625 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
626 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700627 * @attr ref android.R.styleable#View_soundEffectsEnabled
628 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700629 * @attr ref android.R.styleable#View_textAlignment
Chet Haase73066682010-11-29 15:55:32 -0800630 * @attr ref android.R.styleable#View_transformPivotX
631 * @attr ref android.R.styleable#View_transformPivotY
632 * @attr ref android.R.styleable#View_translationX
633 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700634 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 *
636 * @see android.view.ViewGroup
637 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700638public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
639 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 private static final boolean DBG = false;
641
642 /**
643 * The logging tag used by this class with android.util.Log.
644 */
645 protected static final String VIEW_LOG_TAG = "View";
646
647 /**
648 * Used to mark a View that has no ID.
649 */
650 public static final int NO_ID = -1;
651
652 /**
653 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
654 * calling setFlags.
655 */
656 private static final int NOT_FOCUSABLE = 0x00000000;
657
658 /**
659 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
660 * setFlags.
661 */
662 private static final int FOCUSABLE = 0x00000001;
663
664 /**
665 * Mask for use with setFlags indicating bits used for focus.
666 */
667 private static final int FOCUSABLE_MASK = 0x00000001;
668
669 /**
670 * This view will adjust its padding to fit sytem windows (e.g. status bar)
671 */
672 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
673
674 /**
Scott Main812634c22011-07-27 13:22:35 -0700675 * This view is visible.
676 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
677 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 */
679 public static final int VISIBLE = 0x00000000;
680
681 /**
682 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700683 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
684 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 */
686 public static final int INVISIBLE = 0x00000004;
687
688 /**
689 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700690 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
691 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 */
693 public static final int GONE = 0x00000008;
694
695 /**
696 * Mask for use with setFlags indicating bits used for visibility.
697 * {@hide}
698 */
699 static final int VISIBILITY_MASK = 0x0000000C;
700
701 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
702
703 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700704 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 * Use with ENABLED_MASK when calling setFlags.
706 * {@hide}
707 */
708 static final int ENABLED = 0x00000000;
709
710 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700711 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 * Use with ENABLED_MASK when calling setFlags.
713 * {@hide}
714 */
715 static final int DISABLED = 0x00000020;
716
717 /**
718 * Mask for use with setFlags indicating bits used for indicating whether
719 * this view is enabled
720 * {@hide}
721 */
722 static final int ENABLED_MASK = 0x00000020;
723
724 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700725 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
726 * called and further optimizations will be performed. It is okay to have
727 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 * {@hide}
729 */
730 static final int WILL_NOT_DRAW = 0x00000080;
731
732 /**
733 * Mask for use with setFlags indicating bits used for indicating whether
734 * this view is will draw
735 * {@hide}
736 */
737 static final int DRAW_MASK = 0x00000080;
738
739 /**
740 * <p>This view doesn't show scrollbars.</p>
741 * {@hide}
742 */
743 static final int SCROLLBARS_NONE = 0x00000000;
744
745 /**
746 * <p>This view shows horizontal scrollbars.</p>
747 * {@hide}
748 */
749 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
750
751 /**
752 * <p>This view shows vertical scrollbars.</p>
753 * {@hide}
754 */
755 static final int SCROLLBARS_VERTICAL = 0x00000200;
756
757 /**
758 * <p>Mask for use with setFlags indicating bits used for indicating which
759 * scrollbars are enabled.</p>
760 * {@hide}
761 */
762 static final int SCROLLBARS_MASK = 0x00000300;
763
Jeff Brown85a31762010-09-01 17:01:00 -0700764 /**
765 * Indicates that the view should filter touches when its window is obscured.
766 * Refer to the class comments for more information about this security feature.
767 * {@hide}
768 */
769 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
770
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700771 /**
772 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
773 * that they are optional and should be skipped if the window has
774 * requested system UI flags that ignore those insets for layout.
775 */
776 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777
778 /**
779 * <p>This view doesn't show fading edges.</p>
780 * {@hide}
781 */
782 static final int FADING_EDGE_NONE = 0x00000000;
783
784 /**
785 * <p>This view shows horizontal fading edges.</p>
786 * {@hide}
787 */
788 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
789
790 /**
791 * <p>This view shows vertical fading edges.</p>
792 * {@hide}
793 */
794 static final int FADING_EDGE_VERTICAL = 0x00002000;
795
796 /**
797 * <p>Mask for use with setFlags indicating bits used for indicating which
798 * fading edges are enabled.</p>
799 * {@hide}
800 */
801 static final int FADING_EDGE_MASK = 0x00003000;
802
803 /**
804 * <p>Indicates this view can be clicked. When clickable, a View reacts
805 * to clicks by notifying the OnClickListener.<p>
806 * {@hide}
807 */
808 static final int CLICKABLE = 0x00004000;
809
810 /**
811 * <p>Indicates this view is caching its drawing into a bitmap.</p>
812 * {@hide}
813 */
814 static final int DRAWING_CACHE_ENABLED = 0x00008000;
815
816 /**
817 * <p>Indicates that no icicle should be saved for this view.<p>
818 * {@hide}
819 */
820 static final int SAVE_DISABLED = 0x000010000;
821
822 /**
823 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
824 * property.</p>
825 * {@hide}
826 */
827 static final int SAVE_DISABLED_MASK = 0x000010000;
828
829 /**
830 * <p>Indicates that no drawing cache should ever be created for this view.<p>
831 * {@hide}
832 */
833 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
834
835 /**
836 * <p>Indicates this view can take / keep focus when int touch mode.</p>
837 * {@hide}
838 */
839 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
840
841 /**
842 * <p>Enables low quality mode for the drawing cache.</p>
843 */
844 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
845
846 /**
847 * <p>Enables high quality mode for the drawing cache.</p>
848 */
849 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
850
851 /**
852 * <p>Enables automatic quality mode for the drawing cache.</p>
853 */
854 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
855
856 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
857 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
858 };
859
860 /**
861 * <p>Mask for use with setFlags indicating bits used for the cache
862 * quality property.</p>
863 * {@hide}
864 */
865 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
866
867 /**
868 * <p>
869 * Indicates this view can be long clicked. When long clickable, a View
870 * reacts to long clicks by notifying the OnLongClickListener or showing a
871 * context menu.
872 * </p>
873 * {@hide}
874 */
875 static final int LONG_CLICKABLE = 0x00200000;
876
877 /**
878 * <p>Indicates that this view gets its drawable states from its direct parent
879 * and ignores its original internal states.</p>
880 *
881 * @hide
882 */
883 static final int DUPLICATE_PARENT_STATE = 0x00400000;
884
885 /**
886 * The scrollbar style to display the scrollbars inside the content area,
887 * without increasing the padding. The scrollbars will be overlaid with
888 * translucency on the view's content.
889 */
890 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
891
892 /**
893 * The scrollbar style to display the scrollbars inside the padded area,
894 * increasing the padding of the view. The scrollbars will not overlap the
895 * content area of the view.
896 */
897 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
898
899 /**
900 * The scrollbar style to display the scrollbars at the edge of the view,
901 * without increasing the padding. The scrollbars will be overlaid with
902 * translucency.
903 */
904 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
905
906 /**
907 * The scrollbar style to display the scrollbars at the edge of the view,
908 * increasing the padding of the view. The scrollbars will only overlap the
909 * background, if any.
910 */
911 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
912
913 /**
914 * Mask to check if the scrollbar style is overlay or inset.
915 * {@hide}
916 */
917 static final int SCROLLBARS_INSET_MASK = 0x01000000;
918
919 /**
920 * Mask to check if the scrollbar style is inside or outside.
921 * {@hide}
922 */
923 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
924
925 /**
926 * Mask for scrollbar style.
927 * {@hide}
928 */
929 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
930
931 /**
932 * View flag indicating that the screen should remain on while the
933 * window containing this view is visible to the user. This effectively
934 * takes care of automatically setting the WindowManager's
935 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
936 */
937 public static final int KEEP_SCREEN_ON = 0x04000000;
938
939 /**
940 * View flag indicating whether this view should have sound effects enabled
941 * for events such as clicking and touching.
942 */
943 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
944
945 /**
946 * View flag indicating whether this view should have haptic feedback
947 * enabled for events such as long presses.
948 */
949 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
950
951 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700952 * <p>Indicates that the view hierarchy should stop saving state when
953 * it reaches this view. If state saving is initiated immediately at
954 * the view, it will be allowed.
955 * {@hide}
956 */
957 static final int PARENT_SAVE_DISABLED = 0x20000000;
958
959 /**
960 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
961 * {@hide}
962 */
963 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
964
965 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700966 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
967 * should add all focusable Views regardless if they are focusable in touch mode.
968 */
969 public static final int FOCUSABLES_ALL = 0x00000000;
970
971 /**
972 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
973 * should add only Views focusable in touch mode.
974 */
975 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
976
977 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -0700978 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
979 * should add only accessibility focusable Views.
980 *
981 * @hide
982 */
983 public static final int FOCUSABLES_ACCESSIBILITY = 0x00000002;
984
985 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700986 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 * item.
988 */
989 public static final int FOCUS_BACKWARD = 0x00000001;
990
991 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700992 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 * item.
994 */
995 public static final int FOCUS_FORWARD = 0x00000002;
996
997 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700998 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 */
1000 public static final int FOCUS_LEFT = 0x00000011;
1001
1002 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001003 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 */
1005 public static final int FOCUS_UP = 0x00000021;
1006
1007 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001008 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 */
1010 public static final int FOCUS_RIGHT = 0x00000042;
1011
1012 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001013 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 */
1015 public static final int FOCUS_DOWN = 0x00000082;
1016
Svetoslav Ganov42138042012-03-20 11:51:39 -07001017 // Accessibility focus directions.
1018
1019 /**
1020 * The accessibility focus which is the current user position when
1021 * interacting with the accessibility framework.
1022 */
1023 public static final int FOCUS_ACCESSIBILITY = 0x00001000;
1024
1025 /**
1026 * Use with {@link #focusSearch(int)}. Move acessibility focus left.
1027 */
1028 public static final int ACCESSIBILITY_FOCUS_LEFT = FOCUS_LEFT | FOCUS_ACCESSIBILITY;
1029
1030 /**
1031 * Use with {@link #focusSearch(int)}. Move acessibility focus up.
1032 */
1033 public static final int ACCESSIBILITY_FOCUS_UP = FOCUS_UP | FOCUS_ACCESSIBILITY;
1034
1035 /**
1036 * Use with {@link #focusSearch(int)}. Move acessibility focus right.
1037 */
1038 public static final int ACCESSIBILITY_FOCUS_RIGHT = FOCUS_RIGHT | FOCUS_ACCESSIBILITY;
1039
1040 /**
1041 * Use with {@link #focusSearch(int)}. Move acessibility focus down.
1042 */
1043 public static final int ACCESSIBILITY_FOCUS_DOWN = FOCUS_DOWN | FOCUS_ACCESSIBILITY;
1044
1045 /**
1046 * Use with {@link #focusSearch(int)}. Move acessibility focus to the next view.
1047 */
1048 public static final int ACCESSIBILITY_FOCUS_FORWARD = FOCUS_FORWARD | FOCUS_ACCESSIBILITY;
1049
1050 /**
1051 * Use with {@link #focusSearch(int)}. Move acessibility focus to the previous view.
1052 */
1053 public static final int ACCESSIBILITY_FOCUS_BACKWARD = FOCUS_BACKWARD | FOCUS_ACCESSIBILITY;
1054
1055 /**
1056 * Use with {@link #focusSearch(int)}. Move acessibility focus in a view.
1057 */
1058 public static final int ACCESSIBILITY_FOCUS_IN = 0x00000004 | FOCUS_ACCESSIBILITY;
1059
1060 /**
1061 * Use with {@link #focusSearch(int)}. Move acessibility focus out of a view.
1062 */
1063 public static final int ACCESSIBILITY_FOCUS_OUT = 0x00000008 | FOCUS_ACCESSIBILITY;
1064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001066 * Bits of {@link #getMeasuredWidthAndState()} and
1067 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1068 */
1069 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1070
1071 /**
1072 * Bits of {@link #getMeasuredWidthAndState()} and
1073 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1074 */
1075 public static final int MEASURED_STATE_MASK = 0xff000000;
1076
1077 /**
1078 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1079 * for functions that combine both width and height into a single int,
1080 * such as {@link #getMeasuredState()} and the childState argument of
1081 * {@link #resolveSizeAndState(int, int, int)}.
1082 */
1083 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1084
1085 /**
1086 * Bit of {@link #getMeasuredWidthAndState()} and
1087 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1088 * is smaller that the space the view would like to have.
1089 */
1090 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1091
1092 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 * Base View state sets
1094 */
1095 // Singles
1096 /**
1097 * Indicates the view has no states set. States are used with
1098 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1099 * view depending on its state.
1100 *
1101 * @see android.graphics.drawable.Drawable
1102 * @see #getDrawableState()
1103 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001104 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 /**
1106 * Indicates the view is enabled. States are used with
1107 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1108 * view depending on its state.
1109 *
1110 * @see android.graphics.drawable.Drawable
1111 * @see #getDrawableState()
1112 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001113 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 /**
1115 * Indicates the view is focused. States are used with
1116 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1117 * view depending on its state.
1118 *
1119 * @see android.graphics.drawable.Drawable
1120 * @see #getDrawableState()
1121 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001122 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 /**
1124 * Indicates the view is selected. States are used with
1125 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1126 * view depending on its state.
1127 *
1128 * @see android.graphics.drawable.Drawable
1129 * @see #getDrawableState()
1130 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001131 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 /**
1133 * Indicates the view is pressed. States are used with
1134 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1135 * view depending on its state.
1136 *
1137 * @see android.graphics.drawable.Drawable
1138 * @see #getDrawableState()
1139 * @hide
1140 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001141 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 /**
1143 * Indicates the view's window has focus. States are used with
1144 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1145 * view depending on its state.
1146 *
1147 * @see android.graphics.drawable.Drawable
1148 * @see #getDrawableState()
1149 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001150 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 // Doubles
1152 /**
1153 * Indicates the view is enabled and has the focus.
1154 *
1155 * @see #ENABLED_STATE_SET
1156 * @see #FOCUSED_STATE_SET
1157 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001158 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 /**
1160 * Indicates the view is enabled and selected.
1161 *
1162 * @see #ENABLED_STATE_SET
1163 * @see #SELECTED_STATE_SET
1164 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001165 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 /**
1167 * Indicates the view is enabled and that its window has focus.
1168 *
1169 * @see #ENABLED_STATE_SET
1170 * @see #WINDOW_FOCUSED_STATE_SET
1171 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001172 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 /**
1174 * Indicates the view is focused and selected.
1175 *
1176 * @see #FOCUSED_STATE_SET
1177 * @see #SELECTED_STATE_SET
1178 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001179 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 /**
1181 * Indicates the view has the focus and that its window has the focus.
1182 *
1183 * @see #FOCUSED_STATE_SET
1184 * @see #WINDOW_FOCUSED_STATE_SET
1185 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001186 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 /**
1188 * Indicates the view is selected and that its window has the focus.
1189 *
1190 * @see #SELECTED_STATE_SET
1191 * @see #WINDOW_FOCUSED_STATE_SET
1192 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001193 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 // Triples
1195 /**
1196 * Indicates the view is enabled, focused and selected.
1197 *
1198 * @see #ENABLED_STATE_SET
1199 * @see #FOCUSED_STATE_SET
1200 * @see #SELECTED_STATE_SET
1201 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001202 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 /**
1204 * Indicates the view is enabled, focused and its window has the focus.
1205 *
1206 * @see #ENABLED_STATE_SET
1207 * @see #FOCUSED_STATE_SET
1208 * @see #WINDOW_FOCUSED_STATE_SET
1209 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001210 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 /**
1212 * Indicates the view is enabled, selected and its window has the focus.
1213 *
1214 * @see #ENABLED_STATE_SET
1215 * @see #SELECTED_STATE_SET
1216 * @see #WINDOW_FOCUSED_STATE_SET
1217 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001218 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 /**
1220 * Indicates the view is focused, selected and its window has the focus.
1221 *
1222 * @see #FOCUSED_STATE_SET
1223 * @see #SELECTED_STATE_SET
1224 * @see #WINDOW_FOCUSED_STATE_SET
1225 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001226 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 /**
1228 * Indicates the view is enabled, focused, selected and its window
1229 * has the focus.
1230 *
1231 * @see #ENABLED_STATE_SET
1232 * @see #FOCUSED_STATE_SET
1233 * @see #SELECTED_STATE_SET
1234 * @see #WINDOW_FOCUSED_STATE_SET
1235 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001236 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 /**
1238 * Indicates the view is pressed and its window has the focus.
1239 *
1240 * @see #PRESSED_STATE_SET
1241 * @see #WINDOW_FOCUSED_STATE_SET
1242 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001243 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 /**
1245 * Indicates the view is pressed and selected.
1246 *
1247 * @see #PRESSED_STATE_SET
1248 * @see #SELECTED_STATE_SET
1249 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001250 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 /**
1252 * Indicates the view is pressed, selected and its window has the focus.
1253 *
1254 * @see #PRESSED_STATE_SET
1255 * @see #SELECTED_STATE_SET
1256 * @see #WINDOW_FOCUSED_STATE_SET
1257 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001258 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 /**
1260 * Indicates the view is pressed and focused.
1261 *
1262 * @see #PRESSED_STATE_SET
1263 * @see #FOCUSED_STATE_SET
1264 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001265 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 /**
1267 * Indicates the view is pressed, focused and its window has the focus.
1268 *
1269 * @see #PRESSED_STATE_SET
1270 * @see #FOCUSED_STATE_SET
1271 * @see #WINDOW_FOCUSED_STATE_SET
1272 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001273 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 /**
1275 * Indicates the view is pressed, focused and selected.
1276 *
1277 * @see #PRESSED_STATE_SET
1278 * @see #SELECTED_STATE_SET
1279 * @see #FOCUSED_STATE_SET
1280 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001281 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 /**
1283 * Indicates the view is pressed, focused, selected and its window has the focus.
1284 *
1285 * @see #PRESSED_STATE_SET
1286 * @see #FOCUSED_STATE_SET
1287 * @see #SELECTED_STATE_SET
1288 * @see #WINDOW_FOCUSED_STATE_SET
1289 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001290 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 /**
1292 * Indicates the view is pressed and enabled.
1293 *
1294 * @see #PRESSED_STATE_SET
1295 * @see #ENABLED_STATE_SET
1296 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001297 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 /**
1299 * Indicates the view is pressed, enabled and its window has the focus.
1300 *
1301 * @see #PRESSED_STATE_SET
1302 * @see #ENABLED_STATE_SET
1303 * @see #WINDOW_FOCUSED_STATE_SET
1304 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001305 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 /**
1307 * Indicates the view is pressed, enabled and selected.
1308 *
1309 * @see #PRESSED_STATE_SET
1310 * @see #ENABLED_STATE_SET
1311 * @see #SELECTED_STATE_SET
1312 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001313 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 /**
1315 * Indicates the view is pressed, enabled, selected and its window has the
1316 * focus.
1317 *
1318 * @see #PRESSED_STATE_SET
1319 * @see #ENABLED_STATE_SET
1320 * @see #SELECTED_STATE_SET
1321 * @see #WINDOW_FOCUSED_STATE_SET
1322 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001323 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 /**
1325 * Indicates the view is pressed, enabled and focused.
1326 *
1327 * @see #PRESSED_STATE_SET
1328 * @see #ENABLED_STATE_SET
1329 * @see #FOCUSED_STATE_SET
1330 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001331 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 /**
1333 * Indicates the view is pressed, enabled, focused and its window has the
1334 * focus.
1335 *
1336 * @see #PRESSED_STATE_SET
1337 * @see #ENABLED_STATE_SET
1338 * @see #FOCUSED_STATE_SET
1339 * @see #WINDOW_FOCUSED_STATE_SET
1340 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 /**
1343 * Indicates the view is pressed, enabled, focused and selected.
1344 *
1345 * @see #PRESSED_STATE_SET
1346 * @see #ENABLED_STATE_SET
1347 * @see #SELECTED_STATE_SET
1348 * @see #FOCUSED_STATE_SET
1349 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001350 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 /**
1352 * Indicates the view is pressed, enabled, focused, selected and its window
1353 * has the focus.
1354 *
1355 * @see #PRESSED_STATE_SET
1356 * @see #ENABLED_STATE_SET
1357 * @see #SELECTED_STATE_SET
1358 * @see #FOCUSED_STATE_SET
1359 * @see #WINDOW_FOCUSED_STATE_SET
1360 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001361 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362
1363 /**
1364 * The order here is very important to {@link #getDrawableState()}
1365 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001366 private static final int[][] VIEW_STATE_SETS;
1367
Romain Guyb051e892010-09-28 19:09:36 -07001368 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1369 static final int VIEW_STATE_SELECTED = 1 << 1;
1370 static final int VIEW_STATE_FOCUSED = 1 << 2;
1371 static final int VIEW_STATE_ENABLED = 1 << 3;
1372 static final int VIEW_STATE_PRESSED = 1 << 4;
1373 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001374 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001375 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001376 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1377 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001378
1379 static final int[] VIEW_STATE_IDS = new int[] {
1380 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1381 R.attr.state_selected, VIEW_STATE_SELECTED,
1382 R.attr.state_focused, VIEW_STATE_FOCUSED,
1383 R.attr.state_enabled, VIEW_STATE_ENABLED,
1384 R.attr.state_pressed, VIEW_STATE_PRESSED,
1385 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001386 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001387 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001388 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001389 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 };
1391
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001392 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001393 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1394 throw new IllegalStateException(
1395 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1396 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001397 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001398 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001399 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001400 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001401 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001402 orderedIds[i * 2] = viewState;
1403 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001404 }
1405 }
1406 }
Romain Guyb051e892010-09-28 19:09:36 -07001407 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1408 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1409 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001410 int numBits = Integer.bitCount(i);
1411 int[] set = new int[numBits];
1412 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001413 for (int j = 0; j < orderedIds.length; j += 2) {
1414 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001415 set[pos++] = orderedIds[j];
1416 }
1417 }
1418 VIEW_STATE_SETS[i] = set;
1419 }
1420
1421 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1422 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1423 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1424 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1425 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1426 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1427 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1428 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1429 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1430 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1431 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1432 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1433 | VIEW_STATE_FOCUSED];
1434 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1435 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1436 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1437 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1438 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1439 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1440 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1441 | VIEW_STATE_ENABLED];
1442 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1444 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1445 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1446 | VIEW_STATE_ENABLED];
1447 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1449 | VIEW_STATE_ENABLED];
1450 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1451 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1452 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1453
1454 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1455 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1457 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1459 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1460 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1461 | VIEW_STATE_PRESSED];
1462 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1464 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1465 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1466 | VIEW_STATE_PRESSED];
1467 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1468 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1469 | VIEW_STATE_PRESSED];
1470 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1471 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1472 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1473 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1474 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1475 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1476 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1477 | VIEW_STATE_PRESSED];
1478 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1479 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1480 | VIEW_STATE_PRESSED];
1481 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1482 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1483 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1484 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1485 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1486 | VIEW_STATE_PRESSED];
1487 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1488 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1489 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1490 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1491 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1492 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1493 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1494 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1495 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1496 | VIEW_STATE_PRESSED];
1497 }
1498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001500 * Accessibility event types that are dispatched for text population.
1501 */
1502 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1503 AccessibilityEvent.TYPE_VIEW_CLICKED
1504 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1505 | AccessibilityEvent.TYPE_VIEW_SELECTED
1506 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1507 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1508 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001509 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001510 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001511 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
1512 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001513
1514 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 * Temporary Rect currently for use in setBackground(). This will probably
1516 * be extended in the future to hold our own class with more than just
1517 * a Rect. :)
1518 */
1519 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001520
1521 /**
Chet Haasea1cff502012-02-21 13:43:44 -08001522 * Temporary flag, used to enable processing of View properties in the native DisplayList
1523 * object instead of during draw(). Soon to be enabled by default for hardware-accelerated
1524 * apps.
1525 * @hide
1526 */
Chet Haase8d56b0e2012-04-02 16:34:48 -07001527 public static final boolean USE_DISPLAY_LIST_PROPERTIES = true;
Chet Haasea1cff502012-02-21 13:43:44 -08001528
1529 /**
Romain Guyd90a3312009-05-06 14:54:28 -07001530 * Map used to store views' tags.
1531 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001532 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001535 * The next available accessiiblity id.
1536 */
1537 private static int sNextAccessibilityViewId;
1538
1539 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 * The animation currently associated with this view.
1541 * @hide
1542 */
1543 protected Animation mCurrentAnimation = null;
1544
1545 /**
1546 * Width as measured during measure pass.
1547 * {@hide}
1548 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001549 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001550 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551
1552 /**
1553 * Height as measured during measure pass.
1554 * {@hide}
1555 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001556 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001557 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558
1559 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001560 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1561 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1562 * its display list. This flag, used only when hw accelerated, allows us to clear the
1563 * flag while retaining this information until it's needed (at getDisplayList() time and
1564 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1565 *
1566 * {@hide}
1567 */
1568 boolean mRecreateDisplayList = false;
1569
1570 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 * The view's identifier.
1572 * {@hide}
1573 *
1574 * @see #setId(int)
1575 * @see #getId()
1576 */
1577 @ViewDebug.ExportedProperty(resolveId = true)
1578 int mID = NO_ID;
1579
1580 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001581 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001582 */
1583 int mAccessibilityViewId = NO_ID;
1584
1585 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 * The view's tag.
1587 * {@hide}
1588 *
1589 * @see #setTag(Object)
1590 * @see #getTag()
1591 */
1592 protected Object mTag;
1593
1594 // for mPrivateFlags:
1595 /** {@hide} */
1596 static final int WANTS_FOCUS = 0x00000001;
1597 /** {@hide} */
1598 static final int FOCUSED = 0x00000002;
1599 /** {@hide} */
1600 static final int SELECTED = 0x00000004;
1601 /** {@hide} */
1602 static final int IS_ROOT_NAMESPACE = 0x00000008;
1603 /** {@hide} */
1604 static final int HAS_BOUNDS = 0x00000010;
1605 /** {@hide} */
1606 static final int DRAWN = 0x00000020;
1607 /**
1608 * When this flag is set, this view is running an animation on behalf of its
1609 * children and should therefore not cancel invalidate requests, even if they
1610 * lie outside of this view's bounds.
1611 *
1612 * {@hide}
1613 */
1614 static final int DRAW_ANIMATION = 0x00000040;
1615 /** {@hide} */
1616 static final int SKIP_DRAW = 0x00000080;
1617 /** {@hide} */
1618 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1619 /** {@hide} */
1620 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1621 /** {@hide} */
1622 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1623 /** {@hide} */
1624 static final int MEASURED_DIMENSION_SET = 0x00000800;
1625 /** {@hide} */
1626 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001627 /** {@hide} */
1628 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629
1630 private static final int PRESSED = 0x00004000;
1631
1632 /** {@hide} */
1633 static final int DRAWING_CACHE_VALID = 0x00008000;
1634 /**
1635 * Flag used to indicate that this view should be drawn once more (and only once
1636 * more) after its animation has completed.
1637 * {@hide}
1638 */
1639 static final int ANIMATION_STARTED = 0x00010000;
1640
1641 private static final int SAVE_STATE_CALLED = 0x00020000;
1642
1643 /**
1644 * Indicates that the View returned true when onSetAlpha() was called and that
1645 * the alpha must be restored.
1646 * {@hide}
1647 */
1648 static final int ALPHA_SET = 0x00040000;
1649
1650 /**
1651 * Set by {@link #setScrollContainer(boolean)}.
1652 */
1653 static final int SCROLL_CONTAINER = 0x00080000;
1654
1655 /**
1656 * Set by {@link #setScrollContainer(boolean)}.
1657 */
1658 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1659
1660 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001661 * View flag indicating whether this view was invalidated (fully or partially.)
1662 *
1663 * @hide
1664 */
1665 static final int DIRTY = 0x00200000;
1666
1667 /**
1668 * View flag indicating whether this view was invalidated by an opaque
1669 * invalidate request.
1670 *
1671 * @hide
1672 */
1673 static final int DIRTY_OPAQUE = 0x00400000;
1674
1675 /**
1676 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1677 *
1678 * @hide
1679 */
1680 static final int DIRTY_MASK = 0x00600000;
1681
1682 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001683 * Indicates whether the background is opaque.
1684 *
1685 * @hide
1686 */
1687 static final int OPAQUE_BACKGROUND = 0x00800000;
1688
1689 /**
1690 * Indicates whether the scrollbars are opaque.
1691 *
1692 * @hide
1693 */
1694 static final int OPAQUE_SCROLLBARS = 0x01000000;
1695
1696 /**
1697 * Indicates whether the view is opaque.
1698 *
1699 * @hide
1700 */
1701 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001702
Adam Powelle14579b2009-12-16 18:39:52 -08001703 /**
1704 * Indicates a prepressed state;
1705 * the short time between ACTION_DOWN and recognizing
1706 * a 'real' press. Prepressed is used to recognize quick taps
1707 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001708 *
Adam Powelle14579b2009-12-16 18:39:52 -08001709 * @hide
1710 */
1711 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001712
Adam Powellc9fbaab2010-02-16 17:16:19 -08001713 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001714 * Indicates whether the view is temporarily detached.
1715 *
1716 * @hide
1717 */
1718 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001719
Adam Powell8568c3a2010-04-19 14:26:11 -07001720 /**
1721 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001722 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001723 * @hide
1724 */
1725 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001726
1727 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001728 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1729 * @hide
1730 */
1731 private static final int HOVERED = 0x10000000;
1732
1733 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001734 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1735 * for transform operations
1736 *
1737 * @hide
1738 */
Adam Powellf37df072010-09-17 16:22:49 -07001739 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001740
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001741 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001742 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001743
Chet Haasefd2b0022010-08-06 13:08:56 -07001744 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001745 * Indicates that this view was specifically invalidated, not just dirtied because some
1746 * child view was invalidated. The flag is used to determine when we need to recreate
1747 * a view's display list (as opposed to just returning a reference to its existing
1748 * display list).
1749 *
1750 * @hide
1751 */
1752 static final int INVALIDATED = 0x80000000;
1753
Christopher Tate3d4bf172011-03-28 16:16:46 -07001754 /* Masks for mPrivateFlags2 */
1755
1756 /**
1757 * Indicates that this view has reported that it can accept the current drag's content.
1758 * Cleared when the drag operation concludes.
1759 * @hide
1760 */
1761 static final int DRAG_CAN_ACCEPT = 0x00000001;
1762
1763 /**
1764 * Indicates that this view is currently directly under the drag location in a
1765 * drag-and-drop operation involving content that it can accept. Cleared when
1766 * the drag exits the view, or when the drag operation concludes.
1767 * @hide
1768 */
1769 static final int DRAG_HOVERED = 0x00000002;
1770
Cibu Johny86666632010-02-22 13:01:02 -08001771 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001772 * Horizontal layout direction of this view is from Left to Right.
1773 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001774 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001775 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001776
1777 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001778 * Horizontal layout direction of this view is from Right to Left.
1779 * Use with {@link #setLayoutDirection}.
1780 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001781 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001782
1783 /**
1784 * Horizontal layout direction of this view is inherited from its parent.
1785 * Use with {@link #setLayoutDirection}.
1786 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001787 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001788
1789 /**
1790 * Horizontal layout direction of this view is from deduced from the default language
1791 * script for the locale. Use with {@link #setLayoutDirection}.
1792 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001793 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001794
1795 /**
1796 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001797 * @hide
1798 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001799 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1800
1801 /**
1802 * Mask for use with private flags indicating bits used for horizontal layout direction.
1803 * @hide
1804 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001805 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001806
1807 /**
1808 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1809 * right-to-left direction.
1810 * @hide
1811 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001812 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001813
1814 /**
1815 * Indicates whether the view horizontal layout direction has been resolved.
1816 * @hide
1817 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001818 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001819
1820 /**
1821 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1822 * @hide
1823 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001824 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001825
1826 /*
1827 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1828 * flag value.
1829 * @hide
1830 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001831 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1832 LAYOUT_DIRECTION_LTR,
1833 LAYOUT_DIRECTION_RTL,
1834 LAYOUT_DIRECTION_INHERIT,
1835 LAYOUT_DIRECTION_LOCALE
1836 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001837
1838 /**
1839 * Default horizontal layout direction.
1840 * @hide
1841 */
1842 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001843
Adam Powell539ee872012-02-03 19:00:49 -08001844 /**
1845 * Indicates that the view is tracking some sort of transient state
1846 * that the app should not need to be aware of, but that the framework
1847 * should take special care to preserve.
1848 *
1849 * @hide
1850 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001851 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001852
1853
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001854 /**
1855 * Text direction is inherited thru {@link ViewGroup}
1856 */
1857 public static final int TEXT_DIRECTION_INHERIT = 0;
1858
1859 /**
1860 * Text direction is using "first strong algorithm". The first strong directional character
1861 * determines the paragraph direction. If there is no strong directional character, the
1862 * paragraph direction is the view's resolved layout direction.
1863 */
1864 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1865
1866 /**
1867 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1868 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1869 * If there are neither, the paragraph direction is the view's resolved layout direction.
1870 */
1871 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1872
1873 /**
1874 * Text direction is forced to LTR.
1875 */
1876 public static final int TEXT_DIRECTION_LTR = 3;
1877
1878 /**
1879 * Text direction is forced to RTL.
1880 */
1881 public static final int TEXT_DIRECTION_RTL = 4;
1882
1883 /**
1884 * Text direction is coming from the system Locale.
1885 */
1886 public static final int TEXT_DIRECTION_LOCALE = 5;
1887
1888 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001889 * Default text direction is inherited
1890 */
1891 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1892
1893 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001894 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1895 * @hide
1896 */
1897 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1898
1899 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001900 * Mask for use with private flags indicating bits used for text direction.
1901 * @hide
1902 */
1903 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1904
1905 /**
1906 * Array of text direction flags for mapping attribute "textDirection" to correct
1907 * flag value.
1908 * @hide
1909 */
1910 private static final int[] TEXT_DIRECTION_FLAGS = {
1911 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1912 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1913 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1914 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1915 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1916 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1917 };
1918
1919 /**
1920 * Indicates whether the view text direction has been resolved.
1921 * @hide
1922 */
1923 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1924
1925 /**
1926 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1927 * @hide
1928 */
1929 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1930
1931 /**
1932 * Mask for use with private flags indicating bits used for resolved text direction.
1933 * @hide
1934 */
1935 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1936
1937 /**
1938 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1939 * @hide
1940 */
1941 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1942 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1943
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001944 /*
1945 * Default text alignment. The text alignment of this View is inherited from its parent.
1946 * Use with {@link #setTextAlignment(int)}
1947 */
1948 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1949
1950 /**
1951 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1952 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1953 *
1954 * Use with {@link #setTextAlignment(int)}
1955 */
1956 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1957
1958 /**
1959 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1960 *
1961 * Use with {@link #setTextAlignment(int)}
1962 */
1963 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1964
1965 /**
1966 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1967 *
1968 * Use with {@link #setTextAlignment(int)}
1969 */
1970 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
1971
1972 /**
1973 * Center the paragraph, e.g. ALIGN_CENTER.
1974 *
1975 * Use with {@link #setTextAlignment(int)}
1976 */
1977 public static final int TEXT_ALIGNMENT_CENTER = 4;
1978
1979 /**
1980 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
1981 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
1982 *
1983 * Use with {@link #setTextAlignment(int)}
1984 */
1985 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
1986
1987 /**
1988 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
1989 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
1990 *
1991 * Use with {@link #setTextAlignment(int)}
1992 */
1993 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
1994
1995 /**
1996 * Default text alignment is inherited
1997 */
1998 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
1999
2000 /**
2001 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2002 * @hide
2003 */
2004 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
2005
2006 /**
2007 * Mask for use with private flags indicating bits used for text alignment.
2008 * @hide
2009 */
2010 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
2011
2012 /**
2013 * Array of text direction flags for mapping attribute "textAlignment" to correct
2014 * flag value.
2015 * @hide
2016 */
2017 private static final int[] TEXT_ALIGNMENT_FLAGS = {
2018 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
2019 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
2020 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
2021 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
2022 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
2023 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
2024 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
2025 };
2026
2027 /**
2028 * Indicates whether the view text alignment has been resolved.
2029 * @hide
2030 */
2031 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
2032
2033 /**
2034 * Bit shift to get the resolved text alignment.
2035 * @hide
2036 */
2037 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
2038
2039 /**
2040 * Mask for use with private flags indicating bits used for text alignment.
2041 * @hide
2042 */
2043 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2044
2045 /**
2046 * Indicates whether if the view text alignment has been resolved to gravity
2047 */
2048 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2049 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2050
Svetoslav Ganov42138042012-03-20 11:51:39 -07002051 // Accessiblity constants for mPrivateFlags2
2052
2053 /**
2054 * Shift for accessibility related bits in {@link #mPrivateFlags2}.
2055 */
2056 static final int IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
2057
2058 /**
2059 * Automatically determine whether a view is important for accessibility.
2060 */
2061 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2062
2063 /**
2064 * The view is important for accessibility.
2065 */
2066 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2067
2068 /**
2069 * The view is not important for accessibility.
2070 */
2071 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2072
2073 /**
2074 * The default whether the view is important for accessiblity.
2075 */
2076 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2077
2078 /**
2079 * Mask for obtainig the bits which specify how to determine
2080 * whether a view is important for accessibility.
2081 */
2082 static final int IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
2083 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
2084 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2085
2086 /**
2087 * Flag indicating whether a view has accessibility focus.
2088 */
2089 static final int ACCESSIBILITY_FOCUSED = 0x00000040 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2090
2091 /**
2092 * Flag indicating whether a view state for accessibility has changed.
2093 */
2094 static final int ACCESSIBILITY_STATE_CHANGED = 0x00000080 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002095
Christopher Tate3d4bf172011-03-28 16:16:46 -07002096 /* End of masks for mPrivateFlags2 */
2097
2098 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2099
Chet Haasedaf98e92011-01-10 14:10:36 -08002100 /**
Adam Powell637d3372010-08-25 14:37:03 -07002101 * Always allow a user to over-scroll this view, provided it is a
2102 * view that can scroll.
2103 *
2104 * @see #getOverScrollMode()
2105 * @see #setOverScrollMode(int)
2106 */
2107 public static final int OVER_SCROLL_ALWAYS = 0;
2108
2109 /**
2110 * Allow a user to over-scroll this view only if the content is large
2111 * enough to meaningfully scroll, provided it is a view that can scroll.
2112 *
2113 * @see #getOverScrollMode()
2114 * @see #setOverScrollMode(int)
2115 */
2116 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2117
2118 /**
2119 * Never allow a user to over-scroll this view.
2120 *
2121 * @see #getOverScrollMode()
2122 * @see #setOverScrollMode(int)
2123 */
2124 public static final int OVER_SCROLL_NEVER = 2;
2125
2126 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002127 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2128 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002129 *
Joe Malin32736f02011-01-19 16:14:20 -08002130 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002131 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002132 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002133
2134 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002135 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2136 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002137 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002138 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002139 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002140 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002141 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002142 *
Joe Malin32736f02011-01-19 16:14:20 -08002143 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002144 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002145 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2146
2147 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002148 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2149 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002150 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002151 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002152 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2153 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2154 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002155 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002156 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2157 * window flags) for displaying content using every last pixel on the display.
2158 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002159 * <p>There is a limitation: because navigation controls are so important, the least user
2160 * interaction will cause them to reappear immediately. When this happens, both
2161 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2162 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002163 *
2164 * @see #setSystemUiVisibility(int)
2165 */
2166 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2167
2168 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002169 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2170 * into the normal fullscreen mode so that its content can take over the screen
2171 * while still allowing the user to interact with the application.
2172 *
2173 * <p>This has the same visual effect as
2174 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2175 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2176 * meaning that non-critical screen decorations (such as the status bar) will be
2177 * hidden while the user is in the View's window, focusing the experience on
2178 * that content. Unlike the window flag, if you are using ActionBar in
2179 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2180 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2181 * hide the action bar.
2182 *
2183 * <p>This approach to going fullscreen is best used over the window flag when
2184 * it is a transient state -- that is, the application does this at certain
2185 * points in its user interaction where it wants to allow the user to focus
2186 * on content, but not as a continuous state. For situations where the application
2187 * would like to simply stay full screen the entire time (such as a game that
2188 * wants to take over the screen), the
2189 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2190 * is usually a better approach. The state set here will be removed by the system
2191 * in various situations (such as the user moving to another application) like
2192 * the other system UI states.
2193 *
2194 * <p>When using this flag, the application should provide some easy facility
2195 * for the user to go out of it. A common example would be in an e-book
2196 * reader, where tapping on the screen brings back whatever screen and UI
2197 * decorations that had been hidden while the user was immersed in reading
2198 * the book.
2199 *
2200 * @see #setSystemUiVisibility(int)
2201 */
2202 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2203
2204 /**
2205 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2206 * flags, we would like a stable view of the content insets given to
2207 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2208 * will always represent the worst case that the application can expect
2209 * as a continue state. In practice this means with any of system bar,
2210 * nav bar, and status bar shown, but not the space that would be needed
2211 * for an input method.
2212 *
2213 * <p>If you are using ActionBar in
2214 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2215 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2216 * insets it adds to those given to the application.
2217 */
2218 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2219
2220 /**
2221 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2222 * to be layed out as if it has requested
2223 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2224 * allows it to avoid artifacts when switching in and out of that mode, at
2225 * the expense that some of its user interface may be covered by screen
2226 * decorations when they are shown. You can perform layout of your inner
2227 * UI elements to account for the navagation system UI through the
2228 * {@link #fitSystemWindows(Rect)} method.
2229 */
2230 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2231
2232 /**
2233 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2234 * to be layed out as if it has requested
2235 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2236 * allows it to avoid artifacts when switching in and out of that mode, at
2237 * the expense that some of its user interface may be covered by screen
2238 * decorations when they are shown. You can perform layout of your inner
2239 * UI elements to account for non-fullscreen system UI through the
2240 * {@link #fitSystemWindows(Rect)} method.
2241 */
2242 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2243
2244 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002245 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2246 */
2247 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2248
2249 /**
2250 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2251 */
2252 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002253
2254 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002255 * @hide
2256 *
2257 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2258 * out of the public fields to keep the undefined bits out of the developer's way.
2259 *
2260 * Flag to make the status bar not expandable. Unless you also
2261 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2262 */
2263 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2264
2265 /**
2266 * @hide
2267 *
2268 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2269 * out of the public fields to keep the undefined bits out of the developer's way.
2270 *
2271 * Flag to hide notification icons and scrolling ticker text.
2272 */
2273 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2274
2275 /**
2276 * @hide
2277 *
2278 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2279 * out of the public fields to keep the undefined bits out of the developer's way.
2280 *
2281 * Flag to disable incoming notification alerts. This will not block
2282 * icons, but it will block sound, vibrating and other visual or aural notifications.
2283 */
2284 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2285
2286 /**
2287 * @hide
2288 *
2289 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2290 * out of the public fields to keep the undefined bits out of the developer's way.
2291 *
2292 * Flag to hide only the scrolling ticker. Note that
2293 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2294 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2295 */
2296 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2297
2298 /**
2299 * @hide
2300 *
2301 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2302 * out of the public fields to keep the undefined bits out of the developer's way.
2303 *
2304 * Flag to hide the center system info area.
2305 */
2306 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2307
2308 /**
2309 * @hide
2310 *
2311 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2312 * out of the public fields to keep the undefined bits out of the developer's way.
2313 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002314 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002315 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2316 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002317 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002318
2319 /**
2320 * @hide
2321 *
2322 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2323 * out of the public fields to keep the undefined bits out of the developer's way.
2324 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002325 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002326 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2327 */
2328 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2329
2330 /**
2331 * @hide
2332 *
2333 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2334 * out of the public fields to keep the undefined bits out of the developer's way.
2335 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002336 * Flag to hide only the clock. You might use this if your activity has
2337 * its own clock making the status bar's clock redundant.
2338 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002339 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2340
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002341 /**
2342 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002343 *
2344 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2345 * out of the public fields to keep the undefined bits out of the developer's way.
2346 *
2347 * Flag to hide only the recent apps button. Don't use this
2348 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2349 */
2350 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2351
2352 /**
2353 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002354 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002355 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002356
2357 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002358 * These are the system UI flags that can be cleared by events outside
2359 * of an application. Currently this is just the ability to tap on the
2360 * screen while hiding the navigation bar to have it return.
2361 * @hide
2362 */
2363 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002364 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2365 | SYSTEM_UI_FLAG_FULLSCREEN;
2366
2367 /**
2368 * Flags that can impact the layout in relation to system UI.
2369 */
2370 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2371 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2372 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002373
2374 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002375 * Find views that render the specified text.
2376 *
2377 * @see #findViewsWithText(ArrayList, CharSequence, int)
2378 */
2379 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2380
2381 /**
2382 * Find find views that contain the specified content description.
2383 *
2384 * @see #findViewsWithText(ArrayList, CharSequence, int)
2385 */
2386 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2387
2388 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002389 * Find views that contain {@link AccessibilityNodeProvider}. Such
2390 * a View is a root of virtual view hierarchy and may contain the searched
2391 * text. If this flag is set Views with providers are automatically
2392 * added and it is a responsibility of the client to call the APIs of
2393 * the provider to determine whether the virtual tree rooted at this View
2394 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2395 * represeting the virtual views with this text.
2396 *
2397 * @see #findViewsWithText(ArrayList, CharSequence, int)
2398 *
2399 * @hide
2400 */
2401 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2402
2403 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002404 * Indicates that the screen has changed state and is now off.
2405 *
2406 * @see #onScreenStateChanged(int)
2407 */
2408 public static final int SCREEN_STATE_OFF = 0x0;
2409
2410 /**
2411 * Indicates that the screen has changed state and is now on.
2412 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002413 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002414 */
2415 public static final int SCREEN_STATE_ON = 0x1;
2416
2417 /**
Adam Powell637d3372010-08-25 14:37:03 -07002418 * Controls the over-scroll mode for this view.
2419 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2420 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2421 * and {@link #OVER_SCROLL_NEVER}.
2422 */
2423 private int mOverScrollMode;
2424
2425 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 * The parent this view is attached to.
2427 * {@hide}
2428 *
2429 * @see #getParent()
2430 */
2431 protected ViewParent mParent;
2432
2433 /**
2434 * {@hide}
2435 */
2436 AttachInfo mAttachInfo;
2437
2438 /**
2439 * {@hide}
2440 */
Romain Guy809a7f62009-05-14 15:44:42 -07002441 @ViewDebug.ExportedProperty(flagMapping = {
2442 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2443 name = "FORCE_LAYOUT"),
2444 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2445 name = "LAYOUT_REQUIRED"),
2446 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002447 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002448 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2449 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2450 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2451 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2452 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002454 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455
2456 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002457 * This view's request for the visibility of the status bar.
2458 * @hide
2459 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002460 @ViewDebug.ExportedProperty(flagMapping = {
2461 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2462 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2463 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2464 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2465 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2466 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2467 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2468 equals = SYSTEM_UI_FLAG_VISIBLE,
2469 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2470 })
Joe Onorato664644d2011-01-23 17:53:23 -08002471 int mSystemUiVisibility;
2472
2473 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 * Count of how many windows this view has been attached to.
2475 */
2476 int mWindowAttachCount;
2477
2478 /**
2479 * The layout parameters associated with this view and used by the parent
2480 * {@link android.view.ViewGroup} to determine how this view should be
2481 * laid out.
2482 * {@hide}
2483 */
2484 protected ViewGroup.LayoutParams mLayoutParams;
2485
2486 /**
2487 * The view flags hold various views states.
2488 * {@hide}
2489 */
2490 @ViewDebug.ExportedProperty
2491 int mViewFlags;
2492
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002493 static class TransformationInfo {
2494 /**
2495 * The transform matrix for the View. This transform is calculated internally
2496 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2497 * is used by default. Do *not* use this variable directly; instead call
2498 * getMatrix(), which will automatically recalculate the matrix if necessary
2499 * to get the correct matrix based on the latest rotation and scale properties.
2500 */
2501 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002502
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002503 /**
2504 * The transform matrix for the View. This transform is calculated internally
2505 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2506 * is used by default. Do *not* use this variable directly; instead call
2507 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2508 * to get the correct matrix based on the latest rotation and scale properties.
2509 */
2510 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002511
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002512 /**
2513 * An internal variable that tracks whether we need to recalculate the
2514 * transform matrix, based on whether the rotation or scaleX/Y properties
2515 * have changed since the matrix was last calculated.
2516 */
2517 boolean mMatrixDirty = false;
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 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002525
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002526 /**
2527 * A 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. This variable
2530 * is only valid after a call to updateMatrix() or to a function that
2531 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2532 */
2533 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002534
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002535 /**
2536 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2537 */
2538 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002539
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002540 /**
2541 * This matrix is used when computing the matrix for 3D rotations.
2542 */
2543 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002544
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002545 /**
2546 * These prev values are used to recalculate a centered pivot point when necessary. The
2547 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2548 * set), so thes values are only used then as well.
2549 */
2550 private int mPrevWidth = -1;
2551 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002552
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002553 /**
2554 * The degrees rotation around the vertical axis through the pivot point.
2555 */
2556 @ViewDebug.ExportedProperty
2557 float mRotationY = 0f;
2558
2559 /**
2560 * The degrees rotation around the horizontal axis through the pivot point.
2561 */
2562 @ViewDebug.ExportedProperty
2563 float mRotationX = 0f;
2564
2565 /**
2566 * The degrees rotation around the pivot point.
2567 */
2568 @ViewDebug.ExportedProperty
2569 float mRotation = 0f;
2570
2571 /**
2572 * The amount of translation of the object away from its left property (post-layout).
2573 */
2574 @ViewDebug.ExportedProperty
2575 float mTranslationX = 0f;
2576
2577 /**
2578 * The amount of translation of the object away from its top property (post-layout).
2579 */
2580 @ViewDebug.ExportedProperty
2581 float mTranslationY = 0f;
2582
2583 /**
2584 * The amount of scale in the x direction around the pivot point. A
2585 * value of 1 means no scaling is applied.
2586 */
2587 @ViewDebug.ExportedProperty
2588 float mScaleX = 1f;
2589
2590 /**
2591 * The amount of scale in the y direction around the pivot point. A
2592 * value of 1 means no scaling is applied.
2593 */
2594 @ViewDebug.ExportedProperty
2595 float mScaleY = 1f;
2596
2597 /**
Chet Haasea33de552012-02-03 16:28:24 -08002598 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002599 */
2600 @ViewDebug.ExportedProperty
2601 float mPivotX = 0f;
2602
2603 /**
Chet Haasea33de552012-02-03 16:28:24 -08002604 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002605 */
2606 @ViewDebug.ExportedProperty
2607 float mPivotY = 0f;
2608
2609 /**
2610 * The opacity of the View. This is a value from 0 to 1, where 0 means
2611 * completely transparent and 1 means completely opaque.
2612 */
2613 @ViewDebug.ExportedProperty
2614 float mAlpha = 1f;
2615 }
2616
2617 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002618
Joe Malin32736f02011-01-19 16:14:20 -08002619 private boolean mLastIsOpaque;
2620
Chet Haasefd2b0022010-08-06 13:08:56 -07002621 /**
2622 * Convenience value to check for float values that are close enough to zero to be considered
2623 * zero.
2624 */
Romain Guy2542d192010-08-18 11:47:12 -07002625 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002626
2627 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 * The distance in pixels from the left edge of this view's parent
2629 * to the left edge of this view.
2630 * {@hide}
2631 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002632 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002633 protected int mLeft;
2634 /**
2635 * The distance in pixels from the left edge of this view's parent
2636 * to the right 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 mRight;
2641 /**
2642 * The distance in pixels from the top edge of this view's parent
2643 * to the top 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 mTop;
2648 /**
2649 * The distance in pixels from the top edge of this view's parent
2650 * to the bottom 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 mBottom;
2655
2656 /**
2657 * The offset, in pixels, by which the content of this view is scrolled
2658 * horizontally.
2659 * {@hide}
2660 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002661 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 protected int mScrollX;
2663 /**
2664 * The offset, in pixels, by which the content of this view is scrolled
2665 * vertically.
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 mScrollY;
2670
2671 /**
2672 * The left padding in pixels, that is the distance in pixels between the
2673 * left edge of this view and the left edge of its content.
2674 * {@hide}
2675 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002676 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 protected int mPaddingLeft;
2678 /**
2679 * The right padding in pixels, that is the distance in pixels between the
2680 * right edge of this view and the right 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 mPaddingRight;
2685 /**
2686 * The top padding in pixels, that is the distance in pixels between the
2687 * top edge of this view and the top 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 mPaddingTop;
2692 /**
2693 * The bottom padding in pixels, that is the distance in pixels between the
2694 * bottom edge of this view and the bottom 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 mPaddingBottom;
2699
2700 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002701 * Briefly describes the view and is primarily used for accessibility support.
2702 */
2703 private CharSequence mContentDescription;
2704
2705 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002707 *
2708 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002710 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002711 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712
2713 /**
2714 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002715 *
2716 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002718 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002719 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002721 /**
Adam Powell20232d02010-12-08 21:08:53 -08002722 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002723 *
2724 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002725 */
2726 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002727 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002728
2729 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002730 * Cache if the user padding is relative.
2731 *
2732 */
2733 @ViewDebug.ExportedProperty(category = "padding")
2734 boolean mUserPaddingRelative;
2735
2736 /**
2737 * Cache the paddingStart set by the user to append to the scrollbar's size.
2738 *
2739 */
2740 @ViewDebug.ExportedProperty(category = "padding")
2741 int mUserPaddingStart;
2742
2743 /**
2744 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2745 *
2746 */
2747 @ViewDebug.ExportedProperty(category = "padding")
2748 int mUserPaddingEnd;
2749
2750 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002751 * @hide
2752 */
2753 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2754 /**
2755 * @hide
2756 */
2757 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758
Philip Milne6c8ea062012-04-03 17:38:43 -07002759 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760
2761 private int mBackgroundResource;
2762 private boolean mBackgroundSizeChanged;
2763
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002764 static class ListenerInfo {
2765 /**
2766 * Listener used to dispatch focus change events.
2767 * This field should be made private, so it is hidden from the SDK.
2768 * {@hide}
2769 */
2770 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002772 /**
2773 * Listeners for layout change events.
2774 */
2775 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002776
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002777 /**
2778 * Listeners for attach events.
2779 */
2780 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002781
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002782 /**
2783 * Listener used to dispatch click events.
2784 * This field should be made private, so it is hidden from the SDK.
2785 * {@hide}
2786 */
2787 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002789 /**
2790 * Listener used to dispatch long click events.
2791 * This field should be made private, so it is hidden from the SDK.
2792 * {@hide}
2793 */
2794 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002796 /**
2797 * Listener used to build the context menu.
2798 * This field should be made private, so it is hidden from the SDK.
2799 * {@hide}
2800 */
2801 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002803 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002805 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002807 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002808
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002809 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002810
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002811 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002812
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002813 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2814 }
2815
2816 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 /**
2819 * The application environment this view lives in.
2820 * This field should be made private, so it is hidden from the SDK.
2821 * {@hide}
2822 */
2823 protected Context mContext;
2824
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002825 private final Resources mResources;
2826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 private ScrollabilityCache mScrollCache;
2828
2829 private int[] mDrawableState = null;
2830
Romain Guy0211a0a2011-02-14 16:34:59 -08002831 /**
2832 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002833 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002834 * @hide
2835 */
2836 public boolean mCachingFailed;
2837
Romain Guy02890fd2010-08-06 17:58:44 -07002838 private Bitmap mDrawingCache;
2839 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002840 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002841 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842
2843 /**
2844 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2845 * the user may specify which view to go to next.
2846 */
2847 private int mNextFocusLeftId = View.NO_ID;
2848
2849 /**
2850 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2851 * the user may specify which view to go to next.
2852 */
2853 private int mNextFocusRightId = View.NO_ID;
2854
2855 /**
2856 * When this view has focus and the next focus is {@link #FOCUS_UP},
2857 * the user may specify which view to go to next.
2858 */
2859 private int mNextFocusUpId = View.NO_ID;
2860
2861 /**
2862 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2863 * the user may specify which view to go to next.
2864 */
2865 private int mNextFocusDownId = View.NO_ID;
2866
Jeff Brown4e6319b2010-12-13 10:36:51 -08002867 /**
2868 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2869 * the user may specify which view to go to next.
2870 */
2871 int mNextFocusForwardId = View.NO_ID;
2872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002874 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002875 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002876 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 private UnsetPressedState mUnsetPressedState;
2879
2880 /**
2881 * Whether the long press's action has been invoked. The tap's action is invoked on the
2882 * up event while a long press is invoked as soon as the long press duration is reached, so
2883 * a long press could be performed before the tap is checked, in which case the tap's action
2884 * should not be invoked.
2885 */
2886 private boolean mHasPerformedLongPress;
2887
2888 /**
2889 * The minimum height of the view. We'll try our best to have the height
2890 * of this view to at least this amount.
2891 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002892 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 private int mMinHeight;
2894
2895 /**
2896 * The minimum width of the view. We'll try our best to have the width
2897 * of this view to at least this amount.
2898 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002899 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 private int mMinWidth;
2901
2902 /**
2903 * The delegate to handle touch events that are physically in this view
2904 * but should be handled by another view.
2905 */
2906 private TouchDelegate mTouchDelegate = null;
2907
2908 /**
2909 * Solid color to use as a background when creating the drawing cache. Enables
2910 * the cache to use 16 bit bitmaps instead of 32 bit.
2911 */
2912 private int mDrawingCacheBackgroundColor = 0;
2913
2914 /**
2915 * Special tree observer used when mAttachInfo is null.
2916 */
2917 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002918
Adam Powelle14579b2009-12-16 18:39:52 -08002919 /**
2920 * Cache the touch slop from the context that created the view.
2921 */
2922 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002925 * Object that handles automatic animation of view properties.
2926 */
2927 private ViewPropertyAnimator mAnimator = null;
2928
2929 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002930 * Flag indicating that a drag can cross window boundaries. When
2931 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2932 * with this flag set, all visible applications will be able to participate
2933 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002934 *
2935 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002936 */
2937 public static final int DRAG_FLAG_GLOBAL = 1;
2938
2939 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002940 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2941 */
2942 private float mVerticalScrollFactor;
2943
2944 /**
Adam Powell20232d02010-12-08 21:08:53 -08002945 * Position of the vertical scroll bar.
2946 */
2947 private int mVerticalScrollbarPosition;
2948
2949 /**
2950 * Position the scroll bar at the default position as determined by the system.
2951 */
2952 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2953
2954 /**
2955 * Position the scroll bar along the left edge.
2956 */
2957 public static final int SCROLLBAR_POSITION_LEFT = 1;
2958
2959 /**
2960 * Position the scroll bar along the right edge.
2961 */
2962 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2963
2964 /**
Romain Guy171c5922011-01-06 10:04:23 -08002965 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002966 *
2967 * @see #getLayerType()
2968 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002969 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002970 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002971 */
2972 public static final int LAYER_TYPE_NONE = 0;
2973
2974 /**
2975 * <p>Indicates that the view has a software layer. A software layer is backed
2976 * by a bitmap and causes the view to be rendered using Android's software
2977 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002978 *
Romain Guy171c5922011-01-06 10:04:23 -08002979 * <p>Software layers have various usages:</p>
2980 * <p>When the application is not using hardware acceleration, a software layer
2981 * is useful to apply a specific color filter and/or blending mode and/or
2982 * translucency to a view and all its children.</p>
2983 * <p>When the application is using hardware acceleration, a software layer
2984 * is useful to render drawing primitives not supported by the hardware
2985 * accelerated pipeline. It can also be used to cache a complex view tree
2986 * into a texture and reduce the complexity of drawing operations. For instance,
2987 * when animating a complex view tree with a translation, a software layer can
2988 * be used to render the view tree only once.</p>
2989 * <p>Software layers should be avoided when the affected view tree updates
2990 * often. Every update will require to re-render the software layer, which can
2991 * potentially be slow (particularly when hardware acceleration is turned on
2992 * since the layer will have to be uploaded into a hardware texture after every
2993 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002994 *
2995 * @see #getLayerType()
2996 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002997 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002998 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002999 */
3000 public static final int LAYER_TYPE_SOFTWARE = 1;
3001
3002 /**
3003 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3004 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3005 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3006 * rendering pipeline, but only if hardware acceleration is turned on for the
3007 * view hierarchy. When hardware acceleration is turned off, hardware layers
3008 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003009 *
Romain Guy171c5922011-01-06 10:04:23 -08003010 * <p>A hardware layer is useful to apply a specific color filter and/or
3011 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003012 * <p>A hardware layer can be used to cache a complex view tree into a
3013 * texture and reduce the complexity of drawing operations. For instance,
3014 * when animating a complex view tree with a translation, a hardware layer can
3015 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003016 * <p>A hardware layer can also be used to increase the rendering quality when
3017 * rotation transformations are applied on a view. It can also be used to
3018 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003019 *
3020 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003021 * @see #setLayerType(int, android.graphics.Paint)
3022 * @see #LAYER_TYPE_NONE
3023 * @see #LAYER_TYPE_SOFTWARE
3024 */
3025 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003026
Romain Guy3aaff3a2011-01-12 14:18:47 -08003027 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3028 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3029 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3030 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3031 })
Romain Guy171c5922011-01-06 10:04:23 -08003032 int mLayerType = LAYER_TYPE_NONE;
3033 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003034 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003035
3036 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003037 * Set to true when the view is sending hover accessibility events because it
3038 * is the innermost hovered view.
3039 */
3040 private boolean mSendingHoverAccessibilityEvents;
3041
3042 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003043 * Simple constructor to use when creating a view from code.
3044 *
3045 * @param context The Context the view is running in, through which it can
3046 * access the current theme, resources, etc.
3047 */
3048 public View(Context context) {
3049 mContext = context;
3050 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003051 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003052 // Set layout and text direction defaults
3053 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003054 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003055 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
3056 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003057 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003058 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003059 mUserPaddingStart = -1;
3060 mUserPaddingEnd = -1;
3061 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062 }
3063
3064 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003065 * Delegate for injecting accessiblity functionality.
3066 */
3067 AccessibilityDelegate mAccessibilityDelegate;
3068
3069 /**
3070 * Consistency verifier for debugging purposes.
3071 * @hide
3072 */
3073 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3074 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3075 new InputEventConsistencyVerifier(this, 0) : null;
3076
3077 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078 * Constructor that is called when inflating a view from XML. This is called
3079 * when a view is being constructed from an XML file, supplying attributes
3080 * that were specified in the XML file. This version uses a default style of
3081 * 0, so the only attribute values applied are those in the Context's Theme
3082 * and the given AttributeSet.
3083 *
3084 * <p>
3085 * The method onFinishInflate() will be called after all children have been
3086 * added.
3087 *
3088 * @param context The Context the view is running in, through which it can
3089 * access the current theme, resources, etc.
3090 * @param attrs The attributes of the XML tag that is inflating the view.
3091 * @see #View(Context, AttributeSet, int)
3092 */
3093 public View(Context context, AttributeSet attrs) {
3094 this(context, attrs, 0);
3095 }
3096
3097 /**
3098 * Perform inflation from XML and apply a class-specific base style. This
3099 * constructor of View allows subclasses to use their own base style when
3100 * they are inflating. For example, a Button class's constructor would call
3101 * this version of the super class constructor and supply
3102 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3103 * the theme's button style to modify all of the base view attributes (in
3104 * particular its background) as well as the Button class's attributes.
3105 *
3106 * @param context The Context the view is running in, through which it can
3107 * access the current theme, resources, etc.
3108 * @param attrs The attributes of the XML tag that is inflating the view.
3109 * @param defStyle The default style to apply to this view. If 0, no style
3110 * will be applied (beyond what is included in the theme). This may
3111 * either be an attribute resource, whose value will be retrieved
3112 * from the current theme, or an explicit style resource.
3113 * @see #View(Context, AttributeSet)
3114 */
3115 public View(Context context, AttributeSet attrs, int defStyle) {
3116 this(context);
3117
3118 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3119 defStyle, 0);
3120
3121 Drawable background = null;
3122
3123 int leftPadding = -1;
3124 int topPadding = -1;
3125 int rightPadding = -1;
3126 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003127 int startPadding = -1;
3128 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003129
3130 int padding = -1;
3131
3132 int viewFlagValues = 0;
3133 int viewFlagMasks = 0;
3134
3135 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 int x = 0;
3138 int y = 0;
3139
Chet Haase73066682010-11-29 15:55:32 -08003140 float tx = 0;
3141 float ty = 0;
3142 float rotation = 0;
3143 float rotationX = 0;
3144 float rotationY = 0;
3145 float sx = 1f;
3146 float sy = 1f;
3147 boolean transformSet = false;
3148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3150
Adam Powell637d3372010-08-25 14:37:03 -07003151 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003152 final int N = a.getIndexCount();
3153 for (int i = 0; i < N; i++) {
3154 int attr = a.getIndex(i);
3155 switch (attr) {
3156 case com.android.internal.R.styleable.View_background:
3157 background = a.getDrawable(attr);
3158 break;
3159 case com.android.internal.R.styleable.View_padding:
3160 padding = a.getDimensionPixelSize(attr, -1);
3161 break;
3162 case com.android.internal.R.styleable.View_paddingLeft:
3163 leftPadding = a.getDimensionPixelSize(attr, -1);
3164 break;
3165 case com.android.internal.R.styleable.View_paddingTop:
3166 topPadding = a.getDimensionPixelSize(attr, -1);
3167 break;
3168 case com.android.internal.R.styleable.View_paddingRight:
3169 rightPadding = a.getDimensionPixelSize(attr, -1);
3170 break;
3171 case com.android.internal.R.styleable.View_paddingBottom:
3172 bottomPadding = a.getDimensionPixelSize(attr, -1);
3173 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003174 case com.android.internal.R.styleable.View_paddingStart:
3175 startPadding = a.getDimensionPixelSize(attr, -1);
3176 break;
3177 case com.android.internal.R.styleable.View_paddingEnd:
3178 endPadding = a.getDimensionPixelSize(attr, -1);
3179 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 case com.android.internal.R.styleable.View_scrollX:
3181 x = a.getDimensionPixelOffset(attr, 0);
3182 break;
3183 case com.android.internal.R.styleable.View_scrollY:
3184 y = a.getDimensionPixelOffset(attr, 0);
3185 break;
Chet Haase73066682010-11-29 15:55:32 -08003186 case com.android.internal.R.styleable.View_alpha:
3187 setAlpha(a.getFloat(attr, 1f));
3188 break;
3189 case com.android.internal.R.styleable.View_transformPivotX:
3190 setPivotX(a.getDimensionPixelOffset(attr, 0));
3191 break;
3192 case com.android.internal.R.styleable.View_transformPivotY:
3193 setPivotY(a.getDimensionPixelOffset(attr, 0));
3194 break;
3195 case com.android.internal.R.styleable.View_translationX:
3196 tx = a.getDimensionPixelOffset(attr, 0);
3197 transformSet = true;
3198 break;
3199 case com.android.internal.R.styleable.View_translationY:
3200 ty = a.getDimensionPixelOffset(attr, 0);
3201 transformSet = true;
3202 break;
3203 case com.android.internal.R.styleable.View_rotation:
3204 rotation = a.getFloat(attr, 0);
3205 transformSet = true;
3206 break;
3207 case com.android.internal.R.styleable.View_rotationX:
3208 rotationX = a.getFloat(attr, 0);
3209 transformSet = true;
3210 break;
3211 case com.android.internal.R.styleable.View_rotationY:
3212 rotationY = a.getFloat(attr, 0);
3213 transformSet = true;
3214 break;
3215 case com.android.internal.R.styleable.View_scaleX:
3216 sx = a.getFloat(attr, 1f);
3217 transformSet = true;
3218 break;
3219 case com.android.internal.R.styleable.View_scaleY:
3220 sy = a.getFloat(attr, 1f);
3221 transformSet = true;
3222 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 case com.android.internal.R.styleable.View_id:
3224 mID = a.getResourceId(attr, NO_ID);
3225 break;
3226 case com.android.internal.R.styleable.View_tag:
3227 mTag = a.getText(attr);
3228 break;
3229 case com.android.internal.R.styleable.View_fitsSystemWindows:
3230 if (a.getBoolean(attr, false)) {
3231 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3232 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3233 }
3234 break;
3235 case com.android.internal.R.styleable.View_focusable:
3236 if (a.getBoolean(attr, false)) {
3237 viewFlagValues |= FOCUSABLE;
3238 viewFlagMasks |= FOCUSABLE_MASK;
3239 }
3240 break;
3241 case com.android.internal.R.styleable.View_focusableInTouchMode:
3242 if (a.getBoolean(attr, false)) {
3243 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3244 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3245 }
3246 break;
3247 case com.android.internal.R.styleable.View_clickable:
3248 if (a.getBoolean(attr, false)) {
3249 viewFlagValues |= CLICKABLE;
3250 viewFlagMasks |= CLICKABLE;
3251 }
3252 break;
3253 case com.android.internal.R.styleable.View_longClickable:
3254 if (a.getBoolean(attr, false)) {
3255 viewFlagValues |= LONG_CLICKABLE;
3256 viewFlagMasks |= LONG_CLICKABLE;
3257 }
3258 break;
3259 case com.android.internal.R.styleable.View_saveEnabled:
3260 if (!a.getBoolean(attr, true)) {
3261 viewFlagValues |= SAVE_DISABLED;
3262 viewFlagMasks |= SAVE_DISABLED_MASK;
3263 }
3264 break;
3265 case com.android.internal.R.styleable.View_duplicateParentState:
3266 if (a.getBoolean(attr, false)) {
3267 viewFlagValues |= DUPLICATE_PARENT_STATE;
3268 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3269 }
3270 break;
3271 case com.android.internal.R.styleable.View_visibility:
3272 final int visibility = a.getInt(attr, 0);
3273 if (visibility != 0) {
3274 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3275 viewFlagMasks |= VISIBILITY_MASK;
3276 }
3277 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003278 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003279 // Clear any layout direction flags (included resolved bits) already set
3280 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3281 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003282 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003283 final int value = (layoutDirection != -1) ?
3284 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3285 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003286 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287 case com.android.internal.R.styleable.View_drawingCacheQuality:
3288 final int cacheQuality = a.getInt(attr, 0);
3289 if (cacheQuality != 0) {
3290 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3291 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3292 }
3293 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003294 case com.android.internal.R.styleable.View_contentDescription:
3295 mContentDescription = a.getString(attr);
3296 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3298 if (!a.getBoolean(attr, true)) {
3299 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3300 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3301 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003302 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3304 if (!a.getBoolean(attr, true)) {
3305 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3306 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3307 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003308 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 case R.styleable.View_scrollbars:
3310 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3311 if (scrollbars != SCROLLBARS_NONE) {
3312 viewFlagValues |= scrollbars;
3313 viewFlagMasks |= SCROLLBARS_MASK;
3314 initializeScrollbars(a);
3315 }
3316 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003317 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003319 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3320 // Ignore the attribute starting with ICS
3321 break;
3322 }
3323 // With builds < ICS, fall through and apply fading edges
3324 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3326 if (fadingEdge != FADING_EDGE_NONE) {
3327 viewFlagValues |= fadingEdge;
3328 viewFlagMasks |= FADING_EDGE_MASK;
3329 initializeFadingEdge(a);
3330 }
3331 break;
3332 case R.styleable.View_scrollbarStyle:
3333 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3334 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3335 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3336 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3337 }
3338 break;
3339 case R.styleable.View_isScrollContainer:
3340 setScrollContainer = true;
3341 if (a.getBoolean(attr, false)) {
3342 setScrollContainer(true);
3343 }
3344 break;
3345 case com.android.internal.R.styleable.View_keepScreenOn:
3346 if (a.getBoolean(attr, false)) {
3347 viewFlagValues |= KEEP_SCREEN_ON;
3348 viewFlagMasks |= KEEP_SCREEN_ON;
3349 }
3350 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003351 case R.styleable.View_filterTouchesWhenObscured:
3352 if (a.getBoolean(attr, false)) {
3353 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3354 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3355 }
3356 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 case R.styleable.View_nextFocusLeft:
3358 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3359 break;
3360 case R.styleable.View_nextFocusRight:
3361 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3362 break;
3363 case R.styleable.View_nextFocusUp:
3364 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3365 break;
3366 case R.styleable.View_nextFocusDown:
3367 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3368 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003369 case R.styleable.View_nextFocusForward:
3370 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3371 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 case R.styleable.View_minWidth:
3373 mMinWidth = a.getDimensionPixelSize(attr, 0);
3374 break;
3375 case R.styleable.View_minHeight:
3376 mMinHeight = a.getDimensionPixelSize(attr, 0);
3377 break;
Romain Guy9a817362009-05-01 10:57:14 -07003378 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003379 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003380 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003381 + "be used within a restricted context");
3382 }
3383
Romain Guy9a817362009-05-01 10:57:14 -07003384 final String handlerName = a.getString(attr);
3385 if (handlerName != null) {
3386 setOnClickListener(new OnClickListener() {
3387 private Method mHandler;
3388
3389 public void onClick(View v) {
3390 if (mHandler == null) {
3391 try {
3392 mHandler = getContext().getClass().getMethod(handlerName,
3393 View.class);
3394 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003395 int id = getId();
3396 String idText = id == NO_ID ? "" : " with id '"
3397 + getContext().getResources().getResourceEntryName(
3398 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003399 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003400 handlerName + "(View) in the activity "
3401 + getContext().getClass() + " for onClick handler"
3402 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003403 }
3404 }
3405
3406 try {
3407 mHandler.invoke(getContext(), View.this);
3408 } catch (IllegalAccessException e) {
3409 throw new IllegalStateException("Could not execute non "
3410 + "public method of the activity", e);
3411 } catch (InvocationTargetException e) {
3412 throw new IllegalStateException("Could not execute "
3413 + "method of the activity", e);
3414 }
3415 }
3416 });
3417 }
3418 break;
Adam Powell637d3372010-08-25 14:37:03 -07003419 case R.styleable.View_overScrollMode:
3420 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3421 break;
Adam Powell20232d02010-12-08 21:08:53 -08003422 case R.styleable.View_verticalScrollbarPosition:
3423 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3424 break;
Romain Guy171c5922011-01-06 10:04:23 -08003425 case R.styleable.View_layerType:
3426 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3427 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003428 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003429 // Clear any text direction flag already set
3430 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3431 // Set the text direction flags depending on the value of the attribute
3432 final int textDirection = a.getInt(attr, -1);
3433 if (textDirection != -1) {
3434 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3435 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003436 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003437 case R.styleable.View_textAlignment:
3438 // Clear any text alignment flag already set
3439 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3440 // Set the text alignment flag depending on the value of the attribute
3441 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3442 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3443 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003444 case R.styleable.View_importantForAccessibility:
3445 setImportantForAccessibility(a.getInt(attr,
3446 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003447 }
3448 }
3449
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003450 a.recycle();
3451
Adam Powell637d3372010-08-25 14:37:03 -07003452 setOverScrollMode(overScrollMode);
3453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003454 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003455 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 }
3457
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003458 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3459 // layout direction). Those cached values will be used later during padding resolution.
3460 mUserPaddingStart = startPadding;
3461 mUserPaddingEnd = endPadding;
3462
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003463 updateUserPaddingRelative();
3464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465 if (padding >= 0) {
3466 leftPadding = padding;
3467 topPadding = padding;
3468 rightPadding = padding;
3469 bottomPadding = padding;
3470 }
3471
3472 // If the user specified the padding (either with android:padding or
3473 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3474 // use the default padding or the padding from the background drawable
3475 // (stored at this point in mPadding*)
3476 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3477 topPadding >= 0 ? topPadding : mPaddingTop,
3478 rightPadding >= 0 ? rightPadding : mPaddingRight,
3479 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3480
3481 if (viewFlagMasks != 0) {
3482 setFlags(viewFlagValues, viewFlagMasks);
3483 }
3484
3485 // Needs to be called after mViewFlags is set
3486 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3487 recomputePadding();
3488 }
3489
3490 if (x != 0 || y != 0) {
3491 scrollTo(x, y);
3492 }
3493
Chet Haase73066682010-11-29 15:55:32 -08003494 if (transformSet) {
3495 setTranslationX(tx);
3496 setTranslationY(ty);
3497 setRotation(rotation);
3498 setRotationX(rotationX);
3499 setRotationY(rotationY);
3500 setScaleX(sx);
3501 setScaleY(sy);
3502 }
3503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3505 setScrollContainer(true);
3506 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003507
3508 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003509 }
3510
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003511 private void updateUserPaddingRelative() {
3512 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3513 }
3514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515 /**
3516 * Non-public constructor for use in testing
3517 */
3518 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003519 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003520 }
3521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003522 /**
3523 * <p>
3524 * Initializes the fading edges from a given set of styled attributes. This
3525 * method should be called by subclasses that need fading edges and when an
3526 * instance of these subclasses is created programmatically rather than
3527 * being inflated from XML. This method is automatically called when the XML
3528 * is inflated.
3529 * </p>
3530 *
3531 * @param a the styled attributes set to initialize the fading edges from
3532 */
3533 protected void initializeFadingEdge(TypedArray a) {
3534 initScrollCache();
3535
3536 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3537 R.styleable.View_fadingEdgeLength,
3538 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3539 }
3540
3541 /**
3542 * Returns the size of the vertical faded edges used to indicate that more
3543 * content in this view is visible.
3544 *
3545 * @return The size in pixels of the vertical faded edge or 0 if vertical
3546 * faded edges are not enabled for this view.
3547 * @attr ref android.R.styleable#View_fadingEdgeLength
3548 */
3549 public int getVerticalFadingEdgeLength() {
3550 if (isVerticalFadingEdgeEnabled()) {
3551 ScrollabilityCache cache = mScrollCache;
3552 if (cache != null) {
3553 return cache.fadingEdgeLength;
3554 }
3555 }
3556 return 0;
3557 }
3558
3559 /**
3560 * Set the size of the faded edge used to indicate that more content in this
3561 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003562 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3563 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3564 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003565 *
3566 * @param length The size in pixels of the faded edge used to indicate that more
3567 * content in this view is visible.
3568 */
3569 public void setFadingEdgeLength(int length) {
3570 initScrollCache();
3571 mScrollCache.fadingEdgeLength = length;
3572 }
3573
3574 /**
3575 * Returns the size of the horizontal faded edges used to indicate that more
3576 * content in this view is visible.
3577 *
3578 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3579 * faded edges are not enabled for this view.
3580 * @attr ref android.R.styleable#View_fadingEdgeLength
3581 */
3582 public int getHorizontalFadingEdgeLength() {
3583 if (isHorizontalFadingEdgeEnabled()) {
3584 ScrollabilityCache cache = mScrollCache;
3585 if (cache != null) {
3586 return cache.fadingEdgeLength;
3587 }
3588 }
3589 return 0;
3590 }
3591
3592 /**
3593 * Returns the width of the vertical scrollbar.
3594 *
3595 * @return The width in pixels of the vertical scrollbar or 0 if there
3596 * is no vertical scrollbar.
3597 */
3598 public int getVerticalScrollbarWidth() {
3599 ScrollabilityCache cache = mScrollCache;
3600 if (cache != null) {
3601 ScrollBarDrawable scrollBar = cache.scrollBar;
3602 if (scrollBar != null) {
3603 int size = scrollBar.getSize(true);
3604 if (size <= 0) {
3605 size = cache.scrollBarSize;
3606 }
3607 return size;
3608 }
3609 return 0;
3610 }
3611 return 0;
3612 }
3613
3614 /**
3615 * Returns the height of the horizontal scrollbar.
3616 *
3617 * @return The height in pixels of the horizontal scrollbar or 0 if
3618 * there is no horizontal scrollbar.
3619 */
3620 protected int getHorizontalScrollbarHeight() {
3621 ScrollabilityCache cache = mScrollCache;
3622 if (cache != null) {
3623 ScrollBarDrawable scrollBar = cache.scrollBar;
3624 if (scrollBar != null) {
3625 int size = scrollBar.getSize(false);
3626 if (size <= 0) {
3627 size = cache.scrollBarSize;
3628 }
3629 return size;
3630 }
3631 return 0;
3632 }
3633 return 0;
3634 }
3635
3636 /**
3637 * <p>
3638 * Initializes the scrollbars from a given set of styled attributes. This
3639 * method should be called by subclasses that need scrollbars and when an
3640 * instance of these subclasses is created programmatically rather than
3641 * being inflated from XML. This method is automatically called when the XML
3642 * is inflated.
3643 * </p>
3644 *
3645 * @param a the styled attributes set to initialize the scrollbars from
3646 */
3647 protected void initializeScrollbars(TypedArray a) {
3648 initScrollCache();
3649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003650 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003651
Mike Cleronf116bf82009-09-27 19:14:12 -07003652 if (scrollabilityCache.scrollBar == null) {
3653 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3654 }
Joe Malin32736f02011-01-19 16:14:20 -08003655
Romain Guy8bda2482010-03-02 11:42:11 -08003656 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003657
Mike Cleronf116bf82009-09-27 19:14:12 -07003658 if (!fadeScrollbars) {
3659 scrollabilityCache.state = ScrollabilityCache.ON;
3660 }
3661 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003662
3663
Mike Cleronf116bf82009-09-27 19:14:12 -07003664 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3665 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3666 .getScrollBarFadeDuration());
3667 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3668 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3669 ViewConfiguration.getScrollDefaultDelay());
3670
Joe Malin32736f02011-01-19 16:14:20 -08003671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003672 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3673 com.android.internal.R.styleable.View_scrollbarSize,
3674 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3675
3676 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3677 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3678
3679 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3680 if (thumb != null) {
3681 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3682 }
3683
3684 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3685 false);
3686 if (alwaysDraw) {
3687 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3688 }
3689
3690 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3691 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3692
3693 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3694 if (thumb != null) {
3695 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3696 }
3697
3698 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3699 false);
3700 if (alwaysDraw) {
3701 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3702 }
3703
3704 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003705 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003706 }
3707
3708 /**
3709 * <p>
3710 * Initalizes the scrollability cache if necessary.
3711 * </p>
3712 */
3713 private void initScrollCache() {
3714 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003715 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003716 }
3717 }
3718
Philip Milne6c8ea062012-04-03 17:38:43 -07003719 private ScrollabilityCache getScrollCache() {
3720 initScrollCache();
3721 return mScrollCache;
3722 }
3723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003724 /**
Adam Powell20232d02010-12-08 21:08:53 -08003725 * Set the position of the vertical scroll bar. Should be one of
3726 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3727 * {@link #SCROLLBAR_POSITION_RIGHT}.
3728 *
3729 * @param position Where the vertical scroll bar should be positioned.
3730 */
3731 public void setVerticalScrollbarPosition(int position) {
3732 if (mVerticalScrollbarPosition != position) {
3733 mVerticalScrollbarPosition = position;
3734 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003735 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003736 }
3737 }
3738
3739 /**
3740 * @return The position where the vertical scroll bar will show, if applicable.
3741 * @see #setVerticalScrollbarPosition(int)
3742 */
3743 public int getVerticalScrollbarPosition() {
3744 return mVerticalScrollbarPosition;
3745 }
3746
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003747 ListenerInfo getListenerInfo() {
3748 if (mListenerInfo != null) {
3749 return mListenerInfo;
3750 }
3751 mListenerInfo = new ListenerInfo();
3752 return mListenerInfo;
3753 }
3754
Adam Powell20232d02010-12-08 21:08:53 -08003755 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003756 * Register a callback to be invoked when focus of this view changed.
3757 *
3758 * @param l The callback that will run.
3759 */
3760 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003761 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003762 }
3763
3764 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003765 * Add a listener that will be called when the bounds of the view change due to
3766 * layout processing.
3767 *
3768 * @param listener The listener that will be called when layout bounds change.
3769 */
3770 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003771 ListenerInfo li = getListenerInfo();
3772 if (li.mOnLayoutChangeListeners == null) {
3773 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003774 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003775 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3776 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003777 }
Chet Haase21cd1382010-09-01 17:42:29 -07003778 }
3779
3780 /**
3781 * Remove a listener for layout changes.
3782 *
3783 * @param listener The listener for layout bounds change.
3784 */
3785 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003786 ListenerInfo li = mListenerInfo;
3787 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003788 return;
3789 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003790 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003791 }
3792
3793 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003794 * Add a listener for attach state changes.
3795 *
3796 * This listener will be called whenever this view is attached or detached
3797 * from a window. Remove the listener using
3798 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3799 *
3800 * @param listener Listener to attach
3801 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3802 */
3803 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003804 ListenerInfo li = getListenerInfo();
3805 if (li.mOnAttachStateChangeListeners == null) {
3806 li.mOnAttachStateChangeListeners
3807 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003808 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003809 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003810 }
3811
3812 /**
3813 * Remove a listener for attach state changes. The listener will receive no further
3814 * notification of window attach/detach events.
3815 *
3816 * @param listener Listener to remove
3817 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3818 */
3819 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003820 ListenerInfo li = mListenerInfo;
3821 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003822 return;
3823 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003824 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003825 }
3826
3827 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003828 * Returns the focus-change callback registered for this view.
3829 *
3830 * @return The callback, or null if one is not registered.
3831 */
3832 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003833 ListenerInfo li = mListenerInfo;
3834 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003835 }
3836
3837 /**
3838 * Register a callback to be invoked when this view is clicked. If this view is not
3839 * clickable, it becomes clickable.
3840 *
3841 * @param l The callback that will run
3842 *
3843 * @see #setClickable(boolean)
3844 */
3845 public void setOnClickListener(OnClickListener l) {
3846 if (!isClickable()) {
3847 setClickable(true);
3848 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003849 getListenerInfo().mOnClickListener = l;
3850 }
3851
3852 /**
3853 * Return whether this view has an attached OnClickListener. Returns
3854 * true if there is a listener, false if there is none.
3855 */
3856 public boolean hasOnClickListeners() {
3857 ListenerInfo li = mListenerInfo;
3858 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003859 }
3860
3861 /**
3862 * Register a callback to be invoked when this view is clicked and held. If this view is not
3863 * long clickable, it becomes long clickable.
3864 *
3865 * @param l The callback that will run
3866 *
3867 * @see #setLongClickable(boolean)
3868 */
3869 public void setOnLongClickListener(OnLongClickListener l) {
3870 if (!isLongClickable()) {
3871 setLongClickable(true);
3872 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003873 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003874 }
3875
3876 /**
3877 * Register a callback to be invoked when the context menu for this view is
3878 * being built. If this view is not long clickable, it becomes long clickable.
3879 *
3880 * @param l The callback that will run
3881 *
3882 */
3883 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3884 if (!isLongClickable()) {
3885 setLongClickable(true);
3886 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003887 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003888 }
3889
3890 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003891 * Call this view's OnClickListener, if it is defined. Performs all normal
3892 * actions associated with clicking: reporting accessibility event, playing
3893 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 *
3895 * @return True there was an assigned OnClickListener that was called, false
3896 * otherwise is returned.
3897 */
3898 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003899 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3900
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003901 ListenerInfo li = mListenerInfo;
3902 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003903 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003904 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003905 return true;
3906 }
3907
3908 return false;
3909 }
3910
3911 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003912 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3913 * this only calls the listener, and does not do any associated clicking
3914 * actions like reporting an accessibility event.
3915 *
3916 * @return True there was an assigned OnClickListener that was called, false
3917 * otherwise is returned.
3918 */
3919 public boolean callOnClick() {
3920 ListenerInfo li = mListenerInfo;
3921 if (li != null && li.mOnClickListener != null) {
3922 li.mOnClickListener.onClick(this);
3923 return true;
3924 }
3925 return false;
3926 }
3927
3928 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003929 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3930 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003931 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003932 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003933 */
3934 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003935 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003937 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003938 ListenerInfo li = mListenerInfo;
3939 if (li != null && li.mOnLongClickListener != null) {
3940 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003941 }
3942 if (!handled) {
3943 handled = showContextMenu();
3944 }
3945 if (handled) {
3946 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3947 }
3948 return handled;
3949 }
3950
3951 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003952 * Performs button-related actions during a touch down event.
3953 *
3954 * @param event The event.
3955 * @return True if the down was consumed.
3956 *
3957 * @hide
3958 */
3959 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3960 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3961 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3962 return true;
3963 }
3964 }
3965 return false;
3966 }
3967
3968 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003969 * Bring up the context menu for this view.
3970 *
3971 * @return Whether a context menu was displayed.
3972 */
3973 public boolean showContextMenu() {
3974 return getParent().showContextMenuForChild(this);
3975 }
3976
3977 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003978 * Bring up the context menu for this view, referring to the item under the specified point.
3979 *
3980 * @param x The referenced x coordinate.
3981 * @param y The referenced y coordinate.
3982 * @param metaState The keyboard modifiers that were pressed.
3983 * @return Whether a context menu was displayed.
3984 *
3985 * @hide
3986 */
3987 public boolean showContextMenu(float x, float y, int metaState) {
3988 return showContextMenu();
3989 }
3990
3991 /**
Adam Powell6e346362010-07-23 10:18:23 -07003992 * Start an action mode.
3993 *
3994 * @param callback Callback that will control the lifecycle of the action mode
3995 * @return The new action mode if it is started, null otherwise
3996 *
3997 * @see ActionMode
3998 */
3999 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004000 ViewParent parent = getParent();
4001 if (parent == null) return null;
4002 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004003 }
4004
4005 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004006 * Register a callback to be invoked when a key is pressed in this view.
4007 * @param l the key listener to attach to this view
4008 */
4009 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004010 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004011 }
4012
4013 /**
4014 * Register a callback to be invoked when a touch event is sent to this view.
4015 * @param l the touch listener to attach to this view
4016 */
4017 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004018 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004019 }
4020
4021 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004022 * Register a callback to be invoked when a generic motion event is sent to this view.
4023 * @param l the generic motion listener to attach to this view
4024 */
4025 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004026 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004027 }
4028
4029 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004030 * Register a callback to be invoked when a hover event is sent to this view.
4031 * @param l the hover listener to attach to this view
4032 */
4033 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004034 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004035 }
4036
4037 /**
Joe Malin32736f02011-01-19 16:14:20 -08004038 * Register a drag event listener callback object for this View. The parameter is
4039 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4040 * View, the system calls the
4041 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4042 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004043 */
4044 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004045 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004046 }
4047
4048 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004049 * Give this view focus. This will cause
4050 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004051 *
4052 * Note: this does not check whether this {@link View} should get focus, it just
4053 * gives it focus no matter what. It should only be called internally by framework
4054 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4055 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004056 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4057 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004058 * focus moved when requestFocus() is called. It may not always
4059 * apply, in which case use the default View.FOCUS_DOWN.
4060 * @param previouslyFocusedRect The rectangle of the view that had focus
4061 * prior in this View's coordinate system.
4062 */
4063 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4064 if (DBG) {
4065 System.out.println(this + " requestFocus()");
4066 }
4067
4068 if ((mPrivateFlags & FOCUSED) == 0) {
4069 mPrivateFlags |= FOCUSED;
4070
4071 if (mParent != null) {
4072 mParent.requestChildFocus(this, this);
4073 }
4074
4075 onFocusChanged(true, direction, previouslyFocusedRect);
4076 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004077
4078 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4079 notifyAccessibilityStateChanged();
4080 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004081 }
4082 }
4083
4084 /**
4085 * Request that a rectangle of this view be visible on the screen,
4086 * scrolling if necessary just enough.
4087 *
4088 * <p>A View should call this if it maintains some notion of which part
4089 * of its content is interesting. For example, a text editing view
4090 * should call this when its cursor moves.
4091 *
4092 * @param rectangle The rectangle.
4093 * @return Whether any parent scrolled.
4094 */
4095 public boolean requestRectangleOnScreen(Rect rectangle) {
4096 return requestRectangleOnScreen(rectangle, false);
4097 }
4098
4099 /**
4100 * Request that a rectangle of this view be visible on the screen,
4101 * scrolling if necessary just enough.
4102 *
4103 * <p>A View should call this if it maintains some notion of which part
4104 * of its content is interesting. For example, a text editing view
4105 * should call this when its cursor moves.
4106 *
4107 * <p>When <code>immediate</code> is set to true, scrolling will not be
4108 * animated.
4109 *
4110 * @param rectangle The rectangle.
4111 * @param immediate True to forbid animated scrolling, false otherwise
4112 * @return Whether any parent scrolled.
4113 */
4114 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4115 View child = this;
4116 ViewParent parent = mParent;
4117 boolean scrolled = false;
4118 while (parent != null) {
4119 scrolled |= parent.requestChildRectangleOnScreen(child,
4120 rectangle, immediate);
4121
4122 // offset rect so next call has the rectangle in the
4123 // coordinate system of its direct child.
4124 rectangle.offset(child.getLeft(), child.getTop());
4125 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4126
4127 if (!(parent instanceof View)) {
4128 break;
4129 }
Romain Guy8506ab42009-06-11 17:35:47 -07004130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004131 child = (View) parent;
4132 parent = child.getParent();
4133 }
4134 return scrolled;
4135 }
4136
4137 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004138 * Called when this view wants to give up focus. If focus is cleared
4139 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4140 * <p>
4141 * <strong>Note:</strong> When a View clears focus the framework is trying
4142 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004143 * View is the first from the top that can take focus, then all callbacks
4144 * related to clearing focus will be invoked after wich the framework will
4145 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004146 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004147 */
4148 public void clearFocus() {
4149 if (DBG) {
4150 System.out.println(this + " clearFocus()");
4151 }
4152
4153 if ((mPrivateFlags & FOCUSED) != 0) {
4154 mPrivateFlags &= ~FOCUSED;
4155
4156 if (mParent != null) {
4157 mParent.clearChildFocus(this);
4158 }
4159
4160 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004162 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004163
4164 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004165
4166 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4167 notifyAccessibilityStateChanged();
4168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169 }
4170 }
4171
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004172 void ensureInputFocusOnFirstFocusable() {
4173 View root = getRootView();
4174 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004175 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004176 }
4177 }
4178
4179 /**
4180 * Called internally by the view system when a new view is getting focus.
4181 * This is what clears the old focus.
4182 */
4183 void unFocus() {
4184 if (DBG) {
4185 System.out.println(this + " unFocus()");
4186 }
4187
4188 if ((mPrivateFlags & FOCUSED) != 0) {
4189 mPrivateFlags &= ~FOCUSED;
4190
4191 onFocusChanged(false, 0, null);
4192 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004193
4194 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4195 notifyAccessibilityStateChanged();
4196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004197 }
4198 }
4199
4200 /**
4201 * Returns true if this view has focus iteself, or is the ancestor of the
4202 * view that has focus.
4203 *
4204 * @return True if this view has or contains focus, false otherwise.
4205 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004206 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004207 public boolean hasFocus() {
4208 return (mPrivateFlags & FOCUSED) != 0;
4209 }
4210
4211 /**
4212 * Returns true if this view is focusable or if it contains a reachable View
4213 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4214 * is a View whose parents do not block descendants focus.
4215 *
4216 * Only {@link #VISIBLE} views are considered focusable.
4217 *
4218 * @return True if the view is focusable or if the view contains a focusable
4219 * View, false otherwise.
4220 *
4221 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4222 */
4223 public boolean hasFocusable() {
4224 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4225 }
4226
4227 /**
4228 * Called by the view system when the focus state of this view changes.
4229 * When the focus change event is caused by directional navigation, direction
4230 * and previouslyFocusedRect provide insight into where the focus is coming from.
4231 * When overriding, be sure to call up through to the super class so that
4232 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004233 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004234 * @param gainFocus True if the View has focus; false otherwise.
4235 * @param direction The direction focus has moved when requestFocus()
4236 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004237 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4238 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4239 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004240 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4241 * system, of the previously focused view. If applicable, this will be
4242 * passed in as finer grained information about where the focus is coming
4243 * from (in addition to direction). Will be <code>null</code> otherwise.
4244 */
4245 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004246 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004247 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4248 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
4249 requestAccessibilityFocus();
4250 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004251 }
4252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004253 InputMethodManager imm = InputMethodManager.peekInstance();
4254 if (!gainFocus) {
4255 if (isPressed()) {
4256 setPressed(false);
4257 }
4258 if (imm != null && mAttachInfo != null
4259 && mAttachInfo.mHasWindowFocus) {
4260 imm.focusOut(this);
4261 }
Romain Guya2431d02009-04-30 16:30:00 -07004262 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004263 } else if (imm != null && mAttachInfo != null
4264 && mAttachInfo.mHasWindowFocus) {
4265 imm.focusIn(this);
4266 }
Romain Guy8506ab42009-06-11 17:35:47 -07004267
Romain Guy0fd89bf2011-01-26 15:41:30 -08004268 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004269 ListenerInfo li = mListenerInfo;
4270 if (li != null && li.mOnFocusChangeListener != null) {
4271 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004272 }
Joe Malin32736f02011-01-19 16:14:20 -08004273
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004274 if (mAttachInfo != null) {
4275 mAttachInfo.mKeyDispatchState.reset(this);
4276 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004277 }
4278
4279 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004280 * Sends an accessibility event of the given type. If accessiiblity is
4281 * not enabled this method has no effect. The default implementation calls
4282 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4283 * to populate information about the event source (this View), then calls
4284 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4285 * populate the text content of the event source including its descendants,
4286 * and last calls
4287 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4288 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004289 * <p>
4290 * If an {@link AccessibilityDelegate} has been specified via calling
4291 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4292 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4293 * responsible for handling this call.
4294 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004295 *
Scott Mainb303d832011-10-12 16:45:18 -07004296 * @param eventType The type of the event to send, as defined by several types from
4297 * {@link android.view.accessibility.AccessibilityEvent}, such as
4298 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4299 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004300 *
4301 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4302 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4303 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004304 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004305 */
4306 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004307 if (mAccessibilityDelegate != null) {
4308 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4309 } else {
4310 sendAccessibilityEventInternal(eventType);
4311 }
4312 }
4313
4314 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004315 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4316 * {@link AccessibilityEvent} to make an announcement which is related to some
4317 * sort of a context change for which none of the events representing UI transitions
4318 * is a good fit. For example, announcing a new page in a book. If accessibility
4319 * is not enabled this method does nothing.
4320 *
4321 * @param text The announcement text.
4322 */
4323 public void announceForAccessibility(CharSequence text) {
4324 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4325 AccessibilityEvent event = AccessibilityEvent.obtain(
4326 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4327 event.getText().add(text);
4328 sendAccessibilityEventUnchecked(event);
4329 }
4330 }
4331
4332 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004333 * @see #sendAccessibilityEvent(int)
4334 *
4335 * Note: Called from the default {@link AccessibilityDelegate}.
4336 */
4337 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004338 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4339 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4340 }
4341 }
4342
4343 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004344 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4345 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004346 * perform a check whether accessibility is enabled.
4347 * <p>
4348 * If an {@link AccessibilityDelegate} has been specified via calling
4349 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4350 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4351 * is responsible for handling this call.
4352 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004353 *
4354 * @param event The event to send.
4355 *
4356 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004357 */
4358 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004359 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004360 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004361 } else {
4362 sendAccessibilityEventUncheckedInternal(event);
4363 }
4364 }
4365
4366 /**
4367 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4368 *
4369 * Note: Called from the default {@link AccessibilityDelegate}.
4370 */
4371 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004372 if (!isShown()) {
4373 return;
4374 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004375 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004376 // Only a subset of accessibility events populates text content.
4377 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4378 dispatchPopulateAccessibilityEvent(event);
4379 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004380 // Intercept accessibility focus events fired by virtual nodes to keep
4381 // track of accessibility focus position in such nodes.
4382 final int eventType = event.getEventType();
4383 switch (eventType) {
4384 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
4385 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4386 event.getSourceNodeId());
4387 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4388 ViewRootImpl viewRootImpl = getViewRootImpl();
4389 if (viewRootImpl != null) {
4390 viewRootImpl.setAccessibilityFocusedHost(this);
4391 }
4392 }
4393 } break;
4394 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
4395 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4396 event.getSourceNodeId());
4397 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4398 ViewRootImpl viewRootImpl = getViewRootImpl();
4399 if (viewRootImpl != null) {
4400 viewRootImpl.setAccessibilityFocusedHost(null);
4401 }
4402 }
4403 } break;
4404 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004405 // In the beginning we called #isShown(), so we know that getParent() is not null.
4406 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004407 }
4408
4409 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004410 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4411 * to its children for adding their text content to the event. Note that the
4412 * event text is populated in a separate dispatch path since we add to the
4413 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004414 * A typical implementation will call
4415 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4416 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4417 * on each child. Override this method if custom population of the event text
4418 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004419 * <p>
4420 * If an {@link AccessibilityDelegate} has been specified via calling
4421 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4422 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4423 * is responsible for handling this call.
4424 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004425 * <p>
4426 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4427 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4428 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004429 *
4430 * @param event The event.
4431 *
4432 * @return True if the event population was completed.
4433 */
4434 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004435 if (mAccessibilityDelegate != null) {
4436 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4437 } else {
4438 return dispatchPopulateAccessibilityEventInternal(event);
4439 }
4440 }
4441
4442 /**
4443 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4444 *
4445 * Note: Called from the default {@link AccessibilityDelegate}.
4446 */
4447 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004448 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004449 return false;
4450 }
4451
4452 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004453 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004454 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004455 * text content. While this method is free to modify event
4456 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004457 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4458 * <p>
4459 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004460 * to the text added by the super implementation:
4461 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004462 * super.onPopulateAccessibilityEvent(event);
4463 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4464 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4465 * mCurrentDate.getTimeInMillis(), flags);
4466 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004467 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004468 * <p>
4469 * If an {@link AccessibilityDelegate} has been specified via calling
4470 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4471 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4472 * is responsible for handling this call.
4473 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004474 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4475 * information to the event, in case the default implementation has basic information to add.
4476 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004477 *
4478 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004479 *
4480 * @see #sendAccessibilityEvent(int)
4481 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004482 */
4483 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004484 if (mAccessibilityDelegate != null) {
4485 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4486 } else {
4487 onPopulateAccessibilityEventInternal(event);
4488 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004489 }
4490
4491 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004492 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4493 *
4494 * Note: Called from the default {@link AccessibilityDelegate}.
4495 */
4496 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4497
4498 }
4499
4500 /**
4501 * Initializes an {@link AccessibilityEvent} with information about
4502 * this View which is the event source. In other words, the source of
4503 * an accessibility event is the view whose state change triggered firing
4504 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004505 * <p>
4506 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004507 * to properties set by the super implementation:
4508 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4509 * super.onInitializeAccessibilityEvent(event);
4510 * event.setPassword(true);
4511 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004512 * <p>
4513 * If an {@link AccessibilityDelegate} has been specified via calling
4514 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4515 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4516 * is responsible for handling this call.
4517 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004518 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4519 * information to the event, in case the default implementation has basic information to add.
4520 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004521 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004522 *
4523 * @see #sendAccessibilityEvent(int)
4524 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4525 */
4526 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004527 if (mAccessibilityDelegate != null) {
4528 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4529 } else {
4530 onInitializeAccessibilityEventInternal(event);
4531 }
4532 }
4533
4534 /**
4535 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4536 *
4537 * Note: Called from the default {@link AccessibilityDelegate}.
4538 */
4539 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004540 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004541 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004542 event.setPackageName(getContext().getPackageName());
4543 event.setEnabled(isEnabled());
4544 event.setContentDescription(mContentDescription);
4545
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004546 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004547 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004548 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4549 FOCUSABLES_ALL);
4550 event.setItemCount(focusablesTempList.size());
4551 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4552 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004553 }
4554 }
4555
4556 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004557 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4558 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4559 * This method is responsible for obtaining an accessibility node info from a
4560 * pool of reusable instances and calling
4561 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4562 * initialize the former.
4563 * <p>
4564 * Note: The client is responsible for recycling the obtained instance by calling
4565 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4566 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004567 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004568 * @return A populated {@link AccessibilityNodeInfo}.
4569 *
4570 * @see AccessibilityNodeInfo
4571 */
4572 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004573 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4574 if (provider != null) {
4575 return provider.createAccessibilityNodeInfo(View.NO_ID);
4576 } else {
4577 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4578 onInitializeAccessibilityNodeInfo(info);
4579 return info;
4580 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004581 }
4582
4583 /**
4584 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4585 * The base implementation sets:
4586 * <ul>
4587 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004588 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4589 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004590 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4591 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4592 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4593 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4594 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4595 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4596 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4597 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4598 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4599 * </ul>
4600 * <p>
4601 * Subclasses should override this method, call the super implementation,
4602 * and set additional attributes.
4603 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004604 * <p>
4605 * If an {@link AccessibilityDelegate} has been specified via calling
4606 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4607 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4608 * is responsible for handling this call.
4609 * </p>
4610 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004611 * @param info The instance to initialize.
4612 */
4613 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004614 if (mAccessibilityDelegate != null) {
4615 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4616 } else {
4617 onInitializeAccessibilityNodeInfoInternal(info);
4618 }
4619 }
4620
4621 /**
4622 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4623 *
4624 * Note: Called from the default {@link AccessibilityDelegate}.
4625 */
4626 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004627 Rect bounds = mAttachInfo.mTmpInvalRect;
4628 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004629 info.setBoundsInParent(bounds);
4630
Svetoslav Ganovaa6f3de2012-04-09 17:39:00 -07004631 getGlobalVisibleRect(bounds);
4632 bounds.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004633 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004634
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004635 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004636 ViewParent parent = getParentForAccessibility();
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004637 if (parent instanceof View) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004638 info.setParent((View) parent);
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004639 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004640 }
4641
4642 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004643 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004644 info.setContentDescription(getContentDescription());
4645
4646 info.setEnabled(isEnabled());
4647 info.setClickable(isClickable());
4648 info.setFocusable(isFocusable());
4649 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004650 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004651 info.setSelected(isSelected());
4652 info.setLongClickable(isLongClickable());
4653
4654 // TODO: These make sense only if we are in an AdapterView but all
4655 // views can be selected. Maybe from accessiiblity perspective
4656 // we should report as selectable view in an AdapterView.
4657 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4658 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4659
4660 if (isFocusable()) {
4661 if (isFocused()) {
4662 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4663 } else {
4664 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4665 }
4666 }
4667 }
4668
4669 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004670 * Sets a delegate for implementing accessibility support via compositon as
4671 * opposed to inheritance. The delegate's primary use is for implementing
4672 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4673 *
4674 * @param delegate The delegate instance.
4675 *
4676 * @see AccessibilityDelegate
4677 */
4678 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4679 mAccessibilityDelegate = delegate;
4680 }
4681
4682 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004683 * Gets the provider for managing a virtual view hierarchy rooted at this View
4684 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4685 * that explore the window content.
4686 * <p>
4687 * If this method returns an instance, this instance is responsible for managing
4688 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4689 * View including the one representing the View itself. Similarly the returned
4690 * instance is responsible for performing accessibility actions on any virtual
4691 * view or the root view itself.
4692 * </p>
4693 * <p>
4694 * If an {@link AccessibilityDelegate} has been specified via calling
4695 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4696 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4697 * is responsible for handling this call.
4698 * </p>
4699 *
4700 * @return The provider.
4701 *
4702 * @see AccessibilityNodeProvider
4703 */
4704 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4705 if (mAccessibilityDelegate != null) {
4706 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4707 } else {
4708 return null;
4709 }
4710 }
4711
4712 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004713 * Gets the unique identifier of this view on the screen for accessibility purposes.
4714 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4715 *
4716 * @return The view accessibility id.
4717 *
4718 * @hide
4719 */
4720 public int getAccessibilityViewId() {
4721 if (mAccessibilityViewId == NO_ID) {
4722 mAccessibilityViewId = sNextAccessibilityViewId++;
4723 }
4724 return mAccessibilityViewId;
4725 }
4726
4727 /**
4728 * Gets the unique identifier of the window in which this View reseides.
4729 *
4730 * @return The window accessibility id.
4731 *
4732 * @hide
4733 */
4734 public int getAccessibilityWindowId() {
4735 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4736 }
4737
4738 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004739 * Gets the {@link View} description. It briefly describes the view and is
4740 * primarily used for accessibility support. Set this property to enable
4741 * better accessibility support for your application. This is especially
4742 * true for views that do not have textual representation (For example,
4743 * ImageButton).
4744 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07004745 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07004746 *
4747 * @attr ref android.R.styleable#View_contentDescription
4748 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07004749 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07004750 public CharSequence getContentDescription() {
4751 return mContentDescription;
4752 }
4753
4754 /**
4755 * Sets the {@link View} description. It briefly describes the view and is
4756 * primarily used for accessibility support. Set this property to enable
4757 * better accessibility support for your application. This is especially
4758 * true for views that do not have textual representation (For example,
4759 * ImageButton).
4760 *
4761 * @param contentDescription The content description.
4762 *
4763 * @attr ref android.R.styleable#View_contentDescription
4764 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004765 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004766 public void setContentDescription(CharSequence contentDescription) {
4767 mContentDescription = contentDescription;
4768 }
4769
4770 /**
Romain Guya2431d02009-04-30 16:30:00 -07004771 * Invoked whenever this view loses focus, either by losing window focus or by losing
4772 * focus within its window. This method can be used to clear any state tied to the
4773 * focus. For instance, if a button is held pressed with the trackball and the window
4774 * loses focus, this method can be used to cancel the press.
4775 *
4776 * Subclasses of View overriding this method should always call super.onFocusLost().
4777 *
4778 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004779 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004780 *
4781 * @hide pending API council approval
4782 */
4783 protected void onFocusLost() {
4784 resetPressedState();
4785 }
4786
4787 private void resetPressedState() {
4788 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4789 return;
4790 }
4791
4792 if (isPressed()) {
4793 setPressed(false);
4794
4795 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004796 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004797 }
4798 }
4799 }
4800
4801 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004802 * Returns true if this view has focus
4803 *
4804 * @return True if this view has focus, false otherwise.
4805 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004806 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004807 public boolean isFocused() {
4808 return (mPrivateFlags & FOCUSED) != 0;
4809 }
4810
4811 /**
4812 * Find the view in the hierarchy rooted at this view that currently has
4813 * focus.
4814 *
4815 * @return The view that currently has focus, or null if no focused view can
4816 * be found.
4817 */
4818 public View findFocus() {
4819 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4820 }
4821
4822 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004823 * Indicates whether this view is one of the set of scrollable containers in
4824 * its window.
4825 *
4826 * @return whether this view is one of the set of scrollable containers in
4827 * its window
4828 *
4829 * @attr ref android.R.styleable#View_isScrollContainer
4830 */
4831 public boolean isScrollContainer() {
4832 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
4833 }
4834
4835 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004836 * Change whether this view is one of the set of scrollable containers in
4837 * its window. This will be used to determine whether the window can
4838 * resize or must pan when a soft input area is open -- scrollable
4839 * containers allow the window to use resize mode since the container
4840 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07004841 *
4842 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004843 */
4844 public void setScrollContainer(boolean isScrollContainer) {
4845 if (isScrollContainer) {
4846 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4847 mAttachInfo.mScrollContainers.add(this);
4848 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4849 }
4850 mPrivateFlags |= SCROLL_CONTAINER;
4851 } else {
4852 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4853 mAttachInfo.mScrollContainers.remove(this);
4854 }
4855 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4856 }
4857 }
4858
4859 /**
4860 * Returns the quality of the drawing cache.
4861 *
4862 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4863 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4864 *
4865 * @see #setDrawingCacheQuality(int)
4866 * @see #setDrawingCacheEnabled(boolean)
4867 * @see #isDrawingCacheEnabled()
4868 *
4869 * @attr ref android.R.styleable#View_drawingCacheQuality
4870 */
4871 public int getDrawingCacheQuality() {
4872 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4873 }
4874
4875 /**
4876 * Set the drawing cache quality of this view. This value is used only when the
4877 * drawing cache is enabled
4878 *
4879 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4880 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4881 *
4882 * @see #getDrawingCacheQuality()
4883 * @see #setDrawingCacheEnabled(boolean)
4884 * @see #isDrawingCacheEnabled()
4885 *
4886 * @attr ref android.R.styleable#View_drawingCacheQuality
4887 */
4888 public void setDrawingCacheQuality(int quality) {
4889 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4890 }
4891
4892 /**
4893 * Returns whether the screen should remain on, corresponding to the current
4894 * value of {@link #KEEP_SCREEN_ON}.
4895 *
4896 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4897 *
4898 * @see #setKeepScreenOn(boolean)
4899 *
4900 * @attr ref android.R.styleable#View_keepScreenOn
4901 */
4902 public boolean getKeepScreenOn() {
4903 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4904 }
4905
4906 /**
4907 * Controls whether the screen should remain on, modifying the
4908 * value of {@link #KEEP_SCREEN_ON}.
4909 *
4910 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4911 *
4912 * @see #getKeepScreenOn()
4913 *
4914 * @attr ref android.R.styleable#View_keepScreenOn
4915 */
4916 public void setKeepScreenOn(boolean keepScreenOn) {
4917 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4918 }
4919
4920 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004921 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4922 * @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 -08004923 *
4924 * @attr ref android.R.styleable#View_nextFocusLeft
4925 */
4926 public int getNextFocusLeftId() {
4927 return mNextFocusLeftId;
4928 }
4929
4930 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004931 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4932 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4933 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004934 *
4935 * @attr ref android.R.styleable#View_nextFocusLeft
4936 */
4937 public void setNextFocusLeftId(int nextFocusLeftId) {
4938 mNextFocusLeftId = nextFocusLeftId;
4939 }
4940
4941 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004942 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4943 * @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 -08004944 *
4945 * @attr ref android.R.styleable#View_nextFocusRight
4946 */
4947 public int getNextFocusRightId() {
4948 return mNextFocusRightId;
4949 }
4950
4951 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004952 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4953 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4954 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004955 *
4956 * @attr ref android.R.styleable#View_nextFocusRight
4957 */
4958 public void setNextFocusRightId(int nextFocusRightId) {
4959 mNextFocusRightId = nextFocusRightId;
4960 }
4961
4962 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004963 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4964 * @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 -08004965 *
4966 * @attr ref android.R.styleable#View_nextFocusUp
4967 */
4968 public int getNextFocusUpId() {
4969 return mNextFocusUpId;
4970 }
4971
4972 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004973 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4974 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4975 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 *
4977 * @attr ref android.R.styleable#View_nextFocusUp
4978 */
4979 public void setNextFocusUpId(int nextFocusUpId) {
4980 mNextFocusUpId = nextFocusUpId;
4981 }
4982
4983 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004984 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4985 * @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 -08004986 *
4987 * @attr ref android.R.styleable#View_nextFocusDown
4988 */
4989 public int getNextFocusDownId() {
4990 return mNextFocusDownId;
4991 }
4992
4993 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004994 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4995 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4996 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004997 *
4998 * @attr ref android.R.styleable#View_nextFocusDown
4999 */
5000 public void setNextFocusDownId(int nextFocusDownId) {
5001 mNextFocusDownId = nextFocusDownId;
5002 }
5003
5004 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005005 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5006 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5007 *
5008 * @attr ref android.R.styleable#View_nextFocusForward
5009 */
5010 public int getNextFocusForwardId() {
5011 return mNextFocusForwardId;
5012 }
5013
5014 /**
5015 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5016 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5017 * decide automatically.
5018 *
5019 * @attr ref android.R.styleable#View_nextFocusForward
5020 */
5021 public void setNextFocusForwardId(int nextFocusForwardId) {
5022 mNextFocusForwardId = nextFocusForwardId;
5023 }
5024
5025 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005026 * Returns the visibility of this view and all of its ancestors
5027 *
5028 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5029 */
5030 public boolean isShown() {
5031 View current = this;
5032 //noinspection ConstantConditions
5033 do {
5034 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5035 return false;
5036 }
5037 ViewParent parent = current.mParent;
5038 if (parent == null) {
5039 return false; // We are not attached to the view root
5040 }
5041 if (!(parent instanceof View)) {
5042 return true;
5043 }
5044 current = (View) parent;
5045 } while (current != null);
5046
5047 return false;
5048 }
5049
5050 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005051 * Called by the view hierarchy when the content insets for a window have
5052 * changed, to allow it to adjust its content to fit within those windows.
5053 * The content insets tell you the space that the status bar, input method,
5054 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005055 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005056 * <p>You do not normally need to deal with this function, since the default
5057 * window decoration given to applications takes care of applying it to the
5058 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5059 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5060 * and your content can be placed under those system elements. You can then
5061 * use this method within your view hierarchy if you have parts of your UI
5062 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005063 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005064 * <p>The default implementation of this method simply applies the content
5065 * inset's to the view's padding. This can be enabled through
5066 * {@link #setFitsSystemWindows(boolean)}. Alternatively, you can override
5067 * the method and handle the insets however you would like. Note that the
5068 * insets provided by the framework are always relative to the far edges
5069 * of the window, not accounting for the location of the called view within
5070 * that window. (In fact when this method is called you do not yet know
5071 * where the layout will place the view, as it is done before layout happens.)
5072 *
5073 * <p>Note: unlike many View methods, there is no dispatch phase to this
5074 * call. If you are overriding it in a ViewGroup and want to allow the
5075 * call to continue to your children, you must be sure to call the super
5076 * implementation.
5077 *
5078 * @param insets Current content insets of the window. Prior to
5079 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5080 * the insets or else you and Android will be unhappy.
5081 *
5082 * @return Return true if this view applied the insets and it should not
5083 * continue propagating further down the hierarchy, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005084 */
5085 protected boolean fitSystemWindows(Rect insets) {
5086 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005087 mUserPaddingStart = -1;
5088 mUserPaddingEnd = -1;
5089 mUserPaddingRelative = false;
5090 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5091 || mAttachInfo == null
5092 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5093 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5094 return true;
5095 } else {
5096 internalSetPadding(0, 0, 0, 0);
5097 return false;
5098 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005099 }
5100 return false;
5101 }
5102
5103 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005104 * Set whether or not this view should account for system screen decorations
5105 * such as the status bar and inset its content. This allows this view to be
5106 * positioned in absolute screen coordinates and remain visible to the user.
5107 *
5108 * <p>This should only be used by top-level window decor views.
5109 *
5110 * @param fitSystemWindows true to inset content for system screen decorations, false for
5111 * default behavior.
5112 *
5113 * @attr ref android.R.styleable#View_fitsSystemWindows
5114 */
5115 public void setFitsSystemWindows(boolean fitSystemWindows) {
5116 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5117 }
5118
5119 /**
5120 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
5121 * will account for system screen decorations such as the status bar and inset its
5122 * content. This allows the view to be positioned in absolute screen coordinates
5123 * and remain visible to the user.
5124 *
5125 * @return true if this view will adjust its content bounds for system screen decorations.
5126 *
5127 * @attr ref android.R.styleable#View_fitsSystemWindows
5128 */
5129 public boolean fitsSystemWindows() {
5130 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5131 }
5132
5133 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005134 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5135 */
5136 public void requestFitSystemWindows() {
5137 if (mParent != null) {
5138 mParent.requestFitSystemWindows();
5139 }
5140 }
5141
5142 /**
5143 * For use by PhoneWindow to make its own system window fitting optional.
5144 * @hide
5145 */
5146 public void makeOptionalFitsSystemWindows() {
5147 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5148 }
5149
5150 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005151 * Returns the visibility status for this view.
5152 *
5153 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5154 * @attr ref android.R.styleable#View_visibility
5155 */
5156 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005157 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5158 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5159 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005160 })
5161 public int getVisibility() {
5162 return mViewFlags & VISIBILITY_MASK;
5163 }
5164
5165 /**
5166 * Set the enabled state of this view.
5167 *
5168 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5169 * @attr ref android.R.styleable#View_visibility
5170 */
5171 @RemotableViewMethod
5172 public void setVisibility(int visibility) {
5173 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005174 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005175 }
5176
5177 /**
5178 * Returns the enabled status for this view. The interpretation of the
5179 * enabled state varies by subclass.
5180 *
5181 * @return True if this view is enabled, false otherwise.
5182 */
5183 @ViewDebug.ExportedProperty
5184 public boolean isEnabled() {
5185 return (mViewFlags & ENABLED_MASK) == ENABLED;
5186 }
5187
5188 /**
5189 * Set the enabled state of this view. The interpretation of the enabled
5190 * state varies by subclass.
5191 *
5192 * @param enabled True if this view is enabled, false otherwise.
5193 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005194 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005195 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005196 if (enabled == isEnabled()) return;
5197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005198 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5199
5200 /*
5201 * The View most likely has to change its appearance, so refresh
5202 * the drawable state.
5203 */
5204 refreshDrawableState();
5205
5206 // Invalidate too, since the default behavior for views is to be
5207 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005208 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005209 }
5210
5211 /**
5212 * Set whether this view can receive the focus.
5213 *
5214 * Setting this to false will also ensure that this view is not focusable
5215 * in touch mode.
5216 *
5217 * @param focusable If true, this view can receive the focus.
5218 *
5219 * @see #setFocusableInTouchMode(boolean)
5220 * @attr ref android.R.styleable#View_focusable
5221 */
5222 public void setFocusable(boolean focusable) {
5223 if (!focusable) {
5224 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5225 }
5226 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5227 }
5228
5229 /**
5230 * Set whether this view can receive focus while in touch mode.
5231 *
5232 * Setting this to true will also ensure that this view is focusable.
5233 *
5234 * @param focusableInTouchMode If true, this view can receive the focus while
5235 * in touch mode.
5236 *
5237 * @see #setFocusable(boolean)
5238 * @attr ref android.R.styleable#View_focusableInTouchMode
5239 */
5240 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5241 // Focusable in touch mode should always be set before the focusable flag
5242 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5243 // which, in touch mode, will not successfully request focus on this view
5244 // because the focusable in touch mode flag is not set
5245 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5246 if (focusableInTouchMode) {
5247 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5248 }
5249 }
5250
5251 /**
5252 * Set whether this view should have sound effects enabled for events such as
5253 * clicking and touching.
5254 *
5255 * <p>You may wish to disable sound effects for a view if you already play sounds,
5256 * for instance, a dial key that plays dtmf tones.
5257 *
5258 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5259 * @see #isSoundEffectsEnabled()
5260 * @see #playSoundEffect(int)
5261 * @attr ref android.R.styleable#View_soundEffectsEnabled
5262 */
5263 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5264 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5265 }
5266
5267 /**
5268 * @return whether this view should have sound effects enabled for events such as
5269 * clicking and touching.
5270 *
5271 * @see #setSoundEffectsEnabled(boolean)
5272 * @see #playSoundEffect(int)
5273 * @attr ref android.R.styleable#View_soundEffectsEnabled
5274 */
5275 @ViewDebug.ExportedProperty
5276 public boolean isSoundEffectsEnabled() {
5277 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5278 }
5279
5280 /**
5281 * Set whether this view should have haptic feedback for events such as
5282 * long presses.
5283 *
5284 * <p>You may wish to disable haptic feedback if your view already controls
5285 * its own haptic feedback.
5286 *
5287 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5288 * @see #isHapticFeedbackEnabled()
5289 * @see #performHapticFeedback(int)
5290 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5291 */
5292 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5293 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5294 }
5295
5296 /**
5297 * @return whether this view should have haptic feedback enabled for events
5298 * long presses.
5299 *
5300 * @see #setHapticFeedbackEnabled(boolean)
5301 * @see #performHapticFeedback(int)
5302 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5303 */
5304 @ViewDebug.ExportedProperty
5305 public boolean isHapticFeedbackEnabled() {
5306 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5307 }
5308
5309 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005310 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005311 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005312 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5313 * {@link #LAYOUT_DIRECTION_RTL},
5314 * {@link #LAYOUT_DIRECTION_INHERIT} or
5315 * {@link #LAYOUT_DIRECTION_LOCALE}.
5316 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005317 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005318 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005319 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5320 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5321 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5322 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005323 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005324 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005325 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005326 }
5327
5328 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005329 * Set the layout direction for this view. This will propagate a reset of layout direction
5330 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005331 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005332 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5333 * {@link #LAYOUT_DIRECTION_RTL},
5334 * {@link #LAYOUT_DIRECTION_INHERIT} or
5335 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005336 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005337 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005338 */
5339 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005340 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005341 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005342 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005343 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005344 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005345 // Set the new layout direction (filtered) and ask for a layout pass
5346 mPrivateFlags2 |=
5347 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5348 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005349 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005350 }
5351
5352 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005353 * Returns the resolved layout direction for this view.
5354 *
5355 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005356 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005357 */
5358 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005359 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5360 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005361 })
5362 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005363 // The layout diretion will be resolved only if needed
5364 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5365 resolveLayoutDirection();
5366 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005367 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005368 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5369 }
5370
5371 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005372 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5373 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005374 *
5375 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005376 */
5377 @ViewDebug.ExportedProperty(category = "layout")
5378 public boolean isLayoutRtl() {
5379 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5380 }
5381
5382 /**
Adam Powell539ee872012-02-03 19:00:49 -08005383 * Indicates whether the view is currently tracking transient state that the
5384 * app should not need to concern itself with saving and restoring, but that
5385 * the framework should take special note to preserve when possible.
5386 *
5387 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005388 */
5389 @ViewDebug.ExportedProperty(category = "layout")
5390 public boolean hasTransientState() {
5391 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5392 }
5393
5394 /**
5395 * Set whether this view is currently tracking transient state that the
5396 * framework should attempt to preserve when possible.
5397 *
5398 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005399 */
5400 public void setHasTransientState(boolean hasTransientState) {
5401 if (hasTransientState() == hasTransientState) return;
5402
5403 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5404 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5405 if (mParent != null) {
5406 try {
5407 mParent.childHasTransientStateChanged(this, hasTransientState);
5408 } catch (AbstractMethodError e) {
5409 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5410 " does not fully implement ViewParent", e);
5411 }
5412 }
5413 }
5414
5415 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005416 * If this view doesn't do any drawing on its own, set this flag to
5417 * allow further optimizations. By default, this flag is not set on
5418 * View, but could be set on some View subclasses such as ViewGroup.
5419 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005420 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5421 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005422 *
5423 * @param willNotDraw whether or not this View draw on its own
5424 */
5425 public void setWillNotDraw(boolean willNotDraw) {
5426 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5427 }
5428
5429 /**
5430 * Returns whether or not this View draws on its own.
5431 *
5432 * @return true if this view has nothing to draw, false otherwise
5433 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005434 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005435 public boolean willNotDraw() {
5436 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5437 }
5438
5439 /**
5440 * When a View's drawing cache is enabled, drawing is redirected to an
5441 * offscreen bitmap. Some views, like an ImageView, must be able to
5442 * bypass this mechanism if they already draw a single bitmap, to avoid
5443 * unnecessary usage of the memory.
5444 *
5445 * @param willNotCacheDrawing true if this view does not cache its
5446 * drawing, false otherwise
5447 */
5448 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5449 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5450 }
5451
5452 /**
5453 * Returns whether or not this View can cache its drawing or not.
5454 *
5455 * @return true if this view does not cache its drawing, false otherwise
5456 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005457 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005458 public boolean willNotCacheDrawing() {
5459 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5460 }
5461
5462 /**
5463 * Indicates whether this view reacts to click events or not.
5464 *
5465 * @return true if the view is clickable, false otherwise
5466 *
5467 * @see #setClickable(boolean)
5468 * @attr ref android.R.styleable#View_clickable
5469 */
5470 @ViewDebug.ExportedProperty
5471 public boolean isClickable() {
5472 return (mViewFlags & CLICKABLE) == CLICKABLE;
5473 }
5474
5475 /**
5476 * Enables or disables click events for this view. When a view
5477 * is clickable it will change its state to "pressed" on every click.
5478 * Subclasses should set the view clickable to visually react to
5479 * user's clicks.
5480 *
5481 * @param clickable true to make the view clickable, false otherwise
5482 *
5483 * @see #isClickable()
5484 * @attr ref android.R.styleable#View_clickable
5485 */
5486 public void setClickable(boolean clickable) {
5487 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5488 }
5489
5490 /**
5491 * Indicates whether this view reacts to long click events or not.
5492 *
5493 * @return true if the view is long clickable, false otherwise
5494 *
5495 * @see #setLongClickable(boolean)
5496 * @attr ref android.R.styleable#View_longClickable
5497 */
5498 public boolean isLongClickable() {
5499 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5500 }
5501
5502 /**
5503 * Enables or disables long click events for this view. When a view is long
5504 * clickable it reacts to the user holding down the button for a longer
5505 * duration than a tap. This event can either launch the listener or a
5506 * context menu.
5507 *
5508 * @param longClickable true to make the view long clickable, false otherwise
5509 * @see #isLongClickable()
5510 * @attr ref android.R.styleable#View_longClickable
5511 */
5512 public void setLongClickable(boolean longClickable) {
5513 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5514 }
5515
5516 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005517 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005518 *
5519 * @see #isClickable()
5520 * @see #setClickable(boolean)
5521 *
5522 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5523 * the View's internal state from a previously set "pressed" state.
5524 */
5525 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005526 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005528 if (pressed) {
5529 mPrivateFlags |= PRESSED;
5530 } else {
5531 mPrivateFlags &= ~PRESSED;
5532 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005533
5534 if (needsRefresh) {
5535 refreshDrawableState();
5536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005537 dispatchSetPressed(pressed);
5538 }
5539
5540 /**
5541 * Dispatch setPressed to all of this View's children.
5542 *
5543 * @see #setPressed(boolean)
5544 *
5545 * @param pressed The new pressed state
5546 */
5547 protected void dispatchSetPressed(boolean pressed) {
5548 }
5549
5550 /**
5551 * Indicates whether the view is currently in pressed state. Unless
5552 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5553 * the pressed state.
5554 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005555 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005556 * @see #isClickable()
5557 * @see #setClickable(boolean)
5558 *
5559 * @return true if the view is currently pressed, false otherwise
5560 */
5561 public boolean isPressed() {
5562 return (mPrivateFlags & PRESSED) == PRESSED;
5563 }
5564
5565 /**
5566 * Indicates whether this view will save its state (that is,
5567 * whether its {@link #onSaveInstanceState} method will be called).
5568 *
5569 * @return Returns true if the view state saving is enabled, else false.
5570 *
5571 * @see #setSaveEnabled(boolean)
5572 * @attr ref android.R.styleable#View_saveEnabled
5573 */
5574 public boolean isSaveEnabled() {
5575 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5576 }
5577
5578 /**
5579 * Controls whether the saving of this view's state is
5580 * enabled (that is, whether its {@link #onSaveInstanceState} method
5581 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005582 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005583 * for its state to be saved. This flag can only disable the
5584 * saving of this view; any child views may still have their state saved.
5585 *
5586 * @param enabled Set to false to <em>disable</em> state saving, or true
5587 * (the default) to allow it.
5588 *
5589 * @see #isSaveEnabled()
5590 * @see #setId(int)
5591 * @see #onSaveInstanceState()
5592 * @attr ref android.R.styleable#View_saveEnabled
5593 */
5594 public void setSaveEnabled(boolean enabled) {
5595 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5596 }
5597
Jeff Brown85a31762010-09-01 17:01:00 -07005598 /**
5599 * Gets whether the framework should discard touches when the view's
5600 * window is obscured by another visible window.
5601 * Refer to the {@link View} security documentation for more details.
5602 *
5603 * @return True if touch filtering is enabled.
5604 *
5605 * @see #setFilterTouchesWhenObscured(boolean)
5606 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5607 */
5608 @ViewDebug.ExportedProperty
5609 public boolean getFilterTouchesWhenObscured() {
5610 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5611 }
5612
5613 /**
5614 * Sets whether the framework should discard touches when the view's
5615 * window is obscured by another visible window.
5616 * Refer to the {@link View} security documentation for more details.
5617 *
5618 * @param enabled True if touch filtering should be enabled.
5619 *
5620 * @see #getFilterTouchesWhenObscured
5621 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5622 */
5623 public void setFilterTouchesWhenObscured(boolean enabled) {
5624 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5625 FILTER_TOUCHES_WHEN_OBSCURED);
5626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005627
5628 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005629 * Indicates whether the entire hierarchy under this view will save its
5630 * state when a state saving traversal occurs from its parent. The default
5631 * is true; if false, these views will not be saved unless
5632 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5633 *
5634 * @return Returns true if the view state saving from parent is enabled, else false.
5635 *
5636 * @see #setSaveFromParentEnabled(boolean)
5637 */
5638 public boolean isSaveFromParentEnabled() {
5639 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5640 }
5641
5642 /**
5643 * Controls whether the entire hierarchy under this view will save its
5644 * state when a state saving traversal occurs from its parent. The default
5645 * is true; if false, these views will not be saved unless
5646 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5647 *
5648 * @param enabled Set to false to <em>disable</em> state saving, or true
5649 * (the default) to allow it.
5650 *
5651 * @see #isSaveFromParentEnabled()
5652 * @see #setId(int)
5653 * @see #onSaveInstanceState()
5654 */
5655 public void setSaveFromParentEnabled(boolean enabled) {
5656 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5657 }
5658
5659
5660 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005661 * Returns whether this View is able to take focus.
5662 *
5663 * @return True if this view can take focus, or false otherwise.
5664 * @attr ref android.R.styleable#View_focusable
5665 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005666 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005667 public final boolean isFocusable() {
5668 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5669 }
5670
5671 /**
5672 * When a view is focusable, it may not want to take focus when in touch mode.
5673 * For example, a button would like focus when the user is navigating via a D-pad
5674 * so that the user can click on it, but once the user starts touching the screen,
5675 * the button shouldn't take focus
5676 * @return Whether the view is focusable in touch mode.
5677 * @attr ref android.R.styleable#View_focusableInTouchMode
5678 */
5679 @ViewDebug.ExportedProperty
5680 public final boolean isFocusableInTouchMode() {
5681 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5682 }
5683
5684 /**
5685 * Find the nearest view in the specified direction that can take focus.
5686 * This does not actually give focus to that view.
5687 *
5688 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5689 *
5690 * @return The nearest focusable in the specified direction, or null if none
5691 * can be found.
5692 */
5693 public View focusSearch(int direction) {
5694 if (mParent != null) {
5695 return mParent.focusSearch(this, direction);
5696 } else {
5697 return null;
5698 }
5699 }
5700
5701 /**
5702 * This method is the last chance for the focused view and its ancestors to
5703 * respond to an arrow key. This is called when the focused view did not
5704 * consume the key internally, nor could the view system find a new view in
5705 * the requested direction to give focus to.
5706 *
5707 * @param focused The currently focused view.
5708 * @param direction The direction focus wants to move. One of FOCUS_UP,
5709 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5710 * @return True if the this view consumed this unhandled move.
5711 */
5712 public boolean dispatchUnhandledMove(View focused, int direction) {
5713 return false;
5714 }
5715
5716 /**
5717 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005718 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005719 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005720 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5721 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005722 * @return The user specified next view, or null if there is none.
5723 */
5724 View findUserSetNextFocus(View root, int direction) {
5725 switch (direction) {
5726 case FOCUS_LEFT:
5727 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005728 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005729 case FOCUS_RIGHT:
5730 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005731 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005732 case FOCUS_UP:
5733 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005734 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005735 case FOCUS_DOWN:
5736 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005737 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005738 case FOCUS_FORWARD:
5739 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005740 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005741 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005742 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005743 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005744 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005745 @Override
5746 public boolean apply(View t) {
5747 return t.mNextFocusForwardId == id;
5748 }
5749 });
5750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005751 }
5752 return null;
5753 }
5754
Jeff Brown4dfbec22011-08-15 14:55:37 -07005755 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5756 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5757 @Override
5758 public boolean apply(View t) {
5759 return t.mID == childViewId;
5760 }
5761 });
5762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005763 if (result == null) {
5764 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5765 + "by user for id " + childViewId);
5766 }
5767 return result;
5768 }
5769
5770 /**
5771 * Find and return all focusable views that are descendants of this view,
5772 * possibly including this view if it is focusable itself.
5773 *
5774 * @param direction The direction of the focus
5775 * @return A list of focusable views
5776 */
5777 public ArrayList<View> getFocusables(int direction) {
5778 ArrayList<View> result = new ArrayList<View>(24);
5779 addFocusables(result, direction);
5780 return result;
5781 }
5782
5783 /**
5784 * Add any focusable views that are descendants of this view (possibly
5785 * including this view if it is focusable itself) to views. If we are in touch mode,
5786 * only add views that are also focusable in touch mode.
5787 *
5788 * @param views Focusable views found so far
5789 * @param direction The direction of the focus
5790 */
5791 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005792 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5793 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005794
svetoslavganov75986cf2009-05-14 22:28:01 -07005795 /**
5796 * Adds any focusable views that are descendants of this view (possibly
5797 * including this view if it is focusable itself) to views. This method
5798 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07005799 * only views focusable in touch mode if we are in touch mode or
5800 * only views that can take accessibility focus if accessibility is enabeld
5801 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07005802 *
5803 * @param views Focusable views found so far or null if all we are interested is
5804 * the number of focusables.
5805 * @param direction The direction of the focus.
5806 * @param focusableMode The type of focusables to be added.
5807 *
5808 * @see #FOCUSABLES_ALL
5809 * @see #FOCUSABLES_TOUCH_MODE
5810 */
5811 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07005812 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005813 return;
5814 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07005815 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
5816 if (AccessibilityManager.getInstance(mContext).isEnabled()
5817 && includeForAccessibility()) {
5818 views.add(this);
5819 return;
5820 }
5821 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005822 if (!isFocusable()) {
5823 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07005824 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005825 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
5826 && isInTouchMode() && !isFocusableInTouchMode()) {
5827 return;
5828 }
5829 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005830 }
5831
5832 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005833 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005834 * The search is performed by either the text that the View renders or the content
5835 * description that describes the view for accessibility purposes and the view does
5836 * not render or both. Clients can specify how the search is to be performed via
5837 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5838 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005839 *
5840 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005841 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005842 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005843 * @see #FIND_VIEWS_WITH_TEXT
5844 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5845 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005846 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005847 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005848 if (getAccessibilityNodeProvider() != null) {
5849 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5850 outViews.add(this);
5851 }
5852 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5853 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005854 String searchedLowerCase = searched.toString().toLowerCase();
5855 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5856 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5857 outViews.add(this);
5858 }
5859 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005860 }
5861
5862 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005863 * Find and return all touchable views that are descendants of this view,
5864 * possibly including this view if it is touchable itself.
5865 *
5866 * @return A list of touchable views
5867 */
5868 public ArrayList<View> getTouchables() {
5869 ArrayList<View> result = new ArrayList<View>();
5870 addTouchables(result);
5871 return result;
5872 }
5873
5874 /**
5875 * Add any touchable views that are descendants of this view (possibly
5876 * including this view if it is touchable itself) to views.
5877 *
5878 * @param views Touchable views found so far
5879 */
5880 public void addTouchables(ArrayList<View> views) {
5881 final int viewFlags = mViewFlags;
5882
5883 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5884 && (viewFlags & ENABLED_MASK) == ENABLED) {
5885 views.add(this);
5886 }
5887 }
5888
5889 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07005890 * Returns whether this View is accessibility focused.
5891 *
5892 * @return True if this View is accessibility focused.
5893 */
5894 boolean isAccessibilityFocused() {
5895 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
5896 }
5897
5898 /**
5899 * Call this to try to give accessibility focus to this view.
5900 *
5901 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
5902 * returns false or the view is no visible or the view already has accessibility
5903 * focus.
5904 *
5905 * See also {@link #focusSearch(int)}, which is what you call to say that you
5906 * have focus, and you want your parent to look for the next one.
5907 *
5908 * @return Whether this view actually took accessibility focus.
5909 *
5910 * @hide
5911 */
5912 public boolean requestAccessibilityFocus() {
5913 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
5914 return false;
5915 }
5916 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5917 return false;
5918 }
5919 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
5920 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
5921 ViewRootImpl viewRootImpl = getViewRootImpl();
5922 if (viewRootImpl != null) {
5923 viewRootImpl.setAccessibilityFocusedHost(this);
5924 }
5925 invalidate();
5926 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
5927 notifyAccessibilityStateChanged();
5928 // Try to give input focus to this view - not a descendant.
5929 requestFocusNoSearch(View.FOCUS_DOWN, null);
5930 return true;
5931 }
5932 return false;
5933 }
5934
5935 /**
5936 * Call this to try to clear accessibility focus of this view.
5937 *
5938 * See also {@link #focusSearch(int)}, which is what you call to say that you
5939 * have focus, and you want your parent to look for the next one.
5940 *
5941 * @hide
5942 */
5943 public void clearAccessibilityFocus() {
5944 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
5945 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
5946 ViewRootImpl viewRootImpl = getViewRootImpl();
5947 if (viewRootImpl != null) {
5948 viewRootImpl.setAccessibilityFocusedHost(null);
5949 }
5950 invalidate();
5951 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
5952 notifyAccessibilityStateChanged();
5953 // Try to move accessibility focus to the input focus.
5954 View rootView = getRootView();
5955 if (rootView != null) {
5956 View inputFocus = rootView.findFocus();
5957 if (inputFocus != null) {
5958 inputFocus.requestAccessibilityFocus();
5959 }
5960 }
5961 }
5962 }
5963
5964 /**
5965 * Find the best view to take accessibility focus from a hover.
5966 * This function finds the deepest actionable view and if that
5967 * fails ask the parent to take accessibility focus from hover.
5968 *
5969 * @param x The X hovered location in this view coorditantes.
5970 * @param y The Y hovered location in this view coorditantes.
5971 * @return Whether the request was handled.
5972 *
5973 * @hide
5974 */
5975 public boolean requestAccessibilityFocusFromHover(float x, float y) {
5976 if (onRequestAccessibilityFocusFromHover(x, y)) {
5977 return true;
5978 }
5979 ViewParent parent = mParent;
5980 if (parent instanceof View) {
5981 View parentView = (View) parent;
5982
5983 float[] position = mAttachInfo.mTmpTransformLocation;
5984 position[0] = x;
5985 position[1] = y;
5986
5987 // Compensate for the transformation of the current matrix.
5988 if (!hasIdentityMatrix()) {
5989 getMatrix().mapPoints(position);
5990 }
5991
5992 // Compensate for the parent scroll and the offset
5993 // of this view stop from the parent top.
5994 position[0] += mLeft - parentView.mScrollX;
5995 position[1] += mTop - parentView.mScrollY;
5996
5997 return parentView.requestAccessibilityFocusFromHover(position[0], position[1]);
5998 }
5999 return false;
6000 }
6001
6002 /**
6003 * Requests to give this View focus from hover.
6004 *
6005 * @param x The X hovered location in this view coorditantes.
6006 * @param y The Y hovered location in this view coorditantes.
6007 * @return Whether the request was handled.
6008 *
6009 * @hide
6010 */
6011 public boolean onRequestAccessibilityFocusFromHover(float x, float y) {
6012 if (includeForAccessibility()
6013 && (isActionableForAccessibility() || hasListenersForAccessibility())) {
6014 return requestAccessibilityFocus();
6015 }
6016 return false;
6017 }
6018
6019 /**
6020 * Clears accessibility focus without calling any callback methods
6021 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6022 * is used for clearing accessibility focus when giving this focus to
6023 * another view.
6024 */
6025 void clearAccessibilityFocusNoCallbacks() {
6026 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6027 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6028 invalidate();
6029 }
6030 }
6031
6032 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006033 * Call this to try to give focus to a specific view or to one of its
6034 * descendants.
6035 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006036 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6037 * false), or if it is focusable and it is not focusable in touch mode
6038 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006040 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006041 * have focus, and you want your parent to look for the next one.
6042 *
6043 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6044 * {@link #FOCUS_DOWN} and <code>null</code>.
6045 *
6046 * @return Whether this view or one of its descendants actually took focus.
6047 */
6048 public final boolean requestFocus() {
6049 return requestFocus(View.FOCUS_DOWN);
6050 }
6051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006052 /**
6053 * Call this to try to give focus to a specific view or to one of its
6054 * descendants and give it a hint about what direction focus is heading.
6055 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006056 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6057 * false), or if it is focusable and it is not focusable in touch mode
6058 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006059 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006060 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006061 * have focus, and you want your parent to look for the next one.
6062 *
6063 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6064 * <code>null</code> set for the previously focused rectangle.
6065 *
6066 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6067 * @return Whether this view or one of its descendants actually took focus.
6068 */
6069 public final boolean requestFocus(int direction) {
6070 return requestFocus(direction, null);
6071 }
6072
6073 /**
6074 * Call this to try to give focus to a specific view or to one of its descendants
6075 * and give it hints about the direction and a specific rectangle that the focus
6076 * is coming from. The rectangle can help give larger views a finer grained hint
6077 * about where focus is coming from, and therefore, where to show selection, or
6078 * forward focus change internally.
6079 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006080 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6081 * false), or if it is focusable and it is not focusable in touch mode
6082 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006083 *
6084 * A View will not take focus if it is not visible.
6085 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006086 * A View will not take focus if one of its parents has
6087 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6088 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006089 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006090 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006091 * have focus, and you want your parent to look for the next one.
6092 *
6093 * You may wish to override this method if your custom {@link View} has an internal
6094 * {@link View} that it wishes to forward the request to.
6095 *
6096 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6097 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6098 * to give a finer grained hint about where focus is coming from. May be null
6099 * if there is no hint.
6100 * @return Whether this view or one of its descendants actually took focus.
6101 */
6102 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006103 return requestFocusNoSearch(direction, previouslyFocusedRect);
6104 }
6105
6106 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006107 // need to be focusable
6108 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6109 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6110 return false;
6111 }
6112
6113 // need to be focusable in touch mode if in touch mode
6114 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006115 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6116 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006117 }
6118
6119 // need to not have any parents blocking us
6120 if (hasAncestorThatBlocksDescendantFocus()) {
6121 return false;
6122 }
6123
6124 handleFocusGainInternal(direction, previouslyFocusedRect);
6125 return true;
6126 }
6127
6128 /**
6129 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6130 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6131 * touch mode to request focus when they are touched.
6132 *
6133 * @return Whether this view or one of its descendants actually took focus.
6134 *
6135 * @see #isInTouchMode()
6136 *
6137 */
6138 public final boolean requestFocusFromTouch() {
6139 // Leave touch mode if we need to
6140 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006141 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006142 if (viewRoot != null) {
6143 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006144 }
6145 }
6146 return requestFocus(View.FOCUS_DOWN);
6147 }
6148
6149 /**
6150 * @return Whether any ancestor of this view blocks descendant focus.
6151 */
6152 private boolean hasAncestorThatBlocksDescendantFocus() {
6153 ViewParent ancestor = mParent;
6154 while (ancestor instanceof ViewGroup) {
6155 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6156 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6157 return true;
6158 } else {
6159 ancestor = vgAncestor.getParent();
6160 }
6161 }
6162 return false;
6163 }
6164
6165 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006166 * Gets the mode for determining whether this View is important for accessibility
6167 * which is if it fires accessibility events and if it is reported to
6168 * accessibility services that query the screen.
6169 *
6170 * @return The mode for determining whether a View is important for accessibility.
6171 *
6172 * @attr ref android.R.styleable#View_importantForAccessibility
6173 *
6174 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6175 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6176 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6177 */
6178 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6179 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO,
6180 to = "IMPORTANT_FOR_ACCESSIBILITY_AUTO"),
6181 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES,
6182 to = "IMPORTANT_FOR_ACCESSIBILITY_YES"),
6183 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO,
6184 to = "IMPORTANT_FOR_ACCESSIBILITY_NO")
6185 })
6186 public int getImportantForAccessibility() {
6187 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6188 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6189 }
6190
6191 /**
6192 * Sets how to determine whether this view is important for accessibility
6193 * which is if it fires accessibility events and if it is reported to
6194 * accessibility services that query the screen.
6195 *
6196 * @param mode How to determine whether this view is important for accessibility.
6197 *
6198 * @attr ref android.R.styleable#View_importantForAccessibility
6199 *
6200 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6201 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6202 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6203 */
6204 public void setImportantForAccessibility(int mode) {
6205 if (mode != getImportantForAccessibility()) {
6206 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6207 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6208 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6209 notifyAccessibilityStateChanged();
6210 }
6211 }
6212
6213 /**
6214 * Gets whether this view should be exposed for accessibility.
6215 *
6216 * @return Whether the view is exposed for accessibility.
6217 *
6218 * @hide
6219 */
6220 public boolean isImportantForAccessibility() {
6221 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6222 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6223 switch (mode) {
6224 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6225 return true;
6226 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6227 return false;
6228 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6229 return isActionableForAccessibility() || hasListenersForAccessibility();
6230 default:
6231 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6232 + mode);
6233 }
6234 }
6235
6236 /**
6237 * Gets the parent for accessibility purposes. Note that the parent for
6238 * accessibility is not necessary the immediate parent. It is the first
6239 * predecessor that is important for accessibility.
6240 *
6241 * @return The parent for accessibility purposes.
6242 */
6243 public ViewParent getParentForAccessibility() {
6244 if (mParent instanceof View) {
6245 View parentView = (View) mParent;
6246 if (parentView.includeForAccessibility()) {
6247 return mParent;
6248 } else {
6249 return mParent.getParentForAccessibility();
6250 }
6251 }
6252 return null;
6253 }
6254
6255 /**
6256 * Adds the children of a given View for accessibility. Since some Views are
6257 * not important for accessibility the children for accessibility are not
6258 * necessarily direct children of the riew, rather they are the first level of
6259 * descendants important for accessibility.
6260 *
6261 * @param children The list of children for accessibility.
6262 */
6263 public void addChildrenForAccessibility(ArrayList<View> children) {
6264 if (includeForAccessibility()) {
6265 children.add(this);
6266 }
6267 }
6268
6269 /**
6270 * Whether to regard this view for accessibility. A view is regarded for
6271 * accessibility if it is important for accessibility or the querying
6272 * accessibility service has explicitly requested that view not
6273 * important for accessibility are regarded.
6274 *
6275 * @return Whether to regard the view for accessibility.
6276 */
6277 boolean includeForAccessibility() {
6278 if (mAttachInfo != null) {
6279 if (!mAttachInfo.mIncludeNotImportantViews) {
6280 return isImportantForAccessibility();
6281 } else {
6282 return true;
6283 }
6284 }
6285 return false;
6286 }
6287
6288 /**
6289 * Returns whether the View is considered actionable from
6290 * accessibility perspective. Such view are important for
6291 * accessiiblity.
6292 *
6293 * @return True if the view is actionable for accessibility.
6294 */
6295 private boolean isActionableForAccessibility() {
6296 return (isClickable() || isLongClickable() || isFocusable());
6297 }
6298
6299 /**
6300 * Returns whether the View has registered callbacks wich makes it
6301 * important for accessiiblity.
6302 *
6303 * @return True if the view is actionable for accessibility.
6304 */
6305 private boolean hasListenersForAccessibility() {
6306 ListenerInfo info = getListenerInfo();
6307 return mTouchDelegate != null || info.mOnKeyListener != null
6308 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6309 || info.mOnHoverListener != null || info.mOnDragListener != null;
6310 }
6311
6312 /**
6313 * Notifies accessibility services that some view's important for
6314 * accessibility state has changed. Note that such notifications
6315 * are made at most once every
6316 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6317 * to avoid unnecessary load to the system. Also once a view has
6318 * made a notifucation this method is a NOP until the notification has
6319 * been sent to clients.
6320 *
6321 * @hide
6322 *
6323 * TODO: Makse sure this method is called for any view state change
6324 * that is interesting for accessilility purposes.
6325 */
6326 public void notifyAccessibilityStateChanged() {
6327 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6328 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6329 if (mParent != null) {
6330 mParent.childAccessibilityStateChanged(this);
6331 }
6332 }
6333 }
6334
6335 /**
6336 * Reset the state indicating the this view has requested clients
6337 * interested in its accessiblity state to be notified.
6338 *
6339 * @hide
6340 */
6341 public void resetAccessibilityStateChanged() {
6342 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6343 }
6344
6345 /**
6346 * Performs the specified accessibility action on the view. For
6347 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
6348 *
6349 * @param action The action to perform.
6350 * @return Whether the action was performed.
6351 */
6352 public boolean performAccessibilityAction(int action) {
6353 switch (action) {
6354 case AccessibilityNodeInfo.ACTION_CLICK: {
6355 final long now = SystemClock.uptimeMillis();
6356 // Send down.
6357 MotionEvent event = MotionEvent.obtain(now, now, MotionEvent.ACTION_DOWN,
6358 getWidth() / 2, getHeight() / 2, 0);
6359 onTouchEvent(event);
6360 // Send up.
6361 event.setAction(MotionEvent.ACTION_UP);
6362 onTouchEvent(event);
6363 // Clean up.
6364 event.recycle();
6365 } break;
6366 case AccessibilityNodeInfo.ACTION_FOCUS: {
6367 if (!hasFocus()) {
6368 // Get out of touch mode since accessibility
6369 // wants to move focus around.
6370 getViewRootImpl().ensureTouchMode(false);
6371 return requestFocus();
6372 }
6373 } break;
6374 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6375 if (hasFocus()) {
6376 clearFocus();
6377 return !isFocused();
6378 }
6379 } break;
6380 case AccessibilityNodeInfo.ACTION_SELECT: {
6381 if (!isSelected()) {
6382 setSelected(true);
6383 return isSelected();
6384 }
6385 } break;
6386 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6387 if (isSelected()) {
6388 setSelected(false);
6389 return !isSelected();
6390 }
6391 } break;
6392 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
6393 if (!isAccessibilityFocused()) {
6394 return requestAccessibilityFocus();
6395 }
6396 } break;
6397 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6398 if (isAccessibilityFocused()) {
6399 clearAccessibilityFocus();
6400 return true;
6401 }
6402 } break;
6403 }
6404 return false;
6405 }
6406
6407 /**
Romain Guya440b002010-02-24 15:57:54 -08006408 * @hide
6409 */
6410 public void dispatchStartTemporaryDetach() {
6411 onStartTemporaryDetach();
6412 }
6413
6414 /**
6415 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006416 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6417 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006418 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006419 */
6420 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006421 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006422 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006423 }
6424
6425 /**
6426 * @hide
6427 */
6428 public void dispatchFinishTemporaryDetach() {
6429 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006430 }
Romain Guy8506ab42009-06-11 17:35:47 -07006431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006432 /**
6433 * Called after {@link #onStartTemporaryDetach} when the container is done
6434 * changing the view.
6435 */
6436 public void onFinishTemporaryDetach() {
6437 }
Romain Guy8506ab42009-06-11 17:35:47 -07006438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006439 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006440 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6441 * for this view's window. Returns null if the view is not currently attached
6442 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006443 * just use the standard high-level event callbacks like
6444 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006445 */
6446 public KeyEvent.DispatcherState getKeyDispatcherState() {
6447 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6448 }
Joe Malin32736f02011-01-19 16:14:20 -08006449
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006450 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006451 * Dispatch a key event before it is processed by any input method
6452 * associated with the view hierarchy. This can be used to intercept
6453 * key events in special situations before the IME consumes them; a
6454 * typical example would be handling the BACK key to update the application's
6455 * UI instead of allowing the IME to see it and close itself.
6456 *
6457 * @param event The key event to be dispatched.
6458 * @return True if the event was handled, false otherwise.
6459 */
6460 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6461 return onKeyPreIme(event.getKeyCode(), event);
6462 }
6463
6464 /**
6465 * Dispatch a key event to the next view on the focus path. This path runs
6466 * from the top of the view tree down to the currently focused view. If this
6467 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6468 * the next node down the focus path. This method also fires any key
6469 * listeners.
6470 *
6471 * @param event The key event to be dispatched.
6472 * @return True if the event was handled, false otherwise.
6473 */
6474 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006475 if (mInputEventConsistencyVerifier != null) {
6476 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006478
Jeff Brown21bc5c92011-02-28 18:27:14 -08006479 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006480 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006481 ListenerInfo li = mListenerInfo;
6482 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6483 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006484 return true;
6485 }
6486
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006487 if (event.dispatch(this, mAttachInfo != null
6488 ? mAttachInfo.mKeyDispatchState : null, this)) {
6489 return true;
6490 }
6491
6492 if (mInputEventConsistencyVerifier != null) {
6493 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6494 }
6495 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006496 }
6497
6498 /**
6499 * Dispatches a key shortcut event.
6500 *
6501 * @param event The key event to be dispatched.
6502 * @return True if the event was handled by the view, false otherwise.
6503 */
6504 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6505 return onKeyShortcut(event.getKeyCode(), event);
6506 }
6507
6508 /**
6509 * Pass the touch screen motion event down to the target view, or this
6510 * view if it is the target.
6511 *
6512 * @param event The motion event to be dispatched.
6513 * @return True if the event was handled by the view, false otherwise.
6514 */
6515 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006516 if (mInputEventConsistencyVerifier != null) {
6517 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6518 }
6519
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006520 if (onFilterTouchEventForSecurity(event)) {
6521 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006522 ListenerInfo li = mListenerInfo;
6523 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6524 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006525 return true;
6526 }
6527
6528 if (onTouchEvent(event)) {
6529 return true;
6530 }
Jeff Brown85a31762010-09-01 17:01:00 -07006531 }
6532
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006533 if (mInputEventConsistencyVerifier != null) {
6534 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006535 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006536 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006537 }
6538
6539 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006540 * Filter the touch event to apply security policies.
6541 *
6542 * @param event The motion event to be filtered.
6543 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006544 *
Jeff Brown85a31762010-09-01 17:01:00 -07006545 * @see #getFilterTouchesWhenObscured
6546 */
6547 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006548 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006549 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6550 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6551 // Window is obscured, drop this touch.
6552 return false;
6553 }
6554 return true;
6555 }
6556
6557 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006558 * Pass a trackball motion event down to the focused view.
6559 *
6560 * @param event The motion event to be dispatched.
6561 * @return True if the event was handled by the view, false otherwise.
6562 */
6563 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006564 if (mInputEventConsistencyVerifier != null) {
6565 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6566 }
6567
Romain Guy02ccac62011-06-24 13:20:23 -07006568 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006569 }
6570
6571 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006572 * Dispatch a generic motion event.
6573 * <p>
6574 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6575 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006576 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006577 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006578 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006579 *
6580 * @param event The motion event to be dispatched.
6581 * @return True if the event was handled by the view, false otherwise.
6582 */
6583 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006584 if (mInputEventConsistencyVerifier != null) {
6585 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6586 }
6587
Jeff Browna032cc02011-03-07 16:56:21 -08006588 final int source = event.getSource();
6589 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6590 final int action = event.getAction();
6591 if (action == MotionEvent.ACTION_HOVER_ENTER
6592 || action == MotionEvent.ACTION_HOVER_MOVE
6593 || action == MotionEvent.ACTION_HOVER_EXIT) {
6594 if (dispatchHoverEvent(event)) {
6595 return true;
6596 }
6597 } else if (dispatchGenericPointerEvent(event)) {
6598 return true;
6599 }
6600 } else if (dispatchGenericFocusedEvent(event)) {
6601 return true;
6602 }
6603
Jeff Brown10b62902011-06-20 16:40:37 -07006604 if (dispatchGenericMotionEventInternal(event)) {
6605 return true;
6606 }
6607
6608 if (mInputEventConsistencyVerifier != null) {
6609 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6610 }
6611 return false;
6612 }
6613
6614 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07006615 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006616 ListenerInfo li = mListenerInfo;
6617 if (li != null && li.mOnGenericMotionListener != null
6618 && (mViewFlags & ENABLED_MASK) == ENABLED
6619 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006620 return true;
6621 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006622
6623 if (onGenericMotionEvent(event)) {
6624 return true;
6625 }
6626
6627 if (mInputEventConsistencyVerifier != null) {
6628 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6629 }
6630 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006631 }
6632
6633 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006634 * Dispatch a hover event.
6635 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07006636 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07006637 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006638 * </p>
6639 *
6640 * @param event The motion event to be dispatched.
6641 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006642 */
6643 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07006644 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006645 ListenerInfo li = mListenerInfo;
6646 if (li != null && li.mOnHoverListener != null
6647 && (mViewFlags & ENABLED_MASK) == ENABLED
6648 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07006649 return true;
6650 }
6651
Jeff Browna032cc02011-03-07 16:56:21 -08006652 return onHoverEvent(event);
6653 }
6654
6655 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006656 * Returns true if the view has a child to which it has recently sent
6657 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
6658 * it does not have a hovered child, then it must be the innermost hovered view.
6659 * @hide
6660 */
6661 protected boolean hasHoveredChild() {
6662 return false;
6663 }
6664
6665 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006666 * Dispatch a generic motion event to the view under the first pointer.
6667 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006668 * Do not call this method directly.
6669 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006670 * </p>
6671 *
6672 * @param event The motion event to be dispatched.
6673 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006674 */
6675 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
6676 return false;
6677 }
6678
6679 /**
6680 * Dispatch a generic motion event to the currently focused view.
6681 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006682 * Do not call this method directly.
6683 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006684 * </p>
6685 *
6686 * @param event The motion event to be dispatched.
6687 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006688 */
6689 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
6690 return false;
6691 }
6692
6693 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006694 * Dispatch a pointer event.
6695 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006696 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
6697 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
6698 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08006699 * and should not be expected to handle other pointing device features.
6700 * </p>
6701 *
6702 * @param event The motion event to be dispatched.
6703 * @return True if the event was handled by the view, false otherwise.
6704 * @hide
6705 */
6706 public final boolean dispatchPointerEvent(MotionEvent event) {
6707 if (event.isTouchEvent()) {
6708 return dispatchTouchEvent(event);
6709 } else {
6710 return dispatchGenericMotionEvent(event);
6711 }
6712 }
6713
6714 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006715 * Called when the window containing this view gains or loses window focus.
6716 * ViewGroups should override to route to their children.
6717 *
6718 * @param hasFocus True if the window containing this view now has focus,
6719 * false otherwise.
6720 */
6721 public void dispatchWindowFocusChanged(boolean hasFocus) {
6722 onWindowFocusChanged(hasFocus);
6723 }
6724
6725 /**
6726 * Called when the window containing this view gains or loses focus. Note
6727 * that this is separate from view focus: to receive key events, both
6728 * your view and its window must have focus. If a window is displayed
6729 * on top of yours that takes input focus, then your own window will lose
6730 * focus but the view focus will remain unchanged.
6731 *
6732 * @param hasWindowFocus True if the window containing this view now has
6733 * focus, false otherwise.
6734 */
6735 public void onWindowFocusChanged(boolean hasWindowFocus) {
6736 InputMethodManager imm = InputMethodManager.peekInstance();
6737 if (!hasWindowFocus) {
6738 if (isPressed()) {
6739 setPressed(false);
6740 }
6741 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6742 imm.focusOut(this);
6743 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006744 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08006745 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07006746 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006747 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6748 imm.focusIn(this);
6749 }
6750 refreshDrawableState();
6751 }
6752
6753 /**
6754 * Returns true if this view is in a window that currently has window focus.
6755 * Note that this is not the same as the view itself having focus.
6756 *
6757 * @return True if this view is in a window that currently has window focus.
6758 */
6759 public boolean hasWindowFocus() {
6760 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
6761 }
6762
6763 /**
Adam Powell326d8082009-12-09 15:10:07 -08006764 * Dispatch a view visibility change down the view hierarchy.
6765 * ViewGroups should override to route to their children.
6766 * @param changedView The view whose visibility changed. Could be 'this' or
6767 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006768 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6769 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006770 */
6771 protected void dispatchVisibilityChanged(View changedView, int visibility) {
6772 onVisibilityChanged(changedView, visibility);
6773 }
6774
6775 /**
6776 * Called when the visibility of the view or an ancestor of the view is changed.
6777 * @param changedView The view whose visibility changed. Could be 'this' or
6778 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006779 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6780 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006781 */
6782 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006783 if (visibility == VISIBLE) {
6784 if (mAttachInfo != null) {
6785 initialAwakenScrollBars();
6786 } else {
6787 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
6788 }
6789 }
Adam Powell326d8082009-12-09 15:10:07 -08006790 }
6791
6792 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08006793 * Dispatch a hint about whether this view is displayed. For instance, when
6794 * a View moves out of the screen, it might receives a display hint indicating
6795 * the view is not displayed. Applications should not <em>rely</em> on this hint
6796 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006797 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006798 * @param hint A hint about whether or not this view is displayed:
6799 * {@link #VISIBLE} or {@link #INVISIBLE}.
6800 */
6801 public void dispatchDisplayHint(int hint) {
6802 onDisplayHint(hint);
6803 }
6804
6805 /**
6806 * Gives this view a hint about whether is displayed or not. For instance, when
6807 * a View moves out of the screen, it might receives a display hint indicating
6808 * the view is not displayed. Applications should not <em>rely</em> on this hint
6809 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006810 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006811 * @param hint A hint about whether or not this view is displayed:
6812 * {@link #VISIBLE} or {@link #INVISIBLE}.
6813 */
6814 protected void onDisplayHint(int hint) {
6815 }
6816
6817 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006818 * Dispatch a window visibility change down the view hierarchy.
6819 * ViewGroups should override to route to their children.
6820 *
6821 * @param visibility The new visibility of the window.
6822 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006823 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006824 */
6825 public void dispatchWindowVisibilityChanged(int visibility) {
6826 onWindowVisibilityChanged(visibility);
6827 }
6828
6829 /**
6830 * Called when the window containing has change its visibility
6831 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
6832 * that this tells you whether or not your window is being made visible
6833 * to the window manager; this does <em>not</em> tell you whether or not
6834 * your window is obscured by other windows on the screen, even if it
6835 * is itself visible.
6836 *
6837 * @param visibility The new visibility of the window.
6838 */
6839 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006840 if (visibility == VISIBLE) {
6841 initialAwakenScrollBars();
6842 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006843 }
6844
6845 /**
6846 * Returns the current visibility of the window this view is attached to
6847 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
6848 *
6849 * @return Returns the current visibility of the view's window.
6850 */
6851 public int getWindowVisibility() {
6852 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
6853 }
6854
6855 /**
6856 * Retrieve the overall visible display size in which the window this view is
6857 * attached to has been positioned in. This takes into account screen
6858 * decorations above the window, for both cases where the window itself
6859 * is being position inside of them or the window is being placed under
6860 * then and covered insets are used for the window to position its content
6861 * inside. In effect, this tells you the available area where content can
6862 * be placed and remain visible to users.
6863 *
6864 * <p>This function requires an IPC back to the window manager to retrieve
6865 * the requested information, so should not be used in performance critical
6866 * code like drawing.
6867 *
6868 * @param outRect Filled in with the visible display frame. If the view
6869 * is not attached to a window, this is simply the raw display size.
6870 */
6871 public void getWindowVisibleDisplayFrame(Rect outRect) {
6872 if (mAttachInfo != null) {
6873 try {
6874 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
6875 } catch (RemoteException e) {
6876 return;
6877 }
6878 // XXX This is really broken, and probably all needs to be done
6879 // in the window manager, and we need to know more about whether
6880 // we want the area behind or in front of the IME.
6881 final Rect insets = mAttachInfo.mVisibleInsets;
6882 outRect.left += insets.left;
6883 outRect.top += insets.top;
6884 outRect.right -= insets.right;
6885 outRect.bottom -= insets.bottom;
6886 return;
6887 }
6888 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006889 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006890 }
6891
6892 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006893 * Dispatch a notification about a resource configuration change down
6894 * the view hierarchy.
6895 * ViewGroups should override to route to their children.
6896 *
6897 * @param newConfig The new resource configuration.
6898 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006899 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006900 */
6901 public void dispatchConfigurationChanged(Configuration newConfig) {
6902 onConfigurationChanged(newConfig);
6903 }
6904
6905 /**
6906 * Called when the current configuration of the resources being used
6907 * by the application have changed. You can use this to decide when
6908 * to reload resources that can changed based on orientation and other
6909 * configuration characterstics. You only need to use this if you are
6910 * not relying on the normal {@link android.app.Activity} mechanism of
6911 * recreating the activity instance upon a configuration change.
6912 *
6913 * @param newConfig The new resource configuration.
6914 */
6915 protected void onConfigurationChanged(Configuration newConfig) {
6916 }
6917
6918 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006919 * Private function to aggregate all per-view attributes in to the view
6920 * root.
6921 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006922 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6923 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006924 }
6925
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006926 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6927 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08006928 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006929 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006930 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006931 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006932 ListenerInfo li = mListenerInfo;
6933 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006934 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006936 }
6937 }
6938
6939 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006940 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006941 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006942 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6943 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006944 ai.mRecomputeGlobalAttributes = true;
6945 }
6946 }
6947 }
6948
6949 /**
6950 * Returns whether the device is currently in touch mode. Touch mode is entered
6951 * once the user begins interacting with the device by touch, and affects various
6952 * things like whether focus is always visible to the user.
6953 *
6954 * @return Whether the device is in touch mode.
6955 */
6956 @ViewDebug.ExportedProperty
6957 public boolean isInTouchMode() {
6958 if (mAttachInfo != null) {
6959 return mAttachInfo.mInTouchMode;
6960 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006961 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006962 }
6963 }
6964
6965 /**
6966 * Returns the context the view is running in, through which it can
6967 * access the current theme, resources, etc.
6968 *
6969 * @return The view's Context.
6970 */
6971 @ViewDebug.CapturedViewProperty
6972 public final Context getContext() {
6973 return mContext;
6974 }
6975
6976 /**
6977 * Handle a key event before it is processed by any input method
6978 * associated with the view hierarchy. This can be used to intercept
6979 * key events in special situations before the IME consumes them; a
6980 * typical example would be handling the BACK key to update the application's
6981 * UI instead of allowing the IME to see it and close itself.
6982 *
6983 * @param keyCode The value in event.getKeyCode().
6984 * @param event Description of the key event.
6985 * @return If you handled the event, return true. If you want to allow the
6986 * event to be handled by the next receiver, return false.
6987 */
6988 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
6989 return false;
6990 }
6991
6992 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006993 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
6994 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006995 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
6996 * is released, if the view is enabled and clickable.
6997 *
6998 * @param keyCode A key code that represents the button pressed, from
6999 * {@link android.view.KeyEvent}.
7000 * @param event The KeyEvent object that defines the button action.
7001 */
7002 public boolean onKeyDown(int keyCode, KeyEvent event) {
7003 boolean result = false;
7004
7005 switch (keyCode) {
7006 case KeyEvent.KEYCODE_DPAD_CENTER:
7007 case KeyEvent.KEYCODE_ENTER: {
7008 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7009 return true;
7010 }
7011 // Long clickable items don't necessarily have to be clickable
7012 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7013 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7014 (event.getRepeatCount() == 0)) {
7015 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007016 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007017 return true;
7018 }
7019 break;
7020 }
7021 }
7022 return result;
7023 }
7024
7025 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007026 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7027 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7028 * the event).
7029 */
7030 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7031 return false;
7032 }
7033
7034 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007035 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7036 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007037 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7038 * {@link KeyEvent#KEYCODE_ENTER} is released.
7039 *
7040 * @param keyCode A key code that represents the button pressed, from
7041 * {@link android.view.KeyEvent}.
7042 * @param event The KeyEvent object that defines the button action.
7043 */
7044 public boolean onKeyUp(int keyCode, KeyEvent event) {
7045 boolean result = false;
7046
7047 switch (keyCode) {
7048 case KeyEvent.KEYCODE_DPAD_CENTER:
7049 case KeyEvent.KEYCODE_ENTER: {
7050 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7051 return true;
7052 }
7053 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7054 setPressed(false);
7055
7056 if (!mHasPerformedLongPress) {
7057 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007058 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007059
7060 result = performClick();
7061 }
7062 }
7063 break;
7064 }
7065 }
7066 return result;
7067 }
7068
7069 /**
7070 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7071 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7072 * the event).
7073 *
7074 * @param keyCode A key code that represents the button pressed, from
7075 * {@link android.view.KeyEvent}.
7076 * @param repeatCount The number of times the action was made.
7077 * @param event The KeyEvent object that defines the button action.
7078 */
7079 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7080 return false;
7081 }
7082
7083 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007084 * Called on the focused view when a key shortcut event is not handled.
7085 * Override this method to implement local key shortcuts for the View.
7086 * Key shortcuts can also be implemented by setting the
7087 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007088 *
7089 * @param keyCode The value in event.getKeyCode().
7090 * @param event Description of the key event.
7091 * @return If you handled the event, return true. If you want to allow the
7092 * event to be handled by the next receiver, return false.
7093 */
7094 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7095 return false;
7096 }
7097
7098 /**
7099 * Check whether the called view is a text editor, in which case it
7100 * would make sense to automatically display a soft input window for
7101 * it. Subclasses should override this if they implement
7102 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007103 * a call on that method would return a non-null InputConnection, and
7104 * they are really a first-class editor that the user would normally
7105 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007106 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007107 * <p>The default implementation always returns false. This does
7108 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7109 * will not be called or the user can not otherwise perform edits on your
7110 * view; it is just a hint to the system that this is not the primary
7111 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007112 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007113 * @return Returns true if this view is a text editor, else false.
7114 */
7115 public boolean onCheckIsTextEditor() {
7116 return false;
7117 }
Romain Guy8506ab42009-06-11 17:35:47 -07007118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007119 /**
7120 * Create a new InputConnection for an InputMethod to interact
7121 * with the view. The default implementation returns null, since it doesn't
7122 * support input methods. You can override this to implement such support.
7123 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007124 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007125 * <p>When implementing this, you probably also want to implement
7126 * {@link #onCheckIsTextEditor()} to indicate you will return a
7127 * non-null InputConnection.
7128 *
7129 * @param outAttrs Fill in with attribute information about the connection.
7130 */
7131 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7132 return null;
7133 }
7134
7135 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007136 * Called by the {@link android.view.inputmethod.InputMethodManager}
7137 * when a view who is not the current
7138 * input connection target is trying to make a call on the manager. The
7139 * default implementation returns false; you can override this to return
7140 * true for certain views if you are performing InputConnection proxying
7141 * to them.
7142 * @param view The View that is making the InputMethodManager call.
7143 * @return Return true to allow the call, false to reject.
7144 */
7145 public boolean checkInputConnectionProxy(View view) {
7146 return false;
7147 }
Romain Guy8506ab42009-06-11 17:35:47 -07007148
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007149 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007150 * Show the context menu for this view. It is not safe to hold on to the
7151 * menu after returning from this method.
7152 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007153 * You should normally not overload this method. Overload
7154 * {@link #onCreateContextMenu(ContextMenu)} or define an
7155 * {@link OnCreateContextMenuListener} to add items to the context menu.
7156 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007157 * @param menu The context menu to populate
7158 */
7159 public void createContextMenu(ContextMenu menu) {
7160 ContextMenuInfo menuInfo = getContextMenuInfo();
7161
7162 // Sets the current menu info so all items added to menu will have
7163 // my extra info set.
7164 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7165
7166 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007167 ListenerInfo li = mListenerInfo;
7168 if (li != null && li.mOnCreateContextMenuListener != null) {
7169 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007170 }
7171
7172 // Clear the extra information so subsequent items that aren't mine don't
7173 // have my extra info.
7174 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7175
7176 if (mParent != null) {
7177 mParent.createContextMenu(menu);
7178 }
7179 }
7180
7181 /**
7182 * Views should implement this if they have extra information to associate
7183 * with the context menu. The return result is supplied as a parameter to
7184 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7185 * callback.
7186 *
7187 * @return Extra information about the item for which the context menu
7188 * should be shown. This information will vary across different
7189 * subclasses of View.
7190 */
7191 protected ContextMenuInfo getContextMenuInfo() {
7192 return null;
7193 }
7194
7195 /**
7196 * Views should implement this if the view itself is going to add items to
7197 * the context menu.
7198 *
7199 * @param menu the context menu to populate
7200 */
7201 protected void onCreateContextMenu(ContextMenu menu) {
7202 }
7203
7204 /**
7205 * Implement this method to handle trackball motion events. The
7206 * <em>relative</em> movement of the trackball since the last event
7207 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7208 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7209 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7210 * they will often be fractional values, representing the more fine-grained
7211 * movement information available from a trackball).
7212 *
7213 * @param event The motion event.
7214 * @return True if the event was handled, false otherwise.
7215 */
7216 public boolean onTrackballEvent(MotionEvent event) {
7217 return false;
7218 }
7219
7220 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007221 * Implement this method to handle generic motion events.
7222 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007223 * Generic motion events describe joystick movements, mouse hovers, track pad
7224 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007225 * {@link MotionEvent#getSource() source} of the motion event specifies
7226 * the class of input that was received. Implementations of this method
7227 * must examine the bits in the source before processing the event.
7228 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007229 * </p><p>
7230 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7231 * are delivered to the view under the pointer. All other generic motion events are
7232 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007233 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007234 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007235 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007236 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7237 * // process the joystick movement...
7238 * return true;
7239 * }
7240 * }
7241 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7242 * switch (event.getAction()) {
7243 * case MotionEvent.ACTION_HOVER_MOVE:
7244 * // process the mouse hover movement...
7245 * return true;
7246 * case MotionEvent.ACTION_SCROLL:
7247 * // process the scroll wheel movement...
7248 * return true;
7249 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007250 * }
7251 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007252 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007253 *
7254 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007255 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007256 */
7257 public boolean onGenericMotionEvent(MotionEvent event) {
7258 return false;
7259 }
7260
7261 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007262 * Implement this method to handle hover events.
7263 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007264 * This method is called whenever a pointer is hovering into, over, or out of the
7265 * bounds of a view and the view is not currently being touched.
7266 * Hover events are represented as pointer events with action
7267 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7268 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7269 * </p>
7270 * <ul>
7271 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7272 * when the pointer enters the bounds of the view.</li>
7273 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7274 * when the pointer has already entered the bounds of the view and has moved.</li>
7275 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7276 * when the pointer has exited the bounds of the view or when the pointer is
7277 * about to go down due to a button click, tap, or similar user action that
7278 * causes the view to be touched.</li>
7279 * </ul>
7280 * <p>
7281 * The view should implement this method to return true to indicate that it is
7282 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007283 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007284 * The default implementation calls {@link #setHovered} to update the hovered state
7285 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007286 * is enabled and is clickable. The default implementation also sends hover
7287 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007288 * </p>
7289 *
7290 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007291 * @return True if the view handled the hover event.
7292 *
7293 * @see #isHovered
7294 * @see #setHovered
7295 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007296 */
7297 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007298 // The root view may receive hover (or touch) events that are outside the bounds of
7299 // the window. This code ensures that we only send accessibility events for
7300 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007301 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007302 if (!mSendingHoverAccessibilityEvents) {
7303 if ((action == MotionEvent.ACTION_HOVER_ENTER
7304 || action == MotionEvent.ACTION_HOVER_MOVE)
7305 && !hasHoveredChild()
7306 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007307 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007308 mSendingHoverAccessibilityEvents = true;
7309 requestAccessibilityFocusFromHover((int) event.getX(), (int) event.getY());
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007310 }
7311 } else {
7312 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007313 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007314 && !pointInView(event.getX(), event.getY()))) {
7315 mSendingHoverAccessibilityEvents = false;
7316 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007317 // If the window does not have input focus we take away accessibility
7318 // focus as soon as the user stop hovering over the view.
7319 if (!mAttachInfo.mHasWindowFocus) {
7320 getViewRootImpl().setAccessibilityFocusedHost(null);
7321 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007322 }
Jeff Browna1b24182011-07-28 13:38:24 -07007323 }
7324
Jeff Brown87b7f802011-06-21 18:35:45 -07007325 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007326 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007327 case MotionEvent.ACTION_HOVER_ENTER:
7328 setHovered(true);
7329 break;
7330 case MotionEvent.ACTION_HOVER_EXIT:
7331 setHovered(false);
7332 break;
7333 }
Jeff Browna1b24182011-07-28 13:38:24 -07007334
7335 // Dispatch the event to onGenericMotionEvent before returning true.
7336 // This is to provide compatibility with existing applications that
7337 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7338 // break because of the new default handling for hoverable views
7339 // in onHoverEvent.
7340 // Note that onGenericMotionEvent will be called by default when
7341 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7342 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007343 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007344 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007345
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007346 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007347 }
7348
7349 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007350 * Returns true if the view should handle {@link #onHoverEvent}
7351 * by calling {@link #setHovered} to change its hovered state.
7352 *
7353 * @return True if the view is hoverable.
7354 */
7355 private boolean isHoverable() {
7356 final int viewFlags = mViewFlags;
7357 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7358 return false;
7359 }
7360
7361 return (viewFlags & CLICKABLE) == CLICKABLE
7362 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7363 }
7364
7365 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007366 * Returns true if the view is currently hovered.
7367 *
7368 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007369 *
7370 * @see #setHovered
7371 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007372 */
Jeff Brown10b62902011-06-20 16:40:37 -07007373 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007374 public boolean isHovered() {
7375 return (mPrivateFlags & HOVERED) != 0;
7376 }
7377
7378 /**
7379 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007380 * <p>
7381 * Calling this method also changes the drawable state of the view. This
7382 * enables the view to react to hover by using different drawable resources
7383 * to change its appearance.
7384 * </p><p>
7385 * The {@link #onHoverChanged} method is called when the hovered state changes.
7386 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007387 *
7388 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007389 *
7390 * @see #isHovered
7391 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007392 */
7393 public void setHovered(boolean hovered) {
7394 if (hovered) {
7395 if ((mPrivateFlags & HOVERED) == 0) {
7396 mPrivateFlags |= HOVERED;
7397 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007398 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007399 }
7400 } else {
7401 if ((mPrivateFlags & HOVERED) != 0) {
7402 mPrivateFlags &= ~HOVERED;
7403 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007404 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007405 }
7406 }
7407 }
7408
7409 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007410 * Implement this method to handle hover state changes.
7411 * <p>
7412 * This method is called whenever the hover state changes as a result of a
7413 * call to {@link #setHovered}.
7414 * </p>
7415 *
7416 * @param hovered The current hover state, as returned by {@link #isHovered}.
7417 *
7418 * @see #isHovered
7419 * @see #setHovered
7420 */
7421 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007422 }
7423
7424 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007425 * Implement this method to handle touch screen motion events.
7426 *
7427 * @param event The motion event.
7428 * @return True if the event was handled, false otherwise.
7429 */
7430 public boolean onTouchEvent(MotionEvent event) {
7431 final int viewFlags = mViewFlags;
7432
7433 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007434 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007435 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007437 // A disabled view that is clickable still consumes the touch
7438 // events, it just doesn't respond to them.
7439 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7440 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7441 }
7442
7443 if (mTouchDelegate != null) {
7444 if (mTouchDelegate.onTouchEvent(event)) {
7445 return true;
7446 }
7447 }
7448
7449 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7450 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7451 switch (event.getAction()) {
7452 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007453 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7454 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007455 // take focus if we don't have it already and we should in
7456 // touch mode.
7457 boolean focusTaken = false;
7458 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7459 focusTaken = requestFocus();
7460 }
7461
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007462 if (prepressed) {
7463 // The button is being released before we actually
7464 // showed it as pressed. Make it show the pressed
7465 // state now (before scheduling the click) to ensure
7466 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007467 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007468 }
Joe Malin32736f02011-01-19 16:14:20 -08007469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007470 if (!mHasPerformedLongPress) {
7471 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007472 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007473
7474 // Only perform take click actions if we were in the pressed state
7475 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007476 // Use a Runnable and post this rather than calling
7477 // performClick directly. This lets other visual state
7478 // of the view update before click actions start.
7479 if (mPerformClick == null) {
7480 mPerformClick = new PerformClick();
7481 }
7482 if (!post(mPerformClick)) {
7483 performClick();
7484 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007485 }
7486 }
7487
7488 if (mUnsetPressedState == null) {
7489 mUnsetPressedState = new UnsetPressedState();
7490 }
7491
Adam Powelle14579b2009-12-16 18:39:52 -08007492 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007493 postDelayed(mUnsetPressedState,
7494 ViewConfiguration.getPressedStateDuration());
7495 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007496 // If the post failed, unpress right now
7497 mUnsetPressedState.run();
7498 }
Adam Powelle14579b2009-12-16 18:39:52 -08007499 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007500 }
7501 break;
7502
7503 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007504 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007505
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007506 if (performButtonActionOnTouchDown(event)) {
7507 break;
7508 }
7509
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007510 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007511 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007512
7513 // For views inside a scrolling container, delay the pressed feedback for
7514 // a short period in case this is a scroll.
7515 if (isInScrollingContainer) {
7516 mPrivateFlags |= PREPRESSED;
7517 if (mPendingCheckForTap == null) {
7518 mPendingCheckForTap = new CheckForTap();
7519 }
7520 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7521 } else {
7522 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007523 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007524 checkForLongClick(0);
7525 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007526 break;
7527
7528 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08007529 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08007530 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007531 break;
7532
7533 case MotionEvent.ACTION_MOVE:
7534 final int x = (int) event.getX();
7535 final int y = (int) event.getY();
7536
7537 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007538 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007539 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007540 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007541 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007542 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007543 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007544
Adam Powell4d6f0662012-02-21 15:11:11 -08007545 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007546 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007547 }
7548 break;
7549 }
7550 return true;
7551 }
7552
7553 return false;
7554 }
7555
7556 /**
Adam Powell10298662011-08-14 18:26:30 -07007557 * @hide
7558 */
7559 public boolean isInScrollingContainer() {
7560 ViewParent p = getParent();
7561 while (p != null && p instanceof ViewGroup) {
7562 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7563 return true;
7564 }
7565 p = p.getParent();
7566 }
7567 return false;
7568 }
7569
7570 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007571 * Remove the longpress detection timer.
7572 */
7573 private void removeLongPressCallback() {
7574 if (mPendingCheckForLongPress != null) {
7575 removeCallbacks(mPendingCheckForLongPress);
7576 }
7577 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007578
7579 /**
7580 * Remove the pending click action
7581 */
7582 private void removePerformClickCallback() {
7583 if (mPerformClick != null) {
7584 removeCallbacks(mPerformClick);
7585 }
7586 }
7587
Adam Powelle14579b2009-12-16 18:39:52 -08007588 /**
Romain Guya440b002010-02-24 15:57:54 -08007589 * Remove the prepress detection timer.
7590 */
7591 private void removeUnsetPressCallback() {
7592 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7593 setPressed(false);
7594 removeCallbacks(mUnsetPressedState);
7595 }
7596 }
7597
7598 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007599 * Remove the tap detection timer.
7600 */
7601 private void removeTapCallback() {
7602 if (mPendingCheckForTap != null) {
7603 mPrivateFlags &= ~PREPRESSED;
7604 removeCallbacks(mPendingCheckForTap);
7605 }
7606 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007607
7608 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007609 * Cancels a pending long press. Your subclass can use this if you
7610 * want the context menu to come up if the user presses and holds
7611 * at the same place, but you don't want it to come up if they press
7612 * and then move around enough to cause scrolling.
7613 */
7614 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05007615 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08007616
7617 /*
7618 * The prepressed state handled by the tap callback is a display
7619 * construct, but the tap callback will post a long press callback
7620 * less its own timeout. Remove it here.
7621 */
7622 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007623 }
7624
7625 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07007626 * Remove the pending callback for sending a
7627 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
7628 */
7629 private void removeSendViewScrolledAccessibilityEventCallback() {
7630 if (mSendViewScrolledAccessibilityEvent != null) {
7631 removeCallbacks(mSendViewScrolledAccessibilityEvent);
7632 }
7633 }
7634
7635 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007636 * Sets the TouchDelegate for this View.
7637 */
7638 public void setTouchDelegate(TouchDelegate delegate) {
7639 mTouchDelegate = delegate;
7640 }
7641
7642 /**
7643 * Gets the TouchDelegate for this View.
7644 */
7645 public TouchDelegate getTouchDelegate() {
7646 return mTouchDelegate;
7647 }
7648
7649 /**
7650 * Set flags controlling behavior of this view.
7651 *
7652 * @param flags Constant indicating the value which should be set
7653 * @param mask Constant indicating the bit range that should be changed
7654 */
7655 void setFlags(int flags, int mask) {
7656 int old = mViewFlags;
7657 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
7658
7659 int changed = mViewFlags ^ old;
7660 if (changed == 0) {
7661 return;
7662 }
7663 int privateFlags = mPrivateFlags;
7664
7665 /* Check if the FOCUSABLE bit has changed */
7666 if (((changed & FOCUSABLE_MASK) != 0) &&
7667 ((privateFlags & HAS_BOUNDS) !=0)) {
7668 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
7669 && ((privateFlags & FOCUSED) != 0)) {
7670 /* Give up focus if we are no longer focusable */
7671 clearFocus();
7672 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
7673 && ((privateFlags & FOCUSED) == 0)) {
7674 /*
7675 * Tell the view system that we are now available to take focus
7676 * if no one else already has it.
7677 */
7678 if (mParent != null) mParent.focusableViewAvailable(this);
7679 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007680 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7681 notifyAccessibilityStateChanged();
7682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007683 }
7684
7685 if ((flags & VISIBILITY_MASK) == VISIBLE) {
7686 if ((changed & VISIBILITY_MASK) != 0) {
7687 /*
Chet Haase4324ead2011-08-24 21:31:03 -07007688 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07007689 * it was not visible. Marking it drawn ensures that the invalidation will
7690 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007691 */
Chet Haaseaceafe62011-08-26 15:44:33 -07007692 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07007693 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007694
7695 needGlobalAttributesUpdate(true);
7696
7697 // a view becoming visible is worth notifying the parent
7698 // about in case nothing has focus. even if this specific view
7699 // isn't focusable, it may contain something that is, so let
7700 // the root view try to give this focus if nothing else does.
7701 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
7702 mParent.focusableViewAvailable(this);
7703 }
7704 }
7705 }
7706
7707 /* Check if the GONE bit has changed */
7708 if ((changed & GONE) != 0) {
7709 needGlobalAttributesUpdate(false);
7710 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007711
Romain Guyecd80ee2009-12-03 17:13:02 -08007712 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
7713 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007714 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08007715 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07007716 if (mParent instanceof View) {
7717 // GONE views noop invalidation, so invalidate the parent
7718 ((View) mParent).invalidate(true);
7719 }
7720 // Mark the view drawn to ensure that it gets invalidated properly the next
7721 // time it is visible and gets invalidated
7722 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007723 }
7724 if (mAttachInfo != null) {
7725 mAttachInfo.mViewVisibilityChanged = true;
7726 }
7727 }
7728
7729 /* Check if the VISIBLE bit has changed */
7730 if ((changed & INVISIBLE) != 0) {
7731 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07007732 /*
7733 * If this view is becoming invisible, set the DRAWN flag so that
7734 * the next invalidate() will not be skipped.
7735 */
7736 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007737
7738 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007739 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007740 if (getRootView() != this) {
7741 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007742 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007743 }
7744 }
7745 if (mAttachInfo != null) {
7746 mAttachInfo.mViewVisibilityChanged = true;
7747 }
7748 }
7749
Adam Powell326d8082009-12-09 15:10:07 -08007750 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07007751 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08007752 ((ViewGroup) mParent).onChildVisibilityChanged(this,
7753 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08007754 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07007755 } else if (mParent != null) {
7756 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07007757 }
Adam Powell326d8082009-12-09 15:10:07 -08007758 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
7759 }
7760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007761 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
7762 destroyDrawingCache();
7763 }
7764
7765 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
7766 destroyDrawingCache();
7767 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007768 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007769 }
7770
7771 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
7772 destroyDrawingCache();
7773 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7774 }
7775
7776 if ((changed & DRAW_MASK) != 0) {
7777 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07007778 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007779 mPrivateFlags &= ~SKIP_DRAW;
7780 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
7781 } else {
7782 mPrivateFlags |= SKIP_DRAW;
7783 }
7784 } else {
7785 mPrivateFlags &= ~SKIP_DRAW;
7786 }
7787 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08007788 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007789 }
7790
7791 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08007792 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007793 mParent.recomputeViewAttributes(this);
7794 }
7795 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007796
7797 if (AccessibilityManager.getInstance(mContext).isEnabled()
7798 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
7799 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
7800 notifyAccessibilityStateChanged();
7801 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007802 }
7803
7804 /**
7805 * Change the view's z order in the tree, so it's on top of other sibling
7806 * views
7807 */
7808 public void bringToFront() {
7809 if (mParent != null) {
7810 mParent.bringChildToFront(this);
7811 }
7812 }
7813
7814 /**
7815 * This is called in response to an internal scroll in this view (i.e., the
7816 * view scrolled its own contents). This is typically as a result of
7817 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
7818 * called.
7819 *
7820 * @param l Current horizontal scroll origin.
7821 * @param t Current vertical scroll origin.
7822 * @param oldl Previous horizontal scroll origin.
7823 * @param oldt Previous vertical scroll origin.
7824 */
7825 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07007826 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7827 postSendViewScrolledAccessibilityEventCallback();
7828 }
7829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007830 mBackgroundSizeChanged = true;
7831
7832 final AttachInfo ai = mAttachInfo;
7833 if (ai != null) {
7834 ai.mViewScrollChanged = true;
7835 }
7836 }
7837
7838 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007839 * Interface definition for a callback to be invoked when the layout bounds of a view
7840 * changes due to layout processing.
7841 */
7842 public interface OnLayoutChangeListener {
7843 /**
7844 * Called when the focus state of a view has changed.
7845 *
7846 * @param v The view whose state has changed.
7847 * @param left The new value of the view's left property.
7848 * @param top The new value of the view's top property.
7849 * @param right The new value of the view's right property.
7850 * @param bottom The new value of the view's bottom property.
7851 * @param oldLeft The previous value of the view's left property.
7852 * @param oldTop The previous value of the view's top property.
7853 * @param oldRight The previous value of the view's right property.
7854 * @param oldBottom The previous value of the view's bottom property.
7855 */
7856 void onLayoutChange(View v, int left, int top, int right, int bottom,
7857 int oldLeft, int oldTop, int oldRight, int oldBottom);
7858 }
7859
7860 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007861 * This is called during layout when the size of this view has changed. If
7862 * you were just added to the view hierarchy, you're called with the old
7863 * values of 0.
7864 *
7865 * @param w Current width of this view.
7866 * @param h Current height of this view.
7867 * @param oldw Old width of this view.
7868 * @param oldh Old height of this view.
7869 */
7870 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
7871 }
7872
7873 /**
7874 * Called by draw to draw the child views. This may be overridden
7875 * by derived classes to gain control just before its children are drawn
7876 * (but after its own view has been drawn).
7877 * @param canvas the canvas on which to draw the view
7878 */
7879 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007881 }
7882
7883 /**
7884 * Gets the parent of this view. Note that the parent is a
7885 * ViewParent and not necessarily a View.
7886 *
7887 * @return Parent of this view.
7888 */
7889 public final ViewParent getParent() {
7890 return mParent;
7891 }
7892
7893 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007894 * Set the horizontal scrolled position of your view. This will cause a call to
7895 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7896 * invalidated.
7897 * @param value the x position to scroll to
7898 */
7899 public void setScrollX(int value) {
7900 scrollTo(value, mScrollY);
7901 }
7902
7903 /**
7904 * Set the vertical scrolled position of your view. This will cause a call to
7905 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7906 * invalidated.
7907 * @param value the y position to scroll to
7908 */
7909 public void setScrollY(int value) {
7910 scrollTo(mScrollX, value);
7911 }
7912
7913 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007914 * Return the scrolled left position of this view. This is the left edge of
7915 * the displayed part of your view. You do not need to draw any pixels
7916 * farther left, since those are outside of the frame of your view on
7917 * screen.
7918 *
7919 * @return The left edge of the displayed part of your view, in pixels.
7920 */
7921 public final int getScrollX() {
7922 return mScrollX;
7923 }
7924
7925 /**
7926 * Return the scrolled top position of this view. This is the top edge of
7927 * the displayed part of your view. You do not need to draw any pixels above
7928 * it, since those are outside of the frame of your view on screen.
7929 *
7930 * @return The top edge of the displayed part of your view, in pixels.
7931 */
7932 public final int getScrollY() {
7933 return mScrollY;
7934 }
7935
7936 /**
7937 * Return the width of the your view.
7938 *
7939 * @return The width of your view, in pixels.
7940 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007941 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007942 public final int getWidth() {
7943 return mRight - mLeft;
7944 }
7945
7946 /**
7947 * Return the height of your view.
7948 *
7949 * @return The height of your view, in pixels.
7950 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007951 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007952 public final int getHeight() {
7953 return mBottom - mTop;
7954 }
7955
7956 /**
7957 * Return the visible drawing bounds of your view. Fills in the output
7958 * rectangle with the values from getScrollX(), getScrollY(),
7959 * getWidth(), and getHeight().
7960 *
7961 * @param outRect The (scrolled) drawing bounds of the view.
7962 */
7963 public void getDrawingRect(Rect outRect) {
7964 outRect.left = mScrollX;
7965 outRect.top = mScrollY;
7966 outRect.right = mScrollX + (mRight - mLeft);
7967 outRect.bottom = mScrollY + (mBottom - mTop);
7968 }
7969
7970 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007971 * Like {@link #getMeasuredWidthAndState()}, but only returns the
7972 * raw width component (that is the result is masked by
7973 * {@link #MEASURED_SIZE_MASK}).
7974 *
7975 * @return The raw measured width of this view.
7976 */
7977 public final int getMeasuredWidth() {
7978 return mMeasuredWidth & MEASURED_SIZE_MASK;
7979 }
7980
7981 /**
7982 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007983 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007984 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007985 * This should be used during measurement and layout calculations only. Use
7986 * {@link #getWidth()} to see how wide a view is after layout.
7987 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007988 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007989 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08007990 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007991 return mMeasuredWidth;
7992 }
7993
7994 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007995 * Like {@link #getMeasuredHeightAndState()}, but only returns the
7996 * raw width component (that is the result is masked by
7997 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007998 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007999 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008000 */
8001 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008002 return mMeasuredHeight & MEASURED_SIZE_MASK;
8003 }
8004
8005 /**
8006 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008007 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008008 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8009 * This should be used during measurement and layout calculations only. Use
8010 * {@link #getHeight()} to see how wide a view is after layout.
8011 *
8012 * @return The measured width of this view as a bit mask.
8013 */
8014 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008015 return mMeasuredHeight;
8016 }
8017
8018 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008019 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8020 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8021 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8022 * and the height component is at the shifted bits
8023 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8024 */
8025 public final int getMeasuredState() {
8026 return (mMeasuredWidth&MEASURED_STATE_MASK)
8027 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8028 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8029 }
8030
8031 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008032 * The transform matrix of this view, which is calculated based on the current
8033 * roation, scale, and pivot properties.
8034 *
8035 * @see #getRotation()
8036 * @see #getScaleX()
8037 * @see #getScaleY()
8038 * @see #getPivotX()
8039 * @see #getPivotY()
8040 * @return The current transform matrix for the view
8041 */
8042 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008043 if (mTransformationInfo != null) {
8044 updateMatrix();
8045 return mTransformationInfo.mMatrix;
8046 }
8047 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008048 }
8049
8050 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008051 * Utility function to determine if the value is far enough away from zero to be
8052 * considered non-zero.
8053 * @param value A floating point value to check for zero-ness
8054 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8055 */
8056 private static boolean nonzero(float value) {
8057 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8058 }
8059
8060 /**
Jeff Brown86671742010-09-30 20:00:15 -07008061 * Returns true if the transform matrix is the identity matrix.
8062 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008063 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008064 * @return True if the transform matrix is the identity matrix, false otherwise.
8065 */
Jeff Brown86671742010-09-30 20:00:15 -07008066 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008067 if (mTransformationInfo != null) {
8068 updateMatrix();
8069 return mTransformationInfo.mMatrixIsIdentity;
8070 }
8071 return true;
8072 }
8073
8074 void ensureTransformationInfo() {
8075 if (mTransformationInfo == null) {
8076 mTransformationInfo = new TransformationInfo();
8077 }
Jeff Brown86671742010-09-30 20:00:15 -07008078 }
8079
8080 /**
8081 * Recomputes the transform matrix if necessary.
8082 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008083 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008084 final TransformationInfo info = mTransformationInfo;
8085 if (info == null) {
8086 return;
8087 }
8088 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008089 // transform-related properties have changed since the last time someone
8090 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008091
8092 // Figure out if we need to update the pivot point
8093 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008094 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8095 info.mPrevWidth = mRight - mLeft;
8096 info.mPrevHeight = mBottom - mTop;
8097 info.mPivotX = info.mPrevWidth / 2f;
8098 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008099 }
8100 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008101 info.mMatrix.reset();
8102 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8103 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8104 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8105 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008106 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008107 if (info.mCamera == null) {
8108 info.mCamera = new Camera();
8109 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008110 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008111 info.mCamera.save();
8112 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8113 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8114 info.mCamera.getMatrix(info.matrix3D);
8115 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8116 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8117 info.mPivotY + info.mTranslationY);
8118 info.mMatrix.postConcat(info.matrix3D);
8119 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008120 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008121 info.mMatrixDirty = false;
8122 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8123 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008124 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008125 }
8126
8127 /**
8128 * Utility method to retrieve the inverse of the current mMatrix property.
8129 * We cache the matrix to avoid recalculating it when transform properties
8130 * have not changed.
8131 *
8132 * @return The inverse of the current matrix of this view.
8133 */
Jeff Brown86671742010-09-30 20:00:15 -07008134 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008135 final TransformationInfo info = mTransformationInfo;
8136 if (info != null) {
8137 updateMatrix();
8138 if (info.mInverseMatrixDirty) {
8139 if (info.mInverseMatrix == null) {
8140 info.mInverseMatrix = new Matrix();
8141 }
8142 info.mMatrix.invert(info.mInverseMatrix);
8143 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008144 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008145 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008146 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008147 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008148 }
8149
8150 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008151 * Gets the distance along the Z axis from the camera to this view.
8152 *
8153 * @see #setCameraDistance(float)
8154 *
8155 * @return The distance along the Z axis.
8156 */
8157 public float getCameraDistance() {
8158 ensureTransformationInfo();
8159 final float dpi = mResources.getDisplayMetrics().densityDpi;
8160 final TransformationInfo info = mTransformationInfo;
8161 if (info.mCamera == null) {
8162 info.mCamera = new Camera();
8163 info.matrix3D = new Matrix();
8164 }
8165 return -(info.mCamera.getLocationZ() * dpi);
8166 }
8167
8168 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008169 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8170 * views are drawn) from the camera to this view. The camera's distance
8171 * affects 3D transformations, for instance rotations around the X and Y
8172 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008173 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008174 * use a camera distance that's greater than the height (X axis rotation) or
8175 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008176 *
Romain Guya5364ee2011-02-24 14:46:04 -08008177 * <p>The distance of the camera from the view plane can have an affect on the
8178 * perspective distortion of the view when it is rotated around the x or y axis.
8179 * For example, a large distance will result in a large viewing angle, and there
8180 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008181 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008182 * also result in some drawing artifacts if the rotated view ends up partially
8183 * behind the camera (which is why the recommendation is to use a distance at
8184 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008185 *
Romain Guya5364ee2011-02-24 14:46:04 -08008186 * <p>The distance is expressed in "depth pixels." The default distance depends
8187 * on the screen density. For instance, on a medium density display, the
8188 * default distance is 1280. On a high density display, the default distance
8189 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008190 *
Romain Guya5364ee2011-02-24 14:46:04 -08008191 * <p>If you want to specify a distance that leads to visually consistent
8192 * results across various densities, use the following formula:</p>
8193 * <pre>
8194 * float scale = context.getResources().getDisplayMetrics().density;
8195 * view.setCameraDistance(distance * scale);
8196 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008197 *
Romain Guya5364ee2011-02-24 14:46:04 -08008198 * <p>The density scale factor of a high density display is 1.5,
8199 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008200 *
Romain Guya5364ee2011-02-24 14:46:04 -08008201 * @param distance The distance in "depth pixels", if negative the opposite
8202 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008203 *
8204 * @see #setRotationX(float)
8205 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008206 */
8207 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008208 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008209
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008210 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008211 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008212 final TransformationInfo info = mTransformationInfo;
8213 if (info.mCamera == null) {
8214 info.mCamera = new Camera();
8215 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008216 }
8217
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008218 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8219 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008220
Chet Haase9d1992d2012-03-13 11:03:25 -07008221 invalidateViewProperty(false, false);
Chet Haasea1cff502012-02-21 13:43:44 -08008222 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008223 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008224 }
Romain Guya5364ee2011-02-24 14:46:04 -08008225 }
8226
8227 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008228 * The degrees that the view is rotated around the pivot point.
8229 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008230 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008231 * @see #getPivotX()
8232 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008233 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008234 * @return The degrees of rotation.
8235 */
Chet Haasea5531132012-02-02 13:41:44 -08008236 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008237 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008238 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008239 }
8240
8241 /**
Chet Haase897247b2010-09-09 14:54:47 -07008242 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8243 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008244 *
8245 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008246 *
8247 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008248 * @see #getPivotX()
8249 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008250 * @see #setRotationX(float)
8251 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008252 *
8253 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008254 */
8255 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008256 ensureTransformationInfo();
8257 final TransformationInfo info = mTransformationInfo;
8258 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008259 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008260 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008261 info.mRotation = rotation;
8262 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008263 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008264 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8265 mDisplayList.setRotation(rotation);
8266 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008267 }
8268 }
8269
8270 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008271 * The degrees that the view is rotated around the vertical axis through the pivot point.
8272 *
8273 * @see #getPivotX()
8274 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008275 * @see #setRotationY(float)
8276 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008277 * @return The degrees of Y rotation.
8278 */
Chet Haasea5531132012-02-02 13:41:44 -08008279 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008280 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008281 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008282 }
8283
8284 /**
Chet Haase897247b2010-09-09 14:54:47 -07008285 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8286 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8287 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008288 *
Romain Guya5364ee2011-02-24 14:46:04 -08008289 * When rotating large views, it is recommended to adjust the camera distance
8290 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008291 *
8292 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008293 *
8294 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008295 * @see #getPivotX()
8296 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008297 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008298 * @see #setRotationX(float)
8299 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008300 *
8301 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008302 */
8303 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008304 ensureTransformationInfo();
8305 final TransformationInfo info = mTransformationInfo;
8306 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008307 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008308 info.mRotationY = rotationY;
8309 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008310 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008311 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8312 mDisplayList.setRotationY(rotationY);
8313 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008314 }
8315 }
8316
8317 /**
8318 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8319 *
8320 * @see #getPivotX()
8321 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008322 * @see #setRotationX(float)
8323 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008324 * @return The degrees of X rotation.
8325 */
Chet Haasea5531132012-02-02 13:41:44 -08008326 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008327 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008328 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008329 }
8330
8331 /**
Chet Haase897247b2010-09-09 14:54:47 -07008332 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8333 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8334 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008335 *
Romain Guya5364ee2011-02-24 14:46:04 -08008336 * When rotating large views, it is recommended to adjust the camera distance
8337 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008338 *
8339 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008340 *
8341 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008342 * @see #getPivotX()
8343 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008344 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008345 * @see #setRotationY(float)
8346 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008347 *
8348 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008349 */
8350 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008351 ensureTransformationInfo();
8352 final TransformationInfo info = mTransformationInfo;
8353 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008354 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008355 info.mRotationX = rotationX;
8356 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008357 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008358 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8359 mDisplayList.setRotationX(rotationX);
8360 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008361 }
8362 }
8363
8364 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008365 * The amount that the view is scaled in x around the pivot point, as a proportion of
8366 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8367 *
Joe Onorato93162322010-09-16 15:42:01 -04008368 * <p>By default, this is 1.0f.
8369 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008370 * @see #getPivotX()
8371 * @see #getPivotY()
8372 * @return The scaling factor.
8373 */
Chet Haasea5531132012-02-02 13:41:44 -08008374 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008375 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008376 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008377 }
8378
8379 /**
8380 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8381 * the view's unscaled width. A value of 1 means that no scaling is applied.
8382 *
8383 * @param scaleX The scaling factor.
8384 * @see #getPivotX()
8385 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008386 *
8387 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008388 */
8389 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008390 ensureTransformationInfo();
8391 final TransformationInfo info = mTransformationInfo;
8392 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008393 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008394 info.mScaleX = scaleX;
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.setScaleX(scaleX);
8399 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008400 }
8401 }
8402
8403 /**
8404 * The amount that the view is scaled in y around the pivot point, as a proportion of
8405 * the view's unscaled height. 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 getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008415 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008416 }
8417
8418 /**
8419 * Sets the amount that the view is scaled in Y 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 scaleY The scaling factor.
8423 * @see #getPivotX()
8424 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008425 *
8426 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008427 */
8428 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008429 ensureTransformationInfo();
8430 final TransformationInfo info = mTransformationInfo;
8431 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008432 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008433 info.mScaleY = scaleY;
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.setScaleY(scaleY);
8438 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008439 }
8440 }
8441
8442 /**
8443 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8444 * and {@link #setScaleX(float) scaled}.
8445 *
8446 * @see #getRotation()
8447 * @see #getScaleX()
8448 * @see #getScaleY()
8449 * @see #getPivotY()
8450 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008451 *
8452 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008453 */
Chet Haasea5531132012-02-02 13:41:44 -08008454 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008455 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008456 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008457 }
8458
8459 /**
8460 * Sets the x location of the point around which the view is
8461 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008462 * By default, the pivot point is centered on the object.
8463 * Setting this property disables this behavior and causes the view to use only the
8464 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008465 *
8466 * @param pivotX The x location of the pivot point.
8467 * @see #getRotation()
8468 * @see #getScaleX()
8469 * @see #getScaleY()
8470 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008471 *
8472 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008473 */
8474 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008475 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008476 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008477 final TransformationInfo info = mTransformationInfo;
8478 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008479 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008480 info.mPivotX = pivotX;
8481 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008482 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008483 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8484 mDisplayList.setPivotX(pivotX);
8485 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008486 }
8487 }
8488
8489 /**
8490 * The y location of the point around which the view is {@link #setRotation(float) rotated}
8491 * and {@link #setScaleY(float) scaled}.
8492 *
8493 * @see #getRotation()
8494 * @see #getScaleX()
8495 * @see #getScaleY()
8496 * @see #getPivotY()
8497 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008498 *
8499 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008500 */
Chet Haasea5531132012-02-02 13:41:44 -08008501 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008502 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008503 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008504 }
8505
8506 /**
8507 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07008508 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
8509 * Setting this property disables this behavior and causes the view to use only the
8510 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008511 *
8512 * @param pivotY The y location of the pivot point.
8513 * @see #getRotation()
8514 * @see #getScaleX()
8515 * @see #getScaleY()
8516 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008517 *
8518 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008519 */
8520 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008521 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008522 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008523 final TransformationInfo info = mTransformationInfo;
8524 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008525 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008526 info.mPivotY = pivotY;
8527 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008528 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008529 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8530 mDisplayList.setPivotY(pivotY);
8531 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008532 }
8533 }
8534
8535 /**
8536 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
8537 * completely transparent and 1 means the view is completely opaque.
8538 *
Joe Onorato93162322010-09-16 15:42:01 -04008539 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07008540 * @return The opacity of the view.
8541 */
Chet Haasea5531132012-02-02 13:41:44 -08008542 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008543 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008544 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008545 }
8546
8547 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07008548 * Returns whether this View has content which overlaps. This function, intended to be
8549 * overridden by specific View types, is an optimization when alpha is set on a view. If
8550 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8551 * and then composited it into place, which can be expensive. If the view has no overlapping
8552 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8553 * An example of overlapping rendering is a TextView with a background image, such as a
8554 * Button. An example of non-overlapping rendering is a TextView with no background, or
8555 * an ImageView with only the foreground image. The default implementation returns true;
8556 * subclasses should override if they have cases which can be optimized.
8557 *
8558 * @return true if the content in this view might overlap, false otherwise.
8559 */
8560 public boolean hasOverlappingRendering() {
8561 return true;
8562 }
8563
8564 /**
Romain Guy171c5922011-01-06 10:04:23 -08008565 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8566 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008567 *
Romain Guy171c5922011-01-06 10:04:23 -08008568 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8569 * responsible for applying the opacity itself. Otherwise, calling this method is
8570 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008571 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008572 *
Chet Haasea5531132012-02-02 13:41:44 -08008573 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8574 * performance implications. It is generally best to use the alpha property sparingly and
8575 * transiently, as in the case of fading animations.</p>
8576 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008577 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08008578 *
Joe Malin32736f02011-01-19 16:14:20 -08008579 * @see #setLayerType(int, android.graphics.Paint)
8580 *
Chet Haase73066682010-11-29 15:55:32 -08008581 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07008582 */
8583 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008584 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008585 if (mTransformationInfo.mAlpha != alpha) {
8586 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008587 if (onSetAlpha((int) (alpha * 255))) {
8588 mPrivateFlags |= ALPHA_SET;
8589 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07008590 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008591 invalidate(true);
8592 } else {
8593 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07008594 invalidateViewProperty(true, false);
Chet Haasea1cff502012-02-21 13:43:44 -08008595 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8596 mDisplayList.setAlpha(alpha);
8597 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008598 }
Chet Haaseed032702010-10-01 14:05:54 -07008599 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008600 }
8601
8602 /**
Chet Haasea00f3862011-02-22 06:34:40 -08008603 * Faster version of setAlpha() which performs the same steps except there are
8604 * no calls to invalidate(). The caller of this function should perform proper invalidation
8605 * on the parent and this object. The return value indicates whether the subclass handles
8606 * alpha (the return value for onSetAlpha()).
8607 *
8608 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008609 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
8610 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08008611 */
8612 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008613 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008614 if (mTransformationInfo.mAlpha != alpha) {
8615 mTransformationInfo.mAlpha = alpha;
8616 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
8617 if (subclassHandlesAlpha) {
8618 mPrivateFlags |= ALPHA_SET;
8619 return true;
8620 } else {
8621 mPrivateFlags &= ~ALPHA_SET;
Chet Haasea1cff502012-02-21 13:43:44 -08008622 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8623 mDisplayList.setAlpha(alpha);
8624 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008625 }
Chet Haasea00f3862011-02-22 06:34:40 -08008626 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008627 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08008628 }
8629
8630 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008631 * Top position of this view relative to its parent.
8632 *
8633 * @return The top of this view, in pixels.
8634 */
8635 @ViewDebug.CapturedViewProperty
8636 public final int getTop() {
8637 return mTop;
8638 }
8639
8640 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008641 * Sets the top position of this view relative to its parent. This method is meant to be called
8642 * by the layout system and should not generally be called otherwise, because the property
8643 * may be changed at any time by the layout.
8644 *
8645 * @param top The top of this view, in pixels.
8646 */
8647 public final void setTop(int top) {
8648 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07008649 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008650 final boolean matrixIsIdentity = mTransformationInfo == null
8651 || mTransformationInfo.mMatrixIsIdentity;
8652 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008653 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008654 int minTop;
8655 int yLoc;
8656 if (top < mTop) {
8657 minTop = top;
8658 yLoc = top - mTop;
8659 } else {
8660 minTop = mTop;
8661 yLoc = 0;
8662 }
Chet Haasee9140a72011-02-16 16:23:29 -08008663 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008664 }
8665 } else {
8666 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008667 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008668 }
8669
Chet Haaseed032702010-10-01 14:05:54 -07008670 int width = mRight - mLeft;
8671 int oldHeight = mBottom - mTop;
8672
Chet Haase21cd1382010-09-01 17:42:29 -07008673 mTop = top;
Chet Haasea1cff502012-02-21 13:43:44 -08008674 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8675 mDisplayList.setTop(mTop);
8676 }
Chet Haase21cd1382010-09-01 17:42:29 -07008677
Chet Haaseed032702010-10-01 14:05:54 -07008678 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8679
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008680 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008681 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8682 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008683 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008684 }
Chet Haase21cd1382010-09-01 17:42:29 -07008685 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008686 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008687 }
Chet Haase55dbb652010-12-21 20:15:08 -08008688 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008689 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008690 }
8691 }
8692
8693 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008694 * Bottom position of this view relative to its parent.
8695 *
8696 * @return The bottom of this view, in pixels.
8697 */
8698 @ViewDebug.CapturedViewProperty
8699 public final int getBottom() {
8700 return mBottom;
8701 }
8702
8703 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08008704 * True if this view has changed since the last time being drawn.
8705 *
8706 * @return The dirty state of this view.
8707 */
8708 public boolean isDirty() {
8709 return (mPrivateFlags & DIRTY_MASK) != 0;
8710 }
8711
8712 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008713 * Sets the bottom position of this view relative to its parent. This method is meant to be
8714 * called by the layout system and should not generally be called otherwise, because the
8715 * property may be changed at any time by the layout.
8716 *
8717 * @param bottom The bottom of this view, in pixels.
8718 */
8719 public final void setBottom(int bottom) {
8720 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07008721 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008722 final boolean matrixIsIdentity = mTransformationInfo == null
8723 || mTransformationInfo.mMatrixIsIdentity;
8724 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008725 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008726 int maxBottom;
8727 if (bottom < mBottom) {
8728 maxBottom = mBottom;
8729 } else {
8730 maxBottom = bottom;
8731 }
Chet Haasee9140a72011-02-16 16:23:29 -08008732 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008733 }
8734 } else {
8735 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008736 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008737 }
8738
Chet Haaseed032702010-10-01 14:05:54 -07008739 int width = mRight - mLeft;
8740 int oldHeight = mBottom - mTop;
8741
Chet Haase21cd1382010-09-01 17:42:29 -07008742 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -08008743 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8744 mDisplayList.setBottom(mBottom);
8745 }
Chet Haase21cd1382010-09-01 17:42:29 -07008746
Chet Haaseed032702010-10-01 14:05:54 -07008747 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8748
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008749 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008750 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8751 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008752 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008753 }
Chet Haase21cd1382010-09-01 17:42:29 -07008754 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008755 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008756 }
Chet Haase55dbb652010-12-21 20:15:08 -08008757 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008758 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008759 }
8760 }
8761
8762 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008763 * Left position of this view relative to its parent.
8764 *
8765 * @return The left edge of this view, in pixels.
8766 */
8767 @ViewDebug.CapturedViewProperty
8768 public final int getLeft() {
8769 return mLeft;
8770 }
8771
8772 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008773 * Sets the left position of this view relative to its parent. This method is meant to be called
8774 * by the layout system and should not generally be called otherwise, because the property
8775 * may be changed at any time by the layout.
8776 *
8777 * @param left The bottom of this view, in pixels.
8778 */
8779 public final void setLeft(int left) {
8780 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07008781 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008782 final boolean matrixIsIdentity = mTransformationInfo == null
8783 || mTransformationInfo.mMatrixIsIdentity;
8784 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008785 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008786 int minLeft;
8787 int xLoc;
8788 if (left < mLeft) {
8789 minLeft = left;
8790 xLoc = left - mLeft;
8791 } else {
8792 minLeft = mLeft;
8793 xLoc = 0;
8794 }
Chet Haasee9140a72011-02-16 16:23:29 -08008795 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008796 }
8797 } else {
8798 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008799 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008800 }
8801
Chet Haaseed032702010-10-01 14:05:54 -07008802 int oldWidth = mRight - mLeft;
8803 int height = mBottom - mTop;
8804
Chet Haase21cd1382010-09-01 17:42:29 -07008805 mLeft = left;
Chet Haasea1cff502012-02-21 13:43:44 -08008806 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8807 mDisplayList.setLeft(left);
8808 }
Chet Haase21cd1382010-09-01 17:42:29 -07008809
Chet Haaseed032702010-10-01 14:05:54 -07008810 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8811
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008812 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008813 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8814 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008815 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008816 }
Chet Haase21cd1382010-09-01 17:42:29 -07008817 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008818 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008819 }
Chet Haase55dbb652010-12-21 20:15:08 -08008820 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008821 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008822 if (USE_DISPLAY_LIST_PROPERTIES) {
8823
8824 }
Chet Haase21cd1382010-09-01 17:42:29 -07008825 }
8826 }
8827
8828 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008829 * Right position of this view relative to its parent.
8830 *
8831 * @return The right edge of this view, in pixels.
8832 */
8833 @ViewDebug.CapturedViewProperty
8834 public final int getRight() {
8835 return mRight;
8836 }
8837
8838 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008839 * Sets the right position of this view relative to its parent. This method is meant to be called
8840 * by the layout system and should not generally be called otherwise, because the property
8841 * may be changed at any time by the layout.
8842 *
8843 * @param right The bottom of this view, in pixels.
8844 */
8845 public final void setRight(int right) {
8846 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07008847 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008848 final boolean matrixIsIdentity = mTransformationInfo == null
8849 || mTransformationInfo.mMatrixIsIdentity;
8850 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008851 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008852 int maxRight;
8853 if (right < mRight) {
8854 maxRight = mRight;
8855 } else {
8856 maxRight = right;
8857 }
Chet Haasee9140a72011-02-16 16:23:29 -08008858 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008859 }
8860 } else {
8861 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008862 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008863 }
8864
Chet Haaseed032702010-10-01 14:05:54 -07008865 int oldWidth = mRight - mLeft;
8866 int height = mBottom - mTop;
8867
Chet Haase21cd1382010-09-01 17:42:29 -07008868 mRight = right;
Chet Haasea1cff502012-02-21 13:43:44 -08008869 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8870 mDisplayList.setRight(mRight);
8871 }
Chet Haase21cd1382010-09-01 17:42:29 -07008872
Chet Haaseed032702010-10-01 14:05:54 -07008873 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8874
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008875 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008876 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8877 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008878 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008879 }
Chet Haase21cd1382010-09-01 17:42:29 -07008880 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008881 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008882 }
Chet Haase55dbb652010-12-21 20:15:08 -08008883 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008884 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008885 }
8886 }
8887
8888 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008889 * The visual x position of this view, in pixels. This is equivalent to the
8890 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08008891 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07008892 *
Chet Haasedf030d22010-07-30 17:22:38 -07008893 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008894 */
Chet Haasea5531132012-02-02 13:41:44 -08008895 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008896 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008897 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008898 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008899
Chet Haasedf030d22010-07-30 17:22:38 -07008900 /**
8901 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
8902 * {@link #setTranslationX(float) translationX} property to be the difference between
8903 * the x value passed in and the current {@link #getLeft() left} property.
8904 *
8905 * @param x The visual x position of this view, in pixels.
8906 */
8907 public void setX(float x) {
8908 setTranslationX(x - mLeft);
8909 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008910
Chet Haasedf030d22010-07-30 17:22:38 -07008911 /**
8912 * The visual y position of this view, in pixels. This is equivalent to the
8913 * {@link #setTranslationY(float) translationY} property plus the current
8914 * {@link #getTop() top} property.
8915 *
8916 * @return The visual y position of this view, in pixels.
8917 */
Chet Haasea5531132012-02-02 13:41:44 -08008918 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008919 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008920 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008921 }
8922
8923 /**
8924 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
8925 * {@link #setTranslationY(float) translationY} property to be the difference between
8926 * the y value passed in and the current {@link #getTop() top} property.
8927 *
8928 * @param y The visual y position of this view, in pixels.
8929 */
8930 public void setY(float y) {
8931 setTranslationY(y - mTop);
8932 }
8933
8934
8935 /**
8936 * The horizontal location of this view relative to its {@link #getLeft() left} position.
8937 * This position is post-layout, in addition to wherever the object's
8938 * layout placed it.
8939 *
8940 * @return The horizontal position of this view relative to its left position, in pixels.
8941 */
Chet Haasea5531132012-02-02 13:41:44 -08008942 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008943 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008944 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07008945 }
8946
8947 /**
8948 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
8949 * This effectively positions the object post-layout, in addition to wherever the object's
8950 * layout placed it.
8951 *
8952 * @param translationX The horizontal position of this view relative to its left position,
8953 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008954 *
8955 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07008956 */
8957 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008958 ensureTransformationInfo();
8959 final TransformationInfo info = mTransformationInfo;
8960 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07008961 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008962 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008963 info.mTranslationX = translationX;
8964 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008965 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008966 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8967 mDisplayList.setTranslationX(translationX);
8968 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008969 }
8970 }
8971
8972 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008973 * The horizontal location of this view relative to its {@link #getTop() top} position.
8974 * This position is post-layout, in addition to wherever the object's
8975 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008976 *
Chet Haasedf030d22010-07-30 17:22:38 -07008977 * @return The vertical position of this view relative to its top position,
8978 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008979 */
Chet Haasea5531132012-02-02 13:41:44 -08008980 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008981 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008982 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008983 }
8984
8985 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008986 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
8987 * This effectively positions the object post-layout, in addition to wherever the object's
8988 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008989 *
Chet Haasedf030d22010-07-30 17:22:38 -07008990 * @param translationY The vertical position of this view relative to its top position,
8991 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008992 *
8993 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07008994 */
Chet Haasedf030d22010-07-30 17:22:38 -07008995 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008996 ensureTransformationInfo();
8997 final TransformationInfo info = mTransformationInfo;
8998 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008999 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009000 info.mTranslationY = translationY;
9001 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009002 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08009003 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9004 mDisplayList.setTranslationY(translationY);
9005 }
Chet Haasedf030d22010-07-30 17:22:38 -07009006 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009007 }
9008
9009 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009010 * Hit rectangle in parent's coordinates
9011 *
9012 * @param outRect The hit rectangle of the view.
9013 */
9014 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009015 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009016 final TransformationInfo info = mTransformationInfo;
9017 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009018 outRect.set(mLeft, mTop, mRight, mBottom);
9019 } else {
9020 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009021 tmpRect.set(-info.mPivotX, -info.mPivotY,
9022 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9023 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009024 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9025 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009026 }
9027 }
9028
9029 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009030 * Determines whether the given point, in local coordinates is inside the view.
9031 */
9032 /*package*/ final boolean pointInView(float localX, float localY) {
9033 return localX >= 0 && localX < (mRight - mLeft)
9034 && localY >= 0 && localY < (mBottom - mTop);
9035 }
9036
9037 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009038 * Utility method to determine whether the given point, in local coordinates,
9039 * is inside the view, where the area of the view is expanded by the slop factor.
9040 * This method is called while processing touch-move events to determine if the event
9041 * is still within the view.
9042 */
9043 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009044 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009045 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009046 }
9047
9048 /**
9049 * When a view has focus and the user navigates away from it, the next view is searched for
9050 * starting from the rectangle filled in by this method.
9051 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009052 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9053 * of the view. However, if your view maintains some idea of internal selection,
9054 * such as a cursor, or a selected row or column, you should override this method and
9055 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009056 *
9057 * @param r The rectangle to fill in, in this view's coordinates.
9058 */
9059 public void getFocusedRect(Rect r) {
9060 getDrawingRect(r);
9061 }
9062
9063 /**
9064 * If some part of this view is not clipped by any of its parents, then
9065 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009066 * coordinates (without taking possible View rotations into account), offset
9067 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9068 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009069 *
9070 * @param r If true is returned, r holds the global coordinates of the
9071 * visible portion of this view.
9072 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9073 * between this view and its root. globalOffet may be null.
9074 * @return true if r is non-empty (i.e. part of the view is visible at the
9075 * root level.
9076 */
9077 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9078 int width = mRight - mLeft;
9079 int height = mBottom - mTop;
9080 if (width > 0 && height > 0) {
9081 r.set(0, 0, width, height);
9082 if (globalOffset != null) {
9083 globalOffset.set(-mScrollX, -mScrollY);
9084 }
9085 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9086 }
9087 return false;
9088 }
9089
9090 public final boolean getGlobalVisibleRect(Rect r) {
9091 return getGlobalVisibleRect(r, null);
9092 }
9093
9094 public final boolean getLocalVisibleRect(Rect r) {
9095 Point offset = new Point();
9096 if (getGlobalVisibleRect(r, offset)) {
9097 r.offset(-offset.x, -offset.y); // make r local
9098 return true;
9099 }
9100 return false;
9101 }
9102
9103 /**
9104 * Offset this view's vertical location by the specified number of pixels.
9105 *
9106 * @param offset the number of pixels to offset the view by
9107 */
9108 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009109 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009110 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009111 final boolean matrixIsIdentity = mTransformationInfo == null
9112 || mTransformationInfo.mMatrixIsIdentity;
9113 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009114 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9115 invalidateViewProperty(false, false);
9116 } else {
9117 final ViewParent p = mParent;
9118 if (p != null && mAttachInfo != null) {
9119 final Rect r = mAttachInfo.mTmpInvalRect;
9120 int minTop;
9121 int maxBottom;
9122 int yLoc;
9123 if (offset < 0) {
9124 minTop = mTop + offset;
9125 maxBottom = mBottom;
9126 yLoc = offset;
9127 } else {
9128 minTop = mTop;
9129 maxBottom = mBottom + offset;
9130 yLoc = 0;
9131 }
9132 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9133 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009134 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009135 }
9136 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009137 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009138 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009139
Chet Haasec3aa3612010-06-17 08:50:37 -07009140 mTop += offset;
9141 mBottom += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08009142 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9143 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009144 invalidateViewProperty(false, false);
9145 } else {
9146 if (!matrixIsIdentity) {
9147 invalidateViewProperty(false, true);
9148 }
9149 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009150 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009152 }
9153
9154 /**
9155 * Offset this view's horizontal location by the specified amount of pixels.
9156 *
9157 * @param offset the numer of pixels to offset the view by
9158 */
9159 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009160 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009161 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009162 final boolean matrixIsIdentity = mTransformationInfo == null
9163 || mTransformationInfo.mMatrixIsIdentity;
9164 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009165 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9166 invalidateViewProperty(false, false);
9167 } else {
9168 final ViewParent p = mParent;
9169 if (p != null && mAttachInfo != null) {
9170 final Rect r = mAttachInfo.mTmpInvalRect;
9171 int minLeft;
9172 int maxRight;
9173 if (offset < 0) {
9174 minLeft = mLeft + offset;
9175 maxRight = mRight;
9176 } else {
9177 minLeft = mLeft;
9178 maxRight = mRight + offset;
9179 }
9180 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9181 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009182 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009183 }
9184 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009185 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009186 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009187
Chet Haasec3aa3612010-06-17 08:50:37 -07009188 mLeft += offset;
9189 mRight += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08009190 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
9191 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009192 invalidateViewProperty(false, false);
9193 } else {
9194 if (!matrixIsIdentity) {
9195 invalidateViewProperty(false, true);
9196 }
9197 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009198 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009200 }
9201
9202 /**
9203 * Get the LayoutParams associated with this view. All views should have
9204 * layout parameters. These supply parameters to the <i>parent</i> of this
9205 * view specifying how it should be arranged. There are many subclasses of
9206 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9207 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009208 *
9209 * This method may return null if this View is not attached to a parent
9210 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9211 * was not invoked successfully. When a View is attached to a parent
9212 * ViewGroup, this method must not return null.
9213 *
9214 * @return The LayoutParams associated with this view, or null if no
9215 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009216 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009217 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009218 public ViewGroup.LayoutParams getLayoutParams() {
9219 return mLayoutParams;
9220 }
9221
9222 /**
9223 * Set the layout parameters associated with this view. These supply
9224 * parameters to the <i>parent</i> of this view specifying how it should be
9225 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9226 * correspond to the different subclasses of ViewGroup that are responsible
9227 * for arranging their children.
9228 *
Romain Guy01c174b2011-02-22 11:51:06 -08009229 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009230 */
9231 public void setLayoutParams(ViewGroup.LayoutParams params) {
9232 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009233 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009234 }
9235 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009236 if (mParent instanceof ViewGroup) {
9237 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009239 requestLayout();
9240 }
9241
9242 /**
9243 * Set the scrolled position of your view. This will cause a call to
9244 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9245 * invalidated.
9246 * @param x the x position to scroll to
9247 * @param y the y position to scroll to
9248 */
9249 public void scrollTo(int x, int y) {
9250 if (mScrollX != x || mScrollY != y) {
9251 int oldX = mScrollX;
9252 int oldY = mScrollY;
9253 mScrollX = x;
9254 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009255 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009256 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009257 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009258 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009260 }
9261 }
9262
9263 /**
9264 * Move the scrolled position of your view. This will cause a call to
9265 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9266 * invalidated.
9267 * @param x the amount of pixels to scroll by horizontally
9268 * @param y the amount of pixels to scroll by vertically
9269 */
9270 public void scrollBy(int x, int y) {
9271 scrollTo(mScrollX + x, mScrollY + y);
9272 }
9273
9274 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009275 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9276 * animation to fade the scrollbars out after a default delay. If a subclass
9277 * provides animated scrolling, the start delay should equal the duration
9278 * of the scrolling animation.</p>
9279 *
9280 * <p>The animation starts only if at least one of the scrollbars is
9281 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9282 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9283 * this method returns true, and false otherwise. If the animation is
9284 * started, this method calls {@link #invalidate()}; in that case the
9285 * caller should not call {@link #invalidate()}.</p>
9286 *
9287 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009288 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009289 *
9290 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9291 * and {@link #scrollTo(int, int)}.</p>
9292 *
9293 * @return true if the animation is played, false otherwise
9294 *
9295 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009296 * @see #scrollBy(int, int)
9297 * @see #scrollTo(int, int)
9298 * @see #isHorizontalScrollBarEnabled()
9299 * @see #isVerticalScrollBarEnabled()
9300 * @see #setHorizontalScrollBarEnabled(boolean)
9301 * @see #setVerticalScrollBarEnabled(boolean)
9302 */
9303 protected boolean awakenScrollBars() {
9304 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009305 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009306 }
9307
9308 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009309 * Trigger the scrollbars to draw.
9310 * This method differs from awakenScrollBars() only in its default duration.
9311 * initialAwakenScrollBars() will show the scroll bars for longer than
9312 * usual to give the user more of a chance to notice them.
9313 *
9314 * @return true if the animation is played, false otherwise.
9315 */
9316 private boolean initialAwakenScrollBars() {
9317 return mScrollCache != null &&
9318 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9319 }
9320
9321 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009322 * <p>
9323 * Trigger the scrollbars to draw. When invoked this method starts an
9324 * animation to fade the scrollbars out after a fixed delay. If a subclass
9325 * provides animated scrolling, the start delay should equal the duration of
9326 * the scrolling animation.
9327 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009328 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009329 * <p>
9330 * The animation starts only if at least one of the scrollbars is enabled,
9331 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9332 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9333 * this method returns true, and false otherwise. If the animation is
9334 * started, this method calls {@link #invalidate()}; in that case the caller
9335 * should not call {@link #invalidate()}.
9336 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009337 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009338 * <p>
9339 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009340 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009341 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009342 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009343 * @param startDelay the delay, in milliseconds, after which the animation
9344 * should start; when the delay is 0, the animation starts
9345 * immediately
9346 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009347 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009348 * @see #scrollBy(int, int)
9349 * @see #scrollTo(int, int)
9350 * @see #isHorizontalScrollBarEnabled()
9351 * @see #isVerticalScrollBarEnabled()
9352 * @see #setHorizontalScrollBarEnabled(boolean)
9353 * @see #setVerticalScrollBarEnabled(boolean)
9354 */
9355 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009356 return awakenScrollBars(startDelay, true);
9357 }
Joe Malin32736f02011-01-19 16:14:20 -08009358
Mike Cleron290947b2009-09-29 18:34:32 -07009359 /**
9360 * <p>
9361 * Trigger the scrollbars to draw. When invoked this method starts an
9362 * animation to fade the scrollbars out after a fixed delay. If a subclass
9363 * provides animated scrolling, the start delay should equal the duration of
9364 * the scrolling animation.
9365 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009366 *
Mike Cleron290947b2009-09-29 18:34:32 -07009367 * <p>
9368 * The animation starts only if at least one of the scrollbars is enabled,
9369 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9370 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9371 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009372 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009373 * is set to true; in that case the caller
9374 * should not call {@link #invalidate()}.
9375 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009376 *
Mike Cleron290947b2009-09-29 18:34:32 -07009377 * <p>
9378 * This method should be invoked everytime a subclass directly updates the
9379 * scroll parameters.
9380 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009381 *
Mike Cleron290947b2009-09-29 18:34:32 -07009382 * @param startDelay the delay, in milliseconds, after which the animation
9383 * should start; when the delay is 0, the animation starts
9384 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009385 *
Mike Cleron290947b2009-09-29 18:34:32 -07009386 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009387 *
Mike Cleron290947b2009-09-29 18:34:32 -07009388 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009389 *
Mike Cleron290947b2009-09-29 18:34:32 -07009390 * @see #scrollBy(int, int)
9391 * @see #scrollTo(int, int)
9392 * @see #isHorizontalScrollBarEnabled()
9393 * @see #isVerticalScrollBarEnabled()
9394 * @see #setHorizontalScrollBarEnabled(boolean)
9395 * @see #setVerticalScrollBarEnabled(boolean)
9396 */
9397 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009398 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009399
Mike Cleronf116bf82009-09-27 19:14:12 -07009400 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9401 return false;
9402 }
9403
9404 if (scrollCache.scrollBar == null) {
9405 scrollCache.scrollBar = new ScrollBarDrawable();
9406 }
9407
9408 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9409
Mike Cleron290947b2009-09-29 18:34:32 -07009410 if (invalidate) {
9411 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009412 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009413 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009414
9415 if (scrollCache.state == ScrollabilityCache.OFF) {
9416 // FIXME: this is copied from WindowManagerService.
9417 // We should get this value from the system when it
9418 // is possible to do so.
9419 final int KEY_REPEAT_FIRST_DELAY = 750;
9420 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9421 }
9422
9423 // Tell mScrollCache when we should start fading. This may
9424 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009425 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009426 scrollCache.fadeStartTime = fadeStartTime;
9427 scrollCache.state = ScrollabilityCache.ON;
9428
9429 // Schedule our fader to run, unscheduling any old ones first
9430 if (mAttachInfo != null) {
9431 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9432 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9433 }
9434
9435 return true;
9436 }
9437
9438 return false;
9439 }
9440
9441 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009442 * Do not invalidate views which are not visible and which are not running an animation. They
9443 * will not get drawn and they should not set dirty flags as if they will be drawn
9444 */
9445 private boolean skipInvalidate() {
9446 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9447 (!(mParent instanceof ViewGroup) ||
9448 !((ViewGroup) mParent).isViewTransitioning(this));
9449 }
9450 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009451 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009452 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
9453 * in the future. This must be called from a UI thread. To call from a non-UI
9454 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009455 *
9456 * WARNING: This method is destructive to dirty.
9457 * @param dirty the rectangle representing the bounds of the dirty region
9458 */
9459 public void invalidate(Rect dirty) {
9460 if (ViewDebug.TRACE_HIERARCHY) {
9461 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9462 }
9463
Chet Haaseaceafe62011-08-26 15:44:33 -07009464 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009465 return;
9466 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009467 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009468 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9469 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009470 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009471 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009472 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009473 final ViewParent p = mParent;
9474 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009475 //noinspection PointlessBooleanExpression,ConstantConditions
9476 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9477 if (p != null && ai != null && ai.mHardwareAccelerated) {
9478 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009479 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009480 p.invalidateChild(this, null);
9481 return;
9482 }
Romain Guyaf636eb2010-12-09 17:47:21 -08009483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009484 if (p != null && ai != null) {
9485 final int scrollX = mScrollX;
9486 final int scrollY = mScrollY;
9487 final Rect r = ai.mTmpInvalRect;
9488 r.set(dirty.left - scrollX, dirty.top - scrollY,
9489 dirty.right - scrollX, dirty.bottom - scrollY);
9490 mParent.invalidateChild(this, r);
9491 }
9492 }
9493 }
9494
9495 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009496 * 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 -08009497 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009498 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
9499 * will be called at some point in the future. This must be called from
9500 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009501 * @param l the left position of the dirty region
9502 * @param t the top position of the dirty region
9503 * @param r the right position of the dirty region
9504 * @param b the bottom position of the dirty region
9505 */
9506 public void invalidate(int l, int t, int r, int b) {
9507 if (ViewDebug.TRACE_HIERARCHY) {
9508 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9509 }
9510
Chet Haaseaceafe62011-08-26 15:44:33 -07009511 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009512 return;
9513 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009514 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009515 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9516 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009517 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009518 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009519 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009520 final ViewParent p = mParent;
9521 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009522 //noinspection PointlessBooleanExpression,ConstantConditions
9523 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9524 if (p != null && ai != null && ai.mHardwareAccelerated) {
9525 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009526 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009527 p.invalidateChild(this, null);
9528 return;
9529 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009531 if (p != null && ai != null && l < r && t < b) {
9532 final int scrollX = mScrollX;
9533 final int scrollY = mScrollY;
9534 final Rect tmpr = ai.mTmpInvalRect;
9535 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
9536 p.invalidateChild(this, tmpr);
9537 }
9538 }
9539 }
9540
9541 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009542 * Invalidate the whole view. If the view is visible,
9543 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
9544 * the future. This must be called from a UI thread. To call from a non-UI thread,
9545 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009546 */
9547 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07009548 invalidate(true);
9549 }
Joe Malin32736f02011-01-19 16:14:20 -08009550
Chet Haaseed032702010-10-01 14:05:54 -07009551 /**
9552 * This is where the invalidate() work actually happens. A full invalidate()
9553 * causes the drawing cache to be invalidated, but this function can be called with
9554 * invalidateCache set to false to skip that invalidation step for cases that do not
9555 * need it (for example, a component that remains at the same dimensions with the same
9556 * content).
9557 *
9558 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9559 * well. This is usually true for a full invalidate, but may be set to false if the
9560 * View's contents or dimensions have not changed.
9561 */
Romain Guy849d0a32011-02-01 17:20:48 -08009562 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009563 if (ViewDebug.TRACE_HIERARCHY) {
9564 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9565 }
9566
Chet Haaseaceafe62011-08-26 15:44:33 -07009567 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009568 return;
9569 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009570 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08009571 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08009572 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
9573 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07009574 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07009575 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07009576 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009577 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07009578 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009580 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07009581 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08009582 //noinspection PointlessBooleanExpression,ConstantConditions
9583 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9584 if (p != null && ai != null && ai.mHardwareAccelerated) {
9585 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009586 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009587 p.invalidateChild(this, null);
9588 return;
9589 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009590 }
Michael Jurkaebefea42010-11-15 16:04:01 -08009591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009592 if (p != null && ai != null) {
9593 final Rect r = ai.mTmpInvalRect;
9594 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9595 // Don't call invalidate -- we don't want to internally scroll
9596 // our own bounds
9597 p.invalidateChild(this, r);
9598 }
9599 }
9600 }
9601
9602 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07009603 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
9604 * set any flags or handle all of the cases handled by the default invalidation methods.
9605 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
9606 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
9607 * walk up the hierarchy, transforming the dirty rect as necessary.
9608 *
9609 * The method also handles normal invalidation logic if display list properties are not
9610 * being used in this view. The invalidateParent and forceRedraw flags are used by that
9611 * backup approach, to handle these cases used in the various property-setting methods.
9612 *
9613 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
9614 * are not being used in this view
9615 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
9616 * list properties are not being used in this view
9617 */
9618 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
9619 if (!USE_DISPLAY_LIST_PROPERTIES || mDisplayList == null ||
9620 (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
9621 if (invalidateParent) {
9622 invalidateParentCaches();
9623 }
9624 if (forceRedraw) {
9625 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
9626 }
9627 invalidate(false);
9628 } else {
9629 final AttachInfo ai = mAttachInfo;
9630 final ViewParent p = mParent;
9631 if (p != null && ai != null) {
9632 final Rect r = ai.mTmpInvalRect;
9633 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9634 if (mParent instanceof ViewGroup) {
9635 ((ViewGroup) mParent).invalidateChildFast(this, r);
9636 } else {
9637 mParent.invalidateChild(this, r);
9638 }
9639 }
9640 }
9641 }
9642
9643 /**
9644 * Utility method to transform a given Rect by the current matrix of this view.
9645 */
9646 void transformRect(final Rect rect) {
9647 if (!getMatrix().isIdentity()) {
9648 RectF boundingRect = mAttachInfo.mTmpTransformRect;
9649 boundingRect.set(rect);
9650 getMatrix().mapRect(boundingRect);
9651 rect.set((int) (boundingRect.left - 0.5f),
9652 (int) (boundingRect.top - 0.5f),
9653 (int) (boundingRect.right + 0.5f),
9654 (int) (boundingRect.bottom + 0.5f));
9655 }
9656 }
9657
9658 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08009659 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08009660 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9661 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08009662 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
9663 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08009664 *
9665 * @hide
9666 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08009667 protected void invalidateParentCaches() {
9668 if (mParent instanceof View) {
9669 ((View) mParent).mPrivateFlags |= INVALIDATED;
9670 }
9671 }
Joe Malin32736f02011-01-19 16:14:20 -08009672
Romain Guy0fd89bf2011-01-26 15:41:30 -08009673 /**
9674 * Used to indicate that the parent of this view should be invalidated. This functionality
9675 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9676 * which is necessary when various parent-managed properties of the view change, such as
9677 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
9678 * an invalidation event to the parent.
9679 *
9680 * @hide
9681 */
9682 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08009683 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08009684 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08009685 }
9686 }
9687
9688 /**
Romain Guy24443ea2009-05-11 11:56:30 -07009689 * Indicates whether this View is opaque. An opaque View guarantees that it will
9690 * draw all the pixels overlapping its bounds using a fully opaque color.
9691 *
9692 * Subclasses of View should override this method whenever possible to indicate
9693 * whether an instance is opaque. Opaque Views are treated in a special way by
9694 * the View hierarchy, possibly allowing it to perform optimizations during
9695 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07009696 *
Romain Guy24443ea2009-05-11 11:56:30 -07009697 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07009698 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009699 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07009700 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07009701 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009702 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
9703 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07009704 }
9705
Adam Powell20232d02010-12-08 21:08:53 -08009706 /**
9707 * @hide
9708 */
9709 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07009710 // Opaque if:
9711 // - Has a background
9712 // - Background is opaque
9713 // - Doesn't have scrollbars or scrollbars are inside overlay
9714
Philip Milne6c8ea062012-04-03 17:38:43 -07009715 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -07009716 mPrivateFlags |= OPAQUE_BACKGROUND;
9717 } else {
9718 mPrivateFlags &= ~OPAQUE_BACKGROUND;
9719 }
9720
9721 final int flags = mViewFlags;
9722 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
9723 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
9724 mPrivateFlags |= OPAQUE_SCROLLBARS;
9725 } else {
9726 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
9727 }
9728 }
9729
9730 /**
9731 * @hide
9732 */
9733 protected boolean hasOpaqueScrollbars() {
9734 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07009735 }
9736
9737 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009738 * @return A handler associated with the thread running the View. This
9739 * handler can be used to pump events in the UI events queue.
9740 */
9741 public Handler getHandler() {
9742 if (mAttachInfo != null) {
9743 return mAttachInfo.mHandler;
9744 }
9745 return null;
9746 }
9747
9748 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08009749 * Gets the view root associated with the View.
9750 * @return The view root, or null if none.
9751 * @hide
9752 */
9753 public ViewRootImpl getViewRootImpl() {
9754 if (mAttachInfo != null) {
9755 return mAttachInfo.mViewRootImpl;
9756 }
9757 return null;
9758 }
9759
9760 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009761 * <p>Causes the Runnable to be added to the message queue.
9762 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009763 *
Romain Guye63a4f32011-08-11 11:33:31 -07009764 * <p>This method can be invoked from outside of the UI thread
9765 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009766 *
9767 * @param action The Runnable that will be executed.
9768 *
9769 * @return Returns true if the Runnable was successfully placed in to the
9770 * message queue. Returns false on failure, usually because the
9771 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009772 *
9773 * @see #postDelayed
9774 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009775 */
9776 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009777 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009778 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009779 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009780 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009781 // Assume that post will succeed later
9782 ViewRootImpl.getRunQueue().post(action);
9783 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009784 }
9785
9786 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009787 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009788 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07009789 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009790 *
Romain Guye63a4f32011-08-11 11:33:31 -07009791 * <p>This method can be invoked from outside of the UI thread
9792 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009793 *
9794 * @param action The Runnable that will be executed.
9795 * @param delayMillis The delay (in milliseconds) until the Runnable
9796 * will be executed.
9797 *
9798 * @return true if the Runnable was successfully placed in to the
9799 * message queue. Returns false on failure, usually because the
9800 * looper processing the message queue is exiting. Note that a
9801 * result of true does not mean the Runnable will be processed --
9802 * if the looper is quit before the delivery time of the message
9803 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009804 *
9805 * @see #post
9806 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009807 */
9808 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009809 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009810 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009811 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009812 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009813 // Assume that post will succeed later
9814 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9815 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009816 }
9817
9818 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009819 * <p>Causes the Runnable to execute on the next animation time step.
9820 * The runnable will be run on the user interface thread.</p>
9821 *
9822 * <p>This method can be invoked from outside of the UI thread
9823 * only when this View is attached to a window.</p>
9824 *
9825 * @param action The Runnable that will be executed.
9826 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009827 * @see #postOnAnimationDelayed
9828 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009829 */
9830 public void postOnAnimation(Runnable action) {
9831 final AttachInfo attachInfo = mAttachInfo;
9832 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009833 attachInfo.mViewRootImpl.mChoreographer.postCallback(
9834 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009835 } else {
9836 // Assume that post will succeed later
9837 ViewRootImpl.getRunQueue().post(action);
9838 }
9839 }
9840
9841 /**
9842 * <p>Causes the Runnable to execute on the next animation time step,
9843 * after the specified amount of time elapses.
9844 * The runnable will be run on the user interface thread.</p>
9845 *
9846 * <p>This method can be invoked from outside of the UI thread
9847 * only when this View is attached to a window.</p>
9848 *
9849 * @param action The Runnable that will be executed.
9850 * @param delayMillis The delay (in milliseconds) until the Runnable
9851 * will be executed.
9852 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009853 * @see #postOnAnimation
9854 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009855 */
9856 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
9857 final AttachInfo attachInfo = mAttachInfo;
9858 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009859 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
9860 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009861 } else {
9862 // Assume that post will succeed later
9863 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9864 }
9865 }
9866
9867 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009868 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009869 *
Romain Guye63a4f32011-08-11 11:33:31 -07009870 * <p>This method can be invoked from outside of the UI thread
9871 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009872 *
9873 * @param action The Runnable to remove from the message handling queue
9874 *
9875 * @return true if this view could ask the Handler to remove the Runnable,
9876 * false otherwise. When the returned value is true, the Runnable
9877 * may or may not have been actually removed from the message queue
9878 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009879 *
9880 * @see #post
9881 * @see #postDelayed
9882 * @see #postOnAnimation
9883 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009884 */
9885 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -08009886 if (action != null) {
9887 final AttachInfo attachInfo = mAttachInfo;
9888 if (attachInfo != null) {
9889 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009890 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
9891 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -08009892 } else {
9893 // Assume that post will succeed later
9894 ViewRootImpl.getRunQueue().removeCallbacks(action);
9895 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009896 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009897 return true;
9898 }
9899
9900 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009901 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
9902 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009903 *
Romain Guye63a4f32011-08-11 11:33:31 -07009904 * <p>This method can be invoked from outside of the UI thread
9905 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009906 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009907 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009908 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009909 */
9910 public void postInvalidate() {
9911 postInvalidateDelayed(0);
9912 }
9913
9914 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009915 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9916 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009917 *
Romain Guye63a4f32011-08-11 11:33:31 -07009918 * <p>This method can be invoked from outside of the UI thread
9919 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009920 *
9921 * @param left The left coordinate of the rectangle to invalidate.
9922 * @param top The top coordinate of the rectangle to invalidate.
9923 * @param right The right coordinate of the rectangle to invalidate.
9924 * @param bottom The bottom coordinate of the rectangle to invalidate.
9925 *
9926 * @see #invalidate(int, int, int, int)
9927 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009928 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009929 */
9930 public void postInvalidate(int left, int top, int right, int bottom) {
9931 postInvalidateDelayed(0, left, top, right, bottom);
9932 }
9933
9934 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009935 * <p>Cause an invalidate to happen on a subsequent cycle through the event
9936 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009937 *
Romain Guye63a4f32011-08-11 11:33:31 -07009938 * <p>This method can be invoked from outside of the UI thread
9939 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009940 *
9941 * @param delayMilliseconds the duration in milliseconds to delay the
9942 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009943 *
9944 * @see #invalidate()
9945 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009946 */
9947 public void postInvalidateDelayed(long delayMilliseconds) {
9948 // We try only with the AttachInfo because there's no point in invalidating
9949 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009950 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009951 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009952 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009953 }
9954 }
9955
9956 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009957 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9958 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009959 *
Romain Guye63a4f32011-08-11 11:33:31 -07009960 * <p>This method can be invoked from outside of the UI thread
9961 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009962 *
9963 * @param delayMilliseconds the duration in milliseconds to delay the
9964 * invalidation by
9965 * @param left The left coordinate of the rectangle to invalidate.
9966 * @param top The top coordinate of the rectangle to invalidate.
9967 * @param right The right coordinate of the rectangle to invalidate.
9968 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009969 *
9970 * @see #invalidate(int, int, int, int)
9971 * @see #invalidate(Rect)
9972 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009973 */
9974 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
9975 int right, int bottom) {
9976
9977 // We try only with the AttachInfo because there's no point in invalidating
9978 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009979 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009980 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009981 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
9982 info.target = this;
9983 info.left = left;
9984 info.top = top;
9985 info.right = right;
9986 info.bottom = bottom;
9987
Jeff Browna175a5b2012-02-15 19:18:31 -08009988 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009989 }
9990 }
9991
9992 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -08009993 * <p>Cause an invalidate to happen on the next animation time step, typically the
9994 * next display frame.</p>
9995 *
9996 * <p>This method can be invoked from outside of the UI thread
9997 * only when this View is attached to a window.</p>
9998 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009999 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010000 */
10001 public void postInvalidateOnAnimation() {
10002 // We try only with the AttachInfo because there's no point in invalidating
10003 // if we are not attached to our window
10004 final AttachInfo attachInfo = mAttachInfo;
10005 if (attachInfo != null) {
10006 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10007 }
10008 }
10009
10010 /**
10011 * <p>Cause an invalidate of the specified area to happen on the next animation
10012 * time step, typically the next display frame.</p>
10013 *
10014 * <p>This method can be invoked from outside of the UI thread
10015 * only when this View is attached to a window.</p>
10016 *
10017 * @param left The left coordinate of the rectangle to invalidate.
10018 * @param top The top coordinate of the rectangle to invalidate.
10019 * @param right The right coordinate of the rectangle to invalidate.
10020 * @param bottom The bottom coordinate of the rectangle to invalidate.
10021 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010022 * @see #invalidate(int, int, int, int)
10023 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010024 */
10025 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10026 // We try only with the AttachInfo because there's no point in invalidating
10027 // if we are not attached to our window
10028 final AttachInfo attachInfo = mAttachInfo;
10029 if (attachInfo != null) {
10030 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10031 info.target = this;
10032 info.left = left;
10033 info.top = top;
10034 info.right = right;
10035 info.bottom = bottom;
10036
10037 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10038 }
10039 }
10040
10041 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010042 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10043 * This event is sent at most once every
10044 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10045 */
10046 private void postSendViewScrolledAccessibilityEventCallback() {
10047 if (mSendViewScrolledAccessibilityEvent == null) {
10048 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10049 }
10050 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10051 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10052 postDelayed(mSendViewScrolledAccessibilityEvent,
10053 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10054 }
10055 }
10056
10057 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010058 * Called by a parent to request that a child update its values for mScrollX
10059 * and mScrollY if necessary. This will typically be done if the child is
10060 * animating a scroll using a {@link android.widget.Scroller Scroller}
10061 * object.
10062 */
10063 public void computeScroll() {
10064 }
10065
10066 /**
10067 * <p>Indicate whether the horizontal edges are faded when the view is
10068 * scrolled horizontally.</p>
10069 *
10070 * @return true if the horizontal edges should are faded on scroll, false
10071 * otherwise
10072 *
10073 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010074 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010075 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010076 */
10077 public boolean isHorizontalFadingEdgeEnabled() {
10078 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10079 }
10080
10081 /**
10082 * <p>Define whether the horizontal edges should be faded when this view
10083 * is scrolled horizontally.</p>
10084 *
10085 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10086 * be faded when the view is scrolled
10087 * horizontally
10088 *
10089 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010090 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010091 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010092 */
10093 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10094 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10095 if (horizontalFadingEdgeEnabled) {
10096 initScrollCache();
10097 }
10098
10099 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10100 }
10101 }
10102
10103 /**
10104 * <p>Indicate whether the vertical edges are faded when the view is
10105 * scrolled horizontally.</p>
10106 *
10107 * @return true if the vertical edges should are faded on scroll, false
10108 * otherwise
10109 *
10110 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010111 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010112 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010113 */
10114 public boolean isVerticalFadingEdgeEnabled() {
10115 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10116 }
10117
10118 /**
10119 * <p>Define whether the vertical edges should be faded when this view
10120 * is scrolled vertically.</p>
10121 *
10122 * @param verticalFadingEdgeEnabled true if the vertical edges should
10123 * be faded when the view is scrolled
10124 * vertically
10125 *
10126 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010127 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010128 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010129 */
10130 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10131 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10132 if (verticalFadingEdgeEnabled) {
10133 initScrollCache();
10134 }
10135
10136 mViewFlags ^= FADING_EDGE_VERTICAL;
10137 }
10138 }
10139
10140 /**
10141 * Returns the strength, or intensity, of the top faded edge. The strength is
10142 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10143 * returns 0.0 or 1.0 but no value in between.
10144 *
10145 * Subclasses should override this method to provide a smoother fade transition
10146 * when scrolling occurs.
10147 *
10148 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10149 */
10150 protected float getTopFadingEdgeStrength() {
10151 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10152 }
10153
10154 /**
10155 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10156 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10157 * returns 0.0 or 1.0 but no value in between.
10158 *
10159 * Subclasses should override this method to provide a smoother fade transition
10160 * when scrolling occurs.
10161 *
10162 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10163 */
10164 protected float getBottomFadingEdgeStrength() {
10165 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10166 computeVerticalScrollRange() ? 1.0f : 0.0f;
10167 }
10168
10169 /**
10170 * Returns the strength, or intensity, of the left faded edge. The strength is
10171 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10172 * returns 0.0 or 1.0 but no value in between.
10173 *
10174 * Subclasses should override this method to provide a smoother fade transition
10175 * when scrolling occurs.
10176 *
10177 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10178 */
10179 protected float getLeftFadingEdgeStrength() {
10180 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10181 }
10182
10183 /**
10184 * Returns the strength, or intensity, of the right faded edge. The strength is
10185 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10186 * returns 0.0 or 1.0 but no value in between.
10187 *
10188 * Subclasses should override this method to provide a smoother fade transition
10189 * when scrolling occurs.
10190 *
10191 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10192 */
10193 protected float getRightFadingEdgeStrength() {
10194 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10195 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10196 }
10197
10198 /**
10199 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10200 * scrollbar is not drawn by default.</p>
10201 *
10202 * @return true if the horizontal scrollbar should be painted, false
10203 * otherwise
10204 *
10205 * @see #setHorizontalScrollBarEnabled(boolean)
10206 */
10207 public boolean isHorizontalScrollBarEnabled() {
10208 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10209 }
10210
10211 /**
10212 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10213 * scrollbar is not drawn by default.</p>
10214 *
10215 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10216 * be painted
10217 *
10218 * @see #isHorizontalScrollBarEnabled()
10219 */
10220 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10221 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10222 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010223 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010224 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010225 }
10226 }
10227
10228 /**
10229 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10230 * scrollbar is not drawn by default.</p>
10231 *
10232 * @return true if the vertical scrollbar should be painted, false
10233 * otherwise
10234 *
10235 * @see #setVerticalScrollBarEnabled(boolean)
10236 */
10237 public boolean isVerticalScrollBarEnabled() {
10238 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10239 }
10240
10241 /**
10242 * <p>Define whether the vertical scrollbar should be drawn or not. The
10243 * scrollbar is not drawn by default.</p>
10244 *
10245 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10246 * be painted
10247 *
10248 * @see #isVerticalScrollBarEnabled()
10249 */
10250 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10251 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10252 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010253 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010254 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010255 }
10256 }
10257
Adam Powell20232d02010-12-08 21:08:53 -080010258 /**
10259 * @hide
10260 */
10261 protected void recomputePadding() {
10262 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010263 }
Joe Malin32736f02011-01-19 16:14:20 -080010264
Mike Cleronfe81d382009-09-28 14:22:16 -070010265 /**
10266 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010267 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010268 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010269 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010270 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010271 */
10272 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10273 initScrollCache();
10274 final ScrollabilityCache scrollabilityCache = mScrollCache;
10275 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010276 if (fadeScrollbars) {
10277 scrollabilityCache.state = ScrollabilityCache.OFF;
10278 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010279 scrollabilityCache.state = ScrollabilityCache.ON;
10280 }
10281 }
Joe Malin32736f02011-01-19 16:14:20 -080010282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010283 /**
Joe Malin32736f02011-01-19 16:14:20 -080010284 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010285 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010286 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010287 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010288 *
10289 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010290 */
10291 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010292 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010293 }
Joe Malin32736f02011-01-19 16:14:20 -080010294
Mike Cleron52f0a642009-09-28 18:21:37 -070010295 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010296 *
10297 * Returns the delay before scrollbars fade.
10298 *
10299 * @return the delay before scrollbars fade
10300 *
10301 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10302 */
10303 public int getScrollBarDefaultDelayBeforeFade() {
10304 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10305 mScrollCache.scrollBarDefaultDelayBeforeFade;
10306 }
10307
10308 /**
10309 * Define the delay before scrollbars fade.
10310 *
10311 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10312 *
10313 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10314 */
10315 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10316 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10317 }
10318
10319 /**
10320 *
10321 * Returns the scrollbar fade duration.
10322 *
10323 * @return the scrollbar fade duration
10324 *
10325 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10326 */
10327 public int getScrollBarFadeDuration() {
10328 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10329 mScrollCache.scrollBarFadeDuration;
10330 }
10331
10332 /**
10333 * Define the scrollbar fade duration.
10334 *
10335 * @param scrollBarFadeDuration - the scrollbar fade duration
10336 *
10337 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10338 */
10339 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10340 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10341 }
10342
10343 /**
10344 *
10345 * Returns the scrollbar size.
10346 *
10347 * @return the scrollbar size
10348 *
10349 * @attr ref android.R.styleable#View_scrollbarSize
10350 */
10351 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010352 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010353 mScrollCache.scrollBarSize;
10354 }
10355
10356 /**
10357 * Define the scrollbar size.
10358 *
10359 * @param scrollBarSize - the scrollbar size
10360 *
10361 * @attr ref android.R.styleable#View_scrollbarSize
10362 */
10363 public void setScrollBarSize(int scrollBarSize) {
10364 getScrollCache().scrollBarSize = scrollBarSize;
10365 }
10366
10367 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010368 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10369 * inset. When inset, they add to the padding of the view. And the scrollbars
10370 * can be drawn inside the padding area or on the edge of the view. For example,
10371 * if a view has a background drawable and you want to draw the scrollbars
10372 * inside the padding specified by the drawable, you can use
10373 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10374 * appear at the edge of the view, ignoring the padding, then you can use
10375 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10376 * @param style the style of the scrollbars. Should be one of
10377 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10378 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10379 * @see #SCROLLBARS_INSIDE_OVERLAY
10380 * @see #SCROLLBARS_INSIDE_INSET
10381 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10382 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010383 *
10384 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010385 */
10386 public void setScrollBarStyle(int style) {
10387 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10388 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010389 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010390 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010391 }
10392 }
10393
10394 /**
10395 * <p>Returns the current scrollbar style.</p>
10396 * @return the current scrollbar style
10397 * @see #SCROLLBARS_INSIDE_OVERLAY
10398 * @see #SCROLLBARS_INSIDE_INSET
10399 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10400 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010401 *
10402 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010403 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010404 @ViewDebug.ExportedProperty(mapping = {
10405 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10406 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10407 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10408 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10409 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010410 public int getScrollBarStyle() {
10411 return mViewFlags & SCROLLBARS_STYLE_MASK;
10412 }
10413
10414 /**
10415 * <p>Compute the horizontal range that the horizontal scrollbar
10416 * represents.</p>
10417 *
10418 * <p>The range is expressed in arbitrary units that must be the same as the
10419 * units used by {@link #computeHorizontalScrollExtent()} and
10420 * {@link #computeHorizontalScrollOffset()}.</p>
10421 *
10422 * <p>The default range is the drawing width of this view.</p>
10423 *
10424 * @return the total horizontal range represented by the horizontal
10425 * scrollbar
10426 *
10427 * @see #computeHorizontalScrollExtent()
10428 * @see #computeHorizontalScrollOffset()
10429 * @see android.widget.ScrollBarDrawable
10430 */
10431 protected int computeHorizontalScrollRange() {
10432 return getWidth();
10433 }
10434
10435 /**
10436 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10437 * within the horizontal range. This value is used to compute the position
10438 * of the thumb within the scrollbar's track.</p>
10439 *
10440 * <p>The range is expressed in arbitrary units that must be the same as the
10441 * units used by {@link #computeHorizontalScrollRange()} and
10442 * {@link #computeHorizontalScrollExtent()}.</p>
10443 *
10444 * <p>The default offset is the scroll offset of this view.</p>
10445 *
10446 * @return the horizontal offset of the scrollbar's thumb
10447 *
10448 * @see #computeHorizontalScrollRange()
10449 * @see #computeHorizontalScrollExtent()
10450 * @see android.widget.ScrollBarDrawable
10451 */
10452 protected int computeHorizontalScrollOffset() {
10453 return mScrollX;
10454 }
10455
10456 /**
10457 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10458 * within the horizontal range. This value is used to compute the length
10459 * of the thumb within the scrollbar's track.</p>
10460 *
10461 * <p>The range is expressed in arbitrary units that must be the same as the
10462 * units used by {@link #computeHorizontalScrollRange()} and
10463 * {@link #computeHorizontalScrollOffset()}.</p>
10464 *
10465 * <p>The default extent is the drawing width of this view.</p>
10466 *
10467 * @return the horizontal extent of the scrollbar's thumb
10468 *
10469 * @see #computeHorizontalScrollRange()
10470 * @see #computeHorizontalScrollOffset()
10471 * @see android.widget.ScrollBarDrawable
10472 */
10473 protected int computeHorizontalScrollExtent() {
10474 return getWidth();
10475 }
10476
10477 /**
10478 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
10479 *
10480 * <p>The range is expressed in arbitrary units that must be the same as the
10481 * units used by {@link #computeVerticalScrollExtent()} and
10482 * {@link #computeVerticalScrollOffset()}.</p>
10483 *
10484 * @return the total vertical range represented by the vertical scrollbar
10485 *
10486 * <p>The default range is the drawing height of this view.</p>
10487 *
10488 * @see #computeVerticalScrollExtent()
10489 * @see #computeVerticalScrollOffset()
10490 * @see android.widget.ScrollBarDrawable
10491 */
10492 protected int computeVerticalScrollRange() {
10493 return getHeight();
10494 }
10495
10496 /**
10497 * <p>Compute the vertical offset of the vertical scrollbar's thumb
10498 * within the horizontal range. This value is used to compute the position
10499 * of the thumb within the scrollbar's track.</p>
10500 *
10501 * <p>The range is expressed in arbitrary units that must be the same as the
10502 * units used by {@link #computeVerticalScrollRange()} and
10503 * {@link #computeVerticalScrollExtent()}.</p>
10504 *
10505 * <p>The default offset is the scroll offset of this view.</p>
10506 *
10507 * @return the vertical offset of the scrollbar's thumb
10508 *
10509 * @see #computeVerticalScrollRange()
10510 * @see #computeVerticalScrollExtent()
10511 * @see android.widget.ScrollBarDrawable
10512 */
10513 protected int computeVerticalScrollOffset() {
10514 return mScrollY;
10515 }
10516
10517 /**
10518 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
10519 * within the vertical range. This value is used to compute the length
10520 * of the thumb within the scrollbar's track.</p>
10521 *
10522 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080010523 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010524 * {@link #computeVerticalScrollOffset()}.</p>
10525 *
10526 * <p>The default extent is the drawing height of this view.</p>
10527 *
10528 * @return the vertical extent of the scrollbar's thumb
10529 *
10530 * @see #computeVerticalScrollRange()
10531 * @see #computeVerticalScrollOffset()
10532 * @see android.widget.ScrollBarDrawable
10533 */
10534 protected int computeVerticalScrollExtent() {
10535 return getHeight();
10536 }
10537
10538 /**
Adam Powell69159442011-06-13 17:53:06 -070010539 * Check if this view can be scrolled horizontally in a certain direction.
10540 *
10541 * @param direction Negative to check scrolling left, positive to check scrolling right.
10542 * @return true if this view can be scrolled in the specified direction, false otherwise.
10543 */
10544 public boolean canScrollHorizontally(int direction) {
10545 final int offset = computeHorizontalScrollOffset();
10546 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
10547 if (range == 0) return false;
10548 if (direction < 0) {
10549 return offset > 0;
10550 } else {
10551 return offset < range - 1;
10552 }
10553 }
10554
10555 /**
10556 * Check if this view can be scrolled vertically in a certain direction.
10557 *
10558 * @param direction Negative to check scrolling up, positive to check scrolling down.
10559 * @return true if this view can be scrolled in the specified direction, false otherwise.
10560 */
10561 public boolean canScrollVertically(int direction) {
10562 final int offset = computeVerticalScrollOffset();
10563 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
10564 if (range == 0) return false;
10565 if (direction < 0) {
10566 return offset > 0;
10567 } else {
10568 return offset < range - 1;
10569 }
10570 }
10571
10572 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010573 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
10574 * scrollbars are painted only if they have been awakened first.</p>
10575 *
10576 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080010577 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010578 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010579 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080010580 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010581 // scrollbars are drawn only when the animation is running
10582 final ScrollabilityCache cache = mScrollCache;
10583 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080010584
Mike Cleronf116bf82009-09-27 19:14:12 -070010585 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080010586
Mike Cleronf116bf82009-09-27 19:14:12 -070010587 if (state == ScrollabilityCache.OFF) {
10588 return;
10589 }
Joe Malin32736f02011-01-19 16:14:20 -080010590
Mike Cleronf116bf82009-09-27 19:14:12 -070010591 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080010592
Mike Cleronf116bf82009-09-27 19:14:12 -070010593 if (state == ScrollabilityCache.FADING) {
10594 // We're fading -- get our fade interpolation
10595 if (cache.interpolatorValues == null) {
10596 cache.interpolatorValues = new float[1];
10597 }
Joe Malin32736f02011-01-19 16:14:20 -080010598
Mike Cleronf116bf82009-09-27 19:14:12 -070010599 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080010600
Mike Cleronf116bf82009-09-27 19:14:12 -070010601 // Stops the animation if we're done
10602 if (cache.scrollBarInterpolator.timeToValues(values) ==
10603 Interpolator.Result.FREEZE_END) {
10604 cache.state = ScrollabilityCache.OFF;
10605 } else {
10606 cache.scrollBar.setAlpha(Math.round(values[0]));
10607 }
Joe Malin32736f02011-01-19 16:14:20 -080010608
10609 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070010610 // drawing. We only want this when we're fading so that
10611 // we prevent excessive redraws
10612 invalidate = true;
10613 } else {
10614 // We're just on -- but we may have been fading before so
10615 // reset alpha
10616 cache.scrollBar.setAlpha(255);
10617 }
10618
Joe Malin32736f02011-01-19 16:14:20 -080010619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010620 final int viewFlags = mViewFlags;
10621
10622 final boolean drawHorizontalScrollBar =
10623 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10624 final boolean drawVerticalScrollBar =
10625 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
10626 && !isVerticalScrollBarHidden();
10627
10628 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
10629 final int width = mRight - mLeft;
10630 final int height = mBottom - mTop;
10631
10632 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010633
Mike Reede8853fc2009-09-04 14:01:48 -040010634 final int scrollX = mScrollX;
10635 final int scrollY = mScrollY;
10636 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
10637
Mike Cleronf116bf82009-09-27 19:14:12 -070010638 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080010639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010640 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010641 int size = scrollBar.getSize(false);
10642 if (size <= 0) {
10643 size = cache.scrollBarSize;
10644 }
10645
Mike Cleronf116bf82009-09-27 19:14:12 -070010646 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040010647 computeHorizontalScrollOffset(),
10648 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040010649 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070010650 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080010651 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070010652 left = scrollX + (mPaddingLeft & inside);
10653 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
10654 bottom = top + size;
10655 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
10656 if (invalidate) {
10657 invalidate(left, top, right, bottom);
10658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010659 }
10660
10661 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010662 int size = scrollBar.getSize(true);
10663 if (size <= 0) {
10664 size = cache.scrollBarSize;
10665 }
10666
Mike Reede8853fc2009-09-04 14:01:48 -040010667 scrollBar.setParameters(computeVerticalScrollRange(),
10668 computeVerticalScrollOffset(),
10669 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080010670 switch (mVerticalScrollbarPosition) {
10671 default:
10672 case SCROLLBAR_POSITION_DEFAULT:
10673 case SCROLLBAR_POSITION_RIGHT:
10674 left = scrollX + width - size - (mUserPaddingRight & inside);
10675 break;
10676 case SCROLLBAR_POSITION_LEFT:
10677 left = scrollX + (mUserPaddingLeft & inside);
10678 break;
10679 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010680 top = scrollY + (mPaddingTop & inside);
10681 right = left + size;
10682 bottom = scrollY + height - (mUserPaddingBottom & inside);
10683 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
10684 if (invalidate) {
10685 invalidate(left, top, right, bottom);
10686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010687 }
10688 }
10689 }
10690 }
Romain Guy8506ab42009-06-11 17:35:47 -070010691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010692 /**
Romain Guy8506ab42009-06-11 17:35:47 -070010693 * 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 -080010694 * FastScroller is visible.
10695 * @return whether to temporarily hide the vertical scrollbar
10696 * @hide
10697 */
10698 protected boolean isVerticalScrollBarHidden() {
10699 return false;
10700 }
10701
10702 /**
10703 * <p>Draw the horizontal scrollbar if
10704 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
10705 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010706 * @param canvas the canvas on which to draw the scrollbar
10707 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010708 *
10709 * @see #isHorizontalScrollBarEnabled()
10710 * @see #computeHorizontalScrollRange()
10711 * @see #computeHorizontalScrollExtent()
10712 * @see #computeHorizontalScrollOffset()
10713 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070010714 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010715 */
Romain Guy8fb95422010-08-17 18:38:51 -070010716 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
10717 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010718 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010719 scrollBar.draw(canvas);
10720 }
Mike Reede8853fc2009-09-04 14:01:48 -040010721
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010722 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010723 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
10724 * returns true.</p>
10725 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010726 * @param canvas the canvas on which to draw the scrollbar
10727 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010728 *
10729 * @see #isVerticalScrollBarEnabled()
10730 * @see #computeVerticalScrollRange()
10731 * @see #computeVerticalScrollExtent()
10732 * @see #computeVerticalScrollOffset()
10733 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040010734 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010735 */
Romain Guy8fb95422010-08-17 18:38:51 -070010736 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
10737 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040010738 scrollBar.setBounds(l, t, r, b);
10739 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010740 }
10741
10742 /**
10743 * Implement this to do your drawing.
10744 *
10745 * @param canvas the canvas on which the background will be drawn
10746 */
10747 protected void onDraw(Canvas canvas) {
10748 }
10749
10750 /*
10751 * Caller is responsible for calling requestLayout if necessary.
10752 * (This allows addViewInLayout to not request a new layout.)
10753 */
10754 void assignParent(ViewParent parent) {
10755 if (mParent == null) {
10756 mParent = parent;
10757 } else if (parent == null) {
10758 mParent = null;
10759 } else {
10760 throw new RuntimeException("view " + this + " being added, but"
10761 + " it already has a parent");
10762 }
10763 }
10764
10765 /**
10766 * This is called when the view is attached to a window. At this point it
10767 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010768 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
10769 * however it may be called any time before the first onDraw -- including
10770 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010771 *
10772 * @see #onDetachedFromWindow()
10773 */
10774 protected void onAttachedToWindow() {
10775 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
10776 mParent.requestTransparentRegion(this);
10777 }
Adam Powell8568c3a2010-04-19 14:26:11 -070010778 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
10779 initialAwakenScrollBars();
10780 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
10781 }
Chet Haasea9b61ac2010-12-20 07:40:25 -080010782 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070010783 // Order is important here: LayoutDirection MUST be resolved before Padding
10784 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010785 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010786 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010787 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070010788 resolveTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070010789 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070010790 if (isFocused()) {
10791 InputMethodManager imm = InputMethodManager.peekInstance();
10792 imm.focusIn(this);
10793 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010794 }
Cibu Johny86666632010-02-22 13:01:02 -080010795
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010796 /**
Romain Guybb9908b2012-03-08 11:14:07 -080010797 * @see #onScreenStateChanged(int)
10798 */
10799 void dispatchScreenStateChanged(int screenState) {
10800 onScreenStateChanged(screenState);
10801 }
10802
10803 /**
10804 * This method is called whenever the state of the screen this view is
10805 * attached to changes. A state change will usually occurs when the screen
10806 * turns on or off (whether it happens automatically or the user does it
10807 * manually.)
10808 *
10809 * @param screenState The new state of the screen. Can be either
10810 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
10811 */
10812 public void onScreenStateChanged(int screenState) {
10813 }
10814
10815 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010816 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
10817 */
10818 private boolean hasRtlSupport() {
10819 return mContext.getApplicationInfo().hasRtlSupport();
10820 }
10821
10822 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010823 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
10824 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010825 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010826 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010827 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010828 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010829 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010830
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010831 if (hasRtlSupport()) {
10832 // Set resolved depending on layout direction
10833 switch (getLayoutDirection()) {
10834 case LAYOUT_DIRECTION_INHERIT:
10835 // If this is root view, no need to look at parent's layout dir.
10836 if (canResolveLayoutDirection()) {
10837 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010838
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010839 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
10840 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10841 }
10842 } else {
10843 // Nothing to do, LTR by default
10844 }
10845 break;
10846 case LAYOUT_DIRECTION_RTL:
10847 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10848 break;
10849 case LAYOUT_DIRECTION_LOCALE:
10850 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010851 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10852 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010853 break;
10854 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010855 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010856 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010857 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010858
10859 // Set to resolved
10860 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010861 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010862 // Resolve padding
10863 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010864 }
10865
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010866 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010867 * Called when layout direction has been resolved.
10868 *
10869 * The default implementation does nothing.
10870 */
10871 public void onResolvedLayoutDirectionChanged() {
10872 }
10873
10874 /**
10875 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010876 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010877 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010878 // If the user specified the absolute padding (either with android:padding or
10879 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
10880 // use the default padding or the padding from the background drawable
10881 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010882 int resolvedLayoutDirection = getResolvedLayoutDirection();
10883 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010884 case LAYOUT_DIRECTION_RTL:
10885 // Start user padding override Right user padding. Otherwise, if Right user
10886 // padding is not defined, use the default Right padding. If Right user padding
10887 // is defined, just use it.
10888 if (mUserPaddingStart >= 0) {
10889 mUserPaddingRight = mUserPaddingStart;
10890 } else if (mUserPaddingRight < 0) {
10891 mUserPaddingRight = mPaddingRight;
10892 }
10893 if (mUserPaddingEnd >= 0) {
10894 mUserPaddingLeft = mUserPaddingEnd;
10895 } else if (mUserPaddingLeft < 0) {
10896 mUserPaddingLeft = mPaddingLeft;
10897 }
10898 break;
10899 case LAYOUT_DIRECTION_LTR:
10900 default:
10901 // Start user padding override Left user padding. Otherwise, if Left user
10902 // padding is not defined, use the default left padding. If Left user padding
10903 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010904 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010905 mUserPaddingLeft = mUserPaddingStart;
10906 } else if (mUserPaddingLeft < 0) {
10907 mUserPaddingLeft = mPaddingLeft;
10908 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010909 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010910 mUserPaddingRight = mUserPaddingEnd;
10911 } else if (mUserPaddingRight < 0) {
10912 mUserPaddingRight = mPaddingRight;
10913 }
10914 }
10915
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010916 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
10917
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010918 if(isPaddingRelative()) {
10919 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
10920 } else {
10921 recomputePadding();
10922 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010923 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010924 }
10925
10926 /**
10927 * Resolve padding depending on the layout direction. Subclasses that care about
10928 * padding resolution should override this method. The default implementation does
10929 * nothing.
10930 *
10931 * @param layoutDirection the direction of the layout
10932 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080010933 * @see {@link #LAYOUT_DIRECTION_LTR}
10934 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010935 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010936 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010937 }
10938
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010939 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010940 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010941 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010942 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010943 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010944 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010945 switch (getLayoutDirection()) {
10946 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010947 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010948 default:
10949 return true;
10950 }
10951 }
10952
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010953 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010954 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
10955 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010956 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010957 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010958 // Reset the current resolved bits
10959 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010960 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080010961 // Reset also the text direction
10962 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010963 }
10964
10965 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010966 * Called during reset of resolved layout direction.
10967 *
10968 * Subclasses need to override this method to clear cached information that depends on the
10969 * resolved layout direction, or to inform child views that inherit their layout direction.
10970 *
10971 * The default implementation does nothing.
10972 */
10973 public void onResolvedLayoutDirectionReset() {
10974 }
10975
10976 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010977 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010978 *
10979 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010980 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010981 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010982 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080010983 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010984 }
10985
10986 /**
10987 * This is called when the view is detached from a window. At this point it
10988 * no longer has a surface for drawing.
10989 *
10990 * @see #onAttachedToWindow()
10991 */
10992 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080010993 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080010994
Romain Guya440b002010-02-24 15:57:54 -080010995 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050010996 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080010997 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070010998 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080010999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011000 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011001
Romain Guya998dff2012-03-23 18:58:36 -070011002 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011003
11004 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011005 if (mDisplayList != null) {
11006 mAttachInfo.mViewRootImpl.invalidateDisplayList(mDisplayList);
11007 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011008 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011009 } else {
11010 if (mDisplayList != null) {
11011 // Should never happen
11012 mDisplayList.invalidate();
11013 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011014 }
11015
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011016 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011017
11018 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011019 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011020 resetAccessibilityStateChanged();
11021 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011022 }
11023
11024 /**
11025 * @return The number of times this view has been attached to a window
11026 */
11027 protected int getWindowAttachCount() {
11028 return mWindowAttachCount;
11029 }
11030
11031 /**
11032 * Retrieve a unique token identifying the window this view is attached to.
11033 * @return Return the window's token for use in
11034 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11035 */
11036 public IBinder getWindowToken() {
11037 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11038 }
11039
11040 /**
11041 * Retrieve a unique token identifying the top-level "real" window of
11042 * the window that this view is attached to. That is, this is like
11043 * {@link #getWindowToken}, except if the window this view in is a panel
11044 * window (attached to another containing window), then the token of
11045 * the containing window is returned instead.
11046 *
11047 * @return Returns the associated window token, either
11048 * {@link #getWindowToken()} or the containing window's token.
11049 */
11050 public IBinder getApplicationWindowToken() {
11051 AttachInfo ai = mAttachInfo;
11052 if (ai != null) {
11053 IBinder appWindowToken = ai.mPanelParentWindowToken;
11054 if (appWindowToken == null) {
11055 appWindowToken = ai.mWindowToken;
11056 }
11057 return appWindowToken;
11058 }
11059 return null;
11060 }
11061
11062 /**
11063 * Retrieve private session object this view hierarchy is using to
11064 * communicate with the window manager.
11065 * @return the session object to communicate with the window manager
11066 */
11067 /*package*/ IWindowSession getWindowSession() {
11068 return mAttachInfo != null ? mAttachInfo.mSession : null;
11069 }
11070
11071 /**
11072 * @param info the {@link android.view.View.AttachInfo} to associated with
11073 * this view
11074 */
11075 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11076 //System.out.println("Attached! " + this);
11077 mAttachInfo = info;
11078 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011079 // We will need to evaluate the drawable state at least once.
11080 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011081 if (mFloatingTreeObserver != null) {
11082 info.mTreeObserver.merge(mFloatingTreeObserver);
11083 mFloatingTreeObserver = null;
11084 }
11085 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11086 mAttachInfo.mScrollContainers.add(this);
11087 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11088 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011089 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011090 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011091
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011092 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011093 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011094 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011095 if (listeners != null && listeners.size() > 0) {
11096 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11097 // perform the dispatching. The iterator is a safe guard against listeners that
11098 // could mutate the list by calling the various add/remove methods. This prevents
11099 // the array from being modified while we iterate it.
11100 for (OnAttachStateChangeListener listener : listeners) {
11101 listener.onViewAttachedToWindow(this);
11102 }
11103 }
11104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011105 int vis = info.mWindowVisibility;
11106 if (vis != GONE) {
11107 onWindowVisibilityChanged(vis);
11108 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011109 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11110 // If nobody has evaluated the drawable state yet, then do it now.
11111 refreshDrawableState();
11112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011113 }
11114
11115 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011116 AttachInfo info = mAttachInfo;
11117 if (info != null) {
11118 int vis = info.mWindowVisibility;
11119 if (vis != GONE) {
11120 onWindowVisibilityChanged(GONE);
11121 }
11122 }
11123
11124 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011125
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011126 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011127 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011128 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011129 if (listeners != null && listeners.size() > 0) {
11130 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11131 // perform the dispatching. The iterator is a safe guard against listeners that
11132 // could mutate the list by calling the various add/remove methods. This prevents
11133 // the array from being modified while we iterate it.
11134 for (OnAttachStateChangeListener listener : listeners) {
11135 listener.onViewDetachedFromWindow(this);
11136 }
11137 }
11138
Romain Guy01d5edc2011-01-28 11:28:53 -080011139 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011140 mAttachInfo.mScrollContainers.remove(this);
11141 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11142 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011144 mAttachInfo = null;
11145 }
11146
11147 /**
11148 * Store this view hierarchy's frozen state into the given container.
11149 *
11150 * @param container The SparseArray in which to save the view's state.
11151 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011152 * @see #restoreHierarchyState(android.util.SparseArray)
11153 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11154 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011155 */
11156 public void saveHierarchyState(SparseArray<Parcelable> container) {
11157 dispatchSaveInstanceState(container);
11158 }
11159
11160 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011161 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11162 * this view and its children. May be overridden to modify how freezing happens to a
11163 * 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 -080011164 *
11165 * @param container The SparseArray in which to save the view's state.
11166 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011167 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11168 * @see #saveHierarchyState(android.util.SparseArray)
11169 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011170 */
11171 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11172 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11173 mPrivateFlags &= ~SAVE_STATE_CALLED;
11174 Parcelable state = onSaveInstanceState();
11175 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11176 throw new IllegalStateException(
11177 "Derived class did not call super.onSaveInstanceState()");
11178 }
11179 if (state != null) {
11180 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11181 // + ": " + state);
11182 container.put(mID, state);
11183 }
11184 }
11185 }
11186
11187 /**
11188 * Hook allowing a view to generate a representation of its internal state
11189 * that can later be used to create a new instance with that same state.
11190 * This state should only contain information that is not persistent or can
11191 * not be reconstructed later. For example, you will never store your
11192 * current position on screen because that will be computed again when a
11193 * new instance of the view is placed in its view hierarchy.
11194 * <p>
11195 * Some examples of things you may store here: the current cursor position
11196 * in a text view (but usually not the text itself since that is stored in a
11197 * content provider or other persistent storage), the currently selected
11198 * item in a list view.
11199 *
11200 * @return Returns a Parcelable object containing the view's current dynamic
11201 * state, or null if there is nothing interesting to save. The
11202 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011203 * @see #onRestoreInstanceState(android.os.Parcelable)
11204 * @see #saveHierarchyState(android.util.SparseArray)
11205 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011206 * @see #setSaveEnabled(boolean)
11207 */
11208 protected Parcelable onSaveInstanceState() {
11209 mPrivateFlags |= SAVE_STATE_CALLED;
11210 return BaseSavedState.EMPTY_STATE;
11211 }
11212
11213 /**
11214 * Restore this view hierarchy's frozen state from the given container.
11215 *
11216 * @param container The SparseArray which holds previously frozen states.
11217 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011218 * @see #saveHierarchyState(android.util.SparseArray)
11219 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11220 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011221 */
11222 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11223 dispatchRestoreInstanceState(container);
11224 }
11225
11226 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011227 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11228 * state for this view and its children. May be overridden to modify how restoring
11229 * happens to a view's children; for example, some views may want to not store state
11230 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011231 *
11232 * @param container The SparseArray which holds previously saved state.
11233 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011234 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11235 * @see #restoreHierarchyState(android.util.SparseArray)
11236 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011237 */
11238 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11239 if (mID != NO_ID) {
11240 Parcelable state = container.get(mID);
11241 if (state != null) {
11242 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11243 // + ": " + state);
11244 mPrivateFlags &= ~SAVE_STATE_CALLED;
11245 onRestoreInstanceState(state);
11246 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11247 throw new IllegalStateException(
11248 "Derived class did not call super.onRestoreInstanceState()");
11249 }
11250 }
11251 }
11252 }
11253
11254 /**
11255 * Hook allowing a view to re-apply a representation of its internal state that had previously
11256 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11257 * null state.
11258 *
11259 * @param state The frozen state that had previously been returned by
11260 * {@link #onSaveInstanceState}.
11261 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011262 * @see #onSaveInstanceState()
11263 * @see #restoreHierarchyState(android.util.SparseArray)
11264 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011265 */
11266 protected void onRestoreInstanceState(Parcelable state) {
11267 mPrivateFlags |= SAVE_STATE_CALLED;
11268 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011269 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11270 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011271 + "when two views of different type have the same id in the same hierarchy. "
11272 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011273 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011274 }
11275 }
11276
11277 /**
11278 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11279 *
11280 * @return the drawing start time in milliseconds
11281 */
11282 public long getDrawingTime() {
11283 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11284 }
11285
11286 /**
11287 * <p>Enables or disables the duplication of the parent's state into this view. When
11288 * duplication is enabled, this view gets its drawable state from its parent rather
11289 * than from its own internal properties.</p>
11290 *
11291 * <p>Note: in the current implementation, setting this property to true after the
11292 * view was added to a ViewGroup might have no effect at all. This property should
11293 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11294 *
11295 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11296 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011297 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011298 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11299 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011300 *
11301 * @param enabled True to enable duplication of the parent's drawable state, false
11302 * to disable it.
11303 *
11304 * @see #getDrawableState()
11305 * @see #isDuplicateParentStateEnabled()
11306 */
11307 public void setDuplicateParentStateEnabled(boolean enabled) {
11308 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11309 }
11310
11311 /**
11312 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11313 *
11314 * @return True if this view's drawable state is duplicated from the parent,
11315 * false otherwise
11316 *
11317 * @see #getDrawableState()
11318 * @see #setDuplicateParentStateEnabled(boolean)
11319 */
11320 public boolean isDuplicateParentStateEnabled() {
11321 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11322 }
11323
11324 /**
Romain Guy171c5922011-01-06 10:04:23 -080011325 * <p>Specifies the type of layer backing this view. The layer can be
11326 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11327 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011328 *
Romain Guy171c5922011-01-06 10:04:23 -080011329 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11330 * instance that controls how the layer is composed on screen. The following
11331 * properties of the paint are taken into account when composing the layer:</p>
11332 * <ul>
11333 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11334 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11335 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11336 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011337 *
Romain Guy171c5922011-01-06 10:04:23 -080011338 * <p>If this view has an alpha value set to < 1.0 by calling
11339 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11340 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11341 * equivalent to setting a hardware layer on this view and providing a paint with
11342 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011343 *
Romain Guy171c5922011-01-06 10:04:23 -080011344 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11345 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11346 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011347 *
Romain Guy171c5922011-01-06 10:04:23 -080011348 * @param layerType The ype of layer to use with this view, must be one of
11349 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11350 * {@link #LAYER_TYPE_HARDWARE}
11351 * @param paint The paint used to compose the layer. This argument is optional
11352 * and can be null. It is ignored when the layer type is
11353 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011354 *
11355 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011356 * @see #LAYER_TYPE_NONE
11357 * @see #LAYER_TYPE_SOFTWARE
11358 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011359 * @see #setAlpha(float)
11360 *
Romain Guy171c5922011-01-06 10:04:23 -080011361 * @attr ref android.R.styleable#View_layerType
11362 */
11363 public void setLayerType(int layerType, Paint paint) {
11364 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011365 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011366 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11367 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011368
Romain Guyd6cd5722011-01-17 14:42:41 -080011369 if (layerType == mLayerType) {
11370 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11371 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011372 invalidateParentCaches();
11373 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011374 }
11375 return;
11376 }
Romain Guy171c5922011-01-06 10:04:23 -080011377
11378 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011379 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011380 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011381 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011382 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011383 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011384 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011385 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011386 default:
11387 break;
Romain Guy171c5922011-01-06 10:04:23 -080011388 }
11389
11390 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011391 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11392 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11393 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011394
Romain Guy0fd89bf2011-01-26 15:41:30 -080011395 invalidateParentCaches();
11396 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011397 }
11398
11399 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011400 * Indicates whether this view has a static layer. A view with layer type
11401 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11402 * dynamic.
11403 */
11404 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011405 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011406 }
11407
11408 /**
Romain Guy171c5922011-01-06 10:04:23 -080011409 * Indicates what type of layer is currently associated with this view. By default
11410 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11411 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11412 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011413 *
Romain Guy171c5922011-01-06 10:04:23 -080011414 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11415 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011416 *
11417 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011418 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011419 * @see #LAYER_TYPE_NONE
11420 * @see #LAYER_TYPE_SOFTWARE
11421 * @see #LAYER_TYPE_HARDWARE
11422 */
11423 public int getLayerType() {
11424 return mLayerType;
11425 }
Joe Malin32736f02011-01-19 16:14:20 -080011426
Romain Guy6c319ca2011-01-11 14:29:25 -080011427 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011428 * Forces this view's layer to be created and this view to be rendered
11429 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11430 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011431 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011432 * This method can for instance be used to render a view into its layer before
11433 * starting an animation. If this view is complex, rendering into the layer
11434 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011435 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011436 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011437 *
11438 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080011439 */
11440 public void buildLayer() {
11441 if (mLayerType == LAYER_TYPE_NONE) return;
11442
11443 if (mAttachInfo == null) {
11444 throw new IllegalStateException("This view must be attached to a window first");
11445 }
11446
11447 switch (mLayerType) {
11448 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080011449 if (mAttachInfo.mHardwareRenderer != null &&
11450 mAttachInfo.mHardwareRenderer.isEnabled() &&
11451 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011452 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080011453 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011454 break;
11455 case LAYER_TYPE_SOFTWARE:
11456 buildDrawingCache(true);
11457 break;
11458 }
11459 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011460
Romain Guy9c4b79a2011-11-10 19:23:58 -080011461 // Make sure the HardwareRenderer.validate() was invoked before calling this method
11462 void flushLayer() {
11463 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
11464 mHardwareLayer.flush();
11465 }
11466 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011467
11468 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080011469 * <p>Returns a hardware layer that can be used to draw this view again
11470 * without executing its draw method.</p>
11471 *
11472 * @return A HardwareLayer ready to render, or null if an error occurred.
11473 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080011474 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070011475 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
11476 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080011477 return null;
11478 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011479
Romain Guy9c4b79a2011-11-10 19:23:58 -080011480 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080011481
11482 final int width = mRight - mLeft;
11483 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080011484
Romain Guy6c319ca2011-01-11 14:29:25 -080011485 if (width == 0 || height == 0) {
11486 return null;
11487 }
11488
11489 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
11490 if (mHardwareLayer == null) {
11491 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
11492 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070011493 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011494 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
11495 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070011496 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011497 }
11498
Romain Guy5cd5c3f2011-10-17 17:10:02 -070011499 // The layer is not valid if the underlying GPU resources cannot be allocated
11500 if (!mHardwareLayer.isValid()) {
11501 return null;
11502 }
11503
Chet Haasea1cff502012-02-21 13:43:44 -080011504 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080011505 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080011506 }
11507
11508 return mHardwareLayer;
11509 }
Romain Guy171c5922011-01-06 10:04:23 -080011510
Romain Guy589b0bb2011-10-10 13:57:47 -070011511 /**
11512 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070011513 *
Romain Guy589b0bb2011-10-10 13:57:47 -070011514 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070011515 *
11516 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070011517 * @see #LAYER_TYPE_HARDWARE
11518 */
Romain Guya998dff2012-03-23 18:58:36 -070011519 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070011520 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011521 AttachInfo info = mAttachInfo;
11522 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070011523 info.mHardwareRenderer.isEnabled() &&
11524 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011525 mHardwareLayer.destroy();
11526 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080011527
Romain Guy9c4b79a2011-11-10 19:23:58 -080011528 invalidate(true);
11529 invalidateParentCaches();
11530 }
Romain Guy65b345f2011-07-27 18:51:50 -070011531 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070011532 }
Romain Guy65b345f2011-07-27 18:51:50 -070011533 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070011534 }
11535
Romain Guy171c5922011-01-06 10:04:23 -080011536 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080011537 * Destroys all hardware rendering resources. This method is invoked
11538 * when the system needs to reclaim resources. Upon execution of this
11539 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070011540 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011541 * Note: you <strong>must</strong> call
11542 * <code>super.destroyHardwareResources()</code> when overriding
11543 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070011544 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011545 * @hide
11546 */
11547 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070011548 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011549 }
11550
11551 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011552 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
11553 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
11554 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
11555 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
11556 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
11557 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011558 *
Romain Guy171c5922011-01-06 10:04:23 -080011559 * <p>Enabling the drawing cache is similar to
11560 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080011561 * acceleration is turned off. When hardware acceleration is turned on, enabling the
11562 * drawing cache has no effect on rendering because the system uses a different mechanism
11563 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
11564 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
11565 * for information on how to enable software and hardware layers.</p>
11566 *
11567 * <p>This API can be used to manually generate
11568 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
11569 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011570 *
11571 * @param enabled true to enable the drawing cache, false otherwise
11572 *
11573 * @see #isDrawingCacheEnabled()
11574 * @see #getDrawingCache()
11575 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080011576 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011577 */
11578 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011579 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011580 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
11581 }
11582
11583 /**
11584 * <p>Indicates whether the drawing cache is enabled for this view.</p>
11585 *
11586 * @return true if the drawing cache is enabled
11587 *
11588 * @see #setDrawingCacheEnabled(boolean)
11589 * @see #getDrawingCache()
11590 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011591 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011592 public boolean isDrawingCacheEnabled() {
11593 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
11594 }
11595
11596 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080011597 * Debugging utility which recursively outputs the dirty state of a view and its
11598 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080011599 *
Chet Haasedaf98e92011-01-10 14:10:36 -080011600 * @hide
11601 */
Romain Guy676b1732011-02-14 14:45:33 -080011602 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080011603 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
11604 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
11605 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
11606 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
11607 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
11608 if (clear) {
11609 mPrivateFlags &= clearMask;
11610 }
11611 if (this instanceof ViewGroup) {
11612 ViewGroup parent = (ViewGroup) this;
11613 final int count = parent.getChildCount();
11614 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080011615 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080011616 child.outputDirtyFlags(indent + " ", clear, clearMask);
11617 }
11618 }
11619 }
11620
11621 /**
11622 * This method is used by ViewGroup to cause its children to restore or recreate their
11623 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
11624 * to recreate its own display list, which would happen if it went through the normal
11625 * draw/dispatchDraw mechanisms.
11626 *
11627 * @hide
11628 */
11629 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080011630
11631 /**
11632 * A view that is not attached or hardware accelerated cannot create a display list.
11633 * This method checks these conditions and returns the appropriate result.
11634 *
11635 * @return true if view has the ability to create a display list, false otherwise.
11636 *
11637 * @hide
11638 */
11639 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080011640 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080011641 }
Joe Malin32736f02011-01-19 16:14:20 -080011642
Chet Haasedaf98e92011-01-10 14:10:36 -080011643 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080011644 * @return The HardwareRenderer associated with that view or null if hardware rendering
11645 * is not supported or this this has not been attached to a window.
11646 *
11647 * @hide
11648 */
11649 public HardwareRenderer getHardwareRenderer() {
11650 if (mAttachInfo != null) {
11651 return mAttachInfo.mHardwareRenderer;
11652 }
11653 return null;
11654 }
11655
11656 /**
Chet Haasea1cff502012-02-21 13:43:44 -080011657 * Returns a DisplayList. If the incoming displayList is null, one will be created.
11658 * Otherwise, the same display list will be returned (after having been rendered into
11659 * along the way, depending on the invalidation state of the view).
11660 *
11661 * @param displayList The previous version of this displayList, could be null.
11662 * @param isLayer Whether the requester of the display list is a layer. If so,
11663 * the view will avoid creating a layer inside the resulting display list.
11664 * @return A new or reused DisplayList object.
11665 */
11666 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
11667 if (!canHaveDisplayList()) {
11668 return null;
11669 }
11670
11671 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
11672 displayList == null || !displayList.isValid() ||
11673 (!isLayer && mRecreateDisplayList))) {
11674 // Don't need to recreate the display list, just need to tell our
11675 // children to restore/recreate theirs
11676 if (displayList != null && displayList.isValid() &&
11677 !isLayer && !mRecreateDisplayList) {
11678 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11679 mPrivateFlags &= ~DIRTY_MASK;
11680 dispatchGetDisplayList();
11681
11682 return displayList;
11683 }
11684
11685 if (!isLayer) {
11686 // If we got here, we're recreating it. Mark it as such to ensure that
11687 // we copy in child display lists into ours in drawChild()
11688 mRecreateDisplayList = true;
11689 }
11690 if (displayList == null) {
11691 final String name = getClass().getSimpleName();
11692 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
11693 // If we're creating a new display list, make sure our parent gets invalidated
11694 // since they will need to recreate their display list to account for this
11695 // new child display list.
11696 invalidateParentCaches();
11697 }
11698
11699 boolean caching = false;
11700 final HardwareCanvas canvas = displayList.start();
11701 int restoreCount = 0;
11702 int width = mRight - mLeft;
11703 int height = mBottom - mTop;
11704
11705 try {
11706 canvas.setViewport(width, height);
11707 // The dirty rect should always be null for a display list
11708 canvas.onPreDraw(null);
11709 int layerType = (
11710 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
11711 getLayerType() : LAYER_TYPE_NONE;
Chet Haaseb85967b2012-03-26 14:37:51 -070011712 if (!isLayer && layerType != LAYER_TYPE_NONE && USE_DISPLAY_LIST_PROPERTIES) {
11713 if (layerType == LAYER_TYPE_HARDWARE) {
11714 final HardwareLayer layer = getHardwareLayer();
11715 if (layer != null && layer.isValid()) {
11716 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
11717 } else {
11718 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
11719 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
11720 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11721 }
11722 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080011723 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070011724 buildDrawingCache(true);
11725 Bitmap cache = getDrawingCache(true);
11726 if (cache != null) {
11727 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
11728 caching = true;
11729 }
Chet Haasea1cff502012-02-21 13:43:44 -080011730 }
Chet Haasea1cff502012-02-21 13:43:44 -080011731 } else {
11732
11733 computeScroll();
11734
11735 if (!USE_DISPLAY_LIST_PROPERTIES) {
11736 restoreCount = canvas.save();
11737 }
11738 canvas.translate(-mScrollX, -mScrollY);
11739 if (!isLayer) {
11740 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11741 mPrivateFlags &= ~DIRTY_MASK;
11742 }
11743
11744 // Fast path for layouts with no backgrounds
11745 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11746 dispatchDraw(canvas);
11747 } else {
11748 draw(canvas);
11749 }
11750 }
11751 } finally {
11752 if (USE_DISPLAY_LIST_PROPERTIES) {
11753 canvas.restoreToCount(restoreCount);
11754 }
11755 canvas.onPostDraw();
11756
11757 displayList.end();
11758 if (USE_DISPLAY_LIST_PROPERTIES) {
11759 displayList.setCaching(caching);
11760 }
11761 if (isLayer && USE_DISPLAY_LIST_PROPERTIES) {
11762 displayList.setLeftTopRightBottom(0, 0, width, height);
11763 } else {
11764 setDisplayListProperties(displayList);
11765 }
11766 }
11767 } else if (!isLayer) {
11768 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11769 mPrivateFlags &= ~DIRTY_MASK;
11770 }
11771
11772 return displayList;
11773 }
11774
11775 /**
11776 * Get the DisplayList for the HardwareLayer
11777 *
11778 * @param layer The HardwareLayer whose DisplayList we want
11779 * @return A DisplayList fopr the specified HardwareLayer
11780 */
11781 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
11782 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
11783 layer.setDisplayList(displayList);
11784 return displayList;
11785 }
11786
11787
11788 /**
Romain Guyb051e892010-09-28 19:09:36 -070011789 * <p>Returns a display list that can be used to draw this view again
11790 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011791 *
Romain Guyb051e892010-09-28 19:09:36 -070011792 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080011793 *
11794 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070011795 */
Chet Haasedaf98e92011-01-10 14:10:36 -080011796 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080011797 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070011798 return mDisplayList;
11799 }
11800
11801 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011802 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011803 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011804 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011805 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011806 * @see #getDrawingCache(boolean)
11807 */
11808 public Bitmap getDrawingCache() {
11809 return getDrawingCache(false);
11810 }
11811
11812 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011813 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
11814 * is null when caching is disabled. If caching is enabled and the cache is not ready,
11815 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
11816 * draw from the cache when the cache is enabled. To benefit from the cache, you must
11817 * request the drawing cache by calling this method and draw it on screen if the
11818 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011819 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011820 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11821 * this method will create a bitmap of the same size as this view. Because this bitmap
11822 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11823 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11824 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11825 * size than the view. This implies that your application must be able to handle this
11826 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011827 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011828 * @param autoScale Indicates whether the generated bitmap should be scaled based on
11829 * the current density of the screen when the application is in compatibility
11830 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011831 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011832 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011833 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011834 * @see #setDrawingCacheEnabled(boolean)
11835 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070011836 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011837 * @see #destroyDrawingCache()
11838 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011839 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011840 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
11841 return null;
11842 }
11843 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011844 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011845 }
Romain Guy02890fd2010-08-06 17:58:44 -070011846 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011847 }
11848
11849 /**
11850 * <p>Frees the resources used by the drawing cache. If you call
11851 * {@link #buildDrawingCache()} manually without calling
11852 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11853 * should cleanup the cache with this method afterwards.</p>
11854 *
11855 * @see #setDrawingCacheEnabled(boolean)
11856 * @see #buildDrawingCache()
11857 * @see #getDrawingCache()
11858 */
11859 public void destroyDrawingCache() {
11860 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011861 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011862 mDrawingCache = null;
11863 }
Romain Guyfbd8f692009-06-26 14:51:58 -070011864 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011865 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070011866 mUnscaledDrawingCache = null;
11867 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011868 }
11869
11870 /**
11871 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070011872 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011873 * view will always be drawn on top of a solid color.
11874 *
11875 * @param color The background color to use for the drawing cache's bitmap
11876 *
11877 * @see #setDrawingCacheEnabled(boolean)
11878 * @see #buildDrawingCache()
11879 * @see #getDrawingCache()
11880 */
11881 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080011882 if (color != mDrawingCacheBackgroundColor) {
11883 mDrawingCacheBackgroundColor = color;
11884 mPrivateFlags &= ~DRAWING_CACHE_VALID;
11885 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011886 }
11887
11888 /**
11889 * @see #setDrawingCacheBackgroundColor(int)
11890 *
11891 * @return The background color to used for the drawing cache's bitmap
11892 */
11893 public int getDrawingCacheBackgroundColor() {
11894 return mDrawingCacheBackgroundColor;
11895 }
11896
11897 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011898 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011899 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011900 * @see #buildDrawingCache(boolean)
11901 */
11902 public void buildDrawingCache() {
11903 buildDrawingCache(false);
11904 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080011905
Romain Guyfbd8f692009-06-26 14:51:58 -070011906 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011907 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
11908 *
11909 * <p>If you call {@link #buildDrawingCache()} manually without calling
11910 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11911 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011912 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011913 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11914 * this method will create a bitmap of the same size as this view. Because this bitmap
11915 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11916 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11917 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11918 * size than the view. This implies that your application must be able to handle this
11919 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011920 *
Romain Guy0d9275e2010-10-26 14:22:30 -070011921 * <p>You should avoid calling this method when hardware acceleration is enabled. If
11922 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080011923 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070011924 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011925 *
11926 * @see #getDrawingCache()
11927 * @see #destroyDrawingCache()
11928 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011929 public void buildDrawingCache(boolean autoScale) {
11930 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070011931 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011932 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011933
11934 if (ViewDebug.TRACE_HIERARCHY) {
11935 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
11936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011937
Romain Guy8506ab42009-06-11 17:35:47 -070011938 int width = mRight - mLeft;
11939 int height = mBottom - mTop;
11940
11941 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070011942 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070011943
Romain Guye1123222009-06-29 14:24:56 -070011944 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011945 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
11946 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070011947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011948
11949 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070011950 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080011951 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011952
11953 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070011954 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080011955 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011956 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
11957 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080011958 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011959 return;
11960 }
11961
11962 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070011963 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011964
11965 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011966 Bitmap.Config quality;
11967 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080011968 // Never pick ARGB_4444 because it looks awful
11969 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011970 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
11971 case DRAWING_CACHE_QUALITY_AUTO:
11972 quality = Bitmap.Config.ARGB_8888;
11973 break;
11974 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080011975 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011976 break;
11977 case DRAWING_CACHE_QUALITY_HIGH:
11978 quality = Bitmap.Config.ARGB_8888;
11979 break;
11980 default:
11981 quality = Bitmap.Config.ARGB_8888;
11982 break;
11983 }
11984 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070011985 // Optimization for translucent windows
11986 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080011987 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011988 }
11989
11990 // Try to cleanup memory
11991 if (bitmap != null) bitmap.recycle();
11992
11993 try {
11994 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070011995 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070011996 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070011997 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070011998 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070011999 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012000 }
Adam Powell26153a32010-11-08 15:22:27 -080012001 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012002 } catch (OutOfMemoryError e) {
12003 // If there is not enough memory to create the bitmap cache, just
12004 // ignore the issue as bitmap caches are not required to draw the
12005 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012006 if (autoScale) {
12007 mDrawingCache = null;
12008 } else {
12009 mUnscaledDrawingCache = null;
12010 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012011 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012012 return;
12013 }
12014
12015 clear = drawingCacheBackgroundColor != 0;
12016 }
12017
12018 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012019 if (attachInfo != null) {
12020 canvas = attachInfo.mCanvas;
12021 if (canvas == null) {
12022 canvas = new Canvas();
12023 }
12024 canvas.setBitmap(bitmap);
12025 // Temporarily clobber the cached Canvas in case one of our children
12026 // is also using a drawing cache. Without this, the children would
12027 // steal the canvas by attaching their own bitmap to it and bad, bad
12028 // thing would happen (invisible views, corrupted drawings, etc.)
12029 attachInfo.mCanvas = null;
12030 } else {
12031 // This case should hopefully never or seldom happen
12032 canvas = new Canvas(bitmap);
12033 }
12034
12035 if (clear) {
12036 bitmap.eraseColor(drawingCacheBackgroundColor);
12037 }
12038
12039 computeScroll();
12040 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012041
Romain Guye1123222009-06-29 14:24:56 -070012042 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012043 final float scale = attachInfo.mApplicationScale;
12044 canvas.scale(scale, scale);
12045 }
Joe Malin32736f02011-01-19 16:14:20 -080012046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012047 canvas.translate(-mScrollX, -mScrollY);
12048
Romain Guy5bcdff42009-05-14 21:27:18 -070012049 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012050 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12051 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012052 mPrivateFlags |= DRAWING_CACHE_VALID;
12053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012054
12055 // Fast path for layouts with no backgrounds
12056 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12057 if (ViewDebug.TRACE_HIERARCHY) {
12058 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12059 }
Romain Guy5bcdff42009-05-14 21:27:18 -070012060 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012061 dispatchDraw(canvas);
12062 } else {
12063 draw(canvas);
12064 }
12065
12066 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012067 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012068
12069 if (attachInfo != null) {
12070 // Restore the cached Canvas for our siblings
12071 attachInfo.mCanvas = canvas;
12072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012073 }
12074 }
12075
12076 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012077 * Create a snapshot of the view into a bitmap. We should probably make
12078 * some form of this public, but should think about the API.
12079 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012080 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012081 int width = mRight - mLeft;
12082 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012083
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012084 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012085 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012086 width = (int) ((width * scale) + 0.5f);
12087 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012088
Romain Guy8c11e312009-09-14 15:15:30 -070012089 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012090 if (bitmap == null) {
12091 throw new OutOfMemoryError();
12092 }
12093
Romain Guyc529d8d2011-09-06 15:01:39 -070012094 Resources resources = getResources();
12095 if (resources != null) {
12096 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12097 }
Joe Malin32736f02011-01-19 16:14:20 -080012098
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012099 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012100 if (attachInfo != null) {
12101 canvas = attachInfo.mCanvas;
12102 if (canvas == null) {
12103 canvas = new Canvas();
12104 }
12105 canvas.setBitmap(bitmap);
12106 // Temporarily clobber the cached Canvas in case one of our children
12107 // is also using a drawing cache. Without this, the children would
12108 // steal the canvas by attaching their own bitmap to it and bad, bad
12109 // things would happen (invisible views, corrupted drawings, etc.)
12110 attachInfo.mCanvas = null;
12111 } else {
12112 // This case should hopefully never or seldom happen
12113 canvas = new Canvas(bitmap);
12114 }
12115
Romain Guy5bcdff42009-05-14 21:27:18 -070012116 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012117 bitmap.eraseColor(backgroundColor);
12118 }
12119
12120 computeScroll();
12121 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012122 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012123 canvas.translate(-mScrollX, -mScrollY);
12124
Romain Guy5bcdff42009-05-14 21:27:18 -070012125 // Temporarily remove the dirty mask
12126 int flags = mPrivateFlags;
12127 mPrivateFlags &= ~DIRTY_MASK;
12128
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012129 // Fast path for layouts with no backgrounds
12130 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12131 dispatchDraw(canvas);
12132 } else {
12133 draw(canvas);
12134 }
12135
Romain Guy5bcdff42009-05-14 21:27:18 -070012136 mPrivateFlags = flags;
12137
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012138 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012139 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012140
12141 if (attachInfo != null) {
12142 // Restore the cached Canvas for our siblings
12143 attachInfo.mCanvas = canvas;
12144 }
Romain Guy8506ab42009-06-11 17:35:47 -070012145
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012146 return bitmap;
12147 }
12148
12149 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012150 * Indicates whether this View is currently in edit mode. A View is usually
12151 * in edit mode when displayed within a developer tool. For instance, if
12152 * this View is being drawn by a visual user interface builder, this method
12153 * should return true.
12154 *
12155 * Subclasses should check the return value of this method to provide
12156 * different behaviors if their normal behavior might interfere with the
12157 * host environment. For instance: the class spawns a thread in its
12158 * constructor, the drawing code relies on device-specific features, etc.
12159 *
12160 * This method is usually checked in the drawing code of custom widgets.
12161 *
12162 * @return True if this View is in edit mode, false otherwise.
12163 */
12164 public boolean isInEditMode() {
12165 return false;
12166 }
12167
12168 /**
12169 * If the View draws content inside its padding and enables fading edges,
12170 * it needs to support padding offsets. Padding offsets are added to the
12171 * fading edges to extend the length of the fade so that it covers pixels
12172 * drawn inside the padding.
12173 *
12174 * Subclasses of this class should override this method if they need
12175 * to draw content inside the padding.
12176 *
12177 * @return True if padding offset must be applied, false otherwise.
12178 *
12179 * @see #getLeftPaddingOffset()
12180 * @see #getRightPaddingOffset()
12181 * @see #getTopPaddingOffset()
12182 * @see #getBottomPaddingOffset()
12183 *
12184 * @since CURRENT
12185 */
12186 protected boolean isPaddingOffsetRequired() {
12187 return false;
12188 }
12189
12190 /**
12191 * Amount by which to extend the left fading region. Called only when
12192 * {@link #isPaddingOffsetRequired()} returns true.
12193 *
12194 * @return The left padding offset in pixels.
12195 *
12196 * @see #isPaddingOffsetRequired()
12197 *
12198 * @since CURRENT
12199 */
12200 protected int getLeftPaddingOffset() {
12201 return 0;
12202 }
12203
12204 /**
12205 * Amount by which to extend the right fading region. Called only when
12206 * {@link #isPaddingOffsetRequired()} returns true.
12207 *
12208 * @return The right padding offset in pixels.
12209 *
12210 * @see #isPaddingOffsetRequired()
12211 *
12212 * @since CURRENT
12213 */
12214 protected int getRightPaddingOffset() {
12215 return 0;
12216 }
12217
12218 /**
12219 * Amount by which to extend the top fading region. Called only when
12220 * {@link #isPaddingOffsetRequired()} returns true.
12221 *
12222 * @return The top padding offset in pixels.
12223 *
12224 * @see #isPaddingOffsetRequired()
12225 *
12226 * @since CURRENT
12227 */
12228 protected int getTopPaddingOffset() {
12229 return 0;
12230 }
12231
12232 /**
12233 * Amount by which to extend the bottom fading region. Called only when
12234 * {@link #isPaddingOffsetRequired()} returns true.
12235 *
12236 * @return The bottom padding offset in pixels.
12237 *
12238 * @see #isPaddingOffsetRequired()
12239 *
12240 * @since CURRENT
12241 */
12242 protected int getBottomPaddingOffset() {
12243 return 0;
12244 }
12245
12246 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012247 * @hide
12248 * @param offsetRequired
12249 */
12250 protected int getFadeTop(boolean offsetRequired) {
12251 int top = mPaddingTop;
12252 if (offsetRequired) top += getTopPaddingOffset();
12253 return top;
12254 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012255
Romain Guyf2fc4602011-07-19 15:20:03 -070012256 /**
12257 * @hide
12258 * @param offsetRequired
12259 */
12260 protected int getFadeHeight(boolean offsetRequired) {
12261 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012262 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012263 return mBottom - mTop - mPaddingBottom - padding;
12264 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012265
Romain Guyf2fc4602011-07-19 15:20:03 -070012266 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012267 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012268 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012269 *
Romain Guy2bffd262010-09-12 17:40:02 -070012270 * <p>Even if this method returns true, it does not mean that every call
12271 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12272 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012273 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012274 * window is hardware accelerated,
12275 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12276 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012277 *
Romain Guy2bffd262010-09-12 17:40:02 -070012278 * @return True if the view is attached to a window and the window is
12279 * hardware accelerated; false in any other case.
12280 */
12281 public boolean isHardwareAccelerated() {
12282 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12283 }
Joe Malin32736f02011-01-19 16:14:20 -080012284
Romain Guy2bffd262010-09-12 17:40:02 -070012285 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012286 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12287 * case of an active Animation being run on the view.
12288 */
12289 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12290 Animation a, boolean scalingRequired) {
12291 Transformation invalidationTransform;
12292 final int flags = parent.mGroupFlags;
12293 final boolean initialized = a.isInitialized();
12294 if (!initialized) {
12295 a.initialize(mRight - mLeft, mBottom - mTop, getWidth(), getHeight());
12296 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
12297 onAnimationStart();
12298 }
12299
12300 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12301 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12302 if (parent.mInvalidationTransformation == null) {
12303 parent.mInvalidationTransformation = new Transformation();
12304 }
12305 invalidationTransform = parent.mInvalidationTransformation;
12306 a.getTransformation(drawingTime, invalidationTransform, 1f);
12307 } else {
12308 invalidationTransform = parent.mChildTransformation;
12309 }
12310 if (more) {
12311 if (!a.willChangeBounds()) {
12312 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12313 parent.FLAG_OPTIMIZE_INVALIDATE) {
12314 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12315 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12316 // The child need to draw an animation, potentially offscreen, so
12317 // make sure we do not cancel invalidate requests
12318 parent.mPrivateFlags |= DRAW_ANIMATION;
12319 parent.invalidate(mLeft, mTop, mRight, mBottom);
12320 }
12321 } else {
12322 if (parent.mInvalidateRegion == null) {
12323 parent.mInvalidateRegion = new RectF();
12324 }
12325 final RectF region = parent.mInvalidateRegion;
12326 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12327 invalidationTransform);
12328
12329 // The child need to draw an animation, potentially offscreen, so
12330 // make sure we do not cancel invalidate requests
12331 parent.mPrivateFlags |= DRAW_ANIMATION;
12332
12333 final int left = mLeft + (int) region.left;
12334 final int top = mTop + (int) region.top;
12335 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12336 top + (int) (region.height() + .5f));
12337 }
12338 }
12339 return more;
12340 }
12341
Chet Haasea1cff502012-02-21 13:43:44 -080012342 void setDisplayListProperties() {
12343 setDisplayListProperties(mDisplayList);
12344 }
12345
12346 /**
12347 * This method is called by getDisplayList() when a display list is created or re-rendered.
12348 * It sets or resets the current value of all properties on that display list (resetting is
12349 * necessary when a display list is being re-created, because we need to make sure that
12350 * previously-set transform values
12351 */
12352 void setDisplayListProperties(DisplayList displayList) {
12353 if (USE_DISPLAY_LIST_PROPERTIES && displayList != null) {
12354 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012355 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012356 if (mParent instanceof ViewGroup) {
12357 displayList.setClipChildren(
12358 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12359 }
Chet Haase9420abd2012-03-29 16:28:32 -070012360 float alpha = 1;
12361 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12362 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12363 ViewGroup parentVG = (ViewGroup) mParent;
12364 final boolean hasTransform =
12365 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12366 if (hasTransform) {
12367 Transformation transform = parentVG.mChildTransformation;
12368 final int transformType = parentVG.mChildTransformation.getTransformationType();
12369 if (transformType != Transformation.TYPE_IDENTITY) {
12370 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12371 alpha = transform.getAlpha();
12372 }
12373 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12374 displayList.setStaticMatrix(transform.getMatrix());
12375 }
12376 }
12377 }
Chet Haasea1cff502012-02-21 13:43:44 -080012378 }
12379 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012380 alpha *= mTransformationInfo.mAlpha;
12381 if (alpha < 1) {
12382 final int multipliedAlpha = (int) (255 * alpha);
12383 if (onSetAlpha(multipliedAlpha)) {
12384 alpha = 1;
12385 }
12386 }
12387 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012388 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12389 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12390 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12391 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012392 if (mTransformationInfo.mCamera == null) {
12393 mTransformationInfo.mCamera = new Camera();
12394 mTransformationInfo.matrix3D = new Matrix();
12395 }
12396 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012397 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12398 displayList.setPivotX(getPivotX());
12399 displayList.setPivotY(getPivotY());
12400 }
Chet Haase9420abd2012-03-29 16:28:32 -070012401 } else if (alpha < 1) {
12402 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012403 }
12404 }
12405 }
12406
Chet Haasebcca79a2012-02-14 08:45:14 -080012407 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012408 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12409 * This draw() method is an implementation detail and is not intended to be overridden or
12410 * to be called from anywhere else other than ViewGroup.drawChild().
12411 */
12412 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haasea1cff502012-02-21 13:43:44 -080012413 boolean useDisplayListProperties = USE_DISPLAY_LIST_PROPERTIES && mAttachInfo != null &&
12414 mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012415 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012416 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012417 final int flags = parent.mGroupFlags;
12418
Chet Haasea1cff502012-02-21 13:43:44 -080012419 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012420 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012421 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012422 }
12423
12424 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012425 boolean concatMatrix = false;
12426
12427 boolean scalingRequired = false;
12428 boolean caching;
12429 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
12430
12431 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012432 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12433 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012434 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012435 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012436 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12437 } else {
12438 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12439 }
12440
Chet Haasebcca79a2012-02-14 08:45:14 -080012441 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080012442 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012443 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080012444 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080012445 transformToApply = parent.mChildTransformation;
Chet Haase9420abd2012-03-29 16:28:32 -070012446 } else if (!useDisplayListProperties &&
12447 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012448 final boolean hasTransform =
12449 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080012450 if (hasTransform) {
12451 final int transformType = parent.mChildTransformation.getTransformationType();
12452 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
12453 parent.mChildTransformation : null;
12454 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
12455 }
12456 }
12457
12458 concatMatrix |= !childHasIdentityMatrix;
12459
12460 // Sets the flag as early as possible to allow draw() implementations
12461 // to call invalidate() successfully when doing animations
12462 mPrivateFlags |= DRAWN;
12463
Chet Haasebcca79a2012-02-14 08:45:14 -080012464 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080012465 (mPrivateFlags & DRAW_ANIMATION) == 0) {
12466 return more;
12467 }
12468
12469 if (hardwareAccelerated) {
12470 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
12471 // retain the flag's value temporarily in the mRecreateDisplayList flag
12472 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
12473 mPrivateFlags &= ~INVALIDATED;
12474 }
12475
12476 computeScroll();
12477
12478 final int sx = mScrollX;
12479 final int sy = mScrollY;
12480
12481 DisplayList displayList = null;
12482 Bitmap cache = null;
12483 boolean hasDisplayList = false;
12484 if (caching) {
12485 if (!hardwareAccelerated) {
12486 if (layerType != LAYER_TYPE_NONE) {
12487 layerType = LAYER_TYPE_SOFTWARE;
12488 buildDrawingCache(true);
12489 }
12490 cache = getDrawingCache(true);
12491 } else {
12492 switch (layerType) {
12493 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070012494 if (useDisplayListProperties) {
12495 hasDisplayList = canHaveDisplayList();
12496 } else {
12497 buildDrawingCache(true);
12498 cache = getDrawingCache(true);
12499 }
Chet Haase64a48c12012-02-13 16:33:29 -080012500 break;
Chet Haasea1cff502012-02-21 13:43:44 -080012501 case LAYER_TYPE_HARDWARE:
12502 if (useDisplayListProperties) {
12503 hasDisplayList = canHaveDisplayList();
12504 }
12505 break;
Chet Haase64a48c12012-02-13 16:33:29 -080012506 case LAYER_TYPE_NONE:
12507 // Delay getting the display list until animation-driven alpha values are
12508 // set up and possibly passed on to the view
12509 hasDisplayList = canHaveDisplayList();
12510 break;
12511 }
12512 }
12513 }
Chet Haasea1cff502012-02-21 13:43:44 -080012514 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070012515 if (useDisplayListProperties) {
12516 displayList = getDisplayList();
12517 if (!displayList.isValid()) {
12518 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12519 // to getDisplayList(), the display list will be marked invalid and we should not
12520 // try to use it again.
12521 displayList = null;
12522 hasDisplayList = false;
12523 useDisplayListProperties = false;
12524 }
12525 }
Chet Haase64a48c12012-02-13 16:33:29 -080012526
12527 final boolean hasNoCache = cache == null || hasDisplayList;
12528 final boolean offsetForScroll = cache == null && !hasDisplayList &&
12529 layerType != LAYER_TYPE_HARDWARE;
12530
Chet Haasea1cff502012-02-21 13:43:44 -080012531 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070012532 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012533 restoreTo = canvas.save();
12534 }
Chet Haase64a48c12012-02-13 16:33:29 -080012535 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012536 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080012537 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080012538 if (!useDisplayListProperties) {
12539 canvas.translate(mLeft, mTop);
12540 }
Chet Haase64a48c12012-02-13 16:33:29 -080012541 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080012542 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070012543 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080012544 restoreTo = canvas.save();
12545 }
Chet Haase64a48c12012-02-13 16:33:29 -080012546 // mAttachInfo cannot be null, otherwise scalingRequired == false
12547 final float scale = 1.0f / mAttachInfo.mApplicationScale;
12548 canvas.scale(scale, scale);
12549 }
12550 }
12551
Chet Haasea1cff502012-02-21 13:43:44 -080012552 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012553 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080012554 if (transformToApply != null || !childHasIdentityMatrix) {
12555 int transX = 0;
12556 int transY = 0;
12557
12558 if (offsetForScroll) {
12559 transX = -sx;
12560 transY = -sy;
12561 }
12562
12563 if (transformToApply != null) {
12564 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070012565 if (useDisplayListProperties) {
12566 displayList.setAnimationMatrix(transformToApply.getMatrix());
12567 } else {
12568 // Undo the scroll translation, apply the transformation matrix,
12569 // then redo the scroll translate to get the correct result.
12570 canvas.translate(-transX, -transY);
12571 canvas.concat(transformToApply.getMatrix());
12572 canvas.translate(transX, transY);
12573 }
Chet Haasea1cff502012-02-21 13:43:44 -080012574 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012575 }
12576
12577 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012578 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012579 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080012580 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012581 }
12582 }
12583
Chet Haasea1cff502012-02-21 13:43:44 -080012584 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012585 canvas.translate(-transX, -transY);
12586 canvas.concat(getMatrix());
12587 canvas.translate(transX, transY);
12588 }
12589 }
12590
Chet Haase9420abd2012-03-29 16:28:32 -070012591 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080012592 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012593 if (hasNoCache) {
12594 final int multipliedAlpha = (int) (255 * alpha);
12595 if (!onSetAlpha(multipliedAlpha)) {
12596 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080012597 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080012598 layerType != LAYER_TYPE_NONE) {
12599 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
12600 }
Chet Haase9420abd2012-03-29 16:28:32 -070012601 if (useDisplayListProperties) {
12602 displayList.setAlpha(alpha * getAlpha());
12603 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070012604 final int scrollX = hasDisplayList ? 0 : sx;
12605 final int scrollY = hasDisplayList ? 0 : sy;
12606 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
12607 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080012608 }
12609 } else {
12610 // Alpha is handled by the child directly, clobber the layer's alpha
12611 mPrivateFlags |= ALPHA_SET;
12612 }
12613 }
12614 }
12615 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12616 onSetAlpha(255);
12617 mPrivateFlags &= ~ALPHA_SET;
12618 }
12619
Chet Haasea1cff502012-02-21 13:43:44 -080012620 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
12621 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012622 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012623 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080012624 } else {
12625 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012626 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080012627 } else {
12628 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
12629 }
12630 }
12631 }
12632
Chet Haase9420abd2012-03-29 16:28:32 -070012633 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080012634 displayList = getDisplayList();
12635 if (!displayList.isValid()) {
12636 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12637 // to getDisplayList(), the display list will be marked invalid and we should not
12638 // try to use it again.
12639 displayList = null;
12640 hasDisplayList = false;
12641 }
12642 }
12643
12644 if (hasNoCache) {
12645 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080012646 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012647 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080012648 if (layer != null && layer.isValid()) {
12649 mLayerPaint.setAlpha((int) (alpha * 255));
12650 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
12651 layerRendered = true;
12652 } else {
12653 final int scrollX = hasDisplayList ? 0 : sx;
12654 final int scrollY = hasDisplayList ? 0 : sy;
12655 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080012656 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080012657 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12658 }
12659 }
12660
12661 if (!layerRendered) {
12662 if (!hasDisplayList) {
12663 // Fast path for layouts with no backgrounds
12664 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12665 if (ViewDebug.TRACE_HIERARCHY) {
12666 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
12667 }
12668 mPrivateFlags &= ~DIRTY_MASK;
12669 dispatchDraw(canvas);
12670 } else {
12671 draw(canvas);
12672 }
12673 } else {
12674 mPrivateFlags &= ~DIRTY_MASK;
Chet Haasebcca79a2012-02-14 08:45:14 -080012675 ((HardwareCanvas) canvas).drawDisplayList(displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -080012676 mRight - mLeft, mBottom - mTop, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080012677 }
12678 }
12679 } else if (cache != null) {
12680 mPrivateFlags &= ~DIRTY_MASK;
12681 Paint cachePaint;
12682
12683 if (layerType == LAYER_TYPE_NONE) {
12684 cachePaint = parent.mCachePaint;
12685 if (cachePaint == null) {
12686 cachePaint = new Paint();
12687 cachePaint.setDither(false);
12688 parent.mCachePaint = cachePaint;
12689 }
Chet Haase9420abd2012-03-29 16:28:32 -070012690 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012691 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080012692 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
12693 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012694 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080012695 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080012696 }
12697 } else {
12698 cachePaint = mLayerPaint;
12699 cachePaint.setAlpha((int) (alpha * 255));
12700 }
12701 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
12702 }
12703
Chet Haasea1cff502012-02-21 13:43:44 -080012704 if (restoreTo >= 0) {
12705 canvas.restoreToCount(restoreTo);
12706 }
Chet Haase64a48c12012-02-13 16:33:29 -080012707
12708 if (a != null && !more) {
12709 if (!hardwareAccelerated && !a.getFillAfter()) {
12710 onSetAlpha(255);
12711 }
12712 parent.finishAnimatingView(this, a);
12713 }
12714
12715 if (more && hardwareAccelerated) {
12716 // invalidation is the trigger to recreate display lists, so if we're using
12717 // display lists to render, force an invalidate to allow the animation to
12718 // continue drawing another frame
12719 parent.invalidate(true);
12720 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12721 // alpha animations should cause the child to recreate its display list
12722 invalidate(true);
12723 }
12724 }
12725
12726 mRecreateDisplayList = false;
12727
12728 return more;
12729 }
12730
12731 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012732 * Manually render this view (and all of its children) to the given Canvas.
12733 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070012734 * called. When implementing a view, implement
12735 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
12736 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012737 *
12738 * @param canvas The Canvas to which the View is rendered.
12739 */
12740 public void draw(Canvas canvas) {
12741 if (ViewDebug.TRACE_HIERARCHY) {
12742 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12743 }
12744
Romain Guy5bcdff42009-05-14 21:27:18 -070012745 final int privateFlags = mPrivateFlags;
12746 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
12747 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
12748 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070012749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012750 /*
12751 * Draw traversal performs several drawing steps which must be executed
12752 * in the appropriate order:
12753 *
12754 * 1. Draw the background
12755 * 2. If necessary, save the canvas' layers to prepare for fading
12756 * 3. Draw view's content
12757 * 4. Draw children
12758 * 5. If necessary, draw the fading edges and restore layers
12759 * 6. Draw decorations (scrollbars for instance)
12760 */
12761
12762 // Step 1, draw the background, if needed
12763 int saveCount;
12764
Romain Guy24443ea2009-05-11 11:56:30 -070012765 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012766 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070012767 if (background != null) {
12768 final int scrollX = mScrollX;
12769 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012770
Romain Guy24443ea2009-05-11 11:56:30 -070012771 if (mBackgroundSizeChanged) {
12772 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
12773 mBackgroundSizeChanged = false;
12774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012775
Romain Guy24443ea2009-05-11 11:56:30 -070012776 if ((scrollX | scrollY) == 0) {
12777 background.draw(canvas);
12778 } else {
12779 canvas.translate(scrollX, scrollY);
12780 background.draw(canvas);
12781 canvas.translate(-scrollX, -scrollY);
12782 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012783 }
12784 }
12785
12786 // skip step 2 & 5 if possible (common case)
12787 final int viewFlags = mViewFlags;
12788 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
12789 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
12790 if (!verticalEdges && !horizontalEdges) {
12791 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012792 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012793
12794 // Step 4, draw the children
12795 dispatchDraw(canvas);
12796
12797 // Step 6, draw decorations (scrollbars)
12798 onDrawScrollBars(canvas);
12799
12800 // we're done...
12801 return;
12802 }
12803
12804 /*
12805 * Here we do the full fledged routine...
12806 * (this is an uncommon case where speed matters less,
12807 * this is why we repeat some of the tests that have been
12808 * done above)
12809 */
12810
12811 boolean drawTop = false;
12812 boolean drawBottom = false;
12813 boolean drawLeft = false;
12814 boolean drawRight = false;
12815
12816 float topFadeStrength = 0.0f;
12817 float bottomFadeStrength = 0.0f;
12818 float leftFadeStrength = 0.0f;
12819 float rightFadeStrength = 0.0f;
12820
12821 // Step 2, save the canvas' layers
12822 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012823
12824 final boolean offsetRequired = isPaddingOffsetRequired();
12825 if (offsetRequired) {
12826 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012827 }
12828
12829 int left = mScrollX + paddingLeft;
12830 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070012831 int top = mScrollY + getFadeTop(offsetRequired);
12832 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012833
12834 if (offsetRequired) {
12835 right += getRightPaddingOffset();
12836 bottom += getBottomPaddingOffset();
12837 }
12838
12839 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070012840 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012841 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012842
12843 // clip the fade length if top and bottom fades overlap
12844 // overlapping fades produce odd-looking artifacts
12845 if (verticalEdges && (top + length > bottom - length)) {
12846 length = (bottom - top) / 2;
12847 }
12848
12849 // also clip horizontal fades if necessary
12850 if (horizontalEdges && (left + length > right - length)) {
12851 length = (right - left) / 2;
12852 }
12853
12854 if (verticalEdges) {
12855 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012856 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012857 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012858 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012859 }
12860
12861 if (horizontalEdges) {
12862 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012863 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012864 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012865 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012866 }
12867
12868 saveCount = canvas.getSaveCount();
12869
12870 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070012871 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012872 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
12873
12874 if (drawTop) {
12875 canvas.saveLayer(left, top, right, top + length, null, flags);
12876 }
12877
12878 if (drawBottom) {
12879 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
12880 }
12881
12882 if (drawLeft) {
12883 canvas.saveLayer(left, top, left + length, bottom, null, flags);
12884 }
12885
12886 if (drawRight) {
12887 canvas.saveLayer(right - length, top, right, bottom, null, flags);
12888 }
12889 } else {
12890 scrollabilityCache.setFadeColor(solidColor);
12891 }
12892
12893 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012894 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012895
12896 // Step 4, draw the children
12897 dispatchDraw(canvas);
12898
12899 // Step 5, draw the fade effect and restore layers
12900 final Paint p = scrollabilityCache.paint;
12901 final Matrix matrix = scrollabilityCache.matrix;
12902 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012903
12904 if (drawTop) {
12905 matrix.setScale(1, fadeHeight * topFadeStrength);
12906 matrix.postTranslate(left, top);
12907 fade.setLocalMatrix(matrix);
12908 canvas.drawRect(left, top, right, top + length, p);
12909 }
12910
12911 if (drawBottom) {
12912 matrix.setScale(1, fadeHeight * bottomFadeStrength);
12913 matrix.postRotate(180);
12914 matrix.postTranslate(left, bottom);
12915 fade.setLocalMatrix(matrix);
12916 canvas.drawRect(left, bottom - length, right, bottom, p);
12917 }
12918
12919 if (drawLeft) {
12920 matrix.setScale(1, fadeHeight * leftFadeStrength);
12921 matrix.postRotate(-90);
12922 matrix.postTranslate(left, top);
12923 fade.setLocalMatrix(matrix);
12924 canvas.drawRect(left, top, left + length, bottom, p);
12925 }
12926
12927 if (drawRight) {
12928 matrix.setScale(1, fadeHeight * rightFadeStrength);
12929 matrix.postRotate(90);
12930 matrix.postTranslate(right, top);
12931 fade.setLocalMatrix(matrix);
12932 canvas.drawRect(right - length, top, right, bottom, p);
12933 }
12934
12935 canvas.restoreToCount(saveCount);
12936
12937 // Step 6, draw decorations (scrollbars)
12938 onDrawScrollBars(canvas);
12939 }
12940
12941 /**
12942 * Override this if your view is known to always be drawn on top of a solid color background,
12943 * and needs to draw fading edges. Returning a non-zero color enables the view system to
12944 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
12945 * should be set to 0xFF.
12946 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012947 * @see #setVerticalFadingEdgeEnabled(boolean)
12948 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012949 *
12950 * @return The known solid color background for this view, or 0 if the color may vary
12951 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012952 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012953 public int getSolidColor() {
12954 return 0;
12955 }
12956
12957 /**
12958 * Build a human readable string representation of the specified view flags.
12959 *
12960 * @param flags the view flags to convert to a string
12961 * @return a String representing the supplied flags
12962 */
12963 private static String printFlags(int flags) {
12964 String output = "";
12965 int numFlags = 0;
12966 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
12967 output += "TAKES_FOCUS";
12968 numFlags++;
12969 }
12970
12971 switch (flags & VISIBILITY_MASK) {
12972 case INVISIBLE:
12973 if (numFlags > 0) {
12974 output += " ";
12975 }
12976 output += "INVISIBLE";
12977 // USELESS HERE numFlags++;
12978 break;
12979 case GONE:
12980 if (numFlags > 0) {
12981 output += " ";
12982 }
12983 output += "GONE";
12984 // USELESS HERE numFlags++;
12985 break;
12986 default:
12987 break;
12988 }
12989 return output;
12990 }
12991
12992 /**
12993 * Build a human readable string representation of the specified private
12994 * view flags.
12995 *
12996 * @param privateFlags the private view flags to convert to a string
12997 * @return a String representing the supplied flags
12998 */
12999 private static String printPrivateFlags(int privateFlags) {
13000 String output = "";
13001 int numFlags = 0;
13002
13003 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13004 output += "WANTS_FOCUS";
13005 numFlags++;
13006 }
13007
13008 if ((privateFlags & FOCUSED) == FOCUSED) {
13009 if (numFlags > 0) {
13010 output += " ";
13011 }
13012 output += "FOCUSED";
13013 numFlags++;
13014 }
13015
13016 if ((privateFlags & SELECTED) == SELECTED) {
13017 if (numFlags > 0) {
13018 output += " ";
13019 }
13020 output += "SELECTED";
13021 numFlags++;
13022 }
13023
13024 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13025 if (numFlags > 0) {
13026 output += " ";
13027 }
13028 output += "IS_ROOT_NAMESPACE";
13029 numFlags++;
13030 }
13031
13032 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13033 if (numFlags > 0) {
13034 output += " ";
13035 }
13036 output += "HAS_BOUNDS";
13037 numFlags++;
13038 }
13039
13040 if ((privateFlags & DRAWN) == DRAWN) {
13041 if (numFlags > 0) {
13042 output += " ";
13043 }
13044 output += "DRAWN";
13045 // USELESS HERE numFlags++;
13046 }
13047 return output;
13048 }
13049
13050 /**
13051 * <p>Indicates whether or not this view's layout will be requested during
13052 * the next hierarchy layout pass.</p>
13053 *
13054 * @return true if the layout will be forced during next layout pass
13055 */
13056 public boolean isLayoutRequested() {
13057 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13058 }
13059
13060 /**
13061 * Assign a size and position to a view and all of its
13062 * descendants
13063 *
13064 * <p>This is the second phase of the layout mechanism.
13065 * (The first is measuring). In this phase, each parent calls
13066 * layout on all of its children to position them.
13067 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013068 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013069 *
Chet Haase9c087442011-01-12 16:20:16 -080013070 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013071 * Derived classes with children should override
13072 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013073 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013074 *
13075 * @param l Left position, relative to parent
13076 * @param t Top position, relative to parent
13077 * @param r Right position, relative to parent
13078 * @param b Bottom position, relative to parent
13079 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013080 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013081 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013082 int oldL = mLeft;
13083 int oldT = mTop;
13084 int oldB = mBottom;
13085 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013086 boolean changed = setFrame(l, t, r, b);
13087 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
13088 if (ViewDebug.TRACE_HIERARCHY) {
13089 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
13090 }
13091
13092 onLayout(changed, l, t, r, b);
13093 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013094
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013095 ListenerInfo li = mListenerInfo;
13096 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013097 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013098 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013099 int numListeners = listenersCopy.size();
13100 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013101 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013102 }
13103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013104 }
13105 mPrivateFlags &= ~FORCE_LAYOUT;
13106 }
13107
13108 /**
13109 * Called from layout when this view should
13110 * assign a size and position to each of its children.
13111 *
13112 * Derived classes with children should override
13113 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013114 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013115 * @param changed This is a new size or position for this view
13116 * @param left Left position, relative to parent
13117 * @param top Top position, relative to parent
13118 * @param right Right position, relative to parent
13119 * @param bottom Bottom position, relative to parent
13120 */
13121 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13122 }
13123
13124 /**
13125 * Assign a size and position to this view.
13126 *
13127 * This is called from layout.
13128 *
13129 * @param left Left position, relative to parent
13130 * @param top Top position, relative to parent
13131 * @param right Right position, relative to parent
13132 * @param bottom Bottom position, relative to parent
13133 * @return true if the new size and position are different than the
13134 * previous ones
13135 * {@hide}
13136 */
13137 protected boolean setFrame(int left, int top, int right, int bottom) {
13138 boolean changed = false;
13139
13140 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013141 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013142 + right + "," + bottom + ")");
13143 }
13144
13145 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13146 changed = true;
13147
13148 // Remember our drawn bit
13149 int drawn = mPrivateFlags & DRAWN;
13150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013151 int oldWidth = mRight - mLeft;
13152 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013153 int newWidth = right - left;
13154 int newHeight = bottom - top;
13155 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13156
13157 // Invalidate our old position
13158 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013159
13160 mLeft = left;
13161 mTop = top;
13162 mRight = right;
13163 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -080013164 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
13165 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13166 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013167
13168 mPrivateFlags |= HAS_BOUNDS;
13169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013170
Chet Haase75755e22011-07-18 17:48:25 -070013171 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013172 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13173 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013174 if (mTransformationInfo != null) {
13175 mTransformationInfo.mMatrixDirty = true;
13176 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013178 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13179 }
13180
13181 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13182 // If we are visible, force the DRAWN bit to on so that
13183 // this invalidate will go through (at least to our parent).
13184 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013185 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013186 // the DRAWN bit.
13187 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013188 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013189 // parent display list may need to be recreated based on a change in the bounds
13190 // of any child
13191 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013192 }
13193
13194 // Reset drawn bit to original value (invalidate turns it off)
13195 mPrivateFlags |= drawn;
13196
13197 mBackgroundSizeChanged = true;
13198 }
13199 return changed;
13200 }
13201
13202 /**
13203 * Finalize inflating a view from XML. This is called as the last phase
13204 * of inflation, after all child views have been added.
13205 *
13206 * <p>Even if the subclass overrides onFinishInflate, they should always be
13207 * sure to call the super method, so that we get called.
13208 */
13209 protected void onFinishInflate() {
13210 }
13211
13212 /**
13213 * Returns the resources associated with this view.
13214 *
13215 * @return Resources object.
13216 */
13217 public Resources getResources() {
13218 return mResources;
13219 }
13220
13221 /**
13222 * Invalidates the specified Drawable.
13223 *
13224 * @param drawable the drawable to invalidate
13225 */
13226 public void invalidateDrawable(Drawable drawable) {
13227 if (verifyDrawable(drawable)) {
13228 final Rect dirty = drawable.getBounds();
13229 final int scrollX = mScrollX;
13230 final int scrollY = mScrollY;
13231
13232 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13233 dirty.right + scrollX, dirty.bottom + scrollY);
13234 }
13235 }
13236
13237 /**
13238 * Schedules an action on a drawable to occur at a specified time.
13239 *
13240 * @param who the recipient of the action
13241 * @param what the action to run on the drawable
13242 * @param when the time at which the action must occur. Uses the
13243 * {@link SystemClock#uptimeMillis} timebase.
13244 */
13245 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013246 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013247 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013248 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013249 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13250 Choreographer.CALLBACK_ANIMATION, what, who,
13251 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013252 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013253 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013255 }
13256 }
13257
13258 /**
13259 * Cancels a scheduled action on a drawable.
13260 *
13261 * @param who the recipient of the action
13262 * @param what the action to cancel
13263 */
13264 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013265 if (verifyDrawable(who) && what != null) {
13266 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013267 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13268 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013269 } else {
13270 ViewRootImpl.getRunQueue().removeCallbacks(what);
13271 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013272 }
13273 }
13274
13275 /**
13276 * Unschedule any events associated with the given Drawable. This can be
13277 * used when selecting a new Drawable into a view, so that the previous
13278 * one is completely unscheduled.
13279 *
13280 * @param who The Drawable to unschedule.
13281 *
13282 * @see #drawableStateChanged
13283 */
13284 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013285 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013286 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13287 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013288 }
13289 }
13290
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013291 /**
13292 * Return the layout direction of a given Drawable.
13293 *
13294 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013295 */
13296 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013297 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013298 }
13299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013300 /**
13301 * If your view subclass is displaying its own Drawable objects, it should
13302 * override this function and return true for any Drawable it is
13303 * displaying. This allows animations for those drawables to be
13304 * scheduled.
13305 *
13306 * <p>Be sure to call through to the super class when overriding this
13307 * function.
13308 *
13309 * @param who The Drawable to verify. Return true if it is one you are
13310 * displaying, else return the result of calling through to the
13311 * super class.
13312 *
13313 * @return boolean If true than the Drawable is being displayed in the
13314 * view; else false and it is not allowed to animate.
13315 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013316 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13317 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013318 */
13319 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013320 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013321 }
13322
13323 /**
13324 * This function is called whenever the state of the view changes in such
13325 * a way that it impacts the state of drawables being shown.
13326 *
13327 * <p>Be sure to call through to the superclass when overriding this
13328 * function.
13329 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013330 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013331 */
13332 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013333 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013334 if (d != null && d.isStateful()) {
13335 d.setState(getDrawableState());
13336 }
13337 }
13338
13339 /**
13340 * Call this to force a view to update its drawable state. This will cause
13341 * drawableStateChanged to be called on this view. Views that are interested
13342 * in the new state should call getDrawableState.
13343 *
13344 * @see #drawableStateChanged
13345 * @see #getDrawableState
13346 */
13347 public void refreshDrawableState() {
13348 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13349 drawableStateChanged();
13350
13351 ViewParent parent = mParent;
13352 if (parent != null) {
13353 parent.childDrawableStateChanged(this);
13354 }
13355 }
13356
13357 /**
13358 * Return an array of resource IDs of the drawable states representing the
13359 * current state of the view.
13360 *
13361 * @return The current drawable state
13362 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013363 * @see Drawable#setState(int[])
13364 * @see #drawableStateChanged()
13365 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013366 */
13367 public final int[] getDrawableState() {
13368 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13369 return mDrawableState;
13370 } else {
13371 mDrawableState = onCreateDrawableState(0);
13372 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13373 return mDrawableState;
13374 }
13375 }
13376
13377 /**
13378 * Generate the new {@link android.graphics.drawable.Drawable} state for
13379 * this view. This is called by the view
13380 * system when the cached Drawable state is determined to be invalid. To
13381 * retrieve the current state, you should use {@link #getDrawableState}.
13382 *
13383 * @param extraSpace if non-zero, this is the number of extra entries you
13384 * would like in the returned array in which you can place your own
13385 * states.
13386 *
13387 * @return Returns an array holding the current {@link Drawable} state of
13388 * the view.
13389 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013390 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013391 */
13392 protected int[] onCreateDrawableState(int extraSpace) {
13393 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13394 mParent instanceof View) {
13395 return ((View) mParent).onCreateDrawableState(extraSpace);
13396 }
13397
13398 int[] drawableState;
13399
13400 int privateFlags = mPrivateFlags;
13401
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013402 int viewStateIndex = 0;
13403 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13404 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13405 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013406 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013407 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13408 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013409 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13410 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013411 // This is set if HW acceleration is requested, even if the current
13412 // process doesn't allow it. This is just to allow app preview
13413 // windows to better match their app.
13414 viewStateIndex |= VIEW_STATE_ACCELERATED;
13415 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013416 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013417
Christopher Tate3d4bf172011-03-28 16:16:46 -070013418 final int privateFlags2 = mPrivateFlags2;
13419 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13420 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013422 drawableState = VIEW_STATE_SETS[viewStateIndex];
13423
13424 //noinspection ConstantIfStatement
13425 if (false) {
13426 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13427 Log.i("View", toString()
13428 + " pressed=" + ((privateFlags & PRESSED) != 0)
13429 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13430 + " fo=" + hasFocus()
13431 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013432 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013433 + ": " + Arrays.toString(drawableState));
13434 }
13435
13436 if (extraSpace == 0) {
13437 return drawableState;
13438 }
13439
13440 final int[] fullState;
13441 if (drawableState != null) {
13442 fullState = new int[drawableState.length + extraSpace];
13443 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
13444 } else {
13445 fullState = new int[extraSpace];
13446 }
13447
13448 return fullState;
13449 }
13450
13451 /**
13452 * Merge your own state values in <var>additionalState</var> into the base
13453 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013454 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013455 *
13456 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013457 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013458 * own additional state values.
13459 *
13460 * @param additionalState The additional state values you would like
13461 * added to <var>baseState</var>; this array is not modified.
13462 *
13463 * @return As a convenience, the <var>baseState</var> array you originally
13464 * passed into the function is returned.
13465 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013466 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013467 */
13468 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
13469 final int N = baseState.length;
13470 int i = N - 1;
13471 while (i >= 0 && baseState[i] == 0) {
13472 i--;
13473 }
13474 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
13475 return baseState;
13476 }
13477
13478 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070013479 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
13480 * on all Drawable objects associated with this view.
13481 */
13482 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013483 if (mBackground != null) {
13484 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070013485 }
13486 }
13487
13488 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013489 * Sets the background color for this view.
13490 * @param color the color of the background
13491 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013492 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013493 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013494 if (mBackground instanceof ColorDrawable) {
13495 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070013496 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070013497 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070013498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013499 }
13500
13501 /**
13502 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070013503 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013504 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070013505 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013506 * @attr ref android.R.styleable#View_background
13507 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013508 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013509 public void setBackgroundResource(int resid) {
13510 if (resid != 0 && resid == mBackgroundResource) {
13511 return;
13512 }
13513
13514 Drawable d= null;
13515 if (resid != 0) {
13516 d = mResources.getDrawable(resid);
13517 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013518 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013519
13520 mBackgroundResource = resid;
13521 }
13522
13523 /**
13524 * Set the background to a given Drawable, or remove the background. If the
13525 * background has padding, this View's padding is set to the background's
13526 * padding. However, when a background is removed, this View's padding isn't
13527 * touched. If setting the padding is desired, please use
13528 * {@link #setPadding(int, int, int, int)}.
13529 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013530 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013531 * background
13532 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013533 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070013534 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070013535 setBackgroundDrawable(background);
13536 }
13537
13538 /**
13539 * @deprecated use {@link #setBackground(Drawable)} instead
13540 */
13541 @Deprecated
13542 public void setBackgroundDrawable(Drawable background) {
13543 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070013544 return;
13545 }
13546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013547 boolean requestLayout = false;
13548
13549 mBackgroundResource = 0;
13550
13551 /*
13552 * Regardless of whether we're setting a new background or not, we want
13553 * to clear the previous drawable.
13554 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013555 if (mBackground != null) {
13556 mBackground.setCallback(null);
13557 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013558 }
13559
Philip Milne6c8ea062012-04-03 17:38:43 -070013560 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013561 Rect padding = sThreadLocal.get();
13562 if (padding == null) {
13563 padding = new Rect();
13564 sThreadLocal.set(padding);
13565 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013566 if (background.getPadding(padding)) {
13567 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013568 case LAYOUT_DIRECTION_RTL:
13569 setPadding(padding.right, padding.top, padding.left, padding.bottom);
13570 break;
13571 case LAYOUT_DIRECTION_LTR:
13572 default:
13573 setPadding(padding.left, padding.top, padding.right, padding.bottom);
13574 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013575 }
13576
13577 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
13578 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070013579 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
13580 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013581 requestLayout = true;
13582 }
13583
Philip Milne6c8ea062012-04-03 17:38:43 -070013584 background.setCallback(this);
13585 if (background.isStateful()) {
13586 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013587 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013588 background.setVisible(getVisibility() == VISIBLE, false);
13589 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013590
13591 if ((mPrivateFlags & SKIP_DRAW) != 0) {
13592 mPrivateFlags &= ~SKIP_DRAW;
13593 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
13594 requestLayout = true;
13595 }
13596 } else {
13597 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070013598 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013599
13600 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
13601 /*
13602 * This view ONLY drew the background before and we're removing
13603 * the background, so now it won't draw anything
13604 * (hence we SKIP_DRAW)
13605 */
13606 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
13607 mPrivateFlags |= SKIP_DRAW;
13608 }
13609
13610 /*
13611 * When the background is set, we try to apply its padding to this
13612 * View. When the background is removed, we don't touch this View's
13613 * padding. This is noted in the Javadocs. Hence, we don't need to
13614 * requestLayout(), the invalidate() below is sufficient.
13615 */
13616
13617 // The old background's minimum size could have affected this
13618 // View's layout, so let's requestLayout
13619 requestLayout = true;
13620 }
13621
Romain Guy8f1344f52009-05-15 16:03:59 -070013622 computeOpaqueFlags();
13623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013624 if (requestLayout) {
13625 requestLayout();
13626 }
13627
13628 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013629 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013630 }
13631
13632 /**
13633 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070013634 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013635 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070013636 *
13637 * @see #setBackground(Drawable)
13638 *
13639 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013640 */
13641 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013642 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013643 }
13644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013645 /**
13646 * Sets the padding. The view may add on the space required to display
13647 * the scrollbars, depending on the style and visibility of the scrollbars.
13648 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
13649 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
13650 * from the values set in this call.
13651 *
13652 * @attr ref android.R.styleable#View_padding
13653 * @attr ref android.R.styleable#View_paddingBottom
13654 * @attr ref android.R.styleable#View_paddingLeft
13655 * @attr ref android.R.styleable#View_paddingRight
13656 * @attr ref android.R.styleable#View_paddingTop
13657 * @param left the left padding in pixels
13658 * @param top the top padding in pixels
13659 * @param right the right padding in pixels
13660 * @param bottom the bottom padding in pixels
13661 */
13662 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013663 mUserPaddingStart = -1;
13664 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013665 mUserPaddingRelative = false;
13666
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013667 internalSetPadding(left, top, right, bottom);
13668 }
13669
13670 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080013671 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013672 mUserPaddingRight = right;
13673 mUserPaddingBottom = bottom;
13674
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013675 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013676 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070013677
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013678 // Common case is there are no scroll bars.
13679 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013680 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080013681 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013682 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080013683 switch (mVerticalScrollbarPosition) {
13684 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013685 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
13686 left += offset;
13687 } else {
13688 right += offset;
13689 }
13690 break;
Adam Powell20232d02010-12-08 21:08:53 -080013691 case SCROLLBAR_POSITION_RIGHT:
13692 right += offset;
13693 break;
13694 case SCROLLBAR_POSITION_LEFT:
13695 left += offset;
13696 break;
13697 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013698 }
Adam Powell20232d02010-12-08 21:08:53 -080013699 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013700 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
13701 ? 0 : getHorizontalScrollbarHeight();
13702 }
13703 }
Romain Guy8506ab42009-06-11 17:35:47 -070013704
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013705 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013706 changed = true;
13707 mPaddingLeft = left;
13708 }
13709 if (mPaddingTop != top) {
13710 changed = true;
13711 mPaddingTop = top;
13712 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013713 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013714 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013715 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013716 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013717 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013718 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013719 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013720 }
13721
13722 if (changed) {
13723 requestLayout();
13724 }
13725 }
13726
13727 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013728 * Sets the relative padding. The view may add on the space required to display
13729 * the scrollbars, depending on the style and visibility of the scrollbars.
13730 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
13731 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
13732 * from the values set in this call.
13733 *
13734 * @attr ref android.R.styleable#View_padding
13735 * @attr ref android.R.styleable#View_paddingBottom
13736 * @attr ref android.R.styleable#View_paddingStart
13737 * @attr ref android.R.styleable#View_paddingEnd
13738 * @attr ref android.R.styleable#View_paddingTop
13739 * @param start the start padding in pixels
13740 * @param top the top padding in pixels
13741 * @param end the end padding in pixels
13742 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013743 */
13744 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013745 mUserPaddingStart = start;
13746 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013747 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013748
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013749 switch(getResolvedLayoutDirection()) {
13750 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013751 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013752 break;
13753 case LAYOUT_DIRECTION_LTR:
13754 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013755 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013756 }
13757 }
13758
13759 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013760 * Returns the top padding of this view.
13761 *
13762 * @return the top padding in pixels
13763 */
13764 public int getPaddingTop() {
13765 return mPaddingTop;
13766 }
13767
13768 /**
13769 * Returns the bottom padding of this view. If there are inset and enabled
13770 * scrollbars, this value may include the space required to display the
13771 * scrollbars as well.
13772 *
13773 * @return the bottom padding in pixels
13774 */
13775 public int getPaddingBottom() {
13776 return mPaddingBottom;
13777 }
13778
13779 /**
13780 * Returns the left padding of this view. If there are inset and enabled
13781 * scrollbars, this value may include the space required to display the
13782 * scrollbars as well.
13783 *
13784 * @return the left padding in pixels
13785 */
13786 public int getPaddingLeft() {
13787 return mPaddingLeft;
13788 }
13789
13790 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013791 * Returns the start padding of this view depending on its resolved layout direction.
13792 * If there are inset and enabled scrollbars, this value may include the space
13793 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013794 *
13795 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013796 */
13797 public int getPaddingStart() {
13798 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13799 mPaddingRight : mPaddingLeft;
13800 }
13801
13802 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013803 * Returns the right padding of this view. If there are inset and enabled
13804 * scrollbars, this value may include the space required to display the
13805 * scrollbars as well.
13806 *
13807 * @return the right padding in pixels
13808 */
13809 public int getPaddingRight() {
13810 return mPaddingRight;
13811 }
13812
13813 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013814 * Returns the end padding of this view depending on its resolved layout direction.
13815 * If there are inset and enabled scrollbars, this value may include the space
13816 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013817 *
13818 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013819 */
13820 public int getPaddingEnd() {
13821 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13822 mPaddingLeft : mPaddingRight;
13823 }
13824
13825 /**
13826 * Return if the padding as been set thru relative values
13827 * {@link #setPaddingRelative(int, int, int, int)} or thru
13828 * @attr ref android.R.styleable#View_paddingStart or
13829 * @attr ref android.R.styleable#View_paddingEnd
13830 *
13831 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013832 */
13833 public boolean isPaddingRelative() {
13834 return mUserPaddingRelative;
13835 }
13836
13837 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013838 * Changes the selection state of this view. A view can be selected or not.
13839 * Note that selection is not the same as focus. Views are typically
13840 * selected in the context of an AdapterView like ListView or GridView;
13841 * the selected view is the view that is highlighted.
13842 *
13843 * @param selected true if the view must be selected, false otherwise
13844 */
13845 public void setSelected(boolean selected) {
13846 if (((mPrivateFlags & SELECTED) != 0) != selected) {
13847 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070013848 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080013849 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013850 refreshDrawableState();
13851 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070013852 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
13853 notifyAccessibilityStateChanged();
13854 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013855 }
13856 }
13857
13858 /**
13859 * Dispatch setSelected to all of this View's children.
13860 *
13861 * @see #setSelected(boolean)
13862 *
13863 * @param selected The new selected state
13864 */
13865 protected void dispatchSetSelected(boolean selected) {
13866 }
13867
13868 /**
13869 * Indicates the selection state of this view.
13870 *
13871 * @return true if the view is selected, false otherwise
13872 */
13873 @ViewDebug.ExportedProperty
13874 public boolean isSelected() {
13875 return (mPrivateFlags & SELECTED) != 0;
13876 }
13877
13878 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013879 * Changes the activated state of this view. A view can be activated or not.
13880 * Note that activation is not the same as selection. Selection is
13881 * a transient property, representing the view (hierarchy) the user is
13882 * currently interacting with. Activation is a longer-term state that the
13883 * user can move views in and out of. For example, in a list view with
13884 * single or multiple selection enabled, the views in the current selection
13885 * set are activated. (Um, yeah, we are deeply sorry about the terminology
13886 * here.) The activated state is propagated down to children of the view it
13887 * is set on.
13888 *
13889 * @param activated true if the view must be activated, false otherwise
13890 */
13891 public void setActivated(boolean activated) {
13892 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
13893 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013894 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013895 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070013896 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013897 }
13898 }
13899
13900 /**
13901 * Dispatch setActivated to all of this View's children.
13902 *
13903 * @see #setActivated(boolean)
13904 *
13905 * @param activated The new activated state
13906 */
13907 protected void dispatchSetActivated(boolean activated) {
13908 }
13909
13910 /**
13911 * Indicates the activation state of this view.
13912 *
13913 * @return true if the view is activated, false otherwise
13914 */
13915 @ViewDebug.ExportedProperty
13916 public boolean isActivated() {
13917 return (mPrivateFlags & ACTIVATED) != 0;
13918 }
13919
13920 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013921 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
13922 * observer can be used to get notifications when global events, like
13923 * layout, happen.
13924 *
13925 * The returned ViewTreeObserver observer is not guaranteed to remain
13926 * valid for the lifetime of this View. If the caller of this method keeps
13927 * a long-lived reference to ViewTreeObserver, it should always check for
13928 * the return value of {@link ViewTreeObserver#isAlive()}.
13929 *
13930 * @return The ViewTreeObserver for this view's hierarchy.
13931 */
13932 public ViewTreeObserver getViewTreeObserver() {
13933 if (mAttachInfo != null) {
13934 return mAttachInfo.mTreeObserver;
13935 }
13936 if (mFloatingTreeObserver == null) {
13937 mFloatingTreeObserver = new ViewTreeObserver();
13938 }
13939 return mFloatingTreeObserver;
13940 }
13941
13942 /**
13943 * <p>Finds the topmost view in the current view hierarchy.</p>
13944 *
13945 * @return the topmost view containing this view
13946 */
13947 public View getRootView() {
13948 if (mAttachInfo != null) {
13949 final View v = mAttachInfo.mRootView;
13950 if (v != null) {
13951 return v;
13952 }
13953 }
Romain Guy8506ab42009-06-11 17:35:47 -070013954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013955 View parent = this;
13956
13957 while (parent.mParent != null && parent.mParent instanceof View) {
13958 parent = (View) parent.mParent;
13959 }
13960
13961 return parent;
13962 }
13963
13964 /**
13965 * <p>Computes the coordinates of this view on the screen. The argument
13966 * must be an array of two integers. After the method returns, the array
13967 * contains the x and y location in that order.</p>
13968 *
13969 * @param location an array of two integers in which to hold the coordinates
13970 */
13971 public void getLocationOnScreen(int[] location) {
13972 getLocationInWindow(location);
13973
13974 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070013975 if (info != null) {
13976 location[0] += info.mWindowLeft;
13977 location[1] += info.mWindowTop;
13978 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013979 }
13980
13981 /**
13982 * <p>Computes the coordinates of this view in its window. The argument
13983 * must be an array of two integers. After the method returns, the array
13984 * contains the x and y location in that order.</p>
13985 *
13986 * @param location an array of two integers in which to hold the coordinates
13987 */
13988 public void getLocationInWindow(int[] location) {
13989 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010013990 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013991 }
13992
Gilles Debunne6583ce52011-12-06 18:09:02 -080013993 if (mAttachInfo == null) {
13994 // When the view is not attached to a window, this method does not make sense
13995 location[0] = location[1] = 0;
13996 return;
13997 }
13998
Gilles Debunnecea45132011-11-24 02:19:27 +010013999 float[] position = mAttachInfo.mTmpTransformLocation;
14000 position[0] = position[1] = 0.0f;
14001
14002 if (!hasIdentityMatrix()) {
14003 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014005
Gilles Debunnecea45132011-11-24 02:19:27 +010014006 position[0] += mLeft;
14007 position[1] += mTop;
14008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014009 ViewParent viewParent = mParent;
14010 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014011 final View view = (View) viewParent;
14012
14013 position[0] -= view.mScrollX;
14014 position[1] -= view.mScrollY;
14015
14016 if (!view.hasIdentityMatrix()) {
14017 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014018 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014019
14020 position[0] += view.mLeft;
14021 position[1] += view.mTop;
14022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014023 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014024 }
Romain Guy8506ab42009-06-11 17:35:47 -070014025
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014026 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014027 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014028 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14029 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014030 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014031
14032 location[0] = (int) (position[0] + 0.5f);
14033 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014034 }
14035
14036 /**
14037 * {@hide}
14038 * @param id the id of the view to be found
14039 * @return the view of the specified id, null if cannot be found
14040 */
14041 protected View findViewTraversal(int id) {
14042 if (id == mID) {
14043 return this;
14044 }
14045 return null;
14046 }
14047
14048 /**
14049 * {@hide}
14050 * @param tag the tag of the view to be found
14051 * @return the view of specified tag, null if cannot be found
14052 */
14053 protected View findViewWithTagTraversal(Object tag) {
14054 if (tag != null && tag.equals(mTag)) {
14055 return this;
14056 }
14057 return null;
14058 }
14059
14060 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014061 * {@hide}
14062 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014063 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014064 * @return The first view that matches the predicate or null.
14065 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014066 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014067 if (predicate.apply(this)) {
14068 return this;
14069 }
14070 return null;
14071 }
14072
14073 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014074 * Look for a child view with the given id. If this view has the given
14075 * id, return this view.
14076 *
14077 * @param id The id to search for.
14078 * @return The view that has the given id in the hierarchy or null
14079 */
14080 public final View findViewById(int id) {
14081 if (id < 0) {
14082 return null;
14083 }
14084 return findViewTraversal(id);
14085 }
14086
14087 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014088 * Finds a view by its unuque and stable accessibility id.
14089 *
14090 * @param accessibilityId The searched accessibility id.
14091 * @return The found view.
14092 */
14093 final View findViewByAccessibilityId(int accessibilityId) {
14094 if (accessibilityId < 0) {
14095 return null;
14096 }
14097 return findViewByAccessibilityIdTraversal(accessibilityId);
14098 }
14099
14100 /**
14101 * Performs the traversal to find a view by its unuque and stable accessibility id.
14102 *
14103 * <strong>Note:</strong>This method does not stop at the root namespace
14104 * boundary since the user can touch the screen at an arbitrary location
14105 * potentially crossing the root namespace bounday which will send an
14106 * accessibility event to accessibility services and they should be able
14107 * to obtain the event source. Also accessibility ids are guaranteed to be
14108 * unique in the window.
14109 *
14110 * @param accessibilityId The accessibility id.
14111 * @return The found view.
14112 */
14113 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14114 if (getAccessibilityViewId() == accessibilityId) {
14115 return this;
14116 }
14117 return null;
14118 }
14119
14120 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014121 * Look for a child view with the given tag. If this view has the given
14122 * tag, return this view.
14123 *
14124 * @param tag The tag to search for, using "tag.equals(getTag())".
14125 * @return The View that has the given tag in the hierarchy or null
14126 */
14127 public final View findViewWithTag(Object tag) {
14128 if (tag == null) {
14129 return null;
14130 }
14131 return findViewWithTagTraversal(tag);
14132 }
14133
14134 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014135 * {@hide}
14136 * Look for a child view that matches the specified predicate.
14137 * If this view matches the predicate, return this view.
14138 *
14139 * @param predicate The predicate to evaluate.
14140 * @return The first view that matches the predicate or null.
14141 */
14142 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014143 return findViewByPredicateTraversal(predicate, null);
14144 }
14145
14146 /**
14147 * {@hide}
14148 * Look for a child view that matches the specified predicate,
14149 * starting with the specified view and its descendents and then
14150 * recusively searching the ancestors and siblings of that view
14151 * until this view is reached.
14152 *
14153 * This method is useful in cases where the predicate does not match
14154 * a single unique view (perhaps multiple views use the same id)
14155 * and we are trying to find the view that is "closest" in scope to the
14156 * starting view.
14157 *
14158 * @param start The view to start from.
14159 * @param predicate The predicate to evaluate.
14160 * @return The first view that matches the predicate or null.
14161 */
14162 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14163 View childToSkip = null;
14164 for (;;) {
14165 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14166 if (view != null || start == this) {
14167 return view;
14168 }
14169
14170 ViewParent parent = start.getParent();
14171 if (parent == null || !(parent instanceof View)) {
14172 return null;
14173 }
14174
14175 childToSkip = start;
14176 start = (View) parent;
14177 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014178 }
14179
14180 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014181 * Sets the identifier for this view. The identifier does not have to be
14182 * unique in this view's hierarchy. The identifier should be a positive
14183 * number.
14184 *
14185 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014186 * @see #getId()
14187 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014188 *
14189 * @param id a number used to identify the view
14190 *
14191 * @attr ref android.R.styleable#View_id
14192 */
14193 public void setId(int id) {
14194 mID = id;
14195 }
14196
14197 /**
14198 * {@hide}
14199 *
14200 * @param isRoot true if the view belongs to the root namespace, false
14201 * otherwise
14202 */
14203 public void setIsRootNamespace(boolean isRoot) {
14204 if (isRoot) {
14205 mPrivateFlags |= IS_ROOT_NAMESPACE;
14206 } else {
14207 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14208 }
14209 }
14210
14211 /**
14212 * {@hide}
14213 *
14214 * @return true if the view belongs to the root namespace, false otherwise
14215 */
14216 public boolean isRootNamespace() {
14217 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14218 }
14219
14220 /**
14221 * Returns this view's identifier.
14222 *
14223 * @return a positive integer used to identify the view or {@link #NO_ID}
14224 * if the view has no ID
14225 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014226 * @see #setId(int)
14227 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014228 * @attr ref android.R.styleable#View_id
14229 */
14230 @ViewDebug.CapturedViewProperty
14231 public int getId() {
14232 return mID;
14233 }
14234
14235 /**
14236 * Returns this view's tag.
14237 *
14238 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014239 *
14240 * @see #setTag(Object)
14241 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014242 */
14243 @ViewDebug.ExportedProperty
14244 public Object getTag() {
14245 return mTag;
14246 }
14247
14248 /**
14249 * Sets the tag associated with this view. A tag can be used to mark
14250 * a view in its hierarchy and does not have to be unique within the
14251 * hierarchy. Tags can also be used to store data within a view without
14252 * resorting to another data structure.
14253 *
14254 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014255 *
14256 * @see #getTag()
14257 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014258 */
14259 public void setTag(final Object tag) {
14260 mTag = tag;
14261 }
14262
14263 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014264 * Returns the tag associated with this view and the specified key.
14265 *
14266 * @param key The key identifying the tag
14267 *
14268 * @return the Object stored in this view as a tag
14269 *
14270 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014271 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014272 */
14273 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014274 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014275 return null;
14276 }
14277
14278 /**
14279 * Sets a tag associated with this view and a key. A tag can be used
14280 * to mark a view in its hierarchy and does not have to be unique within
14281 * the hierarchy. Tags can also be used to store data within a view
14282 * without resorting to another data structure.
14283 *
14284 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014285 * application to ensure it is unique (see the <a
14286 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14287 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014288 * the Android framework or not associated with any package will cause
14289 * an {@link IllegalArgumentException} to be thrown.
14290 *
14291 * @param key The key identifying the tag
14292 * @param tag An Object to tag the view with
14293 *
14294 * @throws IllegalArgumentException If they specified key is not valid
14295 *
14296 * @see #setTag(Object)
14297 * @see #getTag(int)
14298 */
14299 public void setTag(int key, final Object tag) {
14300 // If the package id is 0x00 or 0x01, it's either an undefined package
14301 // or a framework id
14302 if ((key >>> 24) < 2) {
14303 throw new IllegalArgumentException("The key must be an application-specific "
14304 + "resource id.");
14305 }
14306
Adam Powell2b2f6d62011-09-23 11:15:39 -070014307 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014308 }
14309
14310 /**
14311 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14312 * framework id.
14313 *
14314 * @hide
14315 */
14316 public void setTagInternal(int key, Object tag) {
14317 if ((key >>> 24) != 0x1) {
14318 throw new IllegalArgumentException("The key must be a framework-specific "
14319 + "resource id.");
14320 }
14321
Adam Powell2b2f6d62011-09-23 11:15:39 -070014322 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014323 }
14324
Adam Powell2b2f6d62011-09-23 11:15:39 -070014325 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014326 if (mKeyedTags == null) {
14327 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014328 }
14329
Adam Powell7db82ac2011-09-22 19:44:04 -070014330 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014331 }
14332
14333 /**
Romain Guy13922e02009-05-12 17:56:14 -070014334 * @param consistency The type of consistency. See ViewDebug for more information.
14335 *
14336 * @hide
14337 */
14338 protected boolean dispatchConsistencyCheck(int consistency) {
14339 return onConsistencyCheck(consistency);
14340 }
14341
14342 /**
14343 * Method that subclasses should implement to check their consistency. The type of
14344 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070014345 *
Romain Guy13922e02009-05-12 17:56:14 -070014346 * @param consistency The type of consistency. See ViewDebug for more information.
14347 *
14348 * @throws IllegalStateException if the view is in an inconsistent state.
14349 *
14350 * @hide
14351 */
14352 protected boolean onConsistencyCheck(int consistency) {
14353 boolean result = true;
14354
14355 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
14356 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
14357
14358 if (checkLayout) {
14359 if (getParent() == null) {
14360 result = false;
14361 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14362 "View " + this + " does not have a parent.");
14363 }
14364
14365 if (mAttachInfo == null) {
14366 result = false;
14367 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14368 "View " + this + " is not attached to a window.");
14369 }
14370 }
14371
14372 if (checkDrawing) {
14373 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
14374 // from their draw() method
14375
14376 if ((mPrivateFlags & DRAWN) != DRAWN &&
14377 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
14378 result = false;
14379 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14380 "View " + this + " was invalidated but its drawing cache is valid.");
14381 }
14382 }
14383
14384 return result;
14385 }
14386
14387 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014388 * Prints information about this view in the log output, with the tag
14389 * {@link #VIEW_LOG_TAG}.
14390 *
14391 * @hide
14392 */
14393 public void debug() {
14394 debug(0);
14395 }
14396
14397 /**
14398 * Prints information about this view in the log output, with the tag
14399 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14400 * indentation defined by the <code>depth</code>.
14401 *
14402 * @param depth the indentation level
14403 *
14404 * @hide
14405 */
14406 protected void debug(int depth) {
14407 String output = debugIndent(depth - 1);
14408
14409 output += "+ " + this;
14410 int id = getId();
14411 if (id != -1) {
14412 output += " (id=" + id + ")";
14413 }
14414 Object tag = getTag();
14415 if (tag != null) {
14416 output += " (tag=" + tag + ")";
14417 }
14418 Log.d(VIEW_LOG_TAG, output);
14419
14420 if ((mPrivateFlags & FOCUSED) != 0) {
14421 output = debugIndent(depth) + " FOCUSED";
14422 Log.d(VIEW_LOG_TAG, output);
14423 }
14424
14425 output = debugIndent(depth);
14426 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14427 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14428 + "} ";
14429 Log.d(VIEW_LOG_TAG, output);
14430
14431 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14432 || mPaddingBottom != 0) {
14433 output = debugIndent(depth);
14434 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14435 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14436 Log.d(VIEW_LOG_TAG, output);
14437 }
14438
14439 output = debugIndent(depth);
14440 output += "mMeasureWidth=" + mMeasuredWidth +
14441 " mMeasureHeight=" + mMeasuredHeight;
14442 Log.d(VIEW_LOG_TAG, output);
14443
14444 output = debugIndent(depth);
14445 if (mLayoutParams == null) {
14446 output += "BAD! no layout params";
14447 } else {
14448 output = mLayoutParams.debug(output);
14449 }
14450 Log.d(VIEW_LOG_TAG, output);
14451
14452 output = debugIndent(depth);
14453 output += "flags={";
14454 output += View.printFlags(mViewFlags);
14455 output += "}";
14456 Log.d(VIEW_LOG_TAG, output);
14457
14458 output = debugIndent(depth);
14459 output += "privateFlags={";
14460 output += View.printPrivateFlags(mPrivateFlags);
14461 output += "}";
14462 Log.d(VIEW_LOG_TAG, output);
14463 }
14464
14465 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014466 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014467 *
14468 * @param depth the indentation level
14469 * @return a String containing (depth * 2 + 3) * 2 white spaces
14470 *
14471 * @hide
14472 */
14473 protected static String debugIndent(int depth) {
14474 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
14475 for (int i = 0; i < (depth * 2) + 3; i++) {
14476 spaces.append(' ').append(' ');
14477 }
14478 return spaces.toString();
14479 }
14480
14481 /**
14482 * <p>Return the offset of the widget's text baseline from the widget's top
14483 * boundary. If this widget does not support baseline alignment, this
14484 * method returns -1. </p>
14485 *
14486 * @return the offset of the baseline within the widget's bounds or -1
14487 * if baseline alignment is not supported
14488 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070014489 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014490 public int getBaseline() {
14491 return -1;
14492 }
14493
14494 /**
14495 * Call this when something has changed which has invalidated the
14496 * layout of this view. This will schedule a layout pass of the view
14497 * tree.
14498 */
14499 public void requestLayout() {
14500 if (ViewDebug.TRACE_HIERARCHY) {
14501 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
14502 }
14503
14504 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014505 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014506
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070014507 if (mLayoutParams != null) {
14508 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
14509 }
14510
14511 if (mParent != null && !mParent.isLayoutRequested()) {
14512 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014513 }
14514 }
14515
14516 /**
14517 * Forces this view to be laid out during the next layout pass.
14518 * This method does not call requestLayout() or forceLayout()
14519 * on the parent.
14520 */
14521 public void forceLayout() {
14522 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014523 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014524 }
14525
14526 /**
14527 * <p>
14528 * This is called to find out how big a view should be. The parent
14529 * supplies constraint information in the width and height parameters.
14530 * </p>
14531 *
14532 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080014533 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014534 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014535 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014536 * </p>
14537 *
14538 *
14539 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14540 * parent
14541 * @param heightMeasureSpec Vertical space requirements as imposed by the
14542 * parent
14543 *
14544 * @see #onMeasure(int, int)
14545 */
14546 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14547 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14548 widthMeasureSpec != mOldWidthMeasureSpec ||
14549 heightMeasureSpec != mOldHeightMeasureSpec) {
14550
14551 // first clears the measured dimension flag
14552 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14553
14554 if (ViewDebug.TRACE_HIERARCHY) {
14555 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
14556 }
14557
14558 // measure ourselves, this should set the measured dimension flag back
14559 onMeasure(widthMeasureSpec, heightMeasureSpec);
14560
14561 // flag not set, setMeasuredDimension() was not invoked, we raise
14562 // an exception to warn the developer
14563 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14564 throw new IllegalStateException("onMeasure() did not set the"
14565 + " measured dimension by calling"
14566 + " setMeasuredDimension()");
14567 }
14568
14569 mPrivateFlags |= LAYOUT_REQUIRED;
14570 }
14571
14572 mOldWidthMeasureSpec = widthMeasureSpec;
14573 mOldHeightMeasureSpec = heightMeasureSpec;
14574 }
14575
14576 /**
14577 * <p>
14578 * Measure the view and its content to determine the measured width and the
14579 * measured height. This method is invoked by {@link #measure(int, int)} and
14580 * should be overriden by subclasses to provide accurate and efficient
14581 * measurement of their contents.
14582 * </p>
14583 *
14584 * <p>
14585 * <strong>CONTRACT:</strong> When overriding this method, you
14586 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14587 * measured width and height of this view. Failure to do so will trigger an
14588 * <code>IllegalStateException</code>, thrown by
14589 * {@link #measure(int, int)}. Calling the superclass'
14590 * {@link #onMeasure(int, int)} is a valid use.
14591 * </p>
14592 *
14593 * <p>
14594 * The base class implementation of measure defaults to the background size,
14595 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14596 * override {@link #onMeasure(int, int)} to provide better measurements of
14597 * their content.
14598 * </p>
14599 *
14600 * <p>
14601 * If this method is overridden, it is the subclass's responsibility to make
14602 * sure the measured height and width are at least the view's minimum height
14603 * and width ({@link #getSuggestedMinimumHeight()} and
14604 * {@link #getSuggestedMinimumWidth()}).
14605 * </p>
14606 *
14607 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
14608 * The requirements are encoded with
14609 * {@link android.view.View.MeasureSpec}.
14610 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
14611 * The requirements are encoded with
14612 * {@link android.view.View.MeasureSpec}.
14613 *
14614 * @see #getMeasuredWidth()
14615 * @see #getMeasuredHeight()
14616 * @see #setMeasuredDimension(int, int)
14617 * @see #getSuggestedMinimumHeight()
14618 * @see #getSuggestedMinimumWidth()
14619 * @see android.view.View.MeasureSpec#getMode(int)
14620 * @see android.view.View.MeasureSpec#getSize(int)
14621 */
14622 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
14623 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
14624 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
14625 }
14626
14627 /**
14628 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
14629 * measured width and measured height. Failing to do so will trigger an
14630 * exception at measurement time.</p>
14631 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080014632 * @param measuredWidth The measured width of this view. May be a complex
14633 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14634 * {@link #MEASURED_STATE_TOO_SMALL}.
14635 * @param measuredHeight The measured height of this view. May be a complex
14636 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14637 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014638 */
14639 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
14640 mMeasuredWidth = measuredWidth;
14641 mMeasuredHeight = measuredHeight;
14642
14643 mPrivateFlags |= MEASURED_DIMENSION_SET;
14644 }
14645
14646 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080014647 * Merge two states as returned by {@link #getMeasuredState()}.
14648 * @param curState The current state as returned from a view or the result
14649 * of combining multiple views.
14650 * @param newState The new view state to combine.
14651 * @return Returns a new integer reflecting the combination of the two
14652 * states.
14653 */
14654 public static int combineMeasuredStates(int curState, int newState) {
14655 return curState | newState;
14656 }
14657
14658 /**
14659 * Version of {@link #resolveSizeAndState(int, int, int)}
14660 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
14661 */
14662 public static int resolveSize(int size, int measureSpec) {
14663 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
14664 }
14665
14666 /**
14667 * Utility to reconcile a desired size and state, with constraints imposed
14668 * by a MeasureSpec. Will take the desired size, unless a different size
14669 * is imposed by the constraints. The returned value is a compound integer,
14670 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
14671 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
14672 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014673 *
14674 * @param size How big the view wants to be
14675 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080014676 * @return Size information bit mask as defined by
14677 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014678 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080014679 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014680 int result = size;
14681 int specMode = MeasureSpec.getMode(measureSpec);
14682 int specSize = MeasureSpec.getSize(measureSpec);
14683 switch (specMode) {
14684 case MeasureSpec.UNSPECIFIED:
14685 result = size;
14686 break;
14687 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080014688 if (specSize < size) {
14689 result = specSize | MEASURED_STATE_TOO_SMALL;
14690 } else {
14691 result = size;
14692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014693 break;
14694 case MeasureSpec.EXACTLY:
14695 result = specSize;
14696 break;
14697 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080014698 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014699 }
14700
14701 /**
14702 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070014703 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014704 * by the MeasureSpec.
14705 *
14706 * @param size Default size for this view
14707 * @param measureSpec Constraints imposed by the parent
14708 * @return The size this view should be.
14709 */
14710 public static int getDefaultSize(int size, int measureSpec) {
14711 int result = size;
14712 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070014713 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014714
14715 switch (specMode) {
14716 case MeasureSpec.UNSPECIFIED:
14717 result = size;
14718 break;
14719 case MeasureSpec.AT_MOST:
14720 case MeasureSpec.EXACTLY:
14721 result = specSize;
14722 break;
14723 }
14724 return result;
14725 }
14726
14727 /**
14728 * Returns the suggested minimum height that the view should use. This
14729 * returns the maximum of the view's minimum height
14730 * and the background's minimum height
14731 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
14732 * <p>
14733 * When being used in {@link #onMeasure(int, int)}, the caller should still
14734 * ensure the returned height is within the requirements of the parent.
14735 *
14736 * @return The suggested minimum height of the view.
14737 */
14738 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014739 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014741 }
14742
14743 /**
14744 * Returns the suggested minimum width that the view should use. This
14745 * returns the maximum of the view's minimum width)
14746 * and the background's minimum width
14747 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
14748 * <p>
14749 * When being used in {@link #onMeasure(int, int)}, the caller should still
14750 * ensure the returned width is within the requirements of the parent.
14751 *
14752 * @return The suggested minimum width of the view.
14753 */
14754 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014755 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
14756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014757
Philip Milne6c8ea062012-04-03 17:38:43 -070014758 /**
14759 * Returns the minimum height of the view.
14760 *
14761 * @return the minimum height the view will try to be.
14762 *
14763 * @see #setMinimumHeight(int)
14764 *
14765 * @attr ref android.R.styleable#View_minHeight
14766 */
14767 public int getMinimumHeight() {
14768 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014769 }
14770
14771 /**
14772 * Sets the minimum height of the view. It is not guaranteed the view will
14773 * be able to achieve this minimum height (for example, if its parent layout
14774 * constrains it with less available height).
14775 *
14776 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014777 *
14778 * @see #getMinimumHeight()
14779 *
14780 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014781 */
14782 public void setMinimumHeight(int minHeight) {
14783 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070014784 requestLayout();
14785 }
14786
14787 /**
14788 * Returns the minimum width of the view.
14789 *
14790 * @return the minimum width the view will try to be.
14791 *
14792 * @see #setMinimumWidth(int)
14793 *
14794 * @attr ref android.R.styleable#View_minWidth
14795 */
14796 public int getMinimumWidth() {
14797 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014798 }
14799
14800 /**
14801 * Sets the minimum width of the view. It is not guaranteed the view will
14802 * be able to achieve this minimum width (for example, if its parent layout
14803 * constrains it with less available width).
14804 *
14805 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014806 *
14807 * @see #getMinimumWidth()
14808 *
14809 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014810 */
14811 public void setMinimumWidth(int minWidth) {
14812 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070014813 requestLayout();
14814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014815 }
14816
14817 /**
14818 * Get the animation currently associated with this view.
14819 *
14820 * @return The animation that is currently playing or
14821 * scheduled to play for this view.
14822 */
14823 public Animation getAnimation() {
14824 return mCurrentAnimation;
14825 }
14826
14827 /**
14828 * Start the specified animation now.
14829 *
14830 * @param animation the animation to start now
14831 */
14832 public void startAnimation(Animation animation) {
14833 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
14834 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014835 invalidateParentCaches();
14836 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014837 }
14838
14839 /**
14840 * Cancels any animations for this view.
14841 */
14842 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080014843 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080014844 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080014845 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014846 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014847 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014848 }
14849
14850 /**
14851 * Sets the next animation to play for this view.
14852 * If you want the animation to play immediately, use
14853 * startAnimation. This method provides allows fine-grained
14854 * control over the start time and invalidation, but you
14855 * must make sure that 1) the animation has a start time set, and
14856 * 2) the view will be invalidated when the animation is supposed to
14857 * start.
14858 *
14859 * @param animation The next animation, or null.
14860 */
14861 public void setAnimation(Animation animation) {
14862 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070014863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014864 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070014865 // If the screen is off assume the animation start time is now instead of
14866 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
14867 // would cause the animation to start when the screen turns back on
14868 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
14869 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
14870 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
14871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014872 animation.reset();
14873 }
14874 }
14875
14876 /**
14877 * Invoked by a parent ViewGroup to notify the start of the animation
14878 * currently associated with this view. If you override this method,
14879 * always call super.onAnimationStart();
14880 *
14881 * @see #setAnimation(android.view.animation.Animation)
14882 * @see #getAnimation()
14883 */
14884 protected void onAnimationStart() {
14885 mPrivateFlags |= ANIMATION_STARTED;
14886 }
14887
14888 /**
14889 * Invoked by a parent ViewGroup to notify the end of the animation
14890 * currently associated with this view. If you override this method,
14891 * always call super.onAnimationEnd();
14892 *
14893 * @see #setAnimation(android.view.animation.Animation)
14894 * @see #getAnimation()
14895 */
14896 protected void onAnimationEnd() {
14897 mPrivateFlags &= ~ANIMATION_STARTED;
14898 }
14899
14900 /**
14901 * Invoked if there is a Transform that involves alpha. Subclass that can
14902 * draw themselves with the specified alpha should return true, and then
14903 * respect that alpha when their onDraw() is called. If this returns false
14904 * then the view may be redirected to draw into an offscreen buffer to
14905 * fulfill the request, which will look fine, but may be slower than if the
14906 * subclass handles it internally. The default implementation returns false.
14907 *
14908 * @param alpha The alpha (0..255) to apply to the view's drawing
14909 * @return true if the view can draw with the specified alpha.
14910 */
14911 protected boolean onSetAlpha(int alpha) {
14912 return false;
14913 }
14914
14915 /**
14916 * This is used by the RootView to perform an optimization when
14917 * the view hierarchy contains one or several SurfaceView.
14918 * SurfaceView is always considered transparent, but its children are not,
14919 * therefore all View objects remove themselves from the global transparent
14920 * region (passed as a parameter to this function).
14921 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014922 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014923 *
14924 * @return Returns true if the effective visibility of the view at this
14925 * point is opaque, regardless of the transparent region; returns false
14926 * if it is possible for underlying windows to be seen behind the view.
14927 *
14928 * {@hide}
14929 */
14930 public boolean gatherTransparentRegion(Region region) {
14931 final AttachInfo attachInfo = mAttachInfo;
14932 if (region != null && attachInfo != null) {
14933 final int pflags = mPrivateFlags;
14934 if ((pflags & SKIP_DRAW) == 0) {
14935 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
14936 // remove it from the transparent region.
14937 final int[] location = attachInfo.mTransparentLocation;
14938 getLocationInWindow(location);
14939 region.op(location[0], location[1], location[0] + mRight - mLeft,
14940 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070014941 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014942 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
14943 // exists, so we remove the background drawable's non-transparent
14944 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070014945 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014946 }
14947 }
14948 return true;
14949 }
14950
14951 /**
14952 * Play a sound effect for this view.
14953 *
14954 * <p>The framework will play sound effects for some built in actions, such as
14955 * clicking, but you may wish to play these effects in your widget,
14956 * for instance, for internal navigation.
14957 *
14958 * <p>The sound effect will only be played if sound effects are enabled by the user, and
14959 * {@link #isSoundEffectsEnabled()} is true.
14960 *
14961 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
14962 */
14963 public void playSoundEffect(int soundConstant) {
14964 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
14965 return;
14966 }
14967 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
14968 }
14969
14970 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014971 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070014972 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014973 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014974 *
14975 * <p>The framework will provide haptic feedback for some built in actions,
14976 * such as long presses, but you may wish to provide feedback for your
14977 * own widget.
14978 *
14979 * <p>The feedback will only be performed if
14980 * {@link #isHapticFeedbackEnabled()} is true.
14981 *
14982 * @param feedbackConstant One of the constants defined in
14983 * {@link HapticFeedbackConstants}
14984 */
14985 public boolean performHapticFeedback(int feedbackConstant) {
14986 return performHapticFeedback(feedbackConstant, 0);
14987 }
14988
14989 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014990 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070014991 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014992 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014993 *
14994 * @param feedbackConstant One of the constants defined in
14995 * {@link HapticFeedbackConstants}
14996 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
14997 */
14998 public boolean performHapticFeedback(int feedbackConstant, int flags) {
14999 if (mAttachInfo == null) {
15000 return false;
15001 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015002 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015003 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015004 && !isHapticFeedbackEnabled()) {
15005 return false;
15006 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015007 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15008 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015009 }
15010
15011 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015012 * Request that the visibility of the status bar or other screen/window
15013 * decorations be changed.
15014 *
15015 * <p>This method is used to put the over device UI into temporary modes
15016 * where the user's attention is focused more on the application content,
15017 * by dimming or hiding surrounding system affordances. This is typically
15018 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15019 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15020 * to be placed behind the action bar (and with these flags other system
15021 * affordances) so that smooth transitions between hiding and showing them
15022 * can be done.
15023 *
15024 * <p>Two representative examples of the use of system UI visibility is
15025 * implementing a content browsing application (like a magazine reader)
15026 * and a video playing application.
15027 *
15028 * <p>The first code shows a typical implementation of a View in a content
15029 * browsing application. In this implementation, the application goes
15030 * into a content-oriented mode by hiding the status bar and action bar,
15031 * and putting the navigation elements into lights out mode. The user can
15032 * then interact with content while in this mode. Such an application should
15033 * provide an easy way for the user to toggle out of the mode (such as to
15034 * check information in the status bar or access notifications). In the
15035 * implementation here, this is done simply by tapping on the content.
15036 *
15037 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15038 * content}
15039 *
15040 * <p>This second code sample shows a typical implementation of a View
15041 * in a video playing application. In this situation, while the video is
15042 * playing the application would like to go into a complete full-screen mode,
15043 * to use as much of the display as possible for the video. When in this state
15044 * the user can not interact with the application; the system intercepts
15045 * touching on the screen to pop the UI out of full screen mode.
15046 *
15047 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15048 * content}
15049 *
15050 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15051 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15052 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15053 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015054 */
15055 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015056 if (visibility != mSystemUiVisibility) {
15057 mSystemUiVisibility = visibility;
15058 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15059 mParent.recomputeViewAttributes(this);
15060 }
Joe Onorato664644d2011-01-23 17:53:23 -080015061 }
15062 }
15063
15064 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015065 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15066 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15067 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15068 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15069 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015070 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015071 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015072 return mSystemUiVisibility;
15073 }
15074
Scott Mainec6331b2011-05-24 16:55:56 -070015075 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015076 * Returns the current system UI visibility that is currently set for
15077 * the entire window. This is the combination of the
15078 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15079 * views in the window.
15080 */
15081 public int getWindowSystemUiVisibility() {
15082 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15083 }
15084
15085 /**
15086 * Override to find out when the window's requested system UI visibility
15087 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15088 * This is different from the callbacks recieved through
15089 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15090 * in that this is only telling you about the local request of the window,
15091 * not the actual values applied by the system.
15092 */
15093 public void onWindowSystemUiVisibilityChanged(int visible) {
15094 }
15095
15096 /**
15097 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15098 * the view hierarchy.
15099 */
15100 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15101 onWindowSystemUiVisibilityChanged(visible);
15102 }
15103
15104 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015105 * Set a listener to receive callbacks when the visibility of the system bar changes.
15106 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15107 */
Joe Onorato664644d2011-01-23 17:53:23 -080015108 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015109 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015110 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15111 mParent.recomputeViewAttributes(this);
15112 }
15113 }
15114
15115 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015116 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15117 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015118 */
15119 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015120 ListenerInfo li = mListenerInfo;
15121 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15122 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015123 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015124 }
15125 }
15126
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015127 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
15128 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15129 if (val != mSystemUiVisibility) {
15130 setSystemUiVisibility(val);
15131 }
15132 }
15133
Joe Onorato664644d2011-01-23 17:53:23 -080015134 /**
Joe Malin32736f02011-01-19 16:14:20 -080015135 * Creates an image that the system displays during the drag and drop
15136 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15137 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15138 * appearance as the given View. The default also positions the center of the drag shadow
15139 * directly under the touch point. If no View is provided (the constructor with no parameters
15140 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15141 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15142 * default is an invisible drag shadow.
15143 * <p>
15144 * You are not required to use the View you provide to the constructor as the basis of the
15145 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15146 * anything you want as the drag shadow.
15147 * </p>
15148 * <p>
15149 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15150 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15151 * size and position of the drag shadow. It uses this data to construct a
15152 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15153 * so that your application can draw the shadow image in the Canvas.
15154 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015155 *
15156 * <div class="special reference">
15157 * <h3>Developer Guides</h3>
15158 * <p>For a guide to implementing drag and drop features, read the
15159 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15160 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015161 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015162 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015163 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015164
15165 /**
Joe Malin32736f02011-01-19 16:14:20 -080015166 * Constructs a shadow image builder based on a View. By default, the resulting drag
15167 * shadow will have the same appearance and dimensions as the View, with the touch point
15168 * over the center of the View.
15169 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015170 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015171 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015172 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015173 }
15174
Christopher Tate17ed60c2011-01-18 12:50:26 -080015175 /**
15176 * Construct a shadow builder object with no associated View. This
15177 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15178 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15179 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015180 * reference to any View object. If they are not overridden, then the result is an
15181 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015182 */
15183 public DragShadowBuilder() {
15184 mView = new WeakReference<View>(null);
15185 }
15186
15187 /**
15188 * Returns the View object that had been passed to the
15189 * {@link #View.DragShadowBuilder(View)}
15190 * constructor. If that View parameter was {@code null} or if the
15191 * {@link #View.DragShadowBuilder()}
15192 * constructor was used to instantiate the builder object, this method will return
15193 * null.
15194 *
15195 * @return The View object associate with this builder object.
15196 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015197 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015198 final public View getView() {
15199 return mView.get();
15200 }
15201
Christopher Tate2c095f32010-10-04 14:13:40 -070015202 /**
Joe Malin32736f02011-01-19 16:14:20 -080015203 * Provides the metrics for the shadow image. These include the dimensions of
15204 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015205 * be centered under the touch location while dragging.
15206 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015207 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015208 * same as the dimensions of the View itself and centers the shadow under
15209 * the touch point.
15210 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015211 *
Joe Malin32736f02011-01-19 16:14:20 -080015212 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15213 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15214 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15215 * image.
15216 *
15217 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15218 * shadow image that should be underneath the touch point during the drag and drop
15219 * operation. Your application must set {@link android.graphics.Point#x} to the
15220 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015221 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015222 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015223 final View view = mView.get();
15224 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015225 shadowSize.set(view.getWidth(), view.getHeight());
15226 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015227 } else {
15228 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15229 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015230 }
15231
15232 /**
Joe Malin32736f02011-01-19 16:14:20 -080015233 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15234 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015235 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015236 *
Joe Malin32736f02011-01-19 16:14:20 -080015237 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015238 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015239 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015240 final View view = mView.get();
15241 if (view != null) {
15242 view.draw(canvas);
15243 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015244 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015245 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015246 }
15247 }
15248
15249 /**
Joe Malin32736f02011-01-19 16:14:20 -080015250 * Starts a drag and drop operation. When your application calls this method, it passes a
15251 * {@link android.view.View.DragShadowBuilder} object to the system. The
15252 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15253 * to get metrics for the drag shadow, and then calls the object's
15254 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15255 * <p>
15256 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15257 * drag events to all the View objects in your application that are currently visible. It does
15258 * this either by calling the View object's drag listener (an implementation of
15259 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15260 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15261 * Both are passed a {@link android.view.DragEvent} object that has a
15262 * {@link android.view.DragEvent#getAction()} value of
15263 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15264 * </p>
15265 * <p>
15266 * Your application can invoke startDrag() on any attached View object. The View object does not
15267 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15268 * be related to the View the user selected for dragging.
15269 * </p>
15270 * @param data A {@link android.content.ClipData} object pointing to the data to be
15271 * transferred by the drag and drop operation.
15272 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15273 * drag shadow.
15274 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15275 * drop operation. This Object is put into every DragEvent object sent by the system during the
15276 * current drag.
15277 * <p>
15278 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15279 * to the target Views. For example, it can contain flags that differentiate between a
15280 * a copy operation and a move operation.
15281 * </p>
15282 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15283 * so the parameter should be set to 0.
15284 * @return {@code true} if the method completes successfully, or
15285 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15286 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015287 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015288 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015289 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015290 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015291 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015292 }
15293 boolean okay = false;
15294
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015295 Point shadowSize = new Point();
15296 Point shadowTouchPoint = new Point();
15297 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015298
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015299 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15300 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15301 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015302 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015303
Chris Tatea32dcf72010-10-14 12:13:50 -070015304 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015305 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15306 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015307 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015308 Surface surface = new Surface();
15309 try {
15310 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015311 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015312 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015313 + " surface=" + surface);
15314 if (token != null) {
15315 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015316 try {
Chris Tate6b391282010-10-14 15:48:59 -070015317 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015318 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015319 } finally {
15320 surface.unlockCanvasAndPost(canvas);
15321 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015322
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015323 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015324
15325 // Cache the local state object for delivery with DragEvents
15326 root.setLocalDragState(myLocalState);
15327
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015328 // repurpose 'shadowSize' for the last touch point
15329 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015330
Christopher Tatea53146c2010-09-07 11:57:52 -070015331 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015332 shadowSize.x, shadowSize.y,
15333 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015334 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015335
15336 // Off and running! Release our local surface instance; the drag
15337 // shadow surface is now managed by the system process.
15338 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015339 }
15340 } catch (Exception e) {
15341 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15342 surface.destroy();
15343 }
15344
15345 return okay;
15346 }
15347
Christopher Tatea53146c2010-09-07 11:57:52 -070015348 /**
Joe Malin32736f02011-01-19 16:14:20 -080015349 * Handles drag events sent by the system following a call to
15350 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15351 *<p>
15352 * When the system calls this method, it passes a
15353 * {@link android.view.DragEvent} object. A call to
15354 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15355 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15356 * operation.
15357 * @param event The {@link android.view.DragEvent} sent by the system.
15358 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15359 * in DragEvent, indicating the type of drag event represented by this object.
15360 * @return {@code true} if the method was successful, otherwise {@code false}.
15361 * <p>
15362 * The method should return {@code true} in response to an action type of
15363 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15364 * operation.
15365 * </p>
15366 * <p>
15367 * The method should also return {@code true} in response to an action type of
15368 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15369 * {@code false} if it didn't.
15370 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015371 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015372 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015373 return false;
15374 }
15375
15376 /**
Joe Malin32736f02011-01-19 16:14:20 -080015377 * Detects if this View is enabled and has a drag event listener.
15378 * If both are true, then it calls the drag event listener with the
15379 * {@link android.view.DragEvent} it received. If the drag event listener returns
15380 * {@code true}, then dispatchDragEvent() returns {@code true}.
15381 * <p>
15382 * For all other cases, the method calls the
15383 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15384 * method and returns its result.
15385 * </p>
15386 * <p>
15387 * This ensures that a drag event is always consumed, even if the View does not have a drag
15388 * event listener. However, if the View has a listener and the listener returns true, then
15389 * onDragEvent() is not called.
15390 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015391 */
15392 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015393 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015394 ListenerInfo li = mListenerInfo;
15395 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15396 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015397 return true;
15398 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015399 return onDragEvent(event);
15400 }
15401
Christopher Tate3d4bf172011-03-28 16:16:46 -070015402 boolean canAcceptDrag() {
15403 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15404 }
15405
Christopher Tatea53146c2010-09-07 11:57:52 -070015406 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015407 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15408 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015409 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015410 */
15411 public void onCloseSystemDialogs(String reason) {
15412 }
Joe Malin32736f02011-01-19 16:14:20 -080015413
Dianne Hackbornffa42482009-09-23 22:20:11 -070015414 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015415 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015416 * update a Region being computed for
15417 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015418 * that any non-transparent parts of the Drawable are removed from the
15419 * given transparent region.
15420 *
15421 * @param dr The Drawable whose transparency is to be applied to the region.
15422 * @param region A Region holding the current transparency information,
15423 * where any parts of the region that are set are considered to be
15424 * transparent. On return, this region will be modified to have the
15425 * transparency information reduced by the corresponding parts of the
15426 * Drawable that are not transparent.
15427 * {@hide}
15428 */
15429 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15430 if (DBG) {
15431 Log.i("View", "Getting transparent region for: " + this);
15432 }
15433 final Region r = dr.getTransparentRegion();
15434 final Rect db = dr.getBounds();
15435 final AttachInfo attachInfo = mAttachInfo;
15436 if (r != null && attachInfo != null) {
15437 final int w = getRight()-getLeft();
15438 final int h = getBottom()-getTop();
15439 if (db.left > 0) {
15440 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15441 r.op(0, 0, db.left, h, Region.Op.UNION);
15442 }
15443 if (db.right < w) {
15444 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15445 r.op(db.right, 0, w, h, Region.Op.UNION);
15446 }
15447 if (db.top > 0) {
15448 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15449 r.op(0, 0, w, db.top, Region.Op.UNION);
15450 }
15451 if (db.bottom < h) {
15452 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15453 r.op(0, db.bottom, w, h, Region.Op.UNION);
15454 }
15455 final int[] location = attachInfo.mTransparentLocation;
15456 getLocationInWindow(location);
15457 r.translate(location[0], location[1]);
15458 region.op(r, Region.Op.INTERSECT);
15459 } else {
15460 region.op(db, Region.Op.DIFFERENCE);
15461 }
15462 }
15463
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015464 private void checkForLongClick(int delayOffset) {
15465 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
15466 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015467
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015468 if (mPendingCheckForLongPress == null) {
15469 mPendingCheckForLongPress = new CheckForLongPress();
15470 }
15471 mPendingCheckForLongPress.rememberWindowAttachCount();
15472 postDelayed(mPendingCheckForLongPress,
15473 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015474 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015475 }
15476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015477 /**
15478 * Inflate a view from an XML resource. This convenience method wraps the {@link
15479 * LayoutInflater} class, which provides a full range of options for view inflation.
15480 *
15481 * @param context The Context object for your activity or application.
15482 * @param resource The resource ID to inflate
15483 * @param root A view group that will be the parent. Used to properly inflate the
15484 * layout_* parameters.
15485 * @see LayoutInflater
15486 */
15487 public static View inflate(Context context, int resource, ViewGroup root) {
15488 LayoutInflater factory = LayoutInflater.from(context);
15489 return factory.inflate(resource, root);
15490 }
Romain Guy33e72ae2010-07-17 12:40:29 -070015491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015492 /**
Adam Powell637d3372010-08-25 14:37:03 -070015493 * Scroll the view with standard behavior for scrolling beyond the normal
15494 * content boundaries. Views that call this method should override
15495 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
15496 * results of an over-scroll operation.
15497 *
15498 * Views can use this method to handle any touch or fling-based scrolling.
15499 *
15500 * @param deltaX Change in X in pixels
15501 * @param deltaY Change in Y in pixels
15502 * @param scrollX Current X scroll value in pixels before applying deltaX
15503 * @param scrollY Current Y scroll value in pixels before applying deltaY
15504 * @param scrollRangeX Maximum content scroll range along the X axis
15505 * @param scrollRangeY Maximum content scroll range along the Y axis
15506 * @param maxOverScrollX Number of pixels to overscroll by in either direction
15507 * along the X axis.
15508 * @param maxOverScrollY Number of pixels to overscroll by in either direction
15509 * along the Y axis.
15510 * @param isTouchEvent true if this scroll operation is the result of a touch event.
15511 * @return true if scrolling was clamped to an over-scroll boundary along either
15512 * axis, false otherwise.
15513 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070015514 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070015515 protected boolean overScrollBy(int deltaX, int deltaY,
15516 int scrollX, int scrollY,
15517 int scrollRangeX, int scrollRangeY,
15518 int maxOverScrollX, int maxOverScrollY,
15519 boolean isTouchEvent) {
15520 final int overScrollMode = mOverScrollMode;
15521 final boolean canScrollHorizontal =
15522 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
15523 final boolean canScrollVertical =
15524 computeVerticalScrollRange() > computeVerticalScrollExtent();
15525 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
15526 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
15527 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
15528 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15529
15530 int newScrollX = scrollX + deltaX;
15531 if (!overScrollHorizontal) {
15532 maxOverScrollX = 0;
15533 }
15534
15535 int newScrollY = scrollY + deltaY;
15536 if (!overScrollVertical) {
15537 maxOverScrollY = 0;
15538 }
15539
15540 // Clamp values if at the limits and record
15541 final int left = -maxOverScrollX;
15542 final int right = maxOverScrollX + scrollRangeX;
15543 final int top = -maxOverScrollY;
15544 final int bottom = maxOverScrollY + scrollRangeY;
15545
15546 boolean clampedX = false;
15547 if (newScrollX > right) {
15548 newScrollX = right;
15549 clampedX = true;
15550 } else if (newScrollX < left) {
15551 newScrollX = left;
15552 clampedX = true;
15553 }
15554
15555 boolean clampedY = false;
15556 if (newScrollY > bottom) {
15557 newScrollY = bottom;
15558 clampedY = true;
15559 } else if (newScrollY < top) {
15560 newScrollY = top;
15561 clampedY = true;
15562 }
15563
15564 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
15565
15566 return clampedX || clampedY;
15567 }
15568
15569 /**
15570 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
15571 * respond to the results of an over-scroll operation.
15572 *
15573 * @param scrollX New X scroll value in pixels
15574 * @param scrollY New Y scroll value in pixels
15575 * @param clampedX True if scrollX was clamped to an over-scroll boundary
15576 * @param clampedY True if scrollY was clamped to an over-scroll boundary
15577 */
15578 protected void onOverScrolled(int scrollX, int scrollY,
15579 boolean clampedX, boolean clampedY) {
15580 // Intentionally empty.
15581 }
15582
15583 /**
15584 * Returns the over-scroll mode for this view. The result will be
15585 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15586 * (allow over-scrolling only if the view content is larger than the container),
15587 * or {@link #OVER_SCROLL_NEVER}.
15588 *
15589 * @return This view's over-scroll mode.
15590 */
15591 public int getOverScrollMode() {
15592 return mOverScrollMode;
15593 }
15594
15595 /**
15596 * Set the over-scroll mode for this view. Valid over-scroll modes are
15597 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15598 * (allow over-scrolling only if the view content is larger than the container),
15599 * or {@link #OVER_SCROLL_NEVER}.
15600 *
15601 * Setting the over-scroll mode of a view will have an effect only if the
15602 * view is capable of scrolling.
15603 *
15604 * @param overScrollMode The new over-scroll mode for this view.
15605 */
15606 public void setOverScrollMode(int overScrollMode) {
15607 if (overScrollMode != OVER_SCROLL_ALWAYS &&
15608 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
15609 overScrollMode != OVER_SCROLL_NEVER) {
15610 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
15611 }
15612 mOverScrollMode = overScrollMode;
15613 }
15614
15615 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015616 * Gets a scale factor that determines the distance the view should scroll
15617 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
15618 * @return The vertical scroll scale factor.
15619 * @hide
15620 */
15621 protected float getVerticalScrollFactor() {
15622 if (mVerticalScrollFactor == 0) {
15623 TypedValue outValue = new TypedValue();
15624 if (!mContext.getTheme().resolveAttribute(
15625 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
15626 throw new IllegalStateException(
15627 "Expected theme to define listPreferredItemHeight.");
15628 }
15629 mVerticalScrollFactor = outValue.getDimension(
15630 mContext.getResources().getDisplayMetrics());
15631 }
15632 return mVerticalScrollFactor;
15633 }
15634
15635 /**
15636 * Gets a scale factor that determines the distance the view should scroll
15637 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
15638 * @return The horizontal scroll scale factor.
15639 * @hide
15640 */
15641 protected float getHorizontalScrollFactor() {
15642 // TODO: Should use something else.
15643 return getVerticalScrollFactor();
15644 }
15645
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015646 /**
15647 * Return the value specifying the text direction or policy that was set with
15648 * {@link #setTextDirection(int)}.
15649 *
15650 * @return the defined text direction. It can be one of:
15651 *
15652 * {@link #TEXT_DIRECTION_INHERIT},
15653 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15654 * {@link #TEXT_DIRECTION_ANY_RTL},
15655 * {@link #TEXT_DIRECTION_LTR},
15656 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015657 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015658 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015659 @ViewDebug.ExportedProperty(category = "text", mapping = {
15660 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
15661 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
15662 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
15663 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
15664 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
15665 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
15666 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015667 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015668 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015669 }
15670
15671 /**
15672 * Set the text direction.
15673 *
15674 * @param textDirection the direction to set. Should be one of:
15675 *
15676 * {@link #TEXT_DIRECTION_INHERIT},
15677 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15678 * {@link #TEXT_DIRECTION_ANY_RTL},
15679 * {@link #TEXT_DIRECTION_LTR},
15680 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015681 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015682 */
15683 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015684 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015685 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015686 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015687 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015688 // Set the new text direction
15689 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015690 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015691 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015692 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015693 }
15694 }
15695
15696 /**
15697 * Return the resolved text direction.
15698 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015699 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
15700 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
15701 * up the parent chain of the view. if there is no parent, then it will return the default
15702 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
15703 *
15704 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015705 *
Doug Feltcb3791202011-07-07 11:57:48 -070015706 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15707 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015708 * {@link #TEXT_DIRECTION_LTR},
15709 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015710 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015711 */
15712 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070015713 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015714 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015715 resolveTextDirection();
15716 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015717 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015718 }
15719
15720 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015721 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
15722 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015723 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015724 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015725 // Reset any previous text direction resolution
15726 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
15727
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015728 if (hasRtlSupport()) {
15729 // Set resolved text direction flag depending on text direction flag
15730 final int textDirection = getTextDirection();
15731 switch(textDirection) {
15732 case TEXT_DIRECTION_INHERIT:
15733 if (canResolveTextDirection()) {
15734 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015735
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015736 // Set current resolved direction to the same value as the parent's one
15737 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
15738 switch (parentResolvedDirection) {
15739 case TEXT_DIRECTION_FIRST_STRONG:
15740 case TEXT_DIRECTION_ANY_RTL:
15741 case TEXT_DIRECTION_LTR:
15742 case TEXT_DIRECTION_RTL:
15743 case TEXT_DIRECTION_LOCALE:
15744 mPrivateFlags2 |=
15745 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15746 break;
15747 default:
15748 // Default resolved direction is "first strong" heuristic
15749 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
15750 }
15751 } else {
15752 // We cannot do the resolution if there is no parent, so use the default one
15753 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015754 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015755 break;
15756 case TEXT_DIRECTION_FIRST_STRONG:
15757 case TEXT_DIRECTION_ANY_RTL:
15758 case TEXT_DIRECTION_LTR:
15759 case TEXT_DIRECTION_RTL:
15760 case TEXT_DIRECTION_LOCALE:
15761 // Resolved direction is the same as text direction
15762 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15763 break;
15764 default:
15765 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015766 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015767 }
15768 } else {
15769 // Default resolved direction is "first strong" heuristic
15770 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015771 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015772
15773 // Set to resolved
15774 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015775 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015776 }
15777
15778 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015779 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015780 * resolution should override this method.
15781 *
15782 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015783 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015784 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015785 }
15786
15787 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015788 * Check if text direction resolution can be done.
15789 *
15790 * @return true if text direction resolution can be done otherwise return false.
15791 */
15792 public boolean canResolveTextDirection() {
15793 switch (getTextDirection()) {
15794 case TEXT_DIRECTION_INHERIT:
15795 return (mParent != null) && (mParent instanceof ViewGroup);
15796 default:
15797 return true;
15798 }
15799 }
15800
15801 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015802 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015803 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015804 * reset is done.
15805 */
15806 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015807 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015808 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015809 }
15810
15811 /**
15812 * Called when text direction is reset. Subclasses that care about text direction reset should
15813 * override this method and do a reset of the text direction of their children. The default
15814 * implementation does nothing.
15815 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015816 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015817 }
15818
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015819 /**
15820 * Return the value specifying the text alignment or policy that was set with
15821 * {@link #setTextAlignment(int)}.
15822 *
15823 * @return the defined text alignment. It can be one of:
15824 *
15825 * {@link #TEXT_ALIGNMENT_INHERIT},
15826 * {@link #TEXT_ALIGNMENT_GRAVITY},
15827 * {@link #TEXT_ALIGNMENT_CENTER},
15828 * {@link #TEXT_ALIGNMENT_TEXT_START},
15829 * {@link #TEXT_ALIGNMENT_TEXT_END},
15830 * {@link #TEXT_ALIGNMENT_VIEW_START},
15831 * {@link #TEXT_ALIGNMENT_VIEW_END}
15832 */
15833 @ViewDebug.ExportedProperty(category = "text", mapping = {
15834 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15835 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15836 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15837 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15838 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15839 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15840 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15841 })
15842 public int getTextAlignment() {
15843 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
15844 }
15845
15846 /**
15847 * Set the text alignment.
15848 *
15849 * @param textAlignment The text alignment to set. Should be one of
15850 *
15851 * {@link #TEXT_ALIGNMENT_INHERIT},
15852 * {@link #TEXT_ALIGNMENT_GRAVITY},
15853 * {@link #TEXT_ALIGNMENT_CENTER},
15854 * {@link #TEXT_ALIGNMENT_TEXT_START},
15855 * {@link #TEXT_ALIGNMENT_TEXT_END},
15856 * {@link #TEXT_ALIGNMENT_VIEW_START},
15857 * {@link #TEXT_ALIGNMENT_VIEW_END}
15858 *
15859 * @attr ref android.R.styleable#View_textAlignment
15860 */
15861 public void setTextAlignment(int textAlignment) {
15862 if (textAlignment != getTextAlignment()) {
15863 // Reset the current and resolved text alignment
15864 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
15865 resetResolvedTextAlignment();
15866 // Set the new text alignment
15867 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
15868 // Refresh
15869 requestLayout();
15870 invalidate(true);
15871 }
15872 }
15873
15874 /**
15875 * Return the resolved text alignment.
15876 *
15877 * The resolved text alignment. This needs resolution if the value is
15878 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
15879 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
15880 *
15881 * @return the resolved text alignment. Returns one of:
15882 *
15883 * {@link #TEXT_ALIGNMENT_GRAVITY},
15884 * {@link #TEXT_ALIGNMENT_CENTER},
15885 * {@link #TEXT_ALIGNMENT_TEXT_START},
15886 * {@link #TEXT_ALIGNMENT_TEXT_END},
15887 * {@link #TEXT_ALIGNMENT_VIEW_START},
15888 * {@link #TEXT_ALIGNMENT_VIEW_END}
15889 */
15890 @ViewDebug.ExportedProperty(category = "text", mapping = {
15891 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
15892 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
15893 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
15894 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
15895 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
15896 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
15897 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
15898 })
15899 public int getResolvedTextAlignment() {
15900 // If text alignment is not resolved, then resolve it
15901 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
15902 resolveTextAlignment();
15903 }
15904 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
15905 }
15906
15907 /**
15908 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
15909 * resolution is done.
15910 */
15911 public void resolveTextAlignment() {
15912 // Reset any previous text alignment resolution
15913 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
15914
15915 if (hasRtlSupport()) {
15916 // Set resolved text alignment flag depending on text alignment flag
15917 final int textAlignment = getTextAlignment();
15918 switch (textAlignment) {
15919 case TEXT_ALIGNMENT_INHERIT:
15920 // Check if we can resolve the text alignment
15921 if (canResolveLayoutDirection() && mParent instanceof View) {
15922 View view = (View) mParent;
15923
15924 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
15925 switch (parentResolvedTextAlignment) {
15926 case TEXT_ALIGNMENT_GRAVITY:
15927 case TEXT_ALIGNMENT_TEXT_START:
15928 case TEXT_ALIGNMENT_TEXT_END:
15929 case TEXT_ALIGNMENT_CENTER:
15930 case TEXT_ALIGNMENT_VIEW_START:
15931 case TEXT_ALIGNMENT_VIEW_END:
15932 // Resolved text alignment is the same as the parent resolved
15933 // text alignment
15934 mPrivateFlags2 |=
15935 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
15936 break;
15937 default:
15938 // Use default resolved text alignment
15939 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15940 }
15941 }
15942 else {
15943 // We cannot do the resolution if there is no parent so use the default
15944 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15945 }
15946 break;
15947 case TEXT_ALIGNMENT_GRAVITY:
15948 case TEXT_ALIGNMENT_TEXT_START:
15949 case TEXT_ALIGNMENT_TEXT_END:
15950 case TEXT_ALIGNMENT_CENTER:
15951 case TEXT_ALIGNMENT_VIEW_START:
15952 case TEXT_ALIGNMENT_VIEW_END:
15953 // Resolved text alignment is the same as text alignment
15954 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
15955 break;
15956 default:
15957 // Use default resolved text alignment
15958 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15959 }
15960 } else {
15961 // Use default resolved text alignment
15962 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
15963 }
15964
15965 // Set the resolved
15966 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
15967 onResolvedTextAlignmentChanged();
15968 }
15969
15970 /**
15971 * Check if text alignment resolution can be done.
15972 *
15973 * @return true if text alignment resolution can be done otherwise return false.
15974 */
15975 public boolean canResolveTextAlignment() {
15976 switch (getTextAlignment()) {
15977 case TEXT_DIRECTION_INHERIT:
15978 return (mParent != null);
15979 default:
15980 return true;
15981 }
15982 }
15983
15984 /**
15985 * Called when text alignment has been resolved. Subclasses that care about text alignment
15986 * resolution should override this method.
15987 *
15988 * The default implementation does nothing.
15989 */
15990 public void onResolvedTextAlignmentChanged() {
15991 }
15992
15993 /**
15994 * Reset resolved text alignment. Text alignment can be resolved with a call to
15995 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
15996 * reset is done.
15997 */
15998 public void resetResolvedTextAlignment() {
15999 // Reset any previous text alignment resolution
16000 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16001 onResolvedTextAlignmentReset();
16002 }
16003
16004 /**
16005 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16006 * override this method and do a reset of the text alignment of their children. The default
16007 * implementation does nothing.
16008 */
16009 public void onResolvedTextAlignmentReset() {
16010 }
16011
Chet Haaseb39f0512011-05-24 14:36:40 -070016012 //
16013 // Properties
16014 //
16015 /**
16016 * A Property wrapper around the <code>alpha</code> functionality handled by the
16017 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16018 */
Chet Haased47f1532011-12-16 11:18:52 -080016019 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016020 @Override
16021 public void setValue(View object, float value) {
16022 object.setAlpha(value);
16023 }
16024
16025 @Override
16026 public Float get(View object) {
16027 return object.getAlpha();
16028 }
16029 };
16030
16031 /**
16032 * A Property wrapper around the <code>translationX</code> functionality handled by the
16033 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16034 */
Chet Haased47f1532011-12-16 11:18:52 -080016035 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016036 @Override
16037 public void setValue(View object, float value) {
16038 object.setTranslationX(value);
16039 }
16040
16041 @Override
16042 public Float get(View object) {
16043 return object.getTranslationX();
16044 }
16045 };
16046
16047 /**
16048 * A Property wrapper around the <code>translationY</code> functionality handled by the
16049 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16050 */
Chet Haased47f1532011-12-16 11:18:52 -080016051 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016052 @Override
16053 public void setValue(View object, float value) {
16054 object.setTranslationY(value);
16055 }
16056
16057 @Override
16058 public Float get(View object) {
16059 return object.getTranslationY();
16060 }
16061 };
16062
16063 /**
16064 * A Property wrapper around the <code>x</code> functionality handled by the
16065 * {@link View#setX(float)} and {@link View#getX()} methods.
16066 */
Chet Haased47f1532011-12-16 11:18:52 -080016067 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016068 @Override
16069 public void setValue(View object, float value) {
16070 object.setX(value);
16071 }
16072
16073 @Override
16074 public Float get(View object) {
16075 return object.getX();
16076 }
16077 };
16078
16079 /**
16080 * A Property wrapper around the <code>y</code> functionality handled by the
16081 * {@link View#setY(float)} and {@link View#getY()} methods.
16082 */
Chet Haased47f1532011-12-16 11:18:52 -080016083 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016084 @Override
16085 public void setValue(View object, float value) {
16086 object.setY(value);
16087 }
16088
16089 @Override
16090 public Float get(View object) {
16091 return object.getY();
16092 }
16093 };
16094
16095 /**
16096 * A Property wrapper around the <code>rotation</code> functionality handled by the
16097 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16098 */
Chet Haased47f1532011-12-16 11:18:52 -080016099 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016100 @Override
16101 public void setValue(View object, float value) {
16102 object.setRotation(value);
16103 }
16104
16105 @Override
16106 public Float get(View object) {
16107 return object.getRotation();
16108 }
16109 };
16110
16111 /**
16112 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16113 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16114 */
Chet Haased47f1532011-12-16 11:18:52 -080016115 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016116 @Override
16117 public void setValue(View object, float value) {
16118 object.setRotationX(value);
16119 }
16120
16121 @Override
16122 public Float get(View object) {
16123 return object.getRotationX();
16124 }
16125 };
16126
16127 /**
16128 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16129 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16130 */
Chet Haased47f1532011-12-16 11:18:52 -080016131 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016132 @Override
16133 public void setValue(View object, float value) {
16134 object.setRotationY(value);
16135 }
16136
16137 @Override
16138 public Float get(View object) {
16139 return object.getRotationY();
16140 }
16141 };
16142
16143 /**
16144 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16145 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16146 */
Chet Haased47f1532011-12-16 11:18:52 -080016147 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016148 @Override
16149 public void setValue(View object, float value) {
16150 object.setScaleX(value);
16151 }
16152
16153 @Override
16154 public Float get(View object) {
16155 return object.getScaleX();
16156 }
16157 };
16158
16159 /**
16160 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16161 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16162 */
Chet Haased47f1532011-12-16 11:18:52 -080016163 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016164 @Override
16165 public void setValue(View object, float value) {
16166 object.setScaleY(value);
16167 }
16168
16169 @Override
16170 public Float get(View object) {
16171 return object.getScaleY();
16172 }
16173 };
16174
Jeff Brown33bbfd22011-02-24 20:55:35 -080016175 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016176 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16177 * Each MeasureSpec represents a requirement for either the width or the height.
16178 * A MeasureSpec is comprised of a size and a mode. There are three possible
16179 * modes:
16180 * <dl>
16181 * <dt>UNSPECIFIED</dt>
16182 * <dd>
16183 * The parent has not imposed any constraint on the child. It can be whatever size
16184 * it wants.
16185 * </dd>
16186 *
16187 * <dt>EXACTLY</dt>
16188 * <dd>
16189 * The parent has determined an exact size for the child. The child is going to be
16190 * given those bounds regardless of how big it wants to be.
16191 * </dd>
16192 *
16193 * <dt>AT_MOST</dt>
16194 * <dd>
16195 * The child can be as large as it wants up to the specified size.
16196 * </dd>
16197 * </dl>
16198 *
16199 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16200 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16201 */
16202 public static class MeasureSpec {
16203 private static final int MODE_SHIFT = 30;
16204 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16205
16206 /**
16207 * Measure specification mode: The parent has not imposed any constraint
16208 * on the child. It can be whatever size it wants.
16209 */
16210 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16211
16212 /**
16213 * Measure specification mode: The parent has determined an exact size
16214 * for the child. The child is going to be given those bounds regardless
16215 * of how big it wants to be.
16216 */
16217 public static final int EXACTLY = 1 << MODE_SHIFT;
16218
16219 /**
16220 * Measure specification mode: The child can be as large as it wants up
16221 * to the specified size.
16222 */
16223 public static final int AT_MOST = 2 << MODE_SHIFT;
16224
16225 /**
16226 * Creates a measure specification based on the supplied size and mode.
16227 *
16228 * The mode must always be one of the following:
16229 * <ul>
16230 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16231 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16232 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16233 * </ul>
16234 *
16235 * @param size the size of the measure specification
16236 * @param mode the mode of the measure specification
16237 * @return the measure specification based on size and mode
16238 */
16239 public static int makeMeasureSpec(int size, int mode) {
16240 return size + mode;
16241 }
16242
16243 /**
16244 * Extracts the mode from the supplied measure specification.
16245 *
16246 * @param measureSpec the measure specification to extract the mode from
16247 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16248 * {@link android.view.View.MeasureSpec#AT_MOST} or
16249 * {@link android.view.View.MeasureSpec#EXACTLY}
16250 */
16251 public static int getMode(int measureSpec) {
16252 return (measureSpec & MODE_MASK);
16253 }
16254
16255 /**
16256 * Extracts the size from the supplied measure specification.
16257 *
16258 * @param measureSpec the measure specification to extract the size from
16259 * @return the size in pixels defined in the supplied measure specification
16260 */
16261 public static int getSize(int measureSpec) {
16262 return (measureSpec & ~MODE_MASK);
16263 }
16264
16265 /**
16266 * Returns a String representation of the specified measure
16267 * specification.
16268 *
16269 * @param measureSpec the measure specification to convert to a String
16270 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16271 */
16272 public static String toString(int measureSpec) {
16273 int mode = getMode(measureSpec);
16274 int size = getSize(measureSpec);
16275
16276 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16277
16278 if (mode == UNSPECIFIED)
16279 sb.append("UNSPECIFIED ");
16280 else if (mode == EXACTLY)
16281 sb.append("EXACTLY ");
16282 else if (mode == AT_MOST)
16283 sb.append("AT_MOST ");
16284 else
16285 sb.append(mode).append(" ");
16286
16287 sb.append(size);
16288 return sb.toString();
16289 }
16290 }
16291
16292 class CheckForLongPress implements Runnable {
16293
16294 private int mOriginalWindowAttachCount;
16295
16296 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016297 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016298 && mOriginalWindowAttachCount == mWindowAttachCount) {
16299 if (performLongClick()) {
16300 mHasPerformedLongPress = true;
16301 }
16302 }
16303 }
16304
16305 public void rememberWindowAttachCount() {
16306 mOriginalWindowAttachCount = mWindowAttachCount;
16307 }
16308 }
Joe Malin32736f02011-01-19 16:14:20 -080016309
Adam Powelle14579b2009-12-16 18:39:52 -080016310 private final class CheckForTap implements Runnable {
16311 public void run() {
16312 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016313 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016314 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016315 }
16316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016317
Adam Powella35d7682010-03-12 14:48:13 -080016318 private final class PerformClick implements Runnable {
16319 public void run() {
16320 performClick();
16321 }
16322 }
16323
Dianne Hackborn63042d62011-01-26 18:56:29 -080016324 /** @hide */
16325 public void hackTurnOffWindowResizeAnim(boolean off) {
16326 mAttachInfo.mTurnOffWindowResizeAnim = off;
16327 }
Joe Malin32736f02011-01-19 16:14:20 -080016328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016329 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016330 * This method returns a ViewPropertyAnimator object, which can be used to animate
16331 * specific properties on this View.
16332 *
16333 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16334 */
16335 public ViewPropertyAnimator animate() {
16336 if (mAnimator == null) {
16337 mAnimator = new ViewPropertyAnimator(this);
16338 }
16339 return mAnimator;
16340 }
16341
16342 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016343 * Interface definition for a callback to be invoked when a key event is
16344 * dispatched to this view. The callback will be invoked before the key
16345 * event is given to the view.
16346 */
16347 public interface OnKeyListener {
16348 /**
16349 * Called when a key is dispatched to a view. This allows listeners to
16350 * get a chance to respond before the target view.
16351 *
16352 * @param v The view the key has been dispatched to.
16353 * @param keyCode The code for the physical key that was pressed
16354 * @param event The KeyEvent object containing full information about
16355 * the event.
16356 * @return True if the listener has consumed the event, false otherwise.
16357 */
16358 boolean onKey(View v, int keyCode, KeyEvent event);
16359 }
16360
16361 /**
16362 * Interface definition for a callback to be invoked when a touch event is
16363 * dispatched to this view. The callback will be invoked before the touch
16364 * event is given to the view.
16365 */
16366 public interface OnTouchListener {
16367 /**
16368 * Called when a touch event is dispatched to a view. This allows listeners to
16369 * get a chance to respond before the target view.
16370 *
16371 * @param v The view the touch event has been dispatched to.
16372 * @param event The MotionEvent object containing full information about
16373 * the event.
16374 * @return True if the listener has consumed the event, false otherwise.
16375 */
16376 boolean onTouch(View v, MotionEvent event);
16377 }
16378
16379 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016380 * Interface definition for a callback to be invoked when a hover event is
16381 * dispatched to this view. The callback will be invoked before the hover
16382 * event is given to the view.
16383 */
16384 public interface OnHoverListener {
16385 /**
16386 * Called when a hover event is dispatched to a view. This allows listeners to
16387 * get a chance to respond before the target view.
16388 *
16389 * @param v The view the hover event has been dispatched to.
16390 * @param event The MotionEvent object containing full information about
16391 * the event.
16392 * @return True if the listener has consumed the event, false otherwise.
16393 */
16394 boolean onHover(View v, MotionEvent event);
16395 }
16396
16397 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016398 * Interface definition for a callback to be invoked when a generic motion event is
16399 * dispatched to this view. The callback will be invoked before the generic motion
16400 * event is given to the view.
16401 */
16402 public interface OnGenericMotionListener {
16403 /**
16404 * Called when a generic motion event is dispatched to a view. This allows listeners to
16405 * get a chance to respond before the target view.
16406 *
16407 * @param v The view the generic motion event has been dispatched to.
16408 * @param event The MotionEvent object containing full information about
16409 * the event.
16410 * @return True if the listener has consumed the event, false otherwise.
16411 */
16412 boolean onGenericMotion(View v, MotionEvent event);
16413 }
16414
16415 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016416 * Interface definition for a callback to be invoked when a view has been clicked and held.
16417 */
16418 public interface OnLongClickListener {
16419 /**
16420 * Called when a view has been clicked and held.
16421 *
16422 * @param v The view that was clicked and held.
16423 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016424 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016425 */
16426 boolean onLongClick(View v);
16427 }
16428
16429 /**
Chris Tate32affef2010-10-18 15:29:21 -070016430 * Interface definition for a callback to be invoked when a drag is being dispatched
16431 * to this view. The callback will be invoked before the hosting view's own
16432 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16433 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016434 *
16435 * <div class="special reference">
16436 * <h3>Developer Guides</h3>
16437 * <p>For a guide to implementing drag and drop features, read the
16438 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16439 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016440 */
16441 public interface OnDragListener {
16442 /**
16443 * Called when a drag event is dispatched to a view. This allows listeners
16444 * to get a chance to override base View behavior.
16445 *
Joe Malin32736f02011-01-19 16:14:20 -080016446 * @param v The View that received the drag event.
16447 * @param event The {@link android.view.DragEvent} object for the drag event.
16448 * @return {@code true} if the drag event was handled successfully, or {@code false}
16449 * if the drag event was not handled. Note that {@code false} will trigger the View
16450 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070016451 */
16452 boolean onDrag(View v, DragEvent event);
16453 }
16454
16455 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016456 * Interface definition for a callback to be invoked when the focus state of
16457 * a view changed.
16458 */
16459 public interface OnFocusChangeListener {
16460 /**
16461 * Called when the focus state of a view has changed.
16462 *
16463 * @param v The view whose state has changed.
16464 * @param hasFocus The new focus state of v.
16465 */
16466 void onFocusChange(View v, boolean hasFocus);
16467 }
16468
16469 /**
16470 * Interface definition for a callback to be invoked when a view is clicked.
16471 */
16472 public interface OnClickListener {
16473 /**
16474 * Called when a view has been clicked.
16475 *
16476 * @param v The view that was clicked.
16477 */
16478 void onClick(View v);
16479 }
16480
16481 /**
16482 * Interface definition for a callback to be invoked when the context menu
16483 * for this view is being built.
16484 */
16485 public interface OnCreateContextMenuListener {
16486 /**
16487 * Called when the context menu for this view is being built. It is not
16488 * safe to hold onto the menu after this method returns.
16489 *
16490 * @param menu The context menu that is being built
16491 * @param v The view for which the context menu is being built
16492 * @param menuInfo Extra information about the item for which the
16493 * context menu should be shown. This information will vary
16494 * depending on the class of v.
16495 */
16496 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
16497 }
16498
Joe Onorato664644d2011-01-23 17:53:23 -080016499 /**
16500 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016501 * visibility. This reports <strong>global</strong> changes to the system UI
16502 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080016503 *
Philip Milne6c8ea062012-04-03 17:38:43 -070016504 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080016505 */
16506 public interface OnSystemUiVisibilityChangeListener {
16507 /**
16508 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070016509 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080016510 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040016511 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016512 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
16513 * <strong>global</strong> state of the UI visibility flags, not what your
16514 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080016515 */
16516 public void onSystemUiVisibilityChange(int visibility);
16517 }
16518
Adam Powell4afd62b2011-02-18 15:02:18 -080016519 /**
16520 * Interface definition for a callback to be invoked when this view is attached
16521 * or detached from its window.
16522 */
16523 public interface OnAttachStateChangeListener {
16524 /**
16525 * Called when the view is attached to a window.
16526 * @param v The view that was attached
16527 */
16528 public void onViewAttachedToWindow(View v);
16529 /**
16530 * Called when the view is detached from a window.
16531 * @param v The view that was detached
16532 */
16533 public void onViewDetachedFromWindow(View v);
16534 }
16535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016536 private final class UnsetPressedState implements Runnable {
16537 public void run() {
16538 setPressed(false);
16539 }
16540 }
16541
16542 /**
16543 * Base class for derived classes that want to save and restore their own
16544 * state in {@link android.view.View#onSaveInstanceState()}.
16545 */
16546 public static class BaseSavedState extends AbsSavedState {
16547 /**
16548 * Constructor used when reading from a parcel. Reads the state of the superclass.
16549 *
16550 * @param source
16551 */
16552 public BaseSavedState(Parcel source) {
16553 super(source);
16554 }
16555
16556 /**
16557 * Constructor called by derived classes when creating their SavedState objects
16558 *
16559 * @param superState The state of the superclass of this view
16560 */
16561 public BaseSavedState(Parcelable superState) {
16562 super(superState);
16563 }
16564
16565 public static final Parcelable.Creator<BaseSavedState> CREATOR =
16566 new Parcelable.Creator<BaseSavedState>() {
16567 public BaseSavedState createFromParcel(Parcel in) {
16568 return new BaseSavedState(in);
16569 }
16570
16571 public BaseSavedState[] newArray(int size) {
16572 return new BaseSavedState[size];
16573 }
16574 };
16575 }
16576
16577 /**
16578 * A set of information given to a view when it is attached to its parent
16579 * window.
16580 */
16581 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016582 interface Callbacks {
16583 void playSoundEffect(int effectId);
16584 boolean performHapticFeedback(int effectId, boolean always);
16585 }
16586
16587 /**
16588 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
16589 * to a Handler. This class contains the target (View) to invalidate and
16590 * the coordinates of the dirty rectangle.
16591 *
16592 * For performance purposes, this class also implements a pool of up to
16593 * POOL_LIMIT objects that get reused. This reduces memory allocations
16594 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016595 */
Romain Guyd928d682009-03-31 17:52:16 -070016596 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016597 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070016598 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
16599 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070016600 public InvalidateInfo newInstance() {
16601 return new InvalidateInfo();
16602 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016603
Romain Guyd928d682009-03-31 17:52:16 -070016604 public void onAcquired(InvalidateInfo element) {
16605 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016606
Romain Guyd928d682009-03-31 17:52:16 -070016607 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070016608 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070016609 }
16610 }, POOL_LIMIT)
16611 );
16612
16613 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016614 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016615
16616 View target;
16617
16618 int left;
16619 int top;
16620 int right;
16621 int bottom;
16622
Romain Guyd928d682009-03-31 17:52:16 -070016623 public void setNextPoolable(InvalidateInfo element) {
16624 mNext = element;
16625 }
16626
16627 public InvalidateInfo getNextPoolable() {
16628 return mNext;
16629 }
16630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016631 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070016632 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016633 }
16634
16635 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070016636 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016637 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016638
16639 public boolean isPooled() {
16640 return mIsPooled;
16641 }
16642
16643 public void setPooled(boolean isPooled) {
16644 mIsPooled = isPooled;
16645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016646 }
16647
16648 final IWindowSession mSession;
16649
16650 final IWindow mWindow;
16651
16652 final IBinder mWindowToken;
16653
16654 final Callbacks mRootCallbacks;
16655
Romain Guy59a12ca2011-06-09 17:48:21 -070016656 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080016657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016658 /**
16659 * The top view of the hierarchy.
16660 */
16661 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070016662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016663 IBinder mPanelParentWindowToken;
16664 Surface mSurface;
16665
Romain Guyb051e892010-09-28 19:09:36 -070016666 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080016667 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070016668 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080016669
Romain Guy7e4e5612012-03-05 14:37:29 -080016670 boolean mScreenOn;
16671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016672 /**
Romain Guy8506ab42009-06-11 17:35:47 -070016673 * Scale factor used by the compatibility mode
16674 */
16675 float mApplicationScale;
16676
16677 /**
16678 * Indicates whether the application is in compatibility mode
16679 */
16680 boolean mScalingRequired;
16681
16682 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016683 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080016684 */
16685 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080016686
Dianne Hackborn63042d62011-01-26 18:56:29 -080016687 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016688 * Left position of this view's window
16689 */
16690 int mWindowLeft;
16691
16692 /**
16693 * Top position of this view's window
16694 */
16695 int mWindowTop;
16696
16697 /**
Adam Powell26153a32010-11-08 15:22:27 -080016698 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070016699 */
Adam Powell26153a32010-11-08 15:22:27 -080016700 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070016701
16702 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016703 * For windows that are full-screen but using insets to layout inside
16704 * of the screen decorations, these are the current insets for the
16705 * content of the window.
16706 */
16707 final Rect mContentInsets = new Rect();
16708
16709 /**
16710 * For windows that are full-screen but using insets to layout inside
16711 * of the screen decorations, these are the current insets for the
16712 * actual visible parts of the window.
16713 */
16714 final Rect mVisibleInsets = new Rect();
16715
16716 /**
16717 * The internal insets given by this window. This value is
16718 * supplied by the client (through
16719 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
16720 * be given to the window manager when changed to be used in laying
16721 * out windows behind it.
16722 */
16723 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
16724 = new ViewTreeObserver.InternalInsetsInfo();
16725
16726 /**
16727 * All views in the window's hierarchy that serve as scroll containers,
16728 * used to determine if the window can be resized or must be panned
16729 * to adjust for a soft input area.
16730 */
16731 final ArrayList<View> mScrollContainers = new ArrayList<View>();
16732
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070016733 final KeyEvent.DispatcherState mKeyDispatchState
16734 = new KeyEvent.DispatcherState();
16735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016736 /**
16737 * Indicates whether the view's window currently has the focus.
16738 */
16739 boolean mHasWindowFocus;
16740
16741 /**
16742 * The current visibility of the window.
16743 */
16744 int mWindowVisibility;
16745
16746 /**
16747 * Indicates the time at which drawing started to occur.
16748 */
16749 long mDrawingTime;
16750
16751 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070016752 * Indicates whether or not ignoring the DIRTY_MASK flags.
16753 */
16754 boolean mIgnoreDirtyState;
16755
16756 /**
Romain Guy02ccac62011-06-24 13:20:23 -070016757 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
16758 * to avoid clearing that flag prematurely.
16759 */
16760 boolean mSetIgnoreDirtyState = false;
16761
16762 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016763 * Indicates whether the view's window is currently in touch mode.
16764 */
16765 boolean mInTouchMode;
16766
16767 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016768 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016769 * the next time it performs a traversal
16770 */
16771 boolean mRecomputeGlobalAttributes;
16772
16773 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016774 * Always report new attributes at next traversal.
16775 */
16776 boolean mForceReportNewAttributes;
16777
16778 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016779 * Set during a traveral if any views want to keep the screen on.
16780 */
16781 boolean mKeepScreenOn;
16782
16783 /**
Joe Onorato664644d2011-01-23 17:53:23 -080016784 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
16785 */
16786 int mSystemUiVisibility;
16787
16788 /**
16789 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
16790 * attached.
16791 */
16792 boolean mHasSystemUiListeners;
16793
16794 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016795 * Set if the visibility of any views has changed.
16796 */
16797 boolean mViewVisibilityChanged;
16798
16799 /**
16800 * Set to true if a view has been scrolled.
16801 */
16802 boolean mViewScrollChanged;
16803
16804 /**
16805 * Global to the view hierarchy used as a temporary for dealing with
16806 * x/y points in the transparent region computations.
16807 */
16808 final int[] mTransparentLocation = new int[2];
16809
16810 /**
16811 * Global to the view hierarchy used as a temporary for dealing with
16812 * x/y points in the ViewGroup.invalidateChild implementation.
16813 */
16814 final int[] mInvalidateChildLocation = new int[2];
16815
Chet Haasec3aa3612010-06-17 08:50:37 -070016816
16817 /**
16818 * Global to the view hierarchy used as a temporary for dealing with
16819 * x/y location when view is transformed.
16820 */
16821 final float[] mTmpTransformLocation = new float[2];
16822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016823 /**
16824 * The view tree observer used to dispatch global events like
16825 * layout, pre-draw, touch mode change, etc.
16826 */
16827 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
16828
16829 /**
16830 * A Canvas used by the view hierarchy to perform bitmap caching.
16831 */
16832 Canvas mCanvas;
16833
16834 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080016835 * The view root impl.
16836 */
16837 final ViewRootImpl mViewRootImpl;
16838
16839 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016840 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016841 * handler can be used to pump events in the UI events queue.
16842 */
16843 final Handler mHandler;
16844
16845 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016846 * Temporary for use in computing invalidate rectangles while
16847 * calling up the hierarchy.
16848 */
16849 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070016850
16851 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070016852 * Temporary for use in computing hit areas with transformed views
16853 */
16854 final RectF mTmpTransformRect = new RectF();
16855
16856 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070016857 * Temporary list for use in collecting focusable descendents of a view.
16858 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070016859 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070016860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016861 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016862 * The id of the window for accessibility purposes.
16863 */
16864 int mAccessibilityWindowId = View.NO_ID;
16865
16866 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070016867 * Whether to ingore not exposed for accessibility Views when
16868 * reporting the view tree to accessibility services.
16869 */
16870 boolean mIncludeNotImportantViews;
16871
16872 /**
16873 * The drawable for highlighting accessibility focus.
16874 */
16875 Drawable mAccessibilityFocusDrawable;
16876
16877 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016878 * Creates a new set of attachment information with the specified
16879 * events handler and thread.
16880 *
16881 * @param handler the events handler the view must use
16882 */
16883 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080016884 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016885 mSession = session;
16886 mWindow = window;
16887 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080016888 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016889 mHandler = handler;
16890 mRootCallbacks = effectPlayer;
16891 }
16892 }
16893
16894 /**
16895 * <p>ScrollabilityCache holds various fields used by a View when scrolling
16896 * is supported. This avoids keeping too many unused fields in most
16897 * instances of View.</p>
16898 */
Mike Cleronf116bf82009-09-27 19:14:12 -070016899 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080016900
Mike Cleronf116bf82009-09-27 19:14:12 -070016901 /**
16902 * Scrollbars are not visible
16903 */
16904 public static final int OFF = 0;
16905
16906 /**
16907 * Scrollbars are visible
16908 */
16909 public static final int ON = 1;
16910
16911 /**
16912 * Scrollbars are fading away
16913 */
16914 public static final int FADING = 2;
16915
16916 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080016917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016918 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070016919 public int scrollBarDefaultDelayBeforeFade;
16920 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016921
16922 public int scrollBarSize;
16923 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070016924 public float[] interpolatorValues;
16925 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016926
16927 public final Paint paint;
16928 public final Matrix matrix;
16929 public Shader shader;
16930
Mike Cleronf116bf82009-09-27 19:14:12 -070016931 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
16932
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016933 private static final float[] OPAQUE = { 255 };
16934 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080016935
Mike Cleronf116bf82009-09-27 19:14:12 -070016936 /**
16937 * When fading should start. This time moves into the future every time
16938 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
16939 */
16940 public long fadeStartTime;
16941
16942
16943 /**
16944 * The current state of the scrollbars: ON, OFF, or FADING
16945 */
16946 public int state = OFF;
16947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016948 private int mLastColor;
16949
Mike Cleronf116bf82009-09-27 19:14:12 -070016950 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016951 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
16952 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070016953 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
16954 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016955
16956 paint = new Paint();
16957 matrix = new Matrix();
16958 // use use a height of 1, and then wack the matrix each time we
16959 // actually use it.
16960 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070016961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016962 paint.setShader(shader);
16963 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070016964 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016965 }
Romain Guy8506ab42009-06-11 17:35:47 -070016966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016967 public void setFadeColor(int color) {
16968 if (color != 0 && color != mLastColor) {
16969 mLastColor = color;
16970 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070016971
Romain Guye55e1a72009-08-27 10:42:26 -070016972 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
16973 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070016974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016975 paint.setShader(shader);
16976 // Restore the default transfer mode (src_over)
16977 paint.setXfermode(null);
16978 }
16979 }
Joe Malin32736f02011-01-19 16:14:20 -080016980
Mike Cleronf116bf82009-09-27 19:14:12 -070016981 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070016982 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070016983 if (now >= fadeStartTime) {
16984
16985 // the animation fades the scrollbars out by changing
16986 // the opacity (alpha) from fully opaque to fully
16987 // transparent
16988 int nextFrame = (int) now;
16989 int framesCount = 0;
16990
16991 Interpolator interpolator = scrollBarInterpolator;
16992
16993 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016994 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070016995
16996 // End transparent
16997 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016998 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070016999
17000 state = FADING;
17001
17002 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017003 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017004 }
17005 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017006 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017007
Svetoslav Ganova0156172011-06-26 17:55:44 -070017008 /**
17009 * Resuable callback for sending
17010 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17011 */
17012 private class SendViewScrolledAccessibilityEvent implements Runnable {
17013 public volatile boolean mIsPending;
17014
17015 public void run() {
17016 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17017 mIsPending = false;
17018 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017019 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017020
17021 /**
17022 * <p>
17023 * This class represents a delegate that can be registered in a {@link View}
17024 * to enhance accessibility support via composition rather via inheritance.
17025 * It is specifically targeted to widget developers that extend basic View
17026 * classes i.e. classes in package android.view, that would like their
17027 * applications to be backwards compatible.
17028 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017029 * <div class="special reference">
17030 * <h3>Developer Guides</h3>
17031 * <p>For more information about making applications accessible, read the
17032 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17033 * developer guide.</p>
17034 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017035 * <p>
17036 * A scenario in which a developer would like to use an accessibility delegate
17037 * is overriding a method introduced in a later API version then the minimal API
17038 * version supported by the application. For example, the method
17039 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17040 * in API version 4 when the accessibility APIs were first introduced. If a
17041 * developer would like his application to run on API version 4 devices (assuming
17042 * all other APIs used by the application are version 4 or lower) and take advantage
17043 * of this method, instead of overriding the method which would break the application's
17044 * backwards compatibility, he can override the corresponding method in this
17045 * delegate and register the delegate in the target View if the API version of
17046 * the system is high enough i.e. the API version is same or higher to the API
17047 * version that introduced
17048 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17049 * </p>
17050 * <p>
17051 * Here is an example implementation:
17052 * </p>
17053 * <code><pre><p>
17054 * if (Build.VERSION.SDK_INT >= 14) {
17055 * // If the API version is equal of higher than the version in
17056 * // which onInitializeAccessibilityNodeInfo was introduced we
17057 * // register a delegate with a customized implementation.
17058 * View view = findViewById(R.id.view_id);
17059 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17060 * public void onInitializeAccessibilityNodeInfo(View host,
17061 * AccessibilityNodeInfo info) {
17062 * // Let the default implementation populate the info.
17063 * super.onInitializeAccessibilityNodeInfo(host, info);
17064 * // Set some other information.
17065 * info.setEnabled(host.isEnabled());
17066 * }
17067 * });
17068 * }
17069 * </code></pre></p>
17070 * <p>
17071 * This delegate contains methods that correspond to the accessibility methods
17072 * in View. If a delegate has been specified the implementation in View hands
17073 * off handling to the corresponding method in this delegate. The default
17074 * implementation the delegate methods behaves exactly as the corresponding
17075 * method in View for the case of no accessibility delegate been set. Hence,
17076 * to customize the behavior of a View method, clients can override only the
17077 * corresponding delegate method without altering the behavior of the rest
17078 * accessibility related methods of the host view.
17079 * </p>
17080 */
17081 public static class AccessibilityDelegate {
17082
17083 /**
17084 * Sends an accessibility event of the given type. If accessibility is not
17085 * enabled this method has no effect.
17086 * <p>
17087 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17088 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17089 * been set.
17090 * </p>
17091 *
17092 * @param host The View hosting the delegate.
17093 * @param eventType The type of the event to send.
17094 *
17095 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17096 */
17097 public void sendAccessibilityEvent(View host, int eventType) {
17098 host.sendAccessibilityEventInternal(eventType);
17099 }
17100
17101 /**
17102 * Sends an accessibility event. This method behaves exactly as
17103 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17104 * empty {@link AccessibilityEvent} and does not perform a check whether
17105 * accessibility is enabled.
17106 * <p>
17107 * The default implementation behaves as
17108 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17109 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17110 * the case of no accessibility delegate been set.
17111 * </p>
17112 *
17113 * @param host The View hosting the delegate.
17114 * @param event The event to send.
17115 *
17116 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17117 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17118 */
17119 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17120 host.sendAccessibilityEventUncheckedInternal(event);
17121 }
17122
17123 /**
17124 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17125 * to its children for adding their text content to the event.
17126 * <p>
17127 * The default implementation behaves as
17128 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17129 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17130 * the case of no accessibility delegate been set.
17131 * </p>
17132 *
17133 * @param host The View hosting the delegate.
17134 * @param event The event.
17135 * @return True if the event population was completed.
17136 *
17137 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17138 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17139 */
17140 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17141 return host.dispatchPopulateAccessibilityEventInternal(event);
17142 }
17143
17144 /**
17145 * Gives a chance to the host View to populate the accessibility event with its
17146 * text content.
17147 * <p>
17148 * The default implementation behaves as
17149 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17150 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17151 * the case of no accessibility delegate been set.
17152 * </p>
17153 *
17154 * @param host The View hosting the delegate.
17155 * @param event The accessibility event which to populate.
17156 *
17157 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17158 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17159 */
17160 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17161 host.onPopulateAccessibilityEventInternal(event);
17162 }
17163
17164 /**
17165 * Initializes an {@link AccessibilityEvent} with information about the
17166 * the host View which is the event source.
17167 * <p>
17168 * The default implementation behaves as
17169 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17170 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17171 * the case of no accessibility delegate been set.
17172 * </p>
17173 *
17174 * @param host The View hosting the delegate.
17175 * @param event The event to initialize.
17176 *
17177 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17178 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17179 */
17180 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17181 host.onInitializeAccessibilityEventInternal(event);
17182 }
17183
17184 /**
17185 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17186 * <p>
17187 * The default implementation behaves as
17188 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17189 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17190 * the case of no accessibility delegate been set.
17191 * </p>
17192 *
17193 * @param host The View hosting the delegate.
17194 * @param info The instance to initialize.
17195 *
17196 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17197 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17198 */
17199 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17200 host.onInitializeAccessibilityNodeInfoInternal(info);
17201 }
17202
17203 /**
17204 * Called when a child of the host View has requested sending an
17205 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17206 * to augment the event.
17207 * <p>
17208 * The default implementation behaves as
17209 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17210 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17211 * the case of no accessibility delegate been set.
17212 * </p>
17213 *
17214 * @param host The View hosting the delegate.
17215 * @param child The child which requests sending the event.
17216 * @param event The event to be sent.
17217 * @return True if the event should be sent
17218 *
17219 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17220 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17221 */
17222 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17223 AccessibilityEvent event) {
17224 return host.onRequestSendAccessibilityEventInternal(child, event);
17225 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017226
17227 /**
17228 * Gets the provider for managing a virtual view hierarchy rooted at this View
17229 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17230 * that explore the window content.
17231 * <p>
17232 * The default implementation behaves as
17233 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17234 * the case of no accessibility delegate been set.
17235 * </p>
17236 *
17237 * @return The provider.
17238 *
17239 * @see AccessibilityNodeProvider
17240 */
17241 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17242 return null;
17243 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017244 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017245}