blob: 0cd449f35ff013f41ea25c448690b6cf7abb3e5f [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.*;
74
Doug Feltcb3791202011-07-07 11:57:48 -070075import com.android.internal.R;
76import com.android.internal.util.Predicate;
77import com.android.internal.view.menu.MenuBuilder;
78
Christopher Tatea0374192010-10-05 13:06:41 -070079import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070080import java.lang.reflect.InvocationTargetException;
81import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import java.util.ArrayList;
83import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070084import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080085import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
87/**
88 * <p>
89 * This class represents the basic building block for user interface components. A View
90 * occupies a rectangular area on the screen and is responsible for drawing and
91 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070092 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
94 * are invisible containers that hold other Views (or other ViewGroups) and define
95 * their layout properties.
96 * </p>
97 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -070098 * <div class="special reference">
99 * <h3>Developer Guides</h3>
100 * <p>For information about using this class to develop your application's user interface,
101 * 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 -0800102 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700103 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 * <a name="Using"></a>
105 * <h3>Using Views</h3>
106 * <p>
107 * All of the views in a window are arranged in a single tree. You can add views
108 * either from code or by specifying a tree of views in one or more XML layout
109 * files. There are many specialized subclasses of views that act as controls or
110 * are capable of displaying text, images, or other content.
111 * </p>
112 * <p>
113 * Once you have created a tree of views, there are typically a few types of
114 * common operations you may wish to perform:
115 * <ul>
116 * <li><strong>Set properties:</strong> for example setting the text of a
117 * {@link android.widget.TextView}. The available properties and the methods
118 * that set them will vary among the different subclasses of views. Note that
119 * properties that are known at build time can be set in the XML layout
120 * files.</li>
121 * <li><strong>Set focus:</strong> The framework will handled moving focus in
122 * response to user input. To force focus to a specific view, call
123 * {@link #requestFocus}.</li>
124 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
125 * that will be notified when something interesting happens to the view. For
126 * example, all views will let you set a listener to be notified when the view
127 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700128 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
129 * Other view subclasses offer more specialized listeners. For example, a Button
130 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700132 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 * </ul>
134 * </p>
135 * <p><em>
136 * Note: The Android framework is responsible for measuring, laying out and
137 * drawing views. You should not call methods that perform these actions on
138 * views yourself unless you are actually implementing a
139 * {@link android.view.ViewGroup}.
140 * </em></p>
141 *
142 * <a name="Lifecycle"></a>
143 * <h3>Implementing a Custom View</h3>
144 *
145 * <p>
146 * To implement a custom view, you will usually begin by providing overrides for
147 * some of the standard methods that the framework calls on all views. You do
148 * not need to override all of these methods. In fact, you can start by just
149 * overriding {@link #onDraw(android.graphics.Canvas)}.
150 * <table border="2" width="85%" align="center" cellpadding="5">
151 * <thead>
152 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
153 * </thead>
154 *
155 * <tbody>
156 * <tr>
157 * <td rowspan="2">Creation</td>
158 * <td>Constructors</td>
159 * <td>There is a form of the constructor that are called when the view
160 * is created from code and a form that is called when the view is
161 * inflated from a layout file. The second form should parse and apply
162 * any attributes defined in the layout file.
163 * </td>
164 * </tr>
165 * <tr>
166 * <td><code>{@link #onFinishInflate()}</code></td>
167 * <td>Called after a view and all of its children has been inflated
168 * from XML.</td>
169 * </tr>
170 *
171 * <tr>
172 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700173 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 * <td>Called to determine the size requirements for this view and all
175 * of its children.
176 * </td>
177 * </tr>
178 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700179 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 * <td>Called when this view should assign a size and position to all
181 * of its children.
182 * </td>
183 * </tr>
184 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700185 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 * <td>Called when the size of this view has changed.
187 * </td>
188 * </tr>
189 *
190 * <tr>
191 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700192 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * <td>Called when the view should render its content.
194 * </td>
195 * </tr>
196 *
197 * <tr>
198 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700199 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * <td>Called when a new key event occurs.
201 * </td>
202 * </tr>
203 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700204 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 * <td>Called when a key up event occurs.
206 * </td>
207 * </tr>
208 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700209 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 * <td>Called when a trackball motion event occurs.
211 * </td>
212 * </tr>
213 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700214 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 * <td>Called when a touch screen motion event occurs.
216 * </td>
217 * </tr>
218 *
219 * <tr>
220 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700221 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * <td>Called when the view gains or loses focus.
223 * </td>
224 * </tr>
225 *
226 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700227 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 * <td>Called when the window containing the view gains or loses focus.
229 * </td>
230 * </tr>
231 *
232 * <tr>
233 * <td rowspan="3">Attaching</td>
234 * <td><code>{@link #onAttachedToWindow()}</code></td>
235 * <td>Called when the view is attached to a window.
236 * </td>
237 * </tr>
238 *
239 * <tr>
240 * <td><code>{@link #onDetachedFromWindow}</code></td>
241 * <td>Called when the view is detached from its window.
242 * </td>
243 * </tr>
244 *
245 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700246 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 * <td>Called when the visibility of the window containing the view
248 * has changed.
249 * </td>
250 * </tr>
251 * </tbody>
252 *
253 * </table>
254 * </p>
255 *
256 * <a name="IDs"></a>
257 * <h3>IDs</h3>
258 * Views may have an integer id associated with them. These ids are typically
259 * assigned in the layout XML files, and are used to find specific views within
260 * the view tree. A common pattern is to:
261 * <ul>
262 * <li>Define a Button in the layout file and assign it a unique ID.
263 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700264 * &lt;Button
265 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 * android:layout_width="wrap_content"
267 * android:layout_height="wrap_content"
268 * android:text="@string/my_button_text"/&gt;
269 * </pre></li>
270 * <li>From the onCreate method of an Activity, find the Button
271 * <pre class="prettyprint">
272 * Button myButton = (Button) findViewById(R.id.my_button);
273 * </pre></li>
274 * </ul>
275 * <p>
276 * View IDs need not be unique throughout the tree, but it is good practice to
277 * ensure that they are at least unique within the part of the tree you are
278 * searching.
279 * </p>
280 *
281 * <a name="Position"></a>
282 * <h3>Position</h3>
283 * <p>
284 * The geometry of a view is that of a rectangle. A view has a location,
285 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
286 * two dimensions, expressed as a width and a height. The unit for location
287 * and dimensions is the pixel.
288 * </p>
289 *
290 * <p>
291 * It is possible to retrieve the location of a view by invoking the methods
292 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
293 * coordinate of the rectangle representing the view. The latter returns the
294 * top, or Y, coordinate of the rectangle representing the view. These methods
295 * both return the location of the view relative to its parent. For instance,
296 * when getLeft() returns 20, that means the view is located 20 pixels to the
297 * right of the left edge of its direct parent.
298 * </p>
299 *
300 * <p>
301 * In addition, several convenience methods are offered to avoid unnecessary
302 * computations, namely {@link #getRight()} and {@link #getBottom()}.
303 * These methods return the coordinates of the right and bottom edges of the
304 * rectangle representing the view. For instance, calling {@link #getRight()}
305 * is similar to the following computation: <code>getLeft() + getWidth()</code>
306 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
307 * </p>
308 *
309 * <a name="SizePaddingMargins"></a>
310 * <h3>Size, padding and margins</h3>
311 * <p>
312 * The size of a view is expressed with a width and a height. A view actually
313 * possess two pairs of width and height values.
314 * </p>
315 *
316 * <p>
317 * The first pair is known as <em>measured width</em> and
318 * <em>measured height</em>. These dimensions define how big a view wants to be
319 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
320 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
321 * and {@link #getMeasuredHeight()}.
322 * </p>
323 *
324 * <p>
325 * The second pair is simply known as <em>width</em> and <em>height</em>, or
326 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
327 * dimensions define the actual size of the view on screen, at drawing time and
328 * after layout. These values may, but do not have to, be different from the
329 * measured width and height. The width and height can be obtained by calling
330 * {@link #getWidth()} and {@link #getHeight()}.
331 * </p>
332 *
333 * <p>
334 * To measure its dimensions, a view takes into account its padding. The padding
335 * is expressed in pixels for the left, top, right and bottom parts of the view.
336 * Padding can be used to offset the content of the view by a specific amount of
337 * pixels. For instance, a left padding of 2 will push the view's content by
338 * 2 pixels to the right of the left edge. Padding can be set using the
339 * {@link #setPadding(int, int, int, int)} method and queried by calling
340 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
Fabrice Di Megliod8703a92011-06-16 18:54:08 -0700341 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 * </p>
343 *
344 * <p>
345 * Even though a view can define a padding, it does not provide any support for
346 * margins. However, view groups provide such a support. Refer to
347 * {@link android.view.ViewGroup} and
348 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
349 * </p>
350 *
351 * <a name="Layout"></a>
352 * <h3>Layout</h3>
353 * <p>
354 * Layout is a two pass process: a measure pass and a layout pass. The measuring
355 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
356 * of the view tree. Each view pushes dimension specifications down the tree
357 * during the recursion. At the end of the measure pass, every view has stored
358 * its measurements. The second pass happens in
359 * {@link #layout(int,int,int,int)} and is also top-down. During
360 * this pass each parent is responsible for positioning all of its children
361 * using the sizes computed in the measure pass.
362 * </p>
363 *
364 * <p>
365 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
366 * {@link #getMeasuredHeight()} values must be set, along with those for all of
367 * that view's descendants. A view's measured width and measured height values
368 * must respect the constraints imposed by the view's parents. This guarantees
369 * that at the end of the measure pass, all parents accept all of their
370 * children's measurements. A parent view may call measure() more than once on
371 * its children. For example, the parent may measure each child once with
372 * unspecified dimensions to find out how big they want to be, then call
373 * measure() on them again with actual numbers if the sum of all the children's
374 * unconstrained sizes is too big or too small.
375 * </p>
376 *
377 * <p>
378 * The measure pass uses two classes to communicate dimensions. The
379 * {@link MeasureSpec} class is used by views to tell their parents how they
380 * want to be measured and positioned. The base LayoutParams class just
381 * describes how big the view wants to be for both width and height. For each
382 * dimension, it can specify one of:
383 * <ul>
384 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800385 * <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 -0800386 * (minus padding)
387 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
388 * enclose its content (plus padding).
389 * </ul>
390 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
391 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
392 * an X and Y value.
393 * </p>
394 *
395 * <p>
396 * MeasureSpecs are used to push requirements down the tree from parent to
397 * child. A MeasureSpec can be in one of three modes:
398 * <ul>
399 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
400 * of a child view. For example, a LinearLayout may call measure() on its child
401 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
402 * tall the child view wants to be given a width of 240 pixels.
403 * <li>EXACTLY: This is used by the parent to impose an exact size on the
404 * child. The child must use this size, and guarantee that all of its
405 * descendants will fit within this size.
406 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
407 * child. The child must gurantee that it and all of its descendants will fit
408 * within this size.
409 * </ul>
410 * </p>
411 *
412 * <p>
413 * To intiate a layout, call {@link #requestLayout}. This method is typically
414 * called by a view on itself when it believes that is can no longer fit within
415 * its current bounds.
416 * </p>
417 *
418 * <a name="Drawing"></a>
419 * <h3>Drawing</h3>
420 * <p>
421 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700422 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 * this means that parents will draw before (i.e., behind) their children, with
424 * siblings drawn in the order they appear in the tree.
425 * If you set a background drawable for a View, then the View will draw it for you
426 * before calling back to its <code>onDraw()</code> method.
427 * </p>
428 *
429 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700430 * 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 -0800431 * </p>
432 *
433 * <p>
434 * To force a view to draw, call {@link #invalidate()}.
435 * </p>
436 *
437 * <a name="EventHandlingThreading"></a>
438 * <h3>Event Handling and Threading</h3>
439 * <p>
440 * The basic cycle of a view is as follows:
441 * <ol>
442 * <li>An event comes in and is dispatched to the appropriate view. The view
443 * handles the event and notifies any listeners.</li>
444 * <li>If in the course of processing the event, the view's bounds may need
445 * to be changed, the view will call {@link #requestLayout()}.</li>
446 * <li>Similarly, if in the course of processing the event the view's appearance
447 * may need to be changed, the view will call {@link #invalidate()}.</li>
448 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
449 * the framework will take care of measuring, laying out, and drawing the tree
450 * as appropriate.</li>
451 * </ol>
452 * </p>
453 *
454 * <p><em>Note: The entire view tree is single threaded. You must always be on
455 * the UI thread when calling any method on any view.</em>
456 * If you are doing work on other threads and want to update the state of a view
457 * from that thread, you should use a {@link Handler}.
458 * </p>
459 *
460 * <a name="FocusHandling"></a>
461 * <h3>Focus Handling</h3>
462 * <p>
463 * The framework will handle routine focus movement in response to user input.
464 * This includes changing the focus as views are removed or hidden, or as new
465 * views become available. Views indicate their willingness to take focus
466 * through the {@link #isFocusable} method. To change whether a view can take
467 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
468 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
469 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
470 * </p>
471 * <p>
472 * Focus movement is based on an algorithm which finds the nearest neighbor in a
473 * given direction. In rare cases, the default algorithm may not match the
474 * intended behavior of the developer. In these situations, you can provide
475 * explicit overrides by using these XML attributes in the layout file:
476 * <pre>
477 * nextFocusDown
478 * nextFocusLeft
479 * nextFocusRight
480 * nextFocusUp
481 * </pre>
482 * </p>
483 *
484 *
485 * <p>
486 * To get a particular view to take focus, call {@link #requestFocus()}.
487 * </p>
488 *
489 * <a name="TouchMode"></a>
490 * <h3>Touch Mode</h3>
491 * <p>
492 * When a user is navigating a user interface via directional keys such as a D-pad, it is
493 * necessary to give focus to actionable items such as buttons so the user can see
494 * what will take input. If the device has touch capabilities, however, and the user
495 * begins interacting with the interface by touching it, it is no longer necessary to
496 * always highlight, or give focus to, a particular view. This motivates a mode
497 * for interaction named 'touch mode'.
498 * </p>
499 * <p>
500 * For a touch capable device, once the user touches the screen, the device
501 * will enter touch mode. From this point onward, only views for which
502 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
503 * Other views that are touchable, like buttons, will not take focus when touched; they will
504 * only fire the on click listeners.
505 * </p>
506 * <p>
507 * Any time a user hits a directional key, such as a D-pad direction, the view device will
508 * exit touch mode, and find a view to take focus, so that the user may resume interacting
509 * with the user interface without touching the screen again.
510 * </p>
511 * <p>
512 * The touch mode state is maintained across {@link android.app.Activity}s. Call
513 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
514 * </p>
515 *
516 * <a name="Scrolling"></a>
517 * <h3>Scrolling</h3>
518 * <p>
519 * The framework provides basic support for views that wish to internally
520 * scroll their content. This includes keeping track of the X and Y scroll
521 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800522 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700523 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 * </p>
525 *
526 * <a name="Tags"></a>
527 * <h3>Tags</h3>
528 * <p>
529 * Unlike IDs, tags are not used to identify views. Tags are essentially an
530 * extra piece of information that can be associated with a view. They are most
531 * often used as a convenience to store data related to views in the views
532 * themselves rather than by putting them in a separate structure.
533 * </p>
534 *
535 * <a name="Animation"></a>
536 * <h3>Animation</h3>
537 * <p>
538 * You can attach an {@link Animation} object to a view using
539 * {@link #setAnimation(Animation)} or
540 * {@link #startAnimation(Animation)}. The animation can alter the scale,
541 * rotation, translation and alpha of a view over time. If the animation is
542 * attached to a view that has children, the animation will affect the entire
543 * subtree rooted by that node. When an animation is started, the framework will
544 * take care of redrawing the appropriate views until the animation completes.
545 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800546 * <p>
547 * Starting with Android 3.0, the preferred way of animating views is to use the
548 * {@link android.animation} package APIs.
549 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 *
Jeff Brown85a31762010-09-01 17:01:00 -0700551 * <a name="Security"></a>
552 * <h3>Security</h3>
553 * <p>
554 * Sometimes it is essential that an application be able to verify that an action
555 * is being performed with the full knowledge and consent of the user, such as
556 * granting a permission request, making a purchase or clicking on an advertisement.
557 * Unfortunately, a malicious application could try to spoof the user into
558 * performing these actions, unaware, by concealing the intended purpose of the view.
559 * As a remedy, the framework offers a touch filtering mechanism that can be used to
560 * improve the security of views that provide access to sensitive functionality.
561 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700562 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800563 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700564 * will discard touches that are received whenever the view's window is obscured by
565 * another visible window. As a result, the view will not receive touches whenever a
566 * toast, dialog or other window appears above the view's window.
567 * </p><p>
568 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700569 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
570 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700571 * </p>
572 *
Romain Guy171c5922011-01-06 10:04:23 -0800573 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700574 * @attr ref android.R.styleable#View_background
575 * @attr ref android.R.styleable#View_clickable
576 * @attr ref android.R.styleable#View_contentDescription
577 * @attr ref android.R.styleable#View_drawingCacheQuality
578 * @attr ref android.R.styleable#View_duplicateParentState
579 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700580 * @attr ref android.R.styleable#View_requiresFadingEdge
Romain Guyd6a463a2009-05-21 23:10:10 -0700581 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700582 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700584 * @attr ref android.R.styleable#View_isScrollContainer
585 * @attr ref android.R.styleable#View_focusable
586 * @attr ref android.R.styleable#View_focusableInTouchMode
587 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
588 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800589 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700590 * @attr ref android.R.styleable#View_longClickable
591 * @attr ref android.R.styleable#View_minHeight
592 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 * @attr ref android.R.styleable#View_nextFocusDown
594 * @attr ref android.R.styleable#View_nextFocusLeft
595 * @attr ref android.R.styleable#View_nextFocusRight
596 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700597 * @attr ref android.R.styleable#View_onClick
598 * @attr ref android.R.styleable#View_padding
599 * @attr ref android.R.styleable#View_paddingBottom
600 * @attr ref android.R.styleable#View_paddingLeft
601 * @attr ref android.R.styleable#View_paddingRight
602 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800603 * @attr ref android.R.styleable#View_paddingStart
604 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700605 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800606 * @attr ref android.R.styleable#View_rotation
607 * @attr ref android.R.styleable#View_rotationX
608 * @attr ref android.R.styleable#View_rotationY
609 * @attr ref android.R.styleable#View_scaleX
610 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 * @attr ref android.R.styleable#View_scrollX
612 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700613 * @attr ref android.R.styleable#View_scrollbarSize
614 * @attr ref android.R.styleable#View_scrollbarStyle
615 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700616 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
617 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
619 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 * @attr ref android.R.styleable#View_scrollbarThumbVertical
621 * @attr ref android.R.styleable#View_scrollbarTrackVertical
622 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
623 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700624 * @attr ref android.R.styleable#View_soundEffectsEnabled
625 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800626 * @attr ref android.R.styleable#View_transformPivotX
627 * @attr ref android.R.styleable#View_transformPivotY
628 * @attr ref android.R.styleable#View_translationX
629 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700630 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 *
632 * @see android.view.ViewGroup
633 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700634public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
635 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 private static final boolean DBG = false;
637
638 /**
639 * The logging tag used by this class with android.util.Log.
640 */
641 protected static final String VIEW_LOG_TAG = "View";
642
643 /**
644 * Used to mark a View that has no ID.
645 */
646 public static final int NO_ID = -1;
647
648 /**
649 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
650 * calling setFlags.
651 */
652 private static final int NOT_FOCUSABLE = 0x00000000;
653
654 /**
655 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
656 * setFlags.
657 */
658 private static final int FOCUSABLE = 0x00000001;
659
660 /**
661 * Mask for use with setFlags indicating bits used for focus.
662 */
663 private static final int FOCUSABLE_MASK = 0x00000001;
664
665 /**
666 * This view will adjust its padding to fit sytem windows (e.g. status bar)
667 */
668 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
669
670 /**
Scott Main812634c22011-07-27 13:22:35 -0700671 * This view is visible.
672 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
673 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 */
675 public static final int VISIBLE = 0x00000000;
676
677 /**
678 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700679 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
680 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 */
682 public static final int INVISIBLE = 0x00000004;
683
684 /**
685 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700686 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
687 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 */
689 public static final int GONE = 0x00000008;
690
691 /**
692 * Mask for use with setFlags indicating bits used for visibility.
693 * {@hide}
694 */
695 static final int VISIBILITY_MASK = 0x0000000C;
696
697 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
698
699 /**
700 * This view is enabled. Intrepretation varies by subclass.
701 * Use with ENABLED_MASK when calling setFlags.
702 * {@hide}
703 */
704 static final int ENABLED = 0x00000000;
705
706 /**
707 * This view is disabled. Intrepretation varies by subclass.
708 * Use with ENABLED_MASK when calling setFlags.
709 * {@hide}
710 */
711 static final int DISABLED = 0x00000020;
712
713 /**
714 * Mask for use with setFlags indicating bits used for indicating whether
715 * this view is enabled
716 * {@hide}
717 */
718 static final int ENABLED_MASK = 0x00000020;
719
720 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700721 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
722 * called and further optimizations will be performed. It is okay to have
723 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 * {@hide}
725 */
726 static final int WILL_NOT_DRAW = 0x00000080;
727
728 /**
729 * Mask for use with setFlags indicating bits used for indicating whether
730 * this view is will draw
731 * {@hide}
732 */
733 static final int DRAW_MASK = 0x00000080;
734
735 /**
736 * <p>This view doesn't show scrollbars.</p>
737 * {@hide}
738 */
739 static final int SCROLLBARS_NONE = 0x00000000;
740
741 /**
742 * <p>This view shows horizontal scrollbars.</p>
743 * {@hide}
744 */
745 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
746
747 /**
748 * <p>This view shows vertical scrollbars.</p>
749 * {@hide}
750 */
751 static final int SCROLLBARS_VERTICAL = 0x00000200;
752
753 /**
754 * <p>Mask for use with setFlags indicating bits used for indicating which
755 * scrollbars are enabled.</p>
756 * {@hide}
757 */
758 static final int SCROLLBARS_MASK = 0x00000300;
759
Jeff Brown85a31762010-09-01 17:01:00 -0700760 /**
761 * Indicates that the view should filter touches when its window is obscured.
762 * Refer to the class comments for more information about this security feature.
763 * {@hide}
764 */
765 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
766
767 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768
769 /**
770 * <p>This view doesn't show fading edges.</p>
771 * {@hide}
772 */
773 static final int FADING_EDGE_NONE = 0x00000000;
774
775 /**
776 * <p>This view shows horizontal fading edges.</p>
777 * {@hide}
778 */
779 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
780
781 /**
782 * <p>This view shows vertical fading edges.</p>
783 * {@hide}
784 */
785 static final int FADING_EDGE_VERTICAL = 0x00002000;
786
787 /**
788 * <p>Mask for use with setFlags indicating bits used for indicating which
789 * fading edges are enabled.</p>
790 * {@hide}
791 */
792 static final int FADING_EDGE_MASK = 0x00003000;
793
794 /**
795 * <p>Indicates this view can be clicked. When clickable, a View reacts
796 * to clicks by notifying the OnClickListener.<p>
797 * {@hide}
798 */
799 static final int CLICKABLE = 0x00004000;
800
801 /**
802 * <p>Indicates this view is caching its drawing into a bitmap.</p>
803 * {@hide}
804 */
805 static final int DRAWING_CACHE_ENABLED = 0x00008000;
806
807 /**
808 * <p>Indicates that no icicle should be saved for this view.<p>
809 * {@hide}
810 */
811 static final int SAVE_DISABLED = 0x000010000;
812
813 /**
814 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
815 * property.</p>
816 * {@hide}
817 */
818 static final int SAVE_DISABLED_MASK = 0x000010000;
819
820 /**
821 * <p>Indicates that no drawing cache should ever be created for this view.<p>
822 * {@hide}
823 */
824 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
825
826 /**
827 * <p>Indicates this view can take / keep focus when int touch mode.</p>
828 * {@hide}
829 */
830 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
831
832 /**
833 * <p>Enables low quality mode for the drawing cache.</p>
834 */
835 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
836
837 /**
838 * <p>Enables high quality mode for the drawing cache.</p>
839 */
840 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
841
842 /**
843 * <p>Enables automatic quality mode for the drawing cache.</p>
844 */
845 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
846
847 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
848 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
849 };
850
851 /**
852 * <p>Mask for use with setFlags indicating bits used for the cache
853 * quality property.</p>
854 * {@hide}
855 */
856 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
857
858 /**
859 * <p>
860 * Indicates this view can be long clicked. When long clickable, a View
861 * reacts to long clicks by notifying the OnLongClickListener or showing a
862 * context menu.
863 * </p>
864 * {@hide}
865 */
866 static final int LONG_CLICKABLE = 0x00200000;
867
868 /**
869 * <p>Indicates that this view gets its drawable states from its direct parent
870 * and ignores its original internal states.</p>
871 *
872 * @hide
873 */
874 static final int DUPLICATE_PARENT_STATE = 0x00400000;
875
876 /**
877 * The scrollbar style to display the scrollbars inside the content area,
878 * without increasing the padding. The scrollbars will be overlaid with
879 * translucency on the view's content.
880 */
881 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
882
883 /**
884 * The scrollbar style to display the scrollbars inside the padded area,
885 * increasing the padding of the view. The scrollbars will not overlap the
886 * content area of the view.
887 */
888 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
889
890 /**
891 * The scrollbar style to display the scrollbars at the edge of the view,
892 * without increasing the padding. The scrollbars will be overlaid with
893 * translucency.
894 */
895 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
896
897 /**
898 * The scrollbar style to display the scrollbars at the edge of the view,
899 * increasing the padding of the view. The scrollbars will only overlap the
900 * background, if any.
901 */
902 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
903
904 /**
905 * Mask to check if the scrollbar style is overlay or inset.
906 * {@hide}
907 */
908 static final int SCROLLBARS_INSET_MASK = 0x01000000;
909
910 /**
911 * Mask to check if the scrollbar style is inside or outside.
912 * {@hide}
913 */
914 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
915
916 /**
917 * Mask for scrollbar style.
918 * {@hide}
919 */
920 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
921
922 /**
923 * View flag indicating that the screen should remain on while the
924 * window containing this view is visible to the user. This effectively
925 * takes care of automatically setting the WindowManager's
926 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
927 */
928 public static final int KEEP_SCREEN_ON = 0x04000000;
929
930 /**
931 * View flag indicating whether this view should have sound effects enabled
932 * for events such as clicking and touching.
933 */
934 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
935
936 /**
937 * View flag indicating whether this view should have haptic feedback
938 * enabled for events such as long presses.
939 */
940 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
941
942 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700943 * <p>Indicates that the view hierarchy should stop saving state when
944 * it reaches this view. If state saving is initiated immediately at
945 * the view, it will be allowed.
946 * {@hide}
947 */
948 static final int PARENT_SAVE_DISABLED = 0x20000000;
949
950 /**
951 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
952 * {@hide}
953 */
954 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
955
956 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800957 * Horizontal direction of this view is from Left to Right.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700958 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800959 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700960 public static final int LAYOUT_DIRECTION_LTR = 0x00000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800961
962 /**
963 * Horizontal direction of this view is from Right to Left.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700964 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800965 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700966 public static final int LAYOUT_DIRECTION_RTL = 0x40000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800967
968 /**
969 * Horizontal direction of this view is inherited from its parent.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700970 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800971 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700972 public static final int LAYOUT_DIRECTION_INHERIT = 0x80000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800973
974 /**
975 * Horizontal direction of this view is from deduced from the default language
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700976 * script for the locale. Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800977 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700978 public static final int LAYOUT_DIRECTION_LOCALE = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800979
980 /**
981 * Mask for use with setFlags indicating bits used for horizontalDirection.
982 * {@hide}
983 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700984 static final int LAYOUT_DIRECTION_MASK = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800985
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700986 /*
987 * Array of horizontal direction flags for mapping attribute "horizontalDirection" to correct
988 * flag value.
989 * {@hide}
990 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700991 private static final int[] LAYOUT_DIRECTION_FLAGS = {LAYOUT_DIRECTION_LTR,
992 LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE};
Cibu Johny7632cb92010-02-22 13:01:02 -0800993
994 /**
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700995 * Default horizontalDirection.
996 * {@hide}
997 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700998 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700999
1000 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001001 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1002 * should add all focusable Views regardless if they are focusable in touch mode.
1003 */
1004 public static final int FOCUSABLES_ALL = 0x00000000;
1005
1006 /**
1007 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1008 * should add only Views focusable in touch mode.
1009 */
1010 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1011
1012 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001013 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 * item.
1015 */
1016 public static final int FOCUS_BACKWARD = 0x00000001;
1017
1018 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001019 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 * item.
1021 */
1022 public static final int FOCUS_FORWARD = 0x00000002;
1023
1024 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001025 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 */
1027 public static final int FOCUS_LEFT = 0x00000011;
1028
1029 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001030 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 */
1032 public static final int FOCUS_UP = 0x00000021;
1033
1034 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001035 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 */
1037 public static final int FOCUS_RIGHT = 0x00000042;
1038
1039 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001040 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 */
1042 public static final int FOCUS_DOWN = 0x00000082;
1043
1044 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001045 * Bits of {@link #getMeasuredWidthAndState()} and
1046 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1047 */
1048 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1049
1050 /**
1051 * Bits of {@link #getMeasuredWidthAndState()} and
1052 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1053 */
1054 public static final int MEASURED_STATE_MASK = 0xff000000;
1055
1056 /**
1057 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1058 * for functions that combine both width and height into a single int,
1059 * such as {@link #getMeasuredState()} and the childState argument of
1060 * {@link #resolveSizeAndState(int, int, int)}.
1061 */
1062 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1063
1064 /**
1065 * Bit of {@link #getMeasuredWidthAndState()} and
1066 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1067 * is smaller that the space the view would like to have.
1068 */
1069 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1070
1071 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 * Base View state sets
1073 */
1074 // Singles
1075 /**
1076 * Indicates the view has no states set. States are used with
1077 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1078 * view depending on its state.
1079 *
1080 * @see android.graphics.drawable.Drawable
1081 * @see #getDrawableState()
1082 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001083 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 /**
1085 * Indicates the view is enabled. States are used with
1086 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1087 * view depending on its state.
1088 *
1089 * @see android.graphics.drawable.Drawable
1090 * @see #getDrawableState()
1091 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001092 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 /**
1094 * Indicates the view is focused. States are used with
1095 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1096 * view depending on its state.
1097 *
1098 * @see android.graphics.drawable.Drawable
1099 * @see #getDrawableState()
1100 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001101 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 /**
1103 * Indicates the view is selected. States are used with
1104 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1105 * view depending on its state.
1106 *
1107 * @see android.graphics.drawable.Drawable
1108 * @see #getDrawableState()
1109 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001110 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 /**
1112 * Indicates the view is pressed. States are used with
1113 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1114 * view depending on its state.
1115 *
1116 * @see android.graphics.drawable.Drawable
1117 * @see #getDrawableState()
1118 * @hide
1119 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001120 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 /**
1122 * Indicates the view's window has focus. States are used with
1123 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1124 * view depending on its state.
1125 *
1126 * @see android.graphics.drawable.Drawable
1127 * @see #getDrawableState()
1128 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001129 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 // Doubles
1131 /**
1132 * Indicates the view is enabled and has the focus.
1133 *
1134 * @see #ENABLED_STATE_SET
1135 * @see #FOCUSED_STATE_SET
1136 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001137 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 /**
1139 * Indicates the view is enabled and selected.
1140 *
1141 * @see #ENABLED_STATE_SET
1142 * @see #SELECTED_STATE_SET
1143 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001144 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 /**
1146 * Indicates the view is enabled and that its window has focus.
1147 *
1148 * @see #ENABLED_STATE_SET
1149 * @see #WINDOW_FOCUSED_STATE_SET
1150 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001151 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 /**
1153 * Indicates the view is focused and selected.
1154 *
1155 * @see #FOCUSED_STATE_SET
1156 * @see #SELECTED_STATE_SET
1157 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001158 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 /**
1160 * Indicates the view has the focus and that its window has the focus.
1161 *
1162 * @see #FOCUSED_STATE_SET
1163 * @see #WINDOW_FOCUSED_STATE_SET
1164 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001165 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 /**
1167 * Indicates the view is selected and that its window has the focus.
1168 *
1169 * @see #SELECTED_STATE_SET
1170 * @see #WINDOW_FOCUSED_STATE_SET
1171 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001172 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 // Triples
1174 /**
1175 * Indicates the view is enabled, focused and selected.
1176 *
1177 * @see #ENABLED_STATE_SET
1178 * @see #FOCUSED_STATE_SET
1179 * @see #SELECTED_STATE_SET
1180 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001181 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 /**
1183 * Indicates the view is enabled, focused and its window has the focus.
1184 *
1185 * @see #ENABLED_STATE_SET
1186 * @see #FOCUSED_STATE_SET
1187 * @see #WINDOW_FOCUSED_STATE_SET
1188 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001189 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 /**
1191 * Indicates the view is enabled, selected and its window has the focus.
1192 *
1193 * @see #ENABLED_STATE_SET
1194 * @see #SELECTED_STATE_SET
1195 * @see #WINDOW_FOCUSED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view is focused, selected and its window has the focus.
1200 *
1201 * @see #FOCUSED_STATE_SET
1202 * @see #SELECTED_STATE_SET
1203 * @see #WINDOW_FOCUSED_STATE_SET
1204 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001205 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 /**
1207 * Indicates the view is enabled, focused, selected and its window
1208 * has the focus.
1209 *
1210 * @see #ENABLED_STATE_SET
1211 * @see #FOCUSED_STATE_SET
1212 * @see #SELECTED_STATE_SET
1213 * @see #WINDOW_FOCUSED_STATE_SET
1214 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001215 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 /**
1217 * Indicates the view is pressed and its window has the focus.
1218 *
1219 * @see #PRESSED_STATE_SET
1220 * @see #WINDOW_FOCUSED_STATE_SET
1221 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001222 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 /**
1224 * Indicates the view is pressed and selected.
1225 *
1226 * @see #PRESSED_STATE_SET
1227 * @see #SELECTED_STATE_SET
1228 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001229 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 /**
1231 * Indicates the view is pressed, selected and its window has the focus.
1232 *
1233 * @see #PRESSED_STATE_SET
1234 * @see #SELECTED_STATE_SET
1235 * @see #WINDOW_FOCUSED_STATE_SET
1236 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001237 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 /**
1239 * Indicates the view is pressed and focused.
1240 *
1241 * @see #PRESSED_STATE_SET
1242 * @see #FOCUSED_STATE_SET
1243 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001244 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 /**
1246 * Indicates the view is pressed, focused and its window has the focus.
1247 *
1248 * @see #PRESSED_STATE_SET
1249 * @see #FOCUSED_STATE_SET
1250 * @see #WINDOW_FOCUSED_STATE_SET
1251 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001252 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 /**
1254 * Indicates the view is pressed, focused and selected.
1255 *
1256 * @see #PRESSED_STATE_SET
1257 * @see #SELECTED_STATE_SET
1258 * @see #FOCUSED_STATE_SET
1259 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001260 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 /**
1262 * Indicates the view is pressed, focused, selected and its window has the focus.
1263 *
1264 * @see #PRESSED_STATE_SET
1265 * @see #FOCUSED_STATE_SET
1266 * @see #SELECTED_STATE_SET
1267 * @see #WINDOW_FOCUSED_STATE_SET
1268 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001269 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 /**
1271 * Indicates the view is pressed and enabled.
1272 *
1273 * @see #PRESSED_STATE_SET
1274 * @see #ENABLED_STATE_SET
1275 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001276 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 /**
1278 * Indicates the view is pressed, enabled and its window has the focus.
1279 *
1280 * @see #PRESSED_STATE_SET
1281 * @see #ENABLED_STATE_SET
1282 * @see #WINDOW_FOCUSED_STATE_SET
1283 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001284 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 /**
1286 * Indicates the view is pressed, enabled and selected.
1287 *
1288 * @see #PRESSED_STATE_SET
1289 * @see #ENABLED_STATE_SET
1290 * @see #SELECTED_STATE_SET
1291 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001292 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 /**
1294 * Indicates the view is pressed, enabled, selected and its window has the
1295 * focus.
1296 *
1297 * @see #PRESSED_STATE_SET
1298 * @see #ENABLED_STATE_SET
1299 * @see #SELECTED_STATE_SET
1300 * @see #WINDOW_FOCUSED_STATE_SET
1301 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001302 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 /**
1304 * Indicates the view is pressed, enabled and focused.
1305 *
1306 * @see #PRESSED_STATE_SET
1307 * @see #ENABLED_STATE_SET
1308 * @see #FOCUSED_STATE_SET
1309 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001310 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 /**
1312 * Indicates the view is pressed, enabled, focused and its window has the
1313 * focus.
1314 *
1315 * @see #PRESSED_STATE_SET
1316 * @see #ENABLED_STATE_SET
1317 * @see #FOCUSED_STATE_SET
1318 * @see #WINDOW_FOCUSED_STATE_SET
1319 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001320 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 /**
1322 * Indicates the view is pressed, enabled, focused and selected.
1323 *
1324 * @see #PRESSED_STATE_SET
1325 * @see #ENABLED_STATE_SET
1326 * @see #SELECTED_STATE_SET
1327 * @see #FOCUSED_STATE_SET
1328 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001329 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 /**
1331 * Indicates the view is pressed, enabled, focused, selected and its window
1332 * has the focus.
1333 *
1334 * @see #PRESSED_STATE_SET
1335 * @see #ENABLED_STATE_SET
1336 * @see #SELECTED_STATE_SET
1337 * @see #FOCUSED_STATE_SET
1338 * @see #WINDOW_FOCUSED_STATE_SET
1339 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001340 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341
1342 /**
1343 * The order here is very important to {@link #getDrawableState()}
1344 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001345 private static final int[][] VIEW_STATE_SETS;
1346
Romain Guyb051e892010-09-28 19:09:36 -07001347 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1348 static final int VIEW_STATE_SELECTED = 1 << 1;
1349 static final int VIEW_STATE_FOCUSED = 1 << 2;
1350 static final int VIEW_STATE_ENABLED = 1 << 3;
1351 static final int VIEW_STATE_PRESSED = 1 << 4;
1352 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001353 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001354 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001355 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1356 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001357
1358 static final int[] VIEW_STATE_IDS = new int[] {
1359 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1360 R.attr.state_selected, VIEW_STATE_SELECTED,
1361 R.attr.state_focused, VIEW_STATE_FOCUSED,
1362 R.attr.state_enabled, VIEW_STATE_ENABLED,
1363 R.attr.state_pressed, VIEW_STATE_PRESSED,
1364 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001365 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001366 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001367 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1368 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 };
1370
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001371 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001372 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1373 throw new IllegalStateException(
1374 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1375 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001376 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001377 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001378 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001379 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001380 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001381 orderedIds[i * 2] = viewState;
1382 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001383 }
1384 }
1385 }
Romain Guyb051e892010-09-28 19:09:36 -07001386 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1387 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1388 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001389 int numBits = Integer.bitCount(i);
1390 int[] set = new int[numBits];
1391 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001392 for (int j = 0; j < orderedIds.length; j += 2) {
1393 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001394 set[pos++] = orderedIds[j];
1395 }
1396 }
1397 VIEW_STATE_SETS[i] = set;
1398 }
1399
1400 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1401 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1402 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1403 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1404 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1405 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1406 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1407 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1408 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1409 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1410 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1411 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1412 | VIEW_STATE_FOCUSED];
1413 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1414 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1416 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1418 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1419 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1420 | VIEW_STATE_ENABLED];
1421 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1422 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1423 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1424 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1425 | VIEW_STATE_ENABLED];
1426 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1427 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1428 | VIEW_STATE_ENABLED];
1429 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1430 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1431 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1432
1433 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1434 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1435 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1436 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1437 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1438 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1439 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1440 | VIEW_STATE_PRESSED];
1441 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1442 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1443 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1444 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1445 | VIEW_STATE_PRESSED];
1446 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1447 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1448 | VIEW_STATE_PRESSED];
1449 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1451 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1452 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1453 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1454 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1455 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1456 | VIEW_STATE_PRESSED];
1457 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1459 | VIEW_STATE_PRESSED];
1460 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1462 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1463 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1464 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1465 | VIEW_STATE_PRESSED];
1466 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1467 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1468 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1469 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1470 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1471 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1472 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1473 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1474 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1475 | VIEW_STATE_PRESSED];
1476 }
1477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001479 * Accessibility event types that are dispatched for text population.
1480 */
1481 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1482 AccessibilityEvent.TYPE_VIEW_CLICKED
1483 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1484 | AccessibilityEvent.TYPE_VIEW_SELECTED
1485 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1486 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1487 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001488 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001489 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
1490 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001491
1492 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 * Temporary Rect currently for use in setBackground(). This will probably
1494 * be extended in the future to hold our own class with more than just
1495 * a Rect. :)
1496 */
1497 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001498
1499 /**
1500 * Map used to store views' tags.
1501 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001502 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001505 * The next available accessiiblity id.
1506 */
1507 private static int sNextAccessibilityViewId;
1508
1509 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 * The animation currently associated with this view.
1511 * @hide
1512 */
1513 protected Animation mCurrentAnimation = null;
1514
1515 /**
1516 * Width as measured during measure pass.
1517 * {@hide}
1518 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001519 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001520 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521
1522 /**
1523 * Height as measured during measure pass.
1524 * {@hide}
1525 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001526 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001527 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528
1529 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001530 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1531 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1532 * its display list. This flag, used only when hw accelerated, allows us to clear the
1533 * flag while retaining this information until it's needed (at getDisplayList() time and
1534 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1535 *
1536 * {@hide}
1537 */
1538 boolean mRecreateDisplayList = false;
1539
1540 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 * The view's identifier.
1542 * {@hide}
1543 *
1544 * @see #setId(int)
1545 * @see #getId()
1546 */
1547 @ViewDebug.ExportedProperty(resolveId = true)
1548 int mID = NO_ID;
1549
1550 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001551 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001552 */
1553 int mAccessibilityViewId = NO_ID;
1554
1555 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 * The view's tag.
1557 * {@hide}
1558 *
1559 * @see #setTag(Object)
1560 * @see #getTag()
1561 */
1562 protected Object mTag;
1563
1564 // for mPrivateFlags:
1565 /** {@hide} */
1566 static final int WANTS_FOCUS = 0x00000001;
1567 /** {@hide} */
1568 static final int FOCUSED = 0x00000002;
1569 /** {@hide} */
1570 static final int SELECTED = 0x00000004;
1571 /** {@hide} */
1572 static final int IS_ROOT_NAMESPACE = 0x00000008;
1573 /** {@hide} */
1574 static final int HAS_BOUNDS = 0x00000010;
1575 /** {@hide} */
1576 static final int DRAWN = 0x00000020;
1577 /**
1578 * When this flag is set, this view is running an animation on behalf of its
1579 * children and should therefore not cancel invalidate requests, even if they
1580 * lie outside of this view's bounds.
1581 *
1582 * {@hide}
1583 */
1584 static final int DRAW_ANIMATION = 0x00000040;
1585 /** {@hide} */
1586 static final int SKIP_DRAW = 0x00000080;
1587 /** {@hide} */
1588 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1589 /** {@hide} */
1590 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1591 /** {@hide} */
1592 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1593 /** {@hide} */
1594 static final int MEASURED_DIMENSION_SET = 0x00000800;
1595 /** {@hide} */
1596 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001597 /** {@hide} */
1598 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599
1600 private static final int PRESSED = 0x00004000;
1601
1602 /** {@hide} */
1603 static final int DRAWING_CACHE_VALID = 0x00008000;
1604 /**
1605 * Flag used to indicate that this view should be drawn once more (and only once
1606 * more) after its animation has completed.
1607 * {@hide}
1608 */
1609 static final int ANIMATION_STARTED = 0x00010000;
1610
1611 private static final int SAVE_STATE_CALLED = 0x00020000;
1612
1613 /**
1614 * Indicates that the View returned true when onSetAlpha() was called and that
1615 * the alpha must be restored.
1616 * {@hide}
1617 */
1618 static final int ALPHA_SET = 0x00040000;
1619
1620 /**
1621 * Set by {@link #setScrollContainer(boolean)}.
1622 */
1623 static final int SCROLL_CONTAINER = 0x00080000;
1624
1625 /**
1626 * Set by {@link #setScrollContainer(boolean)}.
1627 */
1628 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1629
1630 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001631 * View flag indicating whether this view was invalidated (fully or partially.)
1632 *
1633 * @hide
1634 */
1635 static final int DIRTY = 0x00200000;
1636
1637 /**
1638 * View flag indicating whether this view was invalidated by an opaque
1639 * invalidate request.
1640 *
1641 * @hide
1642 */
1643 static final int DIRTY_OPAQUE = 0x00400000;
1644
1645 /**
1646 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1647 *
1648 * @hide
1649 */
1650 static final int DIRTY_MASK = 0x00600000;
1651
1652 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001653 * Indicates whether the background is opaque.
1654 *
1655 * @hide
1656 */
1657 static final int OPAQUE_BACKGROUND = 0x00800000;
1658
1659 /**
1660 * Indicates whether the scrollbars are opaque.
1661 *
1662 * @hide
1663 */
1664 static final int OPAQUE_SCROLLBARS = 0x01000000;
1665
1666 /**
1667 * Indicates whether the view is opaque.
1668 *
1669 * @hide
1670 */
1671 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001672
Adam Powelle14579b2009-12-16 18:39:52 -08001673 /**
1674 * Indicates a prepressed state;
1675 * the short time between ACTION_DOWN and recognizing
1676 * a 'real' press. Prepressed is used to recognize quick taps
1677 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001678 *
Adam Powelle14579b2009-12-16 18:39:52 -08001679 * @hide
1680 */
1681 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001682
Adam Powellc9fbaab2010-02-16 17:16:19 -08001683 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001684 * Indicates whether the view is temporarily detached.
1685 *
1686 * @hide
1687 */
1688 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001689
Adam Powell8568c3a2010-04-19 14:26:11 -07001690 /**
1691 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001692 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001693 * @hide
1694 */
1695 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001696
1697 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001698 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1699 * @hide
1700 */
1701 private static final int HOVERED = 0x10000000;
1702
1703 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001704 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1705 * for transform operations
1706 *
1707 * @hide
1708 */
Adam Powellf37df072010-09-17 16:22:49 -07001709 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001710
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001711 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001712 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001713
Chet Haasefd2b0022010-08-06 13:08:56 -07001714 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001715 * Indicates that this view was specifically invalidated, not just dirtied because some
1716 * child view was invalidated. The flag is used to determine when we need to recreate
1717 * a view's display list (as opposed to just returning a reference to its existing
1718 * display list).
1719 *
1720 * @hide
1721 */
1722 static final int INVALIDATED = 0x80000000;
1723
Christopher Tate3d4bf172011-03-28 16:16:46 -07001724 /* Masks for mPrivateFlags2 */
1725
1726 /**
1727 * Indicates that this view has reported that it can accept the current drag's content.
1728 * Cleared when the drag operation concludes.
1729 * @hide
1730 */
1731 static final int DRAG_CAN_ACCEPT = 0x00000001;
1732
1733 /**
1734 * Indicates that this view is currently directly under the drag location in a
1735 * drag-and-drop operation involving content that it can accept. Cleared when
1736 * the drag exits the view, or when the drag operation concludes.
1737 * @hide
1738 */
1739 static final int DRAG_HOVERED = 0x00000002;
1740
Cibu Johny86666632010-02-22 13:01:02 -08001741 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001742 * Indicates whether the view layout direction has been resolved and drawn to the
1743 * right-to-left direction.
Cibu Johny86666632010-02-22 13:01:02 -08001744 *
1745 * @hide
1746 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001747 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004;
1748
1749 /**
1750 * Indicates whether the view layout direction has been resolved.
1751 *
1752 * @hide
1753 */
1754 static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008;
1755
Cibu Johny86666632010-02-22 13:01:02 -08001756
Adam Powell539ee872012-02-03 19:00:49 -08001757 /**
1758 * Indicates that the view is tracking some sort of transient state
1759 * that the app should not need to be aware of, but that the framework
1760 * should take special care to preserve.
1761 *
1762 * @hide
1763 */
1764 static final int HAS_TRANSIENT_STATE = 0x00000010;
1765
1766
Christopher Tate3d4bf172011-03-28 16:16:46 -07001767 /* End of masks for mPrivateFlags2 */
1768
1769 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1770
Chet Haasedaf98e92011-01-10 14:10:36 -08001771 /**
Adam Powell637d3372010-08-25 14:37:03 -07001772 * Always allow a user to over-scroll this view, provided it is a
1773 * view that can scroll.
1774 *
1775 * @see #getOverScrollMode()
1776 * @see #setOverScrollMode(int)
1777 */
1778 public static final int OVER_SCROLL_ALWAYS = 0;
1779
1780 /**
1781 * Allow a user to over-scroll this view only if the content is large
1782 * enough to meaningfully scroll, provided it is a view that can scroll.
1783 *
1784 * @see #getOverScrollMode()
1785 * @see #setOverScrollMode(int)
1786 */
1787 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1788
1789 /**
1790 * Never allow a user to over-scroll this view.
1791 *
1792 * @see #getOverScrollMode()
1793 * @see #setOverScrollMode(int)
1794 */
1795 public static final int OVER_SCROLL_NEVER = 2;
1796
1797 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001798 * View has requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08001799 *
Joe Malin32736f02011-01-19 16:14:20 -08001800 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001801 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001802 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08001803
1804 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001805 * View has requested the system UI to enter an unobtrusive "low profile" mode.
1806 *
1807 * This is for use in games, book readers, video players, or any other "immersive" application
1808 * where the usual system chrome is deemed too distracting.
1809 *
1810 * In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08001811 *
Joe Malin32736f02011-01-19 16:14:20 -08001812 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001813 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001814 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
1815
1816 /**
1817 * View has requested that the system navigation be temporarily hidden.
1818 *
1819 * This is an even less obtrusive state than that called for by
1820 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
1821 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
1822 * those to disappear. This is useful (in conjunction with the
1823 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
1824 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
1825 * window flags) for displaying content using every last pixel on the display.
1826 *
1827 * There is a limitation: because navigation controls are so important, the least user
1828 * interaction will cause them to reappear immediately.
1829 *
1830 * @see #setSystemUiVisibility(int)
1831 */
1832 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
1833
1834 /**
1835 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
1836 */
1837 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
1838
1839 /**
1840 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
1841 */
1842 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08001843
1844 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001845 * @hide
1846 *
1847 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1848 * out of the public fields to keep the undefined bits out of the developer's way.
1849 *
1850 * Flag to make the status bar not expandable. Unless you also
1851 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1852 */
1853 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1854
1855 /**
1856 * @hide
1857 *
1858 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1859 * out of the public fields to keep the undefined bits out of the developer's way.
1860 *
1861 * Flag to hide notification icons and scrolling ticker text.
1862 */
1863 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1864
1865 /**
1866 * @hide
1867 *
1868 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1869 * out of the public fields to keep the undefined bits out of the developer's way.
1870 *
1871 * Flag to disable incoming notification alerts. This will not block
1872 * icons, but it will block sound, vibrating and other visual or aural notifications.
1873 */
1874 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1875
1876 /**
1877 * @hide
1878 *
1879 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1880 * out of the public fields to keep the undefined bits out of the developer's way.
1881 *
1882 * Flag to hide only the scrolling ticker. Note that
1883 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1884 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1885 */
1886 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1887
1888 /**
1889 * @hide
1890 *
1891 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1892 * out of the public fields to keep the undefined bits out of the developer's way.
1893 *
1894 * Flag to hide the center system info area.
1895 */
1896 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1897
1898 /**
1899 * @hide
1900 *
1901 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1902 * out of the public fields to keep the undefined bits out of the developer's way.
1903 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04001904 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001905 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1906 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04001907 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001908
1909 /**
1910 * @hide
1911 *
1912 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1913 * out of the public fields to keep the undefined bits out of the developer's way.
1914 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04001915 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08001916 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1917 */
1918 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1919
1920 /**
1921 * @hide
1922 *
1923 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1924 * out of the public fields to keep the undefined bits out of the developer's way.
1925 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001926 * Flag to hide only the clock. You might use this if your activity has
1927 * its own clock making the status bar's clock redundant.
1928 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001929 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1930
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001931 /**
1932 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04001933 *
1934 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1935 * out of the public fields to keep the undefined bits out of the developer's way.
1936 *
1937 * Flag to hide only the recent apps button. Don't use this
1938 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1939 */
1940 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
1941
1942 /**
1943 * @hide
1944 *
1945 * NOTE: This flag may only be used in subtreeSystemUiVisibility, etc. etc.
1946 *
1947 * This hides HOME and RECENT and is provided for compatibility with interim implementations.
1948 */
1949 @Deprecated
1950 public static final int STATUS_BAR_DISABLE_NAVIGATION =
1951 STATUS_BAR_DISABLE_HOME | STATUS_BAR_DISABLE_RECENT;
1952
1953 /**
1954 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001955 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001956 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001957
1958 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001959 * These are the system UI flags that can be cleared by events outside
1960 * of an application. Currently this is just the ability to tap on the
1961 * screen while hiding the navigation bar to have it return.
1962 * @hide
1963 */
1964 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
1965 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION;
1966
1967 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001968 * Find views that render the specified text.
1969 *
1970 * @see #findViewsWithText(ArrayList, CharSequence, int)
1971 */
1972 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
1973
1974 /**
1975 * Find find views that contain the specified content description.
1976 *
1977 * @see #findViewsWithText(ArrayList, CharSequence, int)
1978 */
1979 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
1980
1981 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07001982 * Find views that contain {@link AccessibilityNodeProvider}. Such
1983 * a View is a root of virtual view hierarchy and may contain the searched
1984 * text. If this flag is set Views with providers are automatically
1985 * added and it is a responsibility of the client to call the APIs of
1986 * the provider to determine whether the virtual tree rooted at this View
1987 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
1988 * represeting the virtual views with this text.
1989 *
1990 * @see #findViewsWithText(ArrayList, CharSequence, int)
1991 *
1992 * @hide
1993 */
1994 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
1995
1996 /**
Romain Guybb9908b2012-03-08 11:14:07 -08001997 * Indicates that the screen has changed state and is now off.
1998 *
1999 * @see #onScreenStateChanged(int)
2000 */
2001 public static final int SCREEN_STATE_OFF = 0x0;
2002
2003 /**
2004 * Indicates that the screen has changed state and is now on.
2005 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002006 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002007 */
2008 public static final int SCREEN_STATE_ON = 0x1;
2009
2010 /**
Adam Powell637d3372010-08-25 14:37:03 -07002011 * Controls the over-scroll mode for this view.
2012 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2013 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2014 * and {@link #OVER_SCROLL_NEVER}.
2015 */
2016 private int mOverScrollMode;
2017
2018 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 * The parent this view is attached to.
2020 * {@hide}
2021 *
2022 * @see #getParent()
2023 */
2024 protected ViewParent mParent;
2025
2026 /**
2027 * {@hide}
2028 */
2029 AttachInfo mAttachInfo;
2030
2031 /**
2032 * {@hide}
2033 */
Romain Guy809a7f62009-05-14 15:44:42 -07002034 @ViewDebug.ExportedProperty(flagMapping = {
2035 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2036 name = "FORCE_LAYOUT"),
2037 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2038 name = "LAYOUT_REQUIRED"),
2039 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002040 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002041 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2042 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2043 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2044 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2045 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002047 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048
2049 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002050 * This view's request for the visibility of the status bar.
2051 * @hide
2052 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002053 @ViewDebug.ExportedProperty(flagMapping = {
2054 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2055 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2056 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2057 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2058 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2059 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2060 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2061 equals = SYSTEM_UI_FLAG_VISIBLE,
2062 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2063 })
Joe Onorato664644d2011-01-23 17:53:23 -08002064 int mSystemUiVisibility;
2065
2066 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 * Count of how many windows this view has been attached to.
2068 */
2069 int mWindowAttachCount;
2070
2071 /**
2072 * The layout parameters associated with this view and used by the parent
2073 * {@link android.view.ViewGroup} to determine how this view should be
2074 * laid out.
2075 * {@hide}
2076 */
2077 protected ViewGroup.LayoutParams mLayoutParams;
2078
2079 /**
2080 * The view flags hold various views states.
2081 * {@hide}
2082 */
2083 @ViewDebug.ExportedProperty
2084 int mViewFlags;
2085
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002086 static class TransformationInfo {
2087 /**
2088 * The transform matrix for the View. This transform is calculated internally
2089 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2090 * is used by default. Do *not* use this variable directly; instead call
2091 * getMatrix(), which will automatically recalculate the matrix if necessary
2092 * to get the correct matrix based on the latest rotation and scale properties.
2093 */
2094 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002095
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002096 /**
2097 * The transform matrix for the View. This transform is calculated internally
2098 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2099 * is used by default. Do *not* use this variable directly; instead call
2100 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2101 * to get the correct matrix based on the latest rotation and scale properties.
2102 */
2103 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002104
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002105 /**
2106 * An internal variable that tracks whether we need to recalculate the
2107 * transform matrix, based on whether the rotation or scaleX/Y properties
2108 * have changed since the matrix was last calculated.
2109 */
2110 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002111
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002112 /**
2113 * An internal variable that tracks whether we need to recalculate the
2114 * transform matrix, based on whether the rotation or scaleX/Y properties
2115 * have changed since the matrix was last calculated.
2116 */
2117 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002118
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002119 /**
2120 * A variable that tracks whether we need to recalculate the
2121 * transform matrix, based on whether the rotation or scaleX/Y properties
2122 * have changed since the matrix was last calculated. This variable
2123 * is only valid after a call to updateMatrix() or to a function that
2124 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2125 */
2126 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002127
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002128 /**
2129 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2130 */
2131 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002132
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002133 /**
2134 * This matrix is used when computing the matrix for 3D rotations.
2135 */
2136 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002137
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002138 /**
2139 * These prev values are used to recalculate a centered pivot point when necessary. The
2140 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2141 * set), so thes values are only used then as well.
2142 */
2143 private int mPrevWidth = -1;
2144 private int mPrevHeight = -1;
2145
2146 /**
2147 * The degrees rotation around the vertical axis through the pivot point.
2148 */
2149 @ViewDebug.ExportedProperty
2150 float mRotationY = 0f;
2151
2152 /**
2153 * The degrees rotation around the horizontal axis through the pivot point.
2154 */
2155 @ViewDebug.ExportedProperty
2156 float mRotationX = 0f;
2157
2158 /**
2159 * The degrees rotation around the pivot point.
2160 */
2161 @ViewDebug.ExportedProperty
2162 float mRotation = 0f;
2163
2164 /**
2165 * The amount of translation of the object away from its left property (post-layout).
2166 */
2167 @ViewDebug.ExportedProperty
2168 float mTranslationX = 0f;
2169
2170 /**
2171 * The amount of translation of the object away from its top property (post-layout).
2172 */
2173 @ViewDebug.ExportedProperty
2174 float mTranslationY = 0f;
2175
2176 /**
2177 * The amount of scale in the x direction around the pivot point. A
2178 * value of 1 means no scaling is applied.
2179 */
2180 @ViewDebug.ExportedProperty
2181 float mScaleX = 1f;
2182
2183 /**
2184 * The amount of scale in the y direction around the pivot point. A
2185 * value of 1 means no scaling is applied.
2186 */
2187 @ViewDebug.ExportedProperty
2188 float mScaleY = 1f;
2189
2190 /**
Chet Haasea33de552012-02-03 16:28:24 -08002191 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002192 */
2193 @ViewDebug.ExportedProperty
2194 float mPivotX = 0f;
2195
2196 /**
Chet Haasea33de552012-02-03 16:28:24 -08002197 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002198 */
2199 @ViewDebug.ExportedProperty
2200 float mPivotY = 0f;
2201
2202 /**
2203 * The opacity of the View. This is a value from 0 to 1, where 0 means
2204 * completely transparent and 1 means completely opaque.
2205 */
2206 @ViewDebug.ExportedProperty
2207 float mAlpha = 1f;
2208 }
2209
2210 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002211
Joe Malin32736f02011-01-19 16:14:20 -08002212 private boolean mLastIsOpaque;
2213
Chet Haasefd2b0022010-08-06 13:08:56 -07002214 /**
2215 * Convenience value to check for float values that are close enough to zero to be considered
2216 * zero.
2217 */
Romain Guy2542d192010-08-18 11:47:12 -07002218 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002219
2220 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 * The distance in pixels from the left edge of this view's parent
2222 * to the left edge of this view.
2223 * {@hide}
2224 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002225 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 protected int mLeft;
2227 /**
2228 * The distance in pixels from the left edge of this view's parent
2229 * to the right edge of this view.
2230 * {@hide}
2231 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002232 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 protected int mRight;
2234 /**
2235 * The distance in pixels from the top edge of this view's parent
2236 * to the top edge of this view.
2237 * {@hide}
2238 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002239 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 protected int mTop;
2241 /**
2242 * The distance in pixels from the top edge of this view's parent
2243 * to the bottom edge of this view.
2244 * {@hide}
2245 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002246 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 protected int mBottom;
2248
2249 /**
2250 * The offset, in pixels, by which the content of this view is scrolled
2251 * horizontally.
2252 * {@hide}
2253 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002254 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 protected int mScrollX;
2256 /**
2257 * The offset, in pixels, by which the content of this view is scrolled
2258 * vertically.
2259 * {@hide}
2260 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002261 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 protected int mScrollY;
2263
2264 /**
2265 * The left padding in pixels, that is the distance in pixels between the
2266 * left edge of this view and the left edge of its content.
2267 * {@hide}
2268 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002269 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 protected int mPaddingLeft;
2271 /**
2272 * The right padding in pixels, that is the distance in pixels between the
2273 * right edge of this view and the right edge of its content.
2274 * {@hide}
2275 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002276 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 protected int mPaddingRight;
2278 /**
2279 * The top padding in pixels, that is the distance in pixels between the
2280 * top edge of this view and the top edge of its content.
2281 * {@hide}
2282 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002283 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 protected int mPaddingTop;
2285 /**
2286 * The bottom padding in pixels, that is the distance in pixels between the
2287 * bottom edge of this view and the bottom edge of its content.
2288 * {@hide}
2289 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002290 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 protected int mPaddingBottom;
2292
2293 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002294 * Briefly describes the view and is primarily used for accessibility support.
2295 */
2296 private CharSequence mContentDescription;
2297
2298 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002300 *
2301 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002302 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002303 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002304 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305
2306 /**
2307 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002308 *
2309 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002311 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002312 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002314 /**
Adam Powell20232d02010-12-08 21:08:53 -08002315 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002316 *
2317 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002318 */
2319 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002320 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002321
2322 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002323 * Cache if the user padding is relative.
2324 *
2325 */
2326 @ViewDebug.ExportedProperty(category = "padding")
2327 boolean mUserPaddingRelative;
2328
2329 /**
2330 * Cache the paddingStart set by the user to append to the scrollbar's size.
2331 *
2332 */
2333 @ViewDebug.ExportedProperty(category = "padding")
2334 int mUserPaddingStart;
2335
2336 /**
2337 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2338 *
2339 */
2340 @ViewDebug.ExportedProperty(category = "padding")
2341 int mUserPaddingEnd;
2342
2343 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002344 * @hide
2345 */
2346 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2347 /**
2348 * @hide
2349 */
2350 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 private Drawable mBGDrawable;
2353
2354 private int mBackgroundResource;
2355 private boolean mBackgroundSizeChanged;
2356
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002357 static class ListenerInfo {
2358 /**
2359 * Listener used to dispatch focus change events.
2360 * This field should be made private, so it is hidden from the SDK.
2361 * {@hide}
2362 */
2363 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002365 /**
2366 * Listeners for layout change events.
2367 */
2368 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002369
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002370 /**
2371 * Listeners for attach events.
2372 */
2373 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002374
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002375 /**
2376 * Listener used to dispatch click events.
2377 * This field should be made private, so it is hidden from the SDK.
2378 * {@hide}
2379 */
2380 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002382 /**
2383 * Listener used to dispatch long click events.
2384 * This field should be made private, so it is hidden from the SDK.
2385 * {@hide}
2386 */
2387 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002389 /**
2390 * Listener used to build the context menu.
2391 * This field should be made private, so it is hidden from the SDK.
2392 * {@hide}
2393 */
2394 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002396 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002398 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002400 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002401
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002402 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002403
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002404 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002405
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002406 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2407 }
2408
2409 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 /**
2412 * The application environment this view lives in.
2413 * This field should be made private, so it is hidden from the SDK.
2414 * {@hide}
2415 */
2416 protected Context mContext;
2417
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002418 private final Resources mResources;
2419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 private ScrollabilityCache mScrollCache;
2421
2422 private int[] mDrawableState = null;
2423
Romain Guy0211a0a2011-02-14 16:34:59 -08002424 /**
2425 * Set to true when drawing cache is enabled and cannot be created.
2426 *
2427 * @hide
2428 */
2429 public boolean mCachingFailed;
2430
Romain Guy02890fd2010-08-06 17:58:44 -07002431 private Bitmap mDrawingCache;
2432 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002433 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002434 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435
2436 /**
2437 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2438 * the user may specify which view to go to next.
2439 */
2440 private int mNextFocusLeftId = View.NO_ID;
2441
2442 /**
2443 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2444 * the user may specify which view to go to next.
2445 */
2446 private int mNextFocusRightId = View.NO_ID;
2447
2448 /**
2449 * When this view has focus and the next focus is {@link #FOCUS_UP},
2450 * the user may specify which view to go to next.
2451 */
2452 private int mNextFocusUpId = View.NO_ID;
2453
2454 /**
2455 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2456 * the user may specify which view to go to next.
2457 */
2458 private int mNextFocusDownId = View.NO_ID;
2459
Jeff Brown4e6319b2010-12-13 10:36:51 -08002460 /**
2461 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2462 * the user may specify which view to go to next.
2463 */
2464 int mNextFocusForwardId = View.NO_ID;
2465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002467 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002468 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002469 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 private UnsetPressedState mUnsetPressedState;
2472
2473 /**
2474 * Whether the long press's action has been invoked. The tap's action is invoked on the
2475 * up event while a long press is invoked as soon as the long press duration is reached, so
2476 * a long press could be performed before the tap is checked, in which case the tap's action
2477 * should not be invoked.
2478 */
2479 private boolean mHasPerformedLongPress;
2480
2481 /**
2482 * The minimum height of the view. We'll try our best to have the height
2483 * of this view to at least this amount.
2484 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002485 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 private int mMinHeight;
2487
2488 /**
2489 * The minimum width of the view. We'll try our best to have the width
2490 * of this view to at least this amount.
2491 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002492 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 private int mMinWidth;
2494
2495 /**
2496 * The delegate to handle touch events that are physically in this view
2497 * but should be handled by another view.
2498 */
2499 private TouchDelegate mTouchDelegate = null;
2500
2501 /**
2502 * Solid color to use as a background when creating the drawing cache. Enables
2503 * the cache to use 16 bit bitmaps instead of 32 bit.
2504 */
2505 private int mDrawingCacheBackgroundColor = 0;
2506
2507 /**
2508 * Special tree observer used when mAttachInfo is null.
2509 */
2510 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002511
Adam Powelle14579b2009-12-16 18:39:52 -08002512 /**
2513 * Cache the touch slop from the context that created the view.
2514 */
2515 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002518 * Object that handles automatic animation of view properties.
2519 */
2520 private ViewPropertyAnimator mAnimator = null;
2521
2522 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002523 * Flag indicating that a drag can cross window boundaries. When
2524 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2525 * with this flag set, all visible applications will be able to participate
2526 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002527 *
2528 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002529 */
2530 public static final int DRAG_FLAG_GLOBAL = 1;
2531
2532 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002533 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2534 */
2535 private float mVerticalScrollFactor;
2536
2537 /**
Adam Powell20232d02010-12-08 21:08:53 -08002538 * Position of the vertical scroll bar.
2539 */
2540 private int mVerticalScrollbarPosition;
2541
2542 /**
2543 * Position the scroll bar at the default position as determined by the system.
2544 */
2545 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2546
2547 /**
2548 * Position the scroll bar along the left edge.
2549 */
2550 public static final int SCROLLBAR_POSITION_LEFT = 1;
2551
2552 /**
2553 * Position the scroll bar along the right edge.
2554 */
2555 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2556
2557 /**
Romain Guy171c5922011-01-06 10:04:23 -08002558 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002559 *
2560 * @see #getLayerType()
2561 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002562 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002563 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002564 */
2565 public static final int LAYER_TYPE_NONE = 0;
2566
2567 /**
2568 * <p>Indicates that the view has a software layer. A software layer is backed
2569 * by a bitmap and causes the view to be rendered using Android's software
2570 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002571 *
Romain Guy171c5922011-01-06 10:04:23 -08002572 * <p>Software layers have various usages:</p>
2573 * <p>When the application is not using hardware acceleration, a software layer
2574 * is useful to apply a specific color filter and/or blending mode and/or
2575 * translucency to a view and all its children.</p>
2576 * <p>When the application is using hardware acceleration, a software layer
2577 * is useful to render drawing primitives not supported by the hardware
2578 * accelerated pipeline. It can also be used to cache a complex view tree
2579 * into a texture and reduce the complexity of drawing operations. For instance,
2580 * when animating a complex view tree with a translation, a software layer can
2581 * be used to render the view tree only once.</p>
2582 * <p>Software layers should be avoided when the affected view tree updates
2583 * often. Every update will require to re-render the software layer, which can
2584 * potentially be slow (particularly when hardware acceleration is turned on
2585 * since the layer will have to be uploaded into a hardware texture after every
2586 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002587 *
2588 * @see #getLayerType()
2589 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002590 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002591 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002592 */
2593 public static final int LAYER_TYPE_SOFTWARE = 1;
2594
2595 /**
2596 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2597 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2598 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2599 * rendering pipeline, but only if hardware acceleration is turned on for the
2600 * view hierarchy. When hardware acceleration is turned off, hardware layers
2601 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002602 *
Romain Guy171c5922011-01-06 10:04:23 -08002603 * <p>A hardware layer is useful to apply a specific color filter and/or
2604 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002605 * <p>A hardware layer can be used to cache a complex view tree into a
2606 * texture and reduce the complexity of drawing operations. For instance,
2607 * when animating a complex view tree with a translation, a hardware layer can
2608 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002609 * <p>A hardware layer can also be used to increase the rendering quality when
2610 * rotation transformations are applied on a view. It can also be used to
2611 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002612 *
2613 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002614 * @see #setLayerType(int, android.graphics.Paint)
2615 * @see #LAYER_TYPE_NONE
2616 * @see #LAYER_TYPE_SOFTWARE
2617 */
2618 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002619
Romain Guy3aaff3a2011-01-12 14:18:47 -08002620 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2621 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2622 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2623 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2624 })
Romain Guy171c5922011-01-06 10:04:23 -08002625 int mLayerType = LAYER_TYPE_NONE;
2626 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002627 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002628
2629 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002630 * Set to true when the view is sending hover accessibility events because it
2631 * is the innermost hovered view.
2632 */
2633 private boolean mSendingHoverAccessibilityEvents;
2634
2635 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002636 * Delegate for injecting accessiblity functionality.
2637 */
2638 AccessibilityDelegate mAccessibilityDelegate;
2639
2640 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002641 * Text direction is inherited thru {@link ViewGroup}
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002642 */
2643 public static final int TEXT_DIRECTION_INHERIT = 0;
2644
2645 /**
2646 * Text direction is using "first strong algorithm". The first strong directional character
2647 * determines the paragraph direction. If there is no strong directional character, the
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002648 * paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002649 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002650 */
2651 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2652
2653 /**
2654 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2655 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002656 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002657 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002658 */
2659 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2660
2661 /**
2662 * Text direction is forced to LTR.
2663 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002664 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002665 public static final int TEXT_DIRECTION_LTR = 3;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002666
2667 /**
2668 * Text direction is forced to RTL.
2669 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002670 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002671 public static final int TEXT_DIRECTION_RTL = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002672
2673 /**
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002674 * Text direction is coming from the system Locale.
2675 *
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002676 */
2677 public static final int TEXT_DIRECTION_LOCALE = 5;
2678
2679 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002680 * Default text direction is inherited
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07002681 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002682 */
2683 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2684
2685 /**
2686 * The text direction that has been defined by {@link #setTextDirection(int)}.
2687 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002688 */
2689 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002690 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2691 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2692 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2693 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002694 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
2695 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002696 })
Doug Feltcb3791202011-07-07 11:57:48 -07002697 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002698
2699 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002700 * The resolved text direction. This needs resolution if the value is
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002701 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if it is
Doug Feltcb3791202011-07-07 11:57:48 -07002702 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2703 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002704 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002705 */
2706 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002707 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2708 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2709 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002710 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002711 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
2712 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002713 })
Doug Feltcb3791202011-07-07 11:57:48 -07002714 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002715
2716 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002717 * Consistency verifier for debugging purposes.
2718 * @hide
2719 */
2720 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2721 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2722 new InputEventConsistencyVerifier(this, 0) : null;
2723
2724 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 * Simple constructor to use when creating a view from code.
2726 *
2727 * @param context The Context the view is running in, through which it can
2728 * access the current theme, resources, etc.
2729 */
2730 public View(Context context) {
2731 mContext = context;
2732 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002733 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002734 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002735 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002736 mUserPaddingStart = -1;
2737 mUserPaddingEnd = -1;
2738 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 }
2740
2741 /**
2742 * Constructor that is called when inflating a view from XML. This is called
2743 * when a view is being constructed from an XML file, supplying attributes
2744 * that were specified in the XML file. This version uses a default style of
2745 * 0, so the only attribute values applied are those in the Context's Theme
2746 * and the given AttributeSet.
2747 *
2748 * <p>
2749 * The method onFinishInflate() will be called after all children have been
2750 * added.
2751 *
2752 * @param context The Context the view is running in, through which it can
2753 * access the current theme, resources, etc.
2754 * @param attrs The attributes of the XML tag that is inflating the view.
2755 * @see #View(Context, AttributeSet, int)
2756 */
2757 public View(Context context, AttributeSet attrs) {
2758 this(context, attrs, 0);
2759 }
2760
2761 /**
2762 * Perform inflation from XML and apply a class-specific base style. This
2763 * constructor of View allows subclasses to use their own base style when
2764 * they are inflating. For example, a Button class's constructor would call
2765 * this version of the super class constructor and supply
2766 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2767 * the theme's button style to modify all of the base view attributes (in
2768 * particular its background) as well as the Button class's attributes.
2769 *
2770 * @param context The Context the view is running in, through which it can
2771 * access the current theme, resources, etc.
2772 * @param attrs The attributes of the XML tag that is inflating the view.
2773 * @param defStyle The default style to apply to this view. If 0, no style
2774 * will be applied (beyond what is included in the theme). This may
2775 * either be an attribute resource, whose value will be retrieved
2776 * from the current theme, or an explicit style resource.
2777 * @see #View(Context, AttributeSet)
2778 */
2779 public View(Context context, AttributeSet attrs, int defStyle) {
2780 this(context);
2781
2782 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2783 defStyle, 0);
2784
2785 Drawable background = null;
2786
2787 int leftPadding = -1;
2788 int topPadding = -1;
2789 int rightPadding = -1;
2790 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002791 int startPadding = -1;
2792 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793
2794 int padding = -1;
2795
2796 int viewFlagValues = 0;
2797 int viewFlagMasks = 0;
2798
2799 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 int x = 0;
2802 int y = 0;
2803
Chet Haase73066682010-11-29 15:55:32 -08002804 float tx = 0;
2805 float ty = 0;
2806 float rotation = 0;
2807 float rotationX = 0;
2808 float rotationY = 0;
2809 float sx = 1f;
2810 float sy = 1f;
2811 boolean transformSet = false;
2812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2814
Adam Powell637d3372010-08-25 14:37:03 -07002815 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 final int N = a.getIndexCount();
2817 for (int i = 0; i < N; i++) {
2818 int attr = a.getIndex(i);
2819 switch (attr) {
2820 case com.android.internal.R.styleable.View_background:
2821 background = a.getDrawable(attr);
2822 break;
2823 case com.android.internal.R.styleable.View_padding:
2824 padding = a.getDimensionPixelSize(attr, -1);
2825 break;
2826 case com.android.internal.R.styleable.View_paddingLeft:
2827 leftPadding = a.getDimensionPixelSize(attr, -1);
2828 break;
2829 case com.android.internal.R.styleable.View_paddingTop:
2830 topPadding = a.getDimensionPixelSize(attr, -1);
2831 break;
2832 case com.android.internal.R.styleable.View_paddingRight:
2833 rightPadding = a.getDimensionPixelSize(attr, -1);
2834 break;
2835 case com.android.internal.R.styleable.View_paddingBottom:
2836 bottomPadding = a.getDimensionPixelSize(attr, -1);
2837 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002838 case com.android.internal.R.styleable.View_paddingStart:
2839 startPadding = a.getDimensionPixelSize(attr, -1);
2840 break;
2841 case com.android.internal.R.styleable.View_paddingEnd:
2842 endPadding = a.getDimensionPixelSize(attr, -1);
2843 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 case com.android.internal.R.styleable.View_scrollX:
2845 x = a.getDimensionPixelOffset(attr, 0);
2846 break;
2847 case com.android.internal.R.styleable.View_scrollY:
2848 y = a.getDimensionPixelOffset(attr, 0);
2849 break;
Chet Haase73066682010-11-29 15:55:32 -08002850 case com.android.internal.R.styleable.View_alpha:
2851 setAlpha(a.getFloat(attr, 1f));
2852 break;
2853 case com.android.internal.R.styleable.View_transformPivotX:
2854 setPivotX(a.getDimensionPixelOffset(attr, 0));
2855 break;
2856 case com.android.internal.R.styleable.View_transformPivotY:
2857 setPivotY(a.getDimensionPixelOffset(attr, 0));
2858 break;
2859 case com.android.internal.R.styleable.View_translationX:
2860 tx = a.getDimensionPixelOffset(attr, 0);
2861 transformSet = true;
2862 break;
2863 case com.android.internal.R.styleable.View_translationY:
2864 ty = a.getDimensionPixelOffset(attr, 0);
2865 transformSet = true;
2866 break;
2867 case com.android.internal.R.styleable.View_rotation:
2868 rotation = a.getFloat(attr, 0);
2869 transformSet = true;
2870 break;
2871 case com.android.internal.R.styleable.View_rotationX:
2872 rotationX = a.getFloat(attr, 0);
2873 transformSet = true;
2874 break;
2875 case com.android.internal.R.styleable.View_rotationY:
2876 rotationY = a.getFloat(attr, 0);
2877 transformSet = true;
2878 break;
2879 case com.android.internal.R.styleable.View_scaleX:
2880 sx = a.getFloat(attr, 1f);
2881 transformSet = true;
2882 break;
2883 case com.android.internal.R.styleable.View_scaleY:
2884 sy = a.getFloat(attr, 1f);
2885 transformSet = true;
2886 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 case com.android.internal.R.styleable.View_id:
2888 mID = a.getResourceId(attr, NO_ID);
2889 break;
2890 case com.android.internal.R.styleable.View_tag:
2891 mTag = a.getText(attr);
2892 break;
2893 case com.android.internal.R.styleable.View_fitsSystemWindows:
2894 if (a.getBoolean(attr, false)) {
2895 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2896 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2897 }
2898 break;
2899 case com.android.internal.R.styleable.View_focusable:
2900 if (a.getBoolean(attr, false)) {
2901 viewFlagValues |= FOCUSABLE;
2902 viewFlagMasks |= FOCUSABLE_MASK;
2903 }
2904 break;
2905 case com.android.internal.R.styleable.View_focusableInTouchMode:
2906 if (a.getBoolean(attr, false)) {
2907 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2908 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2909 }
2910 break;
2911 case com.android.internal.R.styleable.View_clickable:
2912 if (a.getBoolean(attr, false)) {
2913 viewFlagValues |= CLICKABLE;
2914 viewFlagMasks |= CLICKABLE;
2915 }
2916 break;
2917 case com.android.internal.R.styleable.View_longClickable:
2918 if (a.getBoolean(attr, false)) {
2919 viewFlagValues |= LONG_CLICKABLE;
2920 viewFlagMasks |= LONG_CLICKABLE;
2921 }
2922 break;
2923 case com.android.internal.R.styleable.View_saveEnabled:
2924 if (!a.getBoolean(attr, true)) {
2925 viewFlagValues |= SAVE_DISABLED;
2926 viewFlagMasks |= SAVE_DISABLED_MASK;
2927 }
2928 break;
2929 case com.android.internal.R.styleable.View_duplicateParentState:
2930 if (a.getBoolean(attr, false)) {
2931 viewFlagValues |= DUPLICATE_PARENT_STATE;
2932 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2933 }
2934 break;
2935 case com.android.internal.R.styleable.View_visibility:
2936 final int visibility = a.getInt(attr, 0);
2937 if (visibility != 0) {
2938 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2939 viewFlagMasks |= VISIBILITY_MASK;
2940 }
2941 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002942 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002943 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002944 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002945 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002946 final int layoutDirection = a.getInt(attr, -1);
2947 if (layoutDirection != -1) {
2948 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002949 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002950 // Set to default (LAYOUT_DIRECTION_INHERIT)
2951 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002952 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002953 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002954 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955 case com.android.internal.R.styleable.View_drawingCacheQuality:
2956 final int cacheQuality = a.getInt(attr, 0);
2957 if (cacheQuality != 0) {
2958 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2959 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2960 }
2961 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002962 case com.android.internal.R.styleable.View_contentDescription:
2963 mContentDescription = a.getString(attr);
2964 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2966 if (!a.getBoolean(attr, true)) {
2967 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2968 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2969 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002970 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2972 if (!a.getBoolean(attr, true)) {
2973 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2974 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2975 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002976 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 case R.styleable.View_scrollbars:
2978 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2979 if (scrollbars != SCROLLBARS_NONE) {
2980 viewFlagValues |= scrollbars;
2981 viewFlagMasks |= SCROLLBARS_MASK;
2982 initializeScrollbars(a);
2983 }
2984 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07002985 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07002987 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
2988 // Ignore the attribute starting with ICS
2989 break;
2990 }
2991 // With builds < ICS, fall through and apply fading edges
2992 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2994 if (fadingEdge != FADING_EDGE_NONE) {
2995 viewFlagValues |= fadingEdge;
2996 viewFlagMasks |= FADING_EDGE_MASK;
2997 initializeFadingEdge(a);
2998 }
2999 break;
3000 case R.styleable.View_scrollbarStyle:
3001 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3002 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3003 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3004 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3005 }
3006 break;
3007 case R.styleable.View_isScrollContainer:
3008 setScrollContainer = true;
3009 if (a.getBoolean(attr, false)) {
3010 setScrollContainer(true);
3011 }
3012 break;
3013 case com.android.internal.R.styleable.View_keepScreenOn:
3014 if (a.getBoolean(attr, false)) {
3015 viewFlagValues |= KEEP_SCREEN_ON;
3016 viewFlagMasks |= KEEP_SCREEN_ON;
3017 }
3018 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003019 case R.styleable.View_filterTouchesWhenObscured:
3020 if (a.getBoolean(attr, false)) {
3021 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3022 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3023 }
3024 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 case R.styleable.View_nextFocusLeft:
3026 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3027 break;
3028 case R.styleable.View_nextFocusRight:
3029 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3030 break;
3031 case R.styleable.View_nextFocusUp:
3032 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3033 break;
3034 case R.styleable.View_nextFocusDown:
3035 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3036 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003037 case R.styleable.View_nextFocusForward:
3038 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3039 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 case R.styleable.View_minWidth:
3041 mMinWidth = a.getDimensionPixelSize(attr, 0);
3042 break;
3043 case R.styleable.View_minHeight:
3044 mMinHeight = a.getDimensionPixelSize(attr, 0);
3045 break;
Romain Guy9a817362009-05-01 10:57:14 -07003046 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003047 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003048 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003049 + "be used within a restricted context");
3050 }
3051
Romain Guy9a817362009-05-01 10:57:14 -07003052 final String handlerName = a.getString(attr);
3053 if (handlerName != null) {
3054 setOnClickListener(new OnClickListener() {
3055 private Method mHandler;
3056
3057 public void onClick(View v) {
3058 if (mHandler == null) {
3059 try {
3060 mHandler = getContext().getClass().getMethod(handlerName,
3061 View.class);
3062 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003063 int id = getId();
3064 String idText = id == NO_ID ? "" : " with id '"
3065 + getContext().getResources().getResourceEntryName(
3066 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003067 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003068 handlerName + "(View) in the activity "
3069 + getContext().getClass() + " for onClick handler"
3070 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003071 }
3072 }
3073
3074 try {
3075 mHandler.invoke(getContext(), View.this);
3076 } catch (IllegalAccessException e) {
3077 throw new IllegalStateException("Could not execute non "
3078 + "public method of the activity", e);
3079 } catch (InvocationTargetException e) {
3080 throw new IllegalStateException("Could not execute "
3081 + "method of the activity", e);
3082 }
3083 }
3084 });
3085 }
3086 break;
Adam Powell637d3372010-08-25 14:37:03 -07003087 case R.styleable.View_overScrollMode:
3088 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3089 break;
Adam Powell20232d02010-12-08 21:08:53 -08003090 case R.styleable.View_verticalScrollbarPosition:
3091 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3092 break;
Romain Guy171c5922011-01-06 10:04:23 -08003093 case R.styleable.View_layerType:
3094 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3095 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003096 case R.styleable.View_textDirection:
3097 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
3098 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 }
3100 }
3101
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003102 a.recycle();
3103
Adam Powell637d3372010-08-25 14:37:03 -07003104 setOverScrollMode(overScrollMode);
3105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106 if (background != null) {
3107 setBackgroundDrawable(background);
3108 }
3109
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003110 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3111 // layout direction). Those cached values will be used later during padding resolution.
3112 mUserPaddingStart = startPadding;
3113 mUserPaddingEnd = endPadding;
3114
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003115 updateUserPaddingRelative();
3116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003117 if (padding >= 0) {
3118 leftPadding = padding;
3119 topPadding = padding;
3120 rightPadding = padding;
3121 bottomPadding = padding;
3122 }
3123
3124 // If the user specified the padding (either with android:padding or
3125 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3126 // use the default padding or the padding from the background drawable
3127 // (stored at this point in mPadding*)
3128 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3129 topPadding >= 0 ? topPadding : mPaddingTop,
3130 rightPadding >= 0 ? rightPadding : mPaddingRight,
3131 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3132
3133 if (viewFlagMasks != 0) {
3134 setFlags(viewFlagValues, viewFlagMasks);
3135 }
3136
3137 // Needs to be called after mViewFlags is set
3138 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3139 recomputePadding();
3140 }
3141
3142 if (x != 0 || y != 0) {
3143 scrollTo(x, y);
3144 }
3145
Chet Haase73066682010-11-29 15:55:32 -08003146 if (transformSet) {
3147 setTranslationX(tx);
3148 setTranslationY(ty);
3149 setRotation(rotation);
3150 setRotationX(rotationX);
3151 setRotationY(rotationY);
3152 setScaleX(sx);
3153 setScaleY(sy);
3154 }
3155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003156 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3157 setScrollContainer(true);
3158 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003159
3160 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 }
3162
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003163 private void updateUserPaddingRelative() {
3164 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3165 }
3166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 /**
3168 * Non-public constructor for use in testing
3169 */
3170 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003171 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 }
3173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 /**
3175 * <p>
3176 * Initializes the fading edges from a given set of styled attributes. This
3177 * method should be called by subclasses that need fading edges and when an
3178 * instance of these subclasses is created programmatically rather than
3179 * being inflated from XML. This method is automatically called when the XML
3180 * is inflated.
3181 * </p>
3182 *
3183 * @param a the styled attributes set to initialize the fading edges from
3184 */
3185 protected void initializeFadingEdge(TypedArray a) {
3186 initScrollCache();
3187
3188 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3189 R.styleable.View_fadingEdgeLength,
3190 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3191 }
3192
3193 /**
3194 * Returns the size of the vertical faded edges used to indicate that more
3195 * content in this view is visible.
3196 *
3197 * @return The size in pixels of the vertical faded edge or 0 if vertical
3198 * faded edges are not enabled for this view.
3199 * @attr ref android.R.styleable#View_fadingEdgeLength
3200 */
3201 public int getVerticalFadingEdgeLength() {
3202 if (isVerticalFadingEdgeEnabled()) {
3203 ScrollabilityCache cache = mScrollCache;
3204 if (cache != null) {
3205 return cache.fadingEdgeLength;
3206 }
3207 }
3208 return 0;
3209 }
3210
3211 /**
3212 * Set the size of the faded edge used to indicate that more content in this
3213 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003214 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3215 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3216 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 *
3218 * @param length The size in pixels of the faded edge used to indicate that more
3219 * content in this view is visible.
3220 */
3221 public void setFadingEdgeLength(int length) {
3222 initScrollCache();
3223 mScrollCache.fadingEdgeLength = length;
3224 }
3225
3226 /**
3227 * Returns the size of the horizontal faded edges used to indicate that more
3228 * content in this view is visible.
3229 *
3230 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3231 * faded edges are not enabled for this view.
3232 * @attr ref android.R.styleable#View_fadingEdgeLength
3233 */
3234 public int getHorizontalFadingEdgeLength() {
3235 if (isHorizontalFadingEdgeEnabled()) {
3236 ScrollabilityCache cache = mScrollCache;
3237 if (cache != null) {
3238 return cache.fadingEdgeLength;
3239 }
3240 }
3241 return 0;
3242 }
3243
3244 /**
3245 * Returns the width of the vertical scrollbar.
3246 *
3247 * @return The width in pixels of the vertical scrollbar or 0 if there
3248 * is no vertical scrollbar.
3249 */
3250 public int getVerticalScrollbarWidth() {
3251 ScrollabilityCache cache = mScrollCache;
3252 if (cache != null) {
3253 ScrollBarDrawable scrollBar = cache.scrollBar;
3254 if (scrollBar != null) {
3255 int size = scrollBar.getSize(true);
3256 if (size <= 0) {
3257 size = cache.scrollBarSize;
3258 }
3259 return size;
3260 }
3261 return 0;
3262 }
3263 return 0;
3264 }
3265
3266 /**
3267 * Returns the height of the horizontal scrollbar.
3268 *
3269 * @return The height in pixels of the horizontal scrollbar or 0 if
3270 * there is no horizontal scrollbar.
3271 */
3272 protected int getHorizontalScrollbarHeight() {
3273 ScrollabilityCache cache = mScrollCache;
3274 if (cache != null) {
3275 ScrollBarDrawable scrollBar = cache.scrollBar;
3276 if (scrollBar != null) {
3277 int size = scrollBar.getSize(false);
3278 if (size <= 0) {
3279 size = cache.scrollBarSize;
3280 }
3281 return size;
3282 }
3283 return 0;
3284 }
3285 return 0;
3286 }
3287
3288 /**
3289 * <p>
3290 * Initializes the scrollbars from a given set of styled attributes. This
3291 * method should be called by subclasses that need scrollbars and when an
3292 * instance of these subclasses is created programmatically rather than
3293 * being inflated from XML. This method is automatically called when the XML
3294 * is inflated.
3295 * </p>
3296 *
3297 * @param a the styled attributes set to initialize the scrollbars from
3298 */
3299 protected void initializeScrollbars(TypedArray a) {
3300 initScrollCache();
3301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003303
Mike Cleronf116bf82009-09-27 19:14:12 -07003304 if (scrollabilityCache.scrollBar == null) {
3305 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3306 }
Joe Malin32736f02011-01-19 16:14:20 -08003307
Romain Guy8bda2482010-03-02 11:42:11 -08003308 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309
Mike Cleronf116bf82009-09-27 19:14:12 -07003310 if (!fadeScrollbars) {
3311 scrollabilityCache.state = ScrollabilityCache.ON;
3312 }
3313 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003314
3315
Mike Cleronf116bf82009-09-27 19:14:12 -07003316 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3317 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3318 .getScrollBarFadeDuration());
3319 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3320 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3321 ViewConfiguration.getScrollDefaultDelay());
3322
Joe Malin32736f02011-01-19 16:14:20 -08003323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003324 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3325 com.android.internal.R.styleable.View_scrollbarSize,
3326 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3327
3328 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3329 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3330
3331 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3332 if (thumb != null) {
3333 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3334 }
3335
3336 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3337 false);
3338 if (alwaysDraw) {
3339 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3340 }
3341
3342 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3343 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3344
3345 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3346 if (thumb != null) {
3347 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3348 }
3349
3350 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3351 false);
3352 if (alwaysDraw) {
3353 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3354 }
3355
3356 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003357 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358 }
3359
3360 /**
3361 * <p>
3362 * Initalizes the scrollability cache if necessary.
3363 * </p>
3364 */
3365 private void initScrollCache() {
3366 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003367 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003368 }
3369 }
3370
3371 /**
Adam Powell20232d02010-12-08 21:08:53 -08003372 * Set the position of the vertical scroll bar. Should be one of
3373 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3374 * {@link #SCROLLBAR_POSITION_RIGHT}.
3375 *
3376 * @param position Where the vertical scroll bar should be positioned.
3377 */
3378 public void setVerticalScrollbarPosition(int position) {
3379 if (mVerticalScrollbarPosition != position) {
3380 mVerticalScrollbarPosition = position;
3381 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003382 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003383 }
3384 }
3385
3386 /**
3387 * @return The position where the vertical scroll bar will show, if applicable.
3388 * @see #setVerticalScrollbarPosition(int)
3389 */
3390 public int getVerticalScrollbarPosition() {
3391 return mVerticalScrollbarPosition;
3392 }
3393
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003394 ListenerInfo getListenerInfo() {
3395 if (mListenerInfo != null) {
3396 return mListenerInfo;
3397 }
3398 mListenerInfo = new ListenerInfo();
3399 return mListenerInfo;
3400 }
3401
Adam Powell20232d02010-12-08 21:08:53 -08003402 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003403 * Register a callback to be invoked when focus of this view changed.
3404 *
3405 * @param l The callback that will run.
3406 */
3407 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003408 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409 }
3410
3411 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003412 * Add a listener that will be called when the bounds of the view change due to
3413 * layout processing.
3414 *
3415 * @param listener The listener that will be called when layout bounds change.
3416 */
3417 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003418 ListenerInfo li = getListenerInfo();
3419 if (li.mOnLayoutChangeListeners == null) {
3420 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003421 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003422 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3423 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003424 }
Chet Haase21cd1382010-09-01 17:42:29 -07003425 }
3426
3427 /**
3428 * Remove a listener for layout changes.
3429 *
3430 * @param listener The listener for layout bounds change.
3431 */
3432 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003433 ListenerInfo li = mListenerInfo;
3434 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003435 return;
3436 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003437 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003438 }
3439
3440 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003441 * Add a listener for attach state changes.
3442 *
3443 * This listener will be called whenever this view is attached or detached
3444 * from a window. Remove the listener using
3445 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3446 *
3447 * @param listener Listener to attach
3448 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3449 */
3450 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003451 ListenerInfo li = getListenerInfo();
3452 if (li.mOnAttachStateChangeListeners == null) {
3453 li.mOnAttachStateChangeListeners
3454 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003455 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003456 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003457 }
3458
3459 /**
3460 * Remove a listener for attach state changes. The listener will receive no further
3461 * notification of window attach/detach events.
3462 *
3463 * @param listener Listener to remove
3464 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3465 */
3466 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003467 ListenerInfo li = mListenerInfo;
3468 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003469 return;
3470 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003471 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003472 }
3473
3474 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003475 * Returns the focus-change callback registered for this view.
3476 *
3477 * @return The callback, or null if one is not registered.
3478 */
3479 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003480 ListenerInfo li = mListenerInfo;
3481 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003482 }
3483
3484 /**
3485 * Register a callback to be invoked when this view is clicked. If this view is not
3486 * clickable, it becomes clickable.
3487 *
3488 * @param l The callback that will run
3489 *
3490 * @see #setClickable(boolean)
3491 */
3492 public void setOnClickListener(OnClickListener l) {
3493 if (!isClickable()) {
3494 setClickable(true);
3495 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003496 getListenerInfo().mOnClickListener = l;
3497 }
3498
3499 /**
3500 * Return whether this view has an attached OnClickListener. Returns
3501 * true if there is a listener, false if there is none.
3502 */
3503 public boolean hasOnClickListeners() {
3504 ListenerInfo li = mListenerInfo;
3505 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003506 }
3507
3508 /**
3509 * Register a callback to be invoked when this view is clicked and held. If this view is not
3510 * long clickable, it becomes long clickable.
3511 *
3512 * @param l The callback that will run
3513 *
3514 * @see #setLongClickable(boolean)
3515 */
3516 public void setOnLongClickListener(OnLongClickListener l) {
3517 if (!isLongClickable()) {
3518 setLongClickable(true);
3519 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003520 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003521 }
3522
3523 /**
3524 * Register a callback to be invoked when the context menu for this view is
3525 * being built. If this view is not long clickable, it becomes long clickable.
3526 *
3527 * @param l The callback that will run
3528 *
3529 */
3530 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3531 if (!isLongClickable()) {
3532 setLongClickable(true);
3533 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003534 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003535 }
3536
3537 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003538 * Call this view's OnClickListener, if it is defined. Performs all normal
3539 * actions associated with clicking: reporting accessibility event, playing
3540 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003541 *
3542 * @return True there was an assigned OnClickListener that was called, false
3543 * otherwise is returned.
3544 */
3545 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003546 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3547
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003548 ListenerInfo li = mListenerInfo;
3549 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003551 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003552 return true;
3553 }
3554
3555 return false;
3556 }
3557
3558 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003559 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3560 * this only calls the listener, and does not do any associated clicking
3561 * actions like reporting an accessibility event.
3562 *
3563 * @return True there was an assigned OnClickListener that was called, false
3564 * otherwise is returned.
3565 */
3566 public boolean callOnClick() {
3567 ListenerInfo li = mListenerInfo;
3568 if (li != null && li.mOnClickListener != null) {
3569 li.mOnClickListener.onClick(this);
3570 return true;
3571 }
3572 return false;
3573 }
3574
3575 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003576 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3577 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003578 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003579 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003580 */
3581 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003582 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003584 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003585 ListenerInfo li = mListenerInfo;
3586 if (li != null && li.mOnLongClickListener != null) {
3587 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003588 }
3589 if (!handled) {
3590 handled = showContextMenu();
3591 }
3592 if (handled) {
3593 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3594 }
3595 return handled;
3596 }
3597
3598 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003599 * Performs button-related actions during a touch down event.
3600 *
3601 * @param event The event.
3602 * @return True if the down was consumed.
3603 *
3604 * @hide
3605 */
3606 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3607 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3608 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3609 return true;
3610 }
3611 }
3612 return false;
3613 }
3614
3615 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003616 * Bring up the context menu for this view.
3617 *
3618 * @return Whether a context menu was displayed.
3619 */
3620 public boolean showContextMenu() {
3621 return getParent().showContextMenuForChild(this);
3622 }
3623
3624 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003625 * Bring up the context menu for this view, referring to the item under the specified point.
3626 *
3627 * @param x The referenced x coordinate.
3628 * @param y The referenced y coordinate.
3629 * @param metaState The keyboard modifiers that were pressed.
3630 * @return Whether a context menu was displayed.
3631 *
3632 * @hide
3633 */
3634 public boolean showContextMenu(float x, float y, int metaState) {
3635 return showContextMenu();
3636 }
3637
3638 /**
Adam Powell6e346362010-07-23 10:18:23 -07003639 * Start an action mode.
3640 *
3641 * @param callback Callback that will control the lifecycle of the action mode
3642 * @return The new action mode if it is started, null otherwise
3643 *
3644 * @see ActionMode
3645 */
3646 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08003647 ViewParent parent = getParent();
3648 if (parent == null) return null;
3649 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07003650 }
3651
3652 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003653 * Register a callback to be invoked when a key is pressed in this view.
3654 * @param l the key listener to attach to this view
3655 */
3656 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003657 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003658 }
3659
3660 /**
3661 * Register a callback to be invoked when a touch event is sent to this view.
3662 * @param l the touch listener to attach to this view
3663 */
3664 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003665 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003666 }
3667
3668 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003669 * Register a callback to be invoked when a generic motion event is sent to this view.
3670 * @param l the generic motion listener to attach to this view
3671 */
3672 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003673 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08003674 }
3675
3676 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003677 * Register a callback to be invoked when a hover event is sent to this view.
3678 * @param l the hover listener to attach to this view
3679 */
3680 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003681 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07003682 }
3683
3684 /**
Joe Malin32736f02011-01-19 16:14:20 -08003685 * Register a drag event listener callback object for this View. The parameter is
3686 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3687 * View, the system calls the
3688 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3689 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003690 */
3691 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003692 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07003693 }
3694
3695 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003696 * Give this view focus. This will cause
3697 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003698 *
3699 * Note: this does not check whether this {@link View} should get focus, it just
3700 * gives it focus no matter what. It should only be called internally by framework
3701 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3702 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003703 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3704 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003705 * focus moved when requestFocus() is called. It may not always
3706 * apply, in which case use the default View.FOCUS_DOWN.
3707 * @param previouslyFocusedRect The rectangle of the view that had focus
3708 * prior in this View's coordinate system.
3709 */
3710 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3711 if (DBG) {
3712 System.out.println(this + " requestFocus()");
3713 }
3714
3715 if ((mPrivateFlags & FOCUSED) == 0) {
3716 mPrivateFlags |= FOCUSED;
3717
3718 if (mParent != null) {
3719 mParent.requestChildFocus(this, this);
3720 }
3721
3722 onFocusChanged(true, direction, previouslyFocusedRect);
3723 refreshDrawableState();
3724 }
3725 }
3726
3727 /**
3728 * Request that a rectangle of this view be visible on the screen,
3729 * scrolling if necessary just enough.
3730 *
3731 * <p>A View should call this if it maintains some notion of which part
3732 * of its content is interesting. For example, a text editing view
3733 * should call this when its cursor moves.
3734 *
3735 * @param rectangle The rectangle.
3736 * @return Whether any parent scrolled.
3737 */
3738 public boolean requestRectangleOnScreen(Rect rectangle) {
3739 return requestRectangleOnScreen(rectangle, false);
3740 }
3741
3742 /**
3743 * Request that a rectangle of this view be visible on the screen,
3744 * scrolling if necessary just enough.
3745 *
3746 * <p>A View should call this if it maintains some notion of which part
3747 * of its content is interesting. For example, a text editing view
3748 * should call this when its cursor moves.
3749 *
3750 * <p>When <code>immediate</code> is set to true, scrolling will not be
3751 * animated.
3752 *
3753 * @param rectangle The rectangle.
3754 * @param immediate True to forbid animated scrolling, false otherwise
3755 * @return Whether any parent scrolled.
3756 */
3757 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3758 View child = this;
3759 ViewParent parent = mParent;
3760 boolean scrolled = false;
3761 while (parent != null) {
3762 scrolled |= parent.requestChildRectangleOnScreen(child,
3763 rectangle, immediate);
3764
3765 // offset rect so next call has the rectangle in the
3766 // coordinate system of its direct child.
3767 rectangle.offset(child.getLeft(), child.getTop());
3768 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3769
3770 if (!(parent instanceof View)) {
3771 break;
3772 }
Romain Guy8506ab42009-06-11 17:35:47 -07003773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003774 child = (View) parent;
3775 parent = child.getParent();
3776 }
3777 return scrolled;
3778 }
3779
3780 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08003781 * Called when this view wants to give up focus. If focus is cleared
3782 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
3783 * <p>
3784 * <strong>Note:</strong> When a View clears focus the framework is trying
3785 * to give focus to the first focusable View from the top. Hence, if this
3786 * View is the first from the top that can take focus, then its focus will
3787 * not be cleared nor will the focus change callback be invoked.
3788 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003789 */
3790 public void clearFocus() {
3791 if (DBG) {
3792 System.out.println(this + " clearFocus()");
3793 }
3794
3795 if ((mPrivateFlags & FOCUSED) != 0) {
3796 mPrivateFlags &= ~FOCUSED;
3797
3798 if (mParent != null) {
3799 mParent.clearChildFocus(this);
3800 }
3801
3802 onFocusChanged(false, 0, null);
3803 refreshDrawableState();
3804 }
3805 }
3806
3807 /**
3808 * Called to clear the focus of a view that is about to be removed.
3809 * Doesn't call clearChildFocus, which prevents this view from taking
3810 * focus again before it has been removed from the parent
3811 */
3812 void clearFocusForRemoval() {
3813 if ((mPrivateFlags & FOCUSED) != 0) {
3814 mPrivateFlags &= ~FOCUSED;
3815
3816 onFocusChanged(false, 0, null);
3817 refreshDrawableState();
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08003818
3819 // The view cleared focus and invoked the callbacks, so now is the
3820 // time to give focus to the the first focusable from the top to
3821 // ensure that the gain focus is announced after clear focus.
3822 getRootView().requestFocus(FOCUS_FORWARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823 }
3824 }
3825
3826 /**
3827 * Called internally by the view system when a new view is getting focus.
3828 * This is what clears the old focus.
3829 */
3830 void unFocus() {
3831 if (DBG) {
3832 System.out.println(this + " unFocus()");
3833 }
3834
3835 if ((mPrivateFlags & FOCUSED) != 0) {
3836 mPrivateFlags &= ~FOCUSED;
3837
3838 onFocusChanged(false, 0, null);
3839 refreshDrawableState();
3840 }
3841 }
3842
3843 /**
3844 * Returns true if this view has focus iteself, or is the ancestor of the
3845 * view that has focus.
3846 *
3847 * @return True if this view has or contains focus, false otherwise.
3848 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003849 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003850 public boolean hasFocus() {
3851 return (mPrivateFlags & FOCUSED) != 0;
3852 }
3853
3854 /**
3855 * Returns true if this view is focusable or if it contains a reachable View
3856 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3857 * is a View whose parents do not block descendants focus.
3858 *
3859 * Only {@link #VISIBLE} views are considered focusable.
3860 *
3861 * @return True if the view is focusable or if the view contains a focusable
3862 * View, false otherwise.
3863 *
3864 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3865 */
3866 public boolean hasFocusable() {
3867 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3868 }
3869
3870 /**
3871 * Called by the view system when the focus state of this view changes.
3872 * When the focus change event is caused by directional navigation, direction
3873 * and previouslyFocusedRect provide insight into where the focus is coming from.
3874 * When overriding, be sure to call up through to the super class so that
3875 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003876 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003877 * @param gainFocus True if the View has focus; false otherwise.
3878 * @param direction The direction focus has moved when requestFocus()
3879 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003880 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3881 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3882 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003883 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3884 * system, of the previously focused view. If applicable, this will be
3885 * passed in as finer grained information about where the focus is coming
3886 * from (in addition to direction). Will be <code>null</code> otherwise.
3887 */
3888 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003889 if (gainFocus) {
3890 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3891 }
3892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003893 InputMethodManager imm = InputMethodManager.peekInstance();
3894 if (!gainFocus) {
3895 if (isPressed()) {
3896 setPressed(false);
3897 }
3898 if (imm != null && mAttachInfo != null
3899 && mAttachInfo.mHasWindowFocus) {
3900 imm.focusOut(this);
3901 }
Romain Guya2431d02009-04-30 16:30:00 -07003902 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003903 } else if (imm != null && mAttachInfo != null
3904 && mAttachInfo.mHasWindowFocus) {
3905 imm.focusIn(this);
3906 }
Romain Guy8506ab42009-06-11 17:35:47 -07003907
Romain Guy0fd89bf2011-01-26 15:41:30 -08003908 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003909 ListenerInfo li = mListenerInfo;
3910 if (li != null && li.mOnFocusChangeListener != null) {
3911 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003912 }
Joe Malin32736f02011-01-19 16:14:20 -08003913
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003914 if (mAttachInfo != null) {
3915 mAttachInfo.mKeyDispatchState.reset(this);
3916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003917 }
3918
3919 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003920 * Sends an accessibility event of the given type. If accessiiblity is
3921 * not enabled this method has no effect. The default implementation calls
3922 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3923 * to populate information about the event source (this View), then calls
3924 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3925 * populate the text content of the event source including its descendants,
3926 * and last calls
3927 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3928 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003929 * <p>
3930 * If an {@link AccessibilityDelegate} has been specified via calling
3931 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3932 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
3933 * responsible for handling this call.
3934 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003935 *
Scott Mainb303d832011-10-12 16:45:18 -07003936 * @param eventType The type of the event to send, as defined by several types from
3937 * {@link android.view.accessibility.AccessibilityEvent}, such as
3938 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
3939 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003940 *
3941 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3942 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3943 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003944 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07003945 */
3946 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003947 if (mAccessibilityDelegate != null) {
3948 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
3949 } else {
3950 sendAccessibilityEventInternal(eventType);
3951 }
3952 }
3953
3954 /**
3955 * @see #sendAccessibilityEvent(int)
3956 *
3957 * Note: Called from the default {@link AccessibilityDelegate}.
3958 */
3959 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003960 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3961 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3962 }
3963 }
3964
3965 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003966 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3967 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003968 * perform a check whether accessibility is enabled.
3969 * <p>
3970 * If an {@link AccessibilityDelegate} has been specified via calling
3971 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3972 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
3973 * is responsible for handling this call.
3974 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003975 *
3976 * @param event The event to send.
3977 *
3978 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003979 */
3980 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003981 if (mAccessibilityDelegate != null) {
3982 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
3983 } else {
3984 sendAccessibilityEventUncheckedInternal(event);
3985 }
3986 }
3987
3988 /**
3989 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
3990 *
3991 * Note: Called from the default {@link AccessibilityDelegate}.
3992 */
3993 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003994 if (!isShown()) {
3995 return;
3996 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003997 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07003998 // Only a subset of accessibility events populates text content.
3999 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4000 dispatchPopulateAccessibilityEvent(event);
4001 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004002 // In the beginning we called #isShown(), so we know that getParent() is not null.
4003 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004004 }
4005
4006 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004007 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4008 * to its children for adding their text content to the event. Note that the
4009 * event text is populated in a separate dispatch path since we add to the
4010 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004011 * A typical implementation will call
4012 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4013 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4014 * on each child. Override this method if custom population of the event text
4015 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004016 * <p>
4017 * If an {@link AccessibilityDelegate} has been specified via calling
4018 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4019 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4020 * is responsible for handling this call.
4021 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004022 * <p>
4023 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4024 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4025 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004026 *
4027 * @param event The event.
4028 *
4029 * @return True if the event population was completed.
4030 */
4031 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004032 if (mAccessibilityDelegate != null) {
4033 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4034 } else {
4035 return dispatchPopulateAccessibilityEventInternal(event);
4036 }
4037 }
4038
4039 /**
4040 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4041 *
4042 * Note: Called from the default {@link AccessibilityDelegate}.
4043 */
4044 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004045 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004046 return false;
4047 }
4048
4049 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004050 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004051 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004052 * text content. While this method is free to modify event
4053 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004054 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4055 * <p>
4056 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004057 * to the text added by the super implementation:
4058 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004059 * super.onPopulateAccessibilityEvent(event);
4060 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4061 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4062 * mCurrentDate.getTimeInMillis(), flags);
4063 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004064 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004065 * <p>
4066 * If an {@link AccessibilityDelegate} has been specified via calling
4067 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4068 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4069 * is responsible for handling this call.
4070 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004071 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4072 * information to the event, in case the default implementation has basic information to add.
4073 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004074 *
4075 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004076 *
4077 * @see #sendAccessibilityEvent(int)
4078 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004079 */
4080 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004081 if (mAccessibilityDelegate != null) {
4082 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4083 } else {
4084 onPopulateAccessibilityEventInternal(event);
4085 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004086 }
4087
4088 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004089 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4090 *
4091 * Note: Called from the default {@link AccessibilityDelegate}.
4092 */
4093 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4094
4095 }
4096
4097 /**
4098 * Initializes an {@link AccessibilityEvent} with information about
4099 * this View which is the event source. In other words, the source of
4100 * an accessibility event is the view whose state change triggered firing
4101 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004102 * <p>
4103 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004104 * to properties set by the super implementation:
4105 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4106 * super.onInitializeAccessibilityEvent(event);
4107 * event.setPassword(true);
4108 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004109 * <p>
4110 * If an {@link AccessibilityDelegate} has been specified via calling
4111 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4112 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4113 * is responsible for handling this call.
4114 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004115 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4116 * information to the event, in case the default implementation has basic information to add.
4117 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004118 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004119 *
4120 * @see #sendAccessibilityEvent(int)
4121 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4122 */
4123 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004124 if (mAccessibilityDelegate != null) {
4125 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4126 } else {
4127 onInitializeAccessibilityEventInternal(event);
4128 }
4129 }
4130
4131 /**
4132 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4133 *
4134 * Note: Called from the default {@link AccessibilityDelegate}.
4135 */
4136 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004137 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004138 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004139 event.setPackageName(getContext().getPackageName());
4140 event.setEnabled(isEnabled());
4141 event.setContentDescription(mContentDescription);
4142
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004143 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
4144 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4145 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4146 FOCUSABLES_ALL);
4147 event.setItemCount(focusablesTempList.size());
4148 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4149 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004150 }
4151 }
4152
4153 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004154 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4155 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4156 * This method is responsible for obtaining an accessibility node info from a
4157 * pool of reusable instances and calling
4158 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4159 * initialize the former.
4160 * <p>
4161 * Note: The client is responsible for recycling the obtained instance by calling
4162 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4163 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004164 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004165 * @return A populated {@link AccessibilityNodeInfo}.
4166 *
4167 * @see AccessibilityNodeInfo
4168 */
4169 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004170 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4171 if (provider != null) {
4172 return provider.createAccessibilityNodeInfo(View.NO_ID);
4173 } else {
4174 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4175 onInitializeAccessibilityNodeInfo(info);
4176 return info;
4177 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004178 }
4179
4180 /**
4181 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4182 * The base implementation sets:
4183 * <ul>
4184 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004185 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4186 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004187 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4188 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4189 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4190 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4191 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4192 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4193 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4194 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4195 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4196 * </ul>
4197 * <p>
4198 * Subclasses should override this method, call the super implementation,
4199 * and set additional attributes.
4200 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004201 * <p>
4202 * If an {@link AccessibilityDelegate} has been specified via calling
4203 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4204 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4205 * is responsible for handling this call.
4206 * </p>
4207 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004208 * @param info The instance to initialize.
4209 */
4210 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004211 if (mAccessibilityDelegate != null) {
4212 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4213 } else {
4214 onInitializeAccessibilityNodeInfoInternal(info);
4215 }
4216 }
4217
4218 /**
4219 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4220 *
4221 * Note: Called from the default {@link AccessibilityDelegate}.
4222 */
4223 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004224 Rect bounds = mAttachInfo.mTmpInvalRect;
4225 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004226 info.setBoundsInParent(bounds);
4227
4228 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4229 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004230 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004231 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4232 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004233
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004234 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
4235 ViewParent parent = getParent();
4236 if (parent instanceof View) {
4237 View parentView = (View) parent;
4238 info.setParent(parentView);
4239 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004240 }
4241
4242 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004243 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004244 info.setContentDescription(getContentDescription());
4245
4246 info.setEnabled(isEnabled());
4247 info.setClickable(isClickable());
4248 info.setFocusable(isFocusable());
4249 info.setFocused(isFocused());
4250 info.setSelected(isSelected());
4251 info.setLongClickable(isLongClickable());
4252
4253 // TODO: These make sense only if we are in an AdapterView but all
4254 // views can be selected. Maybe from accessiiblity perspective
4255 // we should report as selectable view in an AdapterView.
4256 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4257 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4258
4259 if (isFocusable()) {
4260 if (isFocused()) {
4261 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4262 } else {
4263 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4264 }
4265 }
4266 }
4267
4268 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004269 * Sets a delegate for implementing accessibility support via compositon as
4270 * opposed to inheritance. The delegate's primary use is for implementing
4271 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4272 *
4273 * @param delegate The delegate instance.
4274 *
4275 * @see AccessibilityDelegate
4276 */
4277 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4278 mAccessibilityDelegate = delegate;
4279 }
4280
4281 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004282 * Gets the provider for managing a virtual view hierarchy rooted at this View
4283 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4284 * that explore the window content.
4285 * <p>
4286 * If this method returns an instance, this instance is responsible for managing
4287 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4288 * View including the one representing the View itself. Similarly the returned
4289 * instance is responsible for performing accessibility actions on any virtual
4290 * view or the root view itself.
4291 * </p>
4292 * <p>
4293 * If an {@link AccessibilityDelegate} has been specified via calling
4294 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4295 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4296 * is responsible for handling this call.
4297 * </p>
4298 *
4299 * @return The provider.
4300 *
4301 * @see AccessibilityNodeProvider
4302 */
4303 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4304 if (mAccessibilityDelegate != null) {
4305 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4306 } else {
4307 return null;
4308 }
4309 }
4310
4311 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004312 * Gets the unique identifier of this view on the screen for accessibility purposes.
4313 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4314 *
4315 * @return The view accessibility id.
4316 *
4317 * @hide
4318 */
4319 public int getAccessibilityViewId() {
4320 if (mAccessibilityViewId == NO_ID) {
4321 mAccessibilityViewId = sNextAccessibilityViewId++;
4322 }
4323 return mAccessibilityViewId;
4324 }
4325
4326 /**
4327 * Gets the unique identifier of the window in which this View reseides.
4328 *
4329 * @return The window accessibility id.
4330 *
4331 * @hide
4332 */
4333 public int getAccessibilityWindowId() {
4334 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4335 }
4336
4337 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004338 * Gets the {@link View} description. It briefly describes the view and is
4339 * primarily used for accessibility support. Set this property to enable
4340 * better accessibility support for your application. This is especially
4341 * true for views that do not have textual representation (For example,
4342 * ImageButton).
4343 *
4344 * @return The content descriptiopn.
4345 *
4346 * @attr ref android.R.styleable#View_contentDescription
4347 */
4348 public CharSequence getContentDescription() {
4349 return mContentDescription;
4350 }
4351
4352 /**
4353 * Sets the {@link View} description. It briefly describes the view and is
4354 * primarily used for accessibility support. Set this property to enable
4355 * better accessibility support for your application. This is especially
4356 * true for views that do not have textual representation (For example,
4357 * ImageButton).
4358 *
4359 * @param contentDescription The content description.
4360 *
4361 * @attr ref android.R.styleable#View_contentDescription
4362 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004363 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004364 public void setContentDescription(CharSequence contentDescription) {
4365 mContentDescription = contentDescription;
4366 }
4367
4368 /**
Romain Guya2431d02009-04-30 16:30:00 -07004369 * Invoked whenever this view loses focus, either by losing window focus or by losing
4370 * focus within its window. This method can be used to clear any state tied to the
4371 * focus. For instance, if a button is held pressed with the trackball and the window
4372 * loses focus, this method can be used to cancel the press.
4373 *
4374 * Subclasses of View overriding this method should always call super.onFocusLost().
4375 *
4376 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004377 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004378 *
4379 * @hide pending API council approval
4380 */
4381 protected void onFocusLost() {
4382 resetPressedState();
4383 }
4384
4385 private void resetPressedState() {
4386 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4387 return;
4388 }
4389
4390 if (isPressed()) {
4391 setPressed(false);
4392
4393 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004394 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004395 }
4396 }
4397 }
4398
4399 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004400 * Returns true if this view has focus
4401 *
4402 * @return True if this view has focus, false otherwise.
4403 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004404 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004405 public boolean isFocused() {
4406 return (mPrivateFlags & FOCUSED) != 0;
4407 }
4408
4409 /**
4410 * Find the view in the hierarchy rooted at this view that currently has
4411 * focus.
4412 *
4413 * @return The view that currently has focus, or null if no focused view can
4414 * be found.
4415 */
4416 public View findFocus() {
4417 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4418 }
4419
4420 /**
4421 * Change whether this view is one of the set of scrollable containers in
4422 * its window. This will be used to determine whether the window can
4423 * resize or must pan when a soft input area is open -- scrollable
4424 * containers allow the window to use resize mode since the container
4425 * will appropriately shrink.
4426 */
4427 public void setScrollContainer(boolean isScrollContainer) {
4428 if (isScrollContainer) {
4429 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4430 mAttachInfo.mScrollContainers.add(this);
4431 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4432 }
4433 mPrivateFlags |= SCROLL_CONTAINER;
4434 } else {
4435 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4436 mAttachInfo.mScrollContainers.remove(this);
4437 }
4438 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4439 }
4440 }
4441
4442 /**
4443 * Returns the quality of the drawing cache.
4444 *
4445 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4446 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4447 *
4448 * @see #setDrawingCacheQuality(int)
4449 * @see #setDrawingCacheEnabled(boolean)
4450 * @see #isDrawingCacheEnabled()
4451 *
4452 * @attr ref android.R.styleable#View_drawingCacheQuality
4453 */
4454 public int getDrawingCacheQuality() {
4455 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4456 }
4457
4458 /**
4459 * Set the drawing cache quality of this view. This value is used only when the
4460 * drawing cache is enabled
4461 *
4462 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4463 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4464 *
4465 * @see #getDrawingCacheQuality()
4466 * @see #setDrawingCacheEnabled(boolean)
4467 * @see #isDrawingCacheEnabled()
4468 *
4469 * @attr ref android.R.styleable#View_drawingCacheQuality
4470 */
4471 public void setDrawingCacheQuality(int quality) {
4472 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4473 }
4474
4475 /**
4476 * Returns whether the screen should remain on, corresponding to the current
4477 * value of {@link #KEEP_SCREEN_ON}.
4478 *
4479 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4480 *
4481 * @see #setKeepScreenOn(boolean)
4482 *
4483 * @attr ref android.R.styleable#View_keepScreenOn
4484 */
4485 public boolean getKeepScreenOn() {
4486 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4487 }
4488
4489 /**
4490 * Controls whether the screen should remain on, modifying the
4491 * value of {@link #KEEP_SCREEN_ON}.
4492 *
4493 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4494 *
4495 * @see #getKeepScreenOn()
4496 *
4497 * @attr ref android.R.styleable#View_keepScreenOn
4498 */
4499 public void setKeepScreenOn(boolean keepScreenOn) {
4500 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4501 }
4502
4503 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004504 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4505 * @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 -08004506 *
4507 * @attr ref android.R.styleable#View_nextFocusLeft
4508 */
4509 public int getNextFocusLeftId() {
4510 return mNextFocusLeftId;
4511 }
4512
4513 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004514 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4515 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4516 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004517 *
4518 * @attr ref android.R.styleable#View_nextFocusLeft
4519 */
4520 public void setNextFocusLeftId(int nextFocusLeftId) {
4521 mNextFocusLeftId = nextFocusLeftId;
4522 }
4523
4524 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004525 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4526 * @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 -08004527 *
4528 * @attr ref android.R.styleable#View_nextFocusRight
4529 */
4530 public int getNextFocusRightId() {
4531 return mNextFocusRightId;
4532 }
4533
4534 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004535 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4536 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4537 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004538 *
4539 * @attr ref android.R.styleable#View_nextFocusRight
4540 */
4541 public void setNextFocusRightId(int nextFocusRightId) {
4542 mNextFocusRightId = nextFocusRightId;
4543 }
4544
4545 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004546 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4547 * @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 -08004548 *
4549 * @attr ref android.R.styleable#View_nextFocusUp
4550 */
4551 public int getNextFocusUpId() {
4552 return mNextFocusUpId;
4553 }
4554
4555 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004556 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4557 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4558 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004559 *
4560 * @attr ref android.R.styleable#View_nextFocusUp
4561 */
4562 public void setNextFocusUpId(int nextFocusUpId) {
4563 mNextFocusUpId = nextFocusUpId;
4564 }
4565
4566 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004567 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4568 * @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 -08004569 *
4570 * @attr ref android.R.styleable#View_nextFocusDown
4571 */
4572 public int getNextFocusDownId() {
4573 return mNextFocusDownId;
4574 }
4575
4576 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004577 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4578 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4579 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004580 *
4581 * @attr ref android.R.styleable#View_nextFocusDown
4582 */
4583 public void setNextFocusDownId(int nextFocusDownId) {
4584 mNextFocusDownId = nextFocusDownId;
4585 }
4586
4587 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004588 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4589 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4590 *
4591 * @attr ref android.R.styleable#View_nextFocusForward
4592 */
4593 public int getNextFocusForwardId() {
4594 return mNextFocusForwardId;
4595 }
4596
4597 /**
4598 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4599 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4600 * decide automatically.
4601 *
4602 * @attr ref android.R.styleable#View_nextFocusForward
4603 */
4604 public void setNextFocusForwardId(int nextFocusForwardId) {
4605 mNextFocusForwardId = nextFocusForwardId;
4606 }
4607
4608 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004609 * Returns the visibility of this view and all of its ancestors
4610 *
4611 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4612 */
4613 public boolean isShown() {
4614 View current = this;
4615 //noinspection ConstantConditions
4616 do {
4617 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4618 return false;
4619 }
4620 ViewParent parent = current.mParent;
4621 if (parent == null) {
4622 return false; // We are not attached to the view root
4623 }
4624 if (!(parent instanceof View)) {
4625 return true;
4626 }
4627 current = (View) parent;
4628 } while (current != null);
4629
4630 return false;
4631 }
4632
4633 /**
4634 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4635 * is set
4636 *
4637 * @param insets Insets for system windows
4638 *
4639 * @return True if this view applied the insets, false otherwise
4640 */
4641 protected boolean fitSystemWindows(Rect insets) {
4642 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4643 mPaddingLeft = insets.left;
4644 mPaddingTop = insets.top;
4645 mPaddingRight = insets.right;
4646 mPaddingBottom = insets.bottom;
4647 requestLayout();
4648 return true;
4649 }
4650 return false;
4651 }
4652
4653 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004654 * Set whether or not this view should account for system screen decorations
4655 * such as the status bar and inset its content. This allows this view to be
4656 * positioned in absolute screen coordinates and remain visible to the user.
4657 *
4658 * <p>This should only be used by top-level window decor views.
4659 *
4660 * @param fitSystemWindows true to inset content for system screen decorations, false for
4661 * default behavior.
4662 *
4663 * @attr ref android.R.styleable#View_fitsSystemWindows
4664 */
4665 public void setFitsSystemWindows(boolean fitSystemWindows) {
4666 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4667 }
4668
4669 /**
4670 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4671 * will account for system screen decorations such as the status bar and inset its
4672 * content. This allows the view to be positioned in absolute screen coordinates
4673 * and remain visible to the user.
4674 *
4675 * @return true if this view will adjust its content bounds for system screen decorations.
4676 *
4677 * @attr ref android.R.styleable#View_fitsSystemWindows
4678 */
4679 public boolean fitsSystemWindows() {
4680 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4681 }
4682
4683 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004684 * Returns the visibility status for this view.
4685 *
4686 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4687 * @attr ref android.R.styleable#View_visibility
4688 */
4689 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004690 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4691 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4692 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004693 })
4694 public int getVisibility() {
4695 return mViewFlags & VISIBILITY_MASK;
4696 }
4697
4698 /**
4699 * Set the enabled state of this view.
4700 *
4701 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4702 * @attr ref android.R.styleable#View_visibility
4703 */
4704 @RemotableViewMethod
4705 public void setVisibility(int visibility) {
4706 setFlags(visibility, VISIBILITY_MASK);
4707 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4708 }
4709
4710 /**
4711 * Returns the enabled status for this view. The interpretation of the
4712 * enabled state varies by subclass.
4713 *
4714 * @return True if this view is enabled, false otherwise.
4715 */
4716 @ViewDebug.ExportedProperty
4717 public boolean isEnabled() {
4718 return (mViewFlags & ENABLED_MASK) == ENABLED;
4719 }
4720
4721 /**
4722 * Set the enabled state of this view. The interpretation of the enabled
4723 * state varies by subclass.
4724 *
4725 * @param enabled True if this view is enabled, false otherwise.
4726 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004727 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004728 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004729 if (enabled == isEnabled()) return;
4730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004731 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4732
4733 /*
4734 * The View most likely has to change its appearance, so refresh
4735 * the drawable state.
4736 */
4737 refreshDrawableState();
4738
4739 // Invalidate too, since the default behavior for views is to be
4740 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004741 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004742 }
4743
4744 /**
4745 * Set whether this view can receive the focus.
4746 *
4747 * Setting this to false will also ensure that this view is not focusable
4748 * in touch mode.
4749 *
4750 * @param focusable If true, this view can receive the focus.
4751 *
4752 * @see #setFocusableInTouchMode(boolean)
4753 * @attr ref android.R.styleable#View_focusable
4754 */
4755 public void setFocusable(boolean focusable) {
4756 if (!focusable) {
4757 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4758 }
4759 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4760 }
4761
4762 /**
4763 * Set whether this view can receive focus while in touch mode.
4764 *
4765 * Setting this to true will also ensure that this view is focusable.
4766 *
4767 * @param focusableInTouchMode If true, this view can receive the focus while
4768 * in touch mode.
4769 *
4770 * @see #setFocusable(boolean)
4771 * @attr ref android.R.styleable#View_focusableInTouchMode
4772 */
4773 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4774 // Focusable in touch mode should always be set before the focusable flag
4775 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4776 // which, in touch mode, will not successfully request focus on this view
4777 // because the focusable in touch mode flag is not set
4778 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4779 if (focusableInTouchMode) {
4780 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4781 }
4782 }
4783
4784 /**
4785 * Set whether this view should have sound effects enabled for events such as
4786 * clicking and touching.
4787 *
4788 * <p>You may wish to disable sound effects for a view if you already play sounds,
4789 * for instance, a dial key that plays dtmf tones.
4790 *
4791 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4792 * @see #isSoundEffectsEnabled()
4793 * @see #playSoundEffect(int)
4794 * @attr ref android.R.styleable#View_soundEffectsEnabled
4795 */
4796 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4797 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4798 }
4799
4800 /**
4801 * @return whether this view should have sound effects enabled for events such as
4802 * clicking and touching.
4803 *
4804 * @see #setSoundEffectsEnabled(boolean)
4805 * @see #playSoundEffect(int)
4806 * @attr ref android.R.styleable#View_soundEffectsEnabled
4807 */
4808 @ViewDebug.ExportedProperty
4809 public boolean isSoundEffectsEnabled() {
4810 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4811 }
4812
4813 /**
4814 * Set whether this view should have haptic feedback for events such as
4815 * long presses.
4816 *
4817 * <p>You may wish to disable haptic feedback if your view already controls
4818 * its own haptic feedback.
4819 *
4820 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4821 * @see #isHapticFeedbackEnabled()
4822 * @see #performHapticFeedback(int)
4823 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4824 */
4825 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4826 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4827 }
4828
4829 /**
4830 * @return whether this view should have haptic feedback enabled for events
4831 * long presses.
4832 *
4833 * @see #setHapticFeedbackEnabled(boolean)
4834 * @see #performHapticFeedback(int)
4835 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4836 */
4837 @ViewDebug.ExportedProperty
4838 public boolean isHapticFeedbackEnabled() {
4839 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4840 }
4841
4842 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004843 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004844 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004845 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4846 * {@link #LAYOUT_DIRECTION_RTL},
4847 * {@link #LAYOUT_DIRECTION_INHERIT} or
4848 * {@link #LAYOUT_DIRECTION_LOCALE}.
4849 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08004850 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004851 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004852 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4853 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4854 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4855 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004856 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004857 public int getLayoutDirection() {
4858 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004859 }
4860
4861 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004862 * Set the layout direction for this view. This will propagate a reset of layout direction
4863 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004864 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004865 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4866 * {@link #LAYOUT_DIRECTION_RTL},
4867 * {@link #LAYOUT_DIRECTION_INHERIT} or
4868 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004869 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004870 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08004871 */
4872 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004873 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004874 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004875 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004876 // Setting the flag will also request a layout.
4877 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4878 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004879 }
4880
4881 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004882 * Returns the resolved layout direction for this view.
4883 *
4884 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4885 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004886 */
4887 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4888 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4889 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4890 })
4891 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004892 resolveLayoutDirectionIfNeeded();
4893 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004894 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4895 }
4896
4897 /**
4898 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4899 * layout attribute and/or the inherited value from the parent.</p>
4900 *
4901 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004902 */
4903 @ViewDebug.ExportedProperty(category = "layout")
4904 public boolean isLayoutRtl() {
4905 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4906 }
4907
4908 /**
Adam Powell539ee872012-02-03 19:00:49 -08004909 * Indicates whether the view is currently tracking transient state that the
4910 * app should not need to concern itself with saving and restoring, but that
4911 * the framework should take special note to preserve when possible.
4912 *
4913 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08004914 */
4915 @ViewDebug.ExportedProperty(category = "layout")
4916 public boolean hasTransientState() {
4917 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
4918 }
4919
4920 /**
4921 * Set whether this view is currently tracking transient state that the
4922 * framework should attempt to preserve when possible.
4923 *
4924 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08004925 */
4926 public void setHasTransientState(boolean hasTransientState) {
4927 if (hasTransientState() == hasTransientState) return;
4928
4929 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
4930 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
4931 if (mParent != null) {
4932 try {
4933 mParent.childHasTransientStateChanged(this, hasTransientState);
4934 } catch (AbstractMethodError e) {
4935 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
4936 " does not fully implement ViewParent", e);
4937 }
4938 }
4939 }
4940
4941 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004942 * If this view doesn't do any drawing on its own, set this flag to
4943 * allow further optimizations. By default, this flag is not set on
4944 * View, but could be set on some View subclasses such as ViewGroup.
4945 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004946 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4947 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004948 *
4949 * @param willNotDraw whether or not this View draw on its own
4950 */
4951 public void setWillNotDraw(boolean willNotDraw) {
4952 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4953 }
4954
4955 /**
4956 * Returns whether or not this View draws on its own.
4957 *
4958 * @return true if this view has nothing to draw, false otherwise
4959 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004960 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004961 public boolean willNotDraw() {
4962 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4963 }
4964
4965 /**
4966 * When a View's drawing cache is enabled, drawing is redirected to an
4967 * offscreen bitmap. Some views, like an ImageView, must be able to
4968 * bypass this mechanism if they already draw a single bitmap, to avoid
4969 * unnecessary usage of the memory.
4970 *
4971 * @param willNotCacheDrawing true if this view does not cache its
4972 * drawing, false otherwise
4973 */
4974 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4975 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4976 }
4977
4978 /**
4979 * Returns whether or not this View can cache its drawing or not.
4980 *
4981 * @return true if this view does not cache its drawing, false otherwise
4982 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004983 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004984 public boolean willNotCacheDrawing() {
4985 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4986 }
4987
4988 /**
4989 * Indicates whether this view reacts to click events or not.
4990 *
4991 * @return true if the view is clickable, false otherwise
4992 *
4993 * @see #setClickable(boolean)
4994 * @attr ref android.R.styleable#View_clickable
4995 */
4996 @ViewDebug.ExportedProperty
4997 public boolean isClickable() {
4998 return (mViewFlags & CLICKABLE) == CLICKABLE;
4999 }
5000
5001 /**
5002 * Enables or disables click events for this view. When a view
5003 * is clickable it will change its state to "pressed" on every click.
5004 * Subclasses should set the view clickable to visually react to
5005 * user's clicks.
5006 *
5007 * @param clickable true to make the view clickable, false otherwise
5008 *
5009 * @see #isClickable()
5010 * @attr ref android.R.styleable#View_clickable
5011 */
5012 public void setClickable(boolean clickable) {
5013 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5014 }
5015
5016 /**
5017 * Indicates whether this view reacts to long click events or not.
5018 *
5019 * @return true if the view is long clickable, false otherwise
5020 *
5021 * @see #setLongClickable(boolean)
5022 * @attr ref android.R.styleable#View_longClickable
5023 */
5024 public boolean isLongClickable() {
5025 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5026 }
5027
5028 /**
5029 * Enables or disables long click events for this view. When a view is long
5030 * clickable it reacts to the user holding down the button for a longer
5031 * duration than a tap. This event can either launch the listener or a
5032 * context menu.
5033 *
5034 * @param longClickable true to make the view long clickable, false otherwise
5035 * @see #isLongClickable()
5036 * @attr ref android.R.styleable#View_longClickable
5037 */
5038 public void setLongClickable(boolean longClickable) {
5039 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5040 }
5041
5042 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005043 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005044 *
5045 * @see #isClickable()
5046 * @see #setClickable(boolean)
5047 *
5048 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5049 * the View's internal state from a previously set "pressed" state.
5050 */
5051 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005052 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005054 if (pressed) {
5055 mPrivateFlags |= PRESSED;
5056 } else {
5057 mPrivateFlags &= ~PRESSED;
5058 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005059
5060 if (needsRefresh) {
5061 refreshDrawableState();
5062 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005063 dispatchSetPressed(pressed);
5064 }
5065
5066 /**
5067 * Dispatch setPressed to all of this View's children.
5068 *
5069 * @see #setPressed(boolean)
5070 *
5071 * @param pressed The new pressed state
5072 */
5073 protected void dispatchSetPressed(boolean pressed) {
5074 }
5075
5076 /**
5077 * Indicates whether the view is currently in pressed state. Unless
5078 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5079 * the pressed state.
5080 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005081 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005082 * @see #isClickable()
5083 * @see #setClickable(boolean)
5084 *
5085 * @return true if the view is currently pressed, false otherwise
5086 */
5087 public boolean isPressed() {
5088 return (mPrivateFlags & PRESSED) == PRESSED;
5089 }
5090
5091 /**
5092 * Indicates whether this view will save its state (that is,
5093 * whether its {@link #onSaveInstanceState} method will be called).
5094 *
5095 * @return Returns true if the view state saving is enabled, else false.
5096 *
5097 * @see #setSaveEnabled(boolean)
5098 * @attr ref android.R.styleable#View_saveEnabled
5099 */
5100 public boolean isSaveEnabled() {
5101 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5102 }
5103
5104 /**
5105 * Controls whether the saving of this view's state is
5106 * enabled (that is, whether its {@link #onSaveInstanceState} method
5107 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005108 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005109 * for its state to be saved. This flag can only disable the
5110 * saving of this view; any child views may still have their state saved.
5111 *
5112 * @param enabled Set to false to <em>disable</em> state saving, or true
5113 * (the default) to allow it.
5114 *
5115 * @see #isSaveEnabled()
5116 * @see #setId(int)
5117 * @see #onSaveInstanceState()
5118 * @attr ref android.R.styleable#View_saveEnabled
5119 */
5120 public void setSaveEnabled(boolean enabled) {
5121 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5122 }
5123
Jeff Brown85a31762010-09-01 17:01:00 -07005124 /**
5125 * Gets whether the framework should discard touches when the view's
5126 * window is obscured by another visible window.
5127 * Refer to the {@link View} security documentation for more details.
5128 *
5129 * @return True if touch filtering is enabled.
5130 *
5131 * @see #setFilterTouchesWhenObscured(boolean)
5132 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5133 */
5134 @ViewDebug.ExportedProperty
5135 public boolean getFilterTouchesWhenObscured() {
5136 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5137 }
5138
5139 /**
5140 * Sets whether the framework should discard touches when the view's
5141 * window is obscured by another visible window.
5142 * Refer to the {@link View} security documentation for more details.
5143 *
5144 * @param enabled True if touch filtering should be enabled.
5145 *
5146 * @see #getFilterTouchesWhenObscured
5147 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5148 */
5149 public void setFilterTouchesWhenObscured(boolean enabled) {
5150 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5151 FILTER_TOUCHES_WHEN_OBSCURED);
5152 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005153
5154 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005155 * Indicates whether the entire hierarchy under this view will save its
5156 * state when a state saving traversal occurs from its parent. The default
5157 * is true; if false, these views will not be saved unless
5158 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5159 *
5160 * @return Returns true if the view state saving from parent is enabled, else false.
5161 *
5162 * @see #setSaveFromParentEnabled(boolean)
5163 */
5164 public boolean isSaveFromParentEnabled() {
5165 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5166 }
5167
5168 /**
5169 * Controls whether the entire hierarchy under this view will save its
5170 * state when a state saving traversal occurs from its parent. The default
5171 * is true; if false, these views will not be saved unless
5172 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5173 *
5174 * @param enabled Set to false to <em>disable</em> state saving, or true
5175 * (the default) to allow it.
5176 *
5177 * @see #isSaveFromParentEnabled()
5178 * @see #setId(int)
5179 * @see #onSaveInstanceState()
5180 */
5181 public void setSaveFromParentEnabled(boolean enabled) {
5182 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5183 }
5184
5185
5186 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005187 * Returns whether this View is able to take focus.
5188 *
5189 * @return True if this view can take focus, or false otherwise.
5190 * @attr ref android.R.styleable#View_focusable
5191 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005192 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005193 public final boolean isFocusable() {
5194 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5195 }
5196
5197 /**
5198 * When a view is focusable, it may not want to take focus when in touch mode.
5199 * For example, a button would like focus when the user is navigating via a D-pad
5200 * so that the user can click on it, but once the user starts touching the screen,
5201 * the button shouldn't take focus
5202 * @return Whether the view is focusable in touch mode.
5203 * @attr ref android.R.styleable#View_focusableInTouchMode
5204 */
5205 @ViewDebug.ExportedProperty
5206 public final boolean isFocusableInTouchMode() {
5207 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5208 }
5209
5210 /**
5211 * Find the nearest view in the specified direction that can take focus.
5212 * This does not actually give focus to that view.
5213 *
5214 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5215 *
5216 * @return The nearest focusable in the specified direction, or null if none
5217 * can be found.
5218 */
5219 public View focusSearch(int direction) {
5220 if (mParent != null) {
5221 return mParent.focusSearch(this, direction);
5222 } else {
5223 return null;
5224 }
5225 }
5226
5227 /**
5228 * This method is the last chance for the focused view and its ancestors to
5229 * respond to an arrow key. This is called when the focused view did not
5230 * consume the key internally, nor could the view system find a new view in
5231 * the requested direction to give focus to.
5232 *
5233 * @param focused The currently focused view.
5234 * @param direction The direction focus wants to move. One of FOCUS_UP,
5235 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5236 * @return True if the this view consumed this unhandled move.
5237 */
5238 public boolean dispatchUnhandledMove(View focused, int direction) {
5239 return false;
5240 }
5241
5242 /**
5243 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005244 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005245 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005246 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5247 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005248 * @return The user specified next view, or null if there is none.
5249 */
5250 View findUserSetNextFocus(View root, int direction) {
5251 switch (direction) {
5252 case FOCUS_LEFT:
5253 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005254 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005255 case FOCUS_RIGHT:
5256 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005257 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005258 case FOCUS_UP:
5259 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005260 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005261 case FOCUS_DOWN:
5262 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005263 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005264 case FOCUS_FORWARD:
5265 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005266 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005267 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005268 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005269 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005270 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005271 @Override
5272 public boolean apply(View t) {
5273 return t.mNextFocusForwardId == id;
5274 }
5275 });
5276 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 }
5278 return null;
5279 }
5280
Jeff Brown4dfbec22011-08-15 14:55:37 -07005281 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5282 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5283 @Override
5284 public boolean apply(View t) {
5285 return t.mID == childViewId;
5286 }
5287 });
5288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005289 if (result == null) {
5290 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5291 + "by user for id " + childViewId);
5292 }
5293 return result;
5294 }
5295
5296 /**
5297 * Find and return all focusable views that are descendants of this view,
5298 * possibly including this view if it is focusable itself.
5299 *
5300 * @param direction The direction of the focus
5301 * @return A list of focusable views
5302 */
5303 public ArrayList<View> getFocusables(int direction) {
5304 ArrayList<View> result = new ArrayList<View>(24);
5305 addFocusables(result, direction);
5306 return result;
5307 }
5308
5309 /**
5310 * Add any focusable views that are descendants of this view (possibly
5311 * including this view if it is focusable itself) to views. If we are in touch mode,
5312 * only add views that are also focusable in touch mode.
5313 *
5314 * @param views Focusable views found so far
5315 * @param direction The direction of the focus
5316 */
5317 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005318 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005320
svetoslavganov75986cf2009-05-14 22:28:01 -07005321 /**
5322 * Adds any focusable views that are descendants of this view (possibly
5323 * including this view if it is focusable itself) to views. This method
5324 * adds all focusable views regardless if we are in touch mode or
5325 * only views focusable in touch mode if we are in touch mode depending on
5326 * the focusable mode paramater.
5327 *
5328 * @param views Focusable views found so far or null if all we are interested is
5329 * the number of focusables.
5330 * @param direction The direction of the focus.
5331 * @param focusableMode The type of focusables to be added.
5332 *
5333 * @see #FOCUSABLES_ALL
5334 * @see #FOCUSABLES_TOUCH_MODE
5335 */
5336 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5337 if (!isFocusable()) {
5338 return;
5339 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005340
svetoslavganov75986cf2009-05-14 22:28:01 -07005341 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5342 isInTouchMode() && !isFocusableInTouchMode()) {
5343 return;
5344 }
5345
5346 if (views != null) {
5347 views.add(this);
5348 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005349 }
5350
5351 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005352 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005353 * The search is performed by either the text that the View renders or the content
5354 * description that describes the view for accessibility purposes and the view does
5355 * not render or both. Clients can specify how the search is to be performed via
5356 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5357 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005358 *
5359 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005360 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005361 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005362 * @see #FIND_VIEWS_WITH_TEXT
5363 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5364 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005365 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005366 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005367 if (getAccessibilityNodeProvider() != null) {
5368 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5369 outViews.add(this);
5370 }
5371 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5372 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005373 String searchedLowerCase = searched.toString().toLowerCase();
5374 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5375 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5376 outViews.add(this);
5377 }
5378 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005379 }
5380
5381 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005382 * Find and return all touchable views that are descendants of this view,
5383 * possibly including this view if it is touchable itself.
5384 *
5385 * @return A list of touchable views
5386 */
5387 public ArrayList<View> getTouchables() {
5388 ArrayList<View> result = new ArrayList<View>();
5389 addTouchables(result);
5390 return result;
5391 }
5392
5393 /**
5394 * Add any touchable views that are descendants of this view (possibly
5395 * including this view if it is touchable itself) to views.
5396 *
5397 * @param views Touchable views found so far
5398 */
5399 public void addTouchables(ArrayList<View> views) {
5400 final int viewFlags = mViewFlags;
5401
5402 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5403 && (viewFlags & ENABLED_MASK) == ENABLED) {
5404 views.add(this);
5405 }
5406 }
5407
5408 /**
5409 * Call this to try to give focus to a specific view or to one of its
5410 * descendants.
5411 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005412 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5413 * false), or if it is focusable and it is not focusable in touch mode
5414 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005415 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005416 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005417 * have focus, and you want your parent to look for the next one.
5418 *
5419 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5420 * {@link #FOCUS_DOWN} and <code>null</code>.
5421 *
5422 * @return Whether this view or one of its descendants actually took focus.
5423 */
5424 public final boolean requestFocus() {
5425 return requestFocus(View.FOCUS_DOWN);
5426 }
5427
5428
5429 /**
5430 * Call this to try to give focus to a specific view or to one of its
5431 * descendants and give it a hint about what direction focus is heading.
5432 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005433 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5434 * false), or if it is focusable and it is not focusable in touch mode
5435 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005436 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005437 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005438 * have focus, and you want your parent to look for the next one.
5439 *
5440 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5441 * <code>null</code> set for the previously focused rectangle.
5442 *
5443 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5444 * @return Whether this view or one of its descendants actually took focus.
5445 */
5446 public final boolean requestFocus(int direction) {
5447 return requestFocus(direction, null);
5448 }
5449
5450 /**
5451 * Call this to try to give focus to a specific view or to one of its descendants
5452 * and give it hints about the direction and a specific rectangle that the focus
5453 * is coming from. The rectangle can help give larger views a finer grained hint
5454 * about where focus is coming from, and therefore, where to show selection, or
5455 * forward focus change internally.
5456 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005457 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5458 * false), or if it is focusable and it is not focusable in touch mode
5459 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005460 *
5461 * A View will not take focus if it is not visible.
5462 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005463 * A View will not take focus if one of its parents has
5464 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5465 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005466 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005467 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005468 * have focus, and you want your parent to look for the next one.
5469 *
5470 * You may wish to override this method if your custom {@link View} has an internal
5471 * {@link View} that it wishes to forward the request to.
5472 *
5473 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5474 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5475 * to give a finer grained hint about where focus is coming from. May be null
5476 * if there is no hint.
5477 * @return Whether this view or one of its descendants actually took focus.
5478 */
5479 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5480 // need to be focusable
5481 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5482 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5483 return false;
5484 }
5485
5486 // need to be focusable in touch mode if in touch mode
5487 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005488 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5489 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005490 }
5491
5492 // need to not have any parents blocking us
5493 if (hasAncestorThatBlocksDescendantFocus()) {
5494 return false;
5495 }
5496
5497 handleFocusGainInternal(direction, previouslyFocusedRect);
5498 return true;
5499 }
5500
5501 /**
5502 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5503 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5504 * touch mode to request focus when they are touched.
5505 *
5506 * @return Whether this view or one of its descendants actually took focus.
5507 *
5508 * @see #isInTouchMode()
5509 *
5510 */
5511 public final boolean requestFocusFromTouch() {
5512 // Leave touch mode if we need to
5513 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005514 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005515 if (viewRoot != null) {
5516 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005517 }
5518 }
5519 return requestFocus(View.FOCUS_DOWN);
5520 }
5521
5522 /**
5523 * @return Whether any ancestor of this view blocks descendant focus.
5524 */
5525 private boolean hasAncestorThatBlocksDescendantFocus() {
5526 ViewParent ancestor = mParent;
5527 while (ancestor instanceof ViewGroup) {
5528 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5529 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5530 return true;
5531 } else {
5532 ancestor = vgAncestor.getParent();
5533 }
5534 }
5535 return false;
5536 }
5537
5538 /**
Romain Guya440b002010-02-24 15:57:54 -08005539 * @hide
5540 */
5541 public void dispatchStartTemporaryDetach() {
5542 onStartTemporaryDetach();
5543 }
5544
5545 /**
5546 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005547 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5548 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005549 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005550 */
5551 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005552 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005553 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005554 }
5555
5556 /**
5557 * @hide
5558 */
5559 public void dispatchFinishTemporaryDetach() {
5560 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005561 }
Romain Guy8506ab42009-06-11 17:35:47 -07005562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005563 /**
5564 * Called after {@link #onStartTemporaryDetach} when the container is done
5565 * changing the view.
5566 */
5567 public void onFinishTemporaryDetach() {
5568 }
Romain Guy8506ab42009-06-11 17:35:47 -07005569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005570 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005571 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5572 * for this view's window. Returns null if the view is not currently attached
5573 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005574 * just use the standard high-level event callbacks like
5575 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005576 */
5577 public KeyEvent.DispatcherState getKeyDispatcherState() {
5578 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5579 }
Joe Malin32736f02011-01-19 16:14:20 -08005580
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005581 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005582 * Dispatch a key event before it is processed by any input method
5583 * associated with the view hierarchy. This can be used to intercept
5584 * key events in special situations before the IME consumes them; a
5585 * typical example would be handling the BACK key to update the application's
5586 * UI instead of allowing the IME to see it and close itself.
5587 *
5588 * @param event The key event to be dispatched.
5589 * @return True if the event was handled, false otherwise.
5590 */
5591 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5592 return onKeyPreIme(event.getKeyCode(), event);
5593 }
5594
5595 /**
5596 * Dispatch a key event to the next view on the focus path. This path runs
5597 * from the top of the view tree down to the currently focused view. If this
5598 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5599 * the next node down the focus path. This method also fires any key
5600 * listeners.
5601 *
5602 * @param event The key event to be dispatched.
5603 * @return True if the event was handled, false otherwise.
5604 */
5605 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005606 if (mInputEventConsistencyVerifier != null) {
5607 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005609
Jeff Brown21bc5c92011-02-28 18:27:14 -08005610 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005611 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005612 ListenerInfo li = mListenerInfo;
5613 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5614 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005615 return true;
5616 }
5617
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005618 if (event.dispatch(this, mAttachInfo != null
5619 ? mAttachInfo.mKeyDispatchState : null, this)) {
5620 return true;
5621 }
5622
5623 if (mInputEventConsistencyVerifier != null) {
5624 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5625 }
5626 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005627 }
5628
5629 /**
5630 * Dispatches a key shortcut event.
5631 *
5632 * @param event The key event to be dispatched.
5633 * @return True if the event was handled by the view, false otherwise.
5634 */
5635 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5636 return onKeyShortcut(event.getKeyCode(), event);
5637 }
5638
5639 /**
5640 * Pass the touch screen motion event down to the target view, or this
5641 * view if it is the target.
5642 *
5643 * @param event The motion event to be dispatched.
5644 * @return True if the event was handled by the view, false otherwise.
5645 */
5646 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005647 if (mInputEventConsistencyVerifier != null) {
5648 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5649 }
5650
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005651 if (onFilterTouchEventForSecurity(event)) {
5652 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005653 ListenerInfo li = mListenerInfo;
5654 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5655 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005656 return true;
5657 }
5658
5659 if (onTouchEvent(event)) {
5660 return true;
5661 }
Jeff Brown85a31762010-09-01 17:01:00 -07005662 }
5663
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005664 if (mInputEventConsistencyVerifier != null) {
5665 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005666 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005667 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005668 }
5669
5670 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005671 * Filter the touch event to apply security policies.
5672 *
5673 * @param event The motion event to be filtered.
5674 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005675 *
Jeff Brown85a31762010-09-01 17:01:00 -07005676 * @see #getFilterTouchesWhenObscured
5677 */
5678 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005679 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005680 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5681 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5682 // Window is obscured, drop this touch.
5683 return false;
5684 }
5685 return true;
5686 }
5687
5688 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005689 * Pass a trackball motion event down to the focused view.
5690 *
5691 * @param event The motion event to be dispatched.
5692 * @return True if the event was handled by the view, false otherwise.
5693 */
5694 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005695 if (mInputEventConsistencyVerifier != null) {
5696 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5697 }
5698
Romain Guy02ccac62011-06-24 13:20:23 -07005699 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005700 }
5701
5702 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005703 * Dispatch a generic motion event.
5704 * <p>
5705 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5706 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005707 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005708 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005709 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005710 *
5711 * @param event The motion event to be dispatched.
5712 * @return True if the event was handled by the view, false otherwise.
5713 */
5714 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005715 if (mInputEventConsistencyVerifier != null) {
5716 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5717 }
5718
Jeff Browna032cc02011-03-07 16:56:21 -08005719 final int source = event.getSource();
5720 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5721 final int action = event.getAction();
5722 if (action == MotionEvent.ACTION_HOVER_ENTER
5723 || action == MotionEvent.ACTION_HOVER_MOVE
5724 || action == MotionEvent.ACTION_HOVER_EXIT) {
5725 if (dispatchHoverEvent(event)) {
5726 return true;
5727 }
5728 } else if (dispatchGenericPointerEvent(event)) {
5729 return true;
5730 }
5731 } else if (dispatchGenericFocusedEvent(event)) {
5732 return true;
5733 }
5734
Jeff Brown10b62902011-06-20 16:40:37 -07005735 if (dispatchGenericMotionEventInternal(event)) {
5736 return true;
5737 }
5738
5739 if (mInputEventConsistencyVerifier != null) {
5740 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5741 }
5742 return false;
5743 }
5744
5745 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005746 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005747 ListenerInfo li = mListenerInfo;
5748 if (li != null && li.mOnGenericMotionListener != null
5749 && (mViewFlags & ENABLED_MASK) == ENABLED
5750 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005751 return true;
5752 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005753
5754 if (onGenericMotionEvent(event)) {
5755 return true;
5756 }
5757
5758 if (mInputEventConsistencyVerifier != null) {
5759 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5760 }
5761 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005762 }
5763
5764 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005765 * Dispatch a hover event.
5766 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005767 * Do not call this method directly.
5768 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005769 * </p>
5770 *
5771 * @param event The motion event to be dispatched.
5772 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005773 */
5774 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005775 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005776 ListenerInfo li = mListenerInfo;
5777 if (li != null && li.mOnHoverListener != null
5778 && (mViewFlags & ENABLED_MASK) == ENABLED
5779 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07005780 return true;
5781 }
5782
Jeff Browna032cc02011-03-07 16:56:21 -08005783 return onHoverEvent(event);
5784 }
5785
5786 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005787 * Returns true if the view has a child to which it has recently sent
5788 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5789 * it does not have a hovered child, then it must be the innermost hovered view.
5790 * @hide
5791 */
5792 protected boolean hasHoveredChild() {
5793 return false;
5794 }
5795
5796 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005797 * Dispatch a generic motion event to the view under the first pointer.
5798 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005799 * Do not call this method directly.
5800 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005801 * </p>
5802 *
5803 * @param event The motion event to be dispatched.
5804 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005805 */
5806 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5807 return false;
5808 }
5809
5810 /**
5811 * Dispatch a generic motion event to the currently focused view.
5812 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005813 * Do not call this method directly.
5814 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005815 * </p>
5816 *
5817 * @param event The motion event to be dispatched.
5818 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005819 */
5820 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5821 return false;
5822 }
5823
5824 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005825 * Dispatch a pointer event.
5826 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005827 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5828 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5829 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005830 * and should not be expected to handle other pointing device features.
5831 * </p>
5832 *
5833 * @param event The motion event to be dispatched.
5834 * @return True if the event was handled by the view, false otherwise.
5835 * @hide
5836 */
5837 public final boolean dispatchPointerEvent(MotionEvent event) {
5838 if (event.isTouchEvent()) {
5839 return dispatchTouchEvent(event);
5840 } else {
5841 return dispatchGenericMotionEvent(event);
5842 }
5843 }
5844
5845 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005846 * Called when the window containing this view gains or loses window focus.
5847 * ViewGroups should override to route to their children.
5848 *
5849 * @param hasFocus True if the window containing this view now has focus,
5850 * false otherwise.
5851 */
5852 public void dispatchWindowFocusChanged(boolean hasFocus) {
5853 onWindowFocusChanged(hasFocus);
5854 }
5855
5856 /**
5857 * Called when the window containing this view gains or loses focus. Note
5858 * that this is separate from view focus: to receive key events, both
5859 * your view and its window must have focus. If a window is displayed
5860 * on top of yours that takes input focus, then your own window will lose
5861 * focus but the view focus will remain unchanged.
5862 *
5863 * @param hasWindowFocus True if the window containing this view now has
5864 * focus, false otherwise.
5865 */
5866 public void onWindowFocusChanged(boolean hasWindowFocus) {
5867 InputMethodManager imm = InputMethodManager.peekInstance();
5868 if (!hasWindowFocus) {
5869 if (isPressed()) {
5870 setPressed(false);
5871 }
5872 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5873 imm.focusOut(this);
5874 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005875 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005876 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005877 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005878 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5879 imm.focusIn(this);
5880 }
5881 refreshDrawableState();
5882 }
5883
5884 /**
5885 * Returns true if this view is in a window that currently has window focus.
5886 * Note that this is not the same as the view itself having focus.
5887 *
5888 * @return True if this view is in a window that currently has window focus.
5889 */
5890 public boolean hasWindowFocus() {
5891 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5892 }
5893
5894 /**
Adam Powell326d8082009-12-09 15:10:07 -08005895 * Dispatch a view visibility change down the view hierarchy.
5896 * ViewGroups should override to route to their children.
5897 * @param changedView The view whose visibility changed. Could be 'this' or
5898 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005899 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5900 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005901 */
5902 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5903 onVisibilityChanged(changedView, visibility);
5904 }
5905
5906 /**
5907 * Called when the visibility of the view or an ancestor of the view is changed.
5908 * @param changedView The view whose visibility changed. Could be 'this' or
5909 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005910 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5911 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005912 */
5913 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005914 if (visibility == VISIBLE) {
5915 if (mAttachInfo != null) {
5916 initialAwakenScrollBars();
5917 } else {
5918 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5919 }
5920 }
Adam Powell326d8082009-12-09 15:10:07 -08005921 }
5922
5923 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005924 * Dispatch a hint about whether this view is displayed. For instance, when
5925 * a View moves out of the screen, it might receives a display hint indicating
5926 * the view is not displayed. Applications should not <em>rely</em> on this hint
5927 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005928 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005929 * @param hint A hint about whether or not this view is displayed:
5930 * {@link #VISIBLE} or {@link #INVISIBLE}.
5931 */
5932 public void dispatchDisplayHint(int hint) {
5933 onDisplayHint(hint);
5934 }
5935
5936 /**
5937 * Gives this view a hint about whether is displayed or not. For instance, when
5938 * a View moves out of the screen, it might receives a display hint indicating
5939 * the view is not displayed. Applications should not <em>rely</em> on this hint
5940 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005941 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005942 * @param hint A hint about whether or not this view is displayed:
5943 * {@link #VISIBLE} or {@link #INVISIBLE}.
5944 */
5945 protected void onDisplayHint(int hint) {
5946 }
5947
5948 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005949 * Dispatch a window visibility change down the view hierarchy.
5950 * ViewGroups should override to route to their children.
5951 *
5952 * @param visibility The new visibility of the window.
5953 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005954 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005955 */
5956 public void dispatchWindowVisibilityChanged(int visibility) {
5957 onWindowVisibilityChanged(visibility);
5958 }
5959
5960 /**
5961 * Called when the window containing has change its visibility
5962 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5963 * that this tells you whether or not your window is being made visible
5964 * to the window manager; this does <em>not</em> tell you whether or not
5965 * your window is obscured by other windows on the screen, even if it
5966 * is itself visible.
5967 *
5968 * @param visibility The new visibility of the window.
5969 */
5970 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005971 if (visibility == VISIBLE) {
5972 initialAwakenScrollBars();
5973 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005974 }
5975
5976 /**
5977 * Returns the current visibility of the window this view is attached to
5978 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5979 *
5980 * @return Returns the current visibility of the view's window.
5981 */
5982 public int getWindowVisibility() {
5983 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5984 }
5985
5986 /**
5987 * Retrieve the overall visible display size in which the window this view is
5988 * attached to has been positioned in. This takes into account screen
5989 * decorations above the window, for both cases where the window itself
5990 * is being position inside of them or the window is being placed under
5991 * then and covered insets are used for the window to position its content
5992 * inside. In effect, this tells you the available area where content can
5993 * be placed and remain visible to users.
5994 *
5995 * <p>This function requires an IPC back to the window manager to retrieve
5996 * the requested information, so should not be used in performance critical
5997 * code like drawing.
5998 *
5999 * @param outRect Filled in with the visible display frame. If the view
6000 * is not attached to a window, this is simply the raw display size.
6001 */
6002 public void getWindowVisibleDisplayFrame(Rect outRect) {
6003 if (mAttachInfo != null) {
6004 try {
6005 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
6006 } catch (RemoteException e) {
6007 return;
6008 }
6009 // XXX This is really broken, and probably all needs to be done
6010 // in the window manager, and we need to know more about whether
6011 // we want the area behind or in front of the IME.
6012 final Rect insets = mAttachInfo.mVisibleInsets;
6013 outRect.left += insets.left;
6014 outRect.top += insets.top;
6015 outRect.right -= insets.right;
6016 outRect.bottom -= insets.bottom;
6017 return;
6018 }
6019 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006020 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006021 }
6022
6023 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006024 * Dispatch a notification about a resource configuration change down
6025 * the view hierarchy.
6026 * ViewGroups should override to route to their children.
6027 *
6028 * @param newConfig The new resource configuration.
6029 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006030 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006031 */
6032 public void dispatchConfigurationChanged(Configuration newConfig) {
6033 onConfigurationChanged(newConfig);
6034 }
6035
6036 /**
6037 * Called when the current configuration of the resources being used
6038 * by the application have changed. You can use this to decide when
6039 * to reload resources that can changed based on orientation and other
6040 * configuration characterstics. You only need to use this if you are
6041 * not relying on the normal {@link android.app.Activity} mechanism of
6042 * recreating the activity instance upon a configuration change.
6043 *
6044 * @param newConfig The new resource configuration.
6045 */
6046 protected void onConfigurationChanged(Configuration newConfig) {
6047 }
6048
6049 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006050 * Private function to aggregate all per-view attributes in to the view
6051 * root.
6052 */
6053 void dispatchCollectViewAttributes(int visibility) {
6054 performCollectViewAttributes(visibility);
6055 }
6056
6057 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08006058 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006059 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
6060 mAttachInfo.mKeepScreenOn = true;
6061 }
6062 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006063 ListenerInfo li = mListenerInfo;
6064 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006065 mAttachInfo.mHasSystemUiListeners = true;
6066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006067 }
6068 }
6069
6070 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006071 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006072 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006073 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6074 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006075 ai.mRecomputeGlobalAttributes = true;
6076 }
6077 }
6078 }
6079
6080 /**
6081 * Returns whether the device is currently in touch mode. Touch mode is entered
6082 * once the user begins interacting with the device by touch, and affects various
6083 * things like whether focus is always visible to the user.
6084 *
6085 * @return Whether the device is in touch mode.
6086 */
6087 @ViewDebug.ExportedProperty
6088 public boolean isInTouchMode() {
6089 if (mAttachInfo != null) {
6090 return mAttachInfo.mInTouchMode;
6091 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006092 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006093 }
6094 }
6095
6096 /**
6097 * Returns the context the view is running in, through which it can
6098 * access the current theme, resources, etc.
6099 *
6100 * @return The view's Context.
6101 */
6102 @ViewDebug.CapturedViewProperty
6103 public final Context getContext() {
6104 return mContext;
6105 }
6106
6107 /**
6108 * Handle a key event before it is processed by any input method
6109 * associated with the view hierarchy. This can be used to intercept
6110 * key events in special situations before the IME consumes them; a
6111 * typical example would be handling the BACK key to update the application's
6112 * UI instead of allowing the IME to see it and close itself.
6113 *
6114 * @param keyCode The value in event.getKeyCode().
6115 * @param event Description of the key event.
6116 * @return If you handled the event, return true. If you want to allow the
6117 * event to be handled by the next receiver, return false.
6118 */
6119 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
6120 return false;
6121 }
6122
6123 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006124 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
6125 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006126 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
6127 * is released, if the view is enabled and clickable.
6128 *
6129 * @param keyCode A key code that represents the button pressed, from
6130 * {@link android.view.KeyEvent}.
6131 * @param event The KeyEvent object that defines the button action.
6132 */
6133 public boolean onKeyDown(int keyCode, KeyEvent event) {
6134 boolean result = false;
6135
6136 switch (keyCode) {
6137 case KeyEvent.KEYCODE_DPAD_CENTER:
6138 case KeyEvent.KEYCODE_ENTER: {
6139 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6140 return true;
6141 }
6142 // Long clickable items don't necessarily have to be clickable
6143 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
6144 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
6145 (event.getRepeatCount() == 0)) {
6146 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006147 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006148 return true;
6149 }
6150 break;
6151 }
6152 }
6153 return result;
6154 }
6155
6156 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006157 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
6158 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
6159 * the event).
6160 */
6161 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
6162 return false;
6163 }
6164
6165 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006166 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
6167 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006168 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
6169 * {@link KeyEvent#KEYCODE_ENTER} is released.
6170 *
6171 * @param keyCode A key code that represents the button pressed, from
6172 * {@link android.view.KeyEvent}.
6173 * @param event The KeyEvent object that defines the button action.
6174 */
6175 public boolean onKeyUp(int keyCode, KeyEvent event) {
6176 boolean result = false;
6177
6178 switch (keyCode) {
6179 case KeyEvent.KEYCODE_DPAD_CENTER:
6180 case KeyEvent.KEYCODE_ENTER: {
6181 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6182 return true;
6183 }
6184 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
6185 setPressed(false);
6186
6187 if (!mHasPerformedLongPress) {
6188 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006189 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006190
6191 result = performClick();
6192 }
6193 }
6194 break;
6195 }
6196 }
6197 return result;
6198 }
6199
6200 /**
6201 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
6202 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
6203 * the event).
6204 *
6205 * @param keyCode A key code that represents the button pressed, from
6206 * {@link android.view.KeyEvent}.
6207 * @param repeatCount The number of times the action was made.
6208 * @param event The KeyEvent object that defines the button action.
6209 */
6210 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6211 return false;
6212 }
6213
6214 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006215 * Called on the focused view when a key shortcut event is not handled.
6216 * Override this method to implement local key shortcuts for the View.
6217 * Key shortcuts can also be implemented by setting the
6218 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006219 *
6220 * @param keyCode The value in event.getKeyCode().
6221 * @param event Description of the key event.
6222 * @return If you handled the event, return true. If you want to allow the
6223 * event to be handled by the next receiver, return false.
6224 */
6225 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6226 return false;
6227 }
6228
6229 /**
6230 * Check whether the called view is a text editor, in which case it
6231 * would make sense to automatically display a soft input window for
6232 * it. Subclasses should override this if they implement
6233 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006234 * a call on that method would return a non-null InputConnection, and
6235 * they are really a first-class editor that the user would normally
6236 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006237 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006238 * <p>The default implementation always returns false. This does
6239 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6240 * will not be called or the user can not otherwise perform edits on your
6241 * view; it is just a hint to the system that this is not the primary
6242 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006243 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006244 * @return Returns true if this view is a text editor, else false.
6245 */
6246 public boolean onCheckIsTextEditor() {
6247 return false;
6248 }
Romain Guy8506ab42009-06-11 17:35:47 -07006249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006250 /**
6251 * Create a new InputConnection for an InputMethod to interact
6252 * with the view. The default implementation returns null, since it doesn't
6253 * support input methods. You can override this to implement such support.
6254 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006255 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006256 * <p>When implementing this, you probably also want to implement
6257 * {@link #onCheckIsTextEditor()} to indicate you will return a
6258 * non-null InputConnection.
6259 *
6260 * @param outAttrs Fill in with attribute information about the connection.
6261 */
6262 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6263 return null;
6264 }
6265
6266 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006267 * Called by the {@link android.view.inputmethod.InputMethodManager}
6268 * when a view who is not the current
6269 * input connection target is trying to make a call on the manager. The
6270 * default implementation returns false; you can override this to return
6271 * true for certain views if you are performing InputConnection proxying
6272 * to them.
6273 * @param view The View that is making the InputMethodManager call.
6274 * @return Return true to allow the call, false to reject.
6275 */
6276 public boolean checkInputConnectionProxy(View view) {
6277 return false;
6278 }
Romain Guy8506ab42009-06-11 17:35:47 -07006279
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006280 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006281 * Show the context menu for this view. It is not safe to hold on to the
6282 * menu after returning from this method.
6283 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006284 * You should normally not overload this method. Overload
6285 * {@link #onCreateContextMenu(ContextMenu)} or define an
6286 * {@link OnCreateContextMenuListener} to add items to the context menu.
6287 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006288 * @param menu The context menu to populate
6289 */
6290 public void createContextMenu(ContextMenu menu) {
6291 ContextMenuInfo menuInfo = getContextMenuInfo();
6292
6293 // Sets the current menu info so all items added to menu will have
6294 // my extra info set.
6295 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6296
6297 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006298 ListenerInfo li = mListenerInfo;
6299 if (li != null && li.mOnCreateContextMenuListener != null) {
6300 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006301 }
6302
6303 // Clear the extra information so subsequent items that aren't mine don't
6304 // have my extra info.
6305 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6306
6307 if (mParent != null) {
6308 mParent.createContextMenu(menu);
6309 }
6310 }
6311
6312 /**
6313 * Views should implement this if they have extra information to associate
6314 * with the context menu. The return result is supplied as a parameter to
6315 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6316 * callback.
6317 *
6318 * @return Extra information about the item for which the context menu
6319 * should be shown. This information will vary across different
6320 * subclasses of View.
6321 */
6322 protected ContextMenuInfo getContextMenuInfo() {
6323 return null;
6324 }
6325
6326 /**
6327 * Views should implement this if the view itself is going to add items to
6328 * the context menu.
6329 *
6330 * @param menu the context menu to populate
6331 */
6332 protected void onCreateContextMenu(ContextMenu menu) {
6333 }
6334
6335 /**
6336 * Implement this method to handle trackball motion events. The
6337 * <em>relative</em> movement of the trackball since the last event
6338 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6339 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6340 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6341 * they will often be fractional values, representing the more fine-grained
6342 * movement information available from a trackball).
6343 *
6344 * @param event The motion event.
6345 * @return True if the event was handled, false otherwise.
6346 */
6347 public boolean onTrackballEvent(MotionEvent event) {
6348 return false;
6349 }
6350
6351 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006352 * Implement this method to handle generic motion events.
6353 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006354 * Generic motion events describe joystick movements, mouse hovers, track pad
6355 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006356 * {@link MotionEvent#getSource() source} of the motion event specifies
6357 * the class of input that was received. Implementations of this method
6358 * must examine the bits in the source before processing the event.
6359 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006360 * </p><p>
6361 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6362 * are delivered to the view under the pointer. All other generic motion events are
6363 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006364 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07006365 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006366 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006367 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6368 * // process the joystick movement...
6369 * return true;
6370 * }
6371 * }
6372 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6373 * switch (event.getAction()) {
6374 * case MotionEvent.ACTION_HOVER_MOVE:
6375 * // process the mouse hover movement...
6376 * return true;
6377 * case MotionEvent.ACTION_SCROLL:
6378 * // process the scroll wheel movement...
6379 * return true;
6380 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006381 * }
6382 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07006383 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08006384 *
6385 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006386 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006387 */
6388 public boolean onGenericMotionEvent(MotionEvent event) {
6389 return false;
6390 }
6391
6392 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006393 * Implement this method to handle hover events.
6394 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006395 * This method is called whenever a pointer is hovering into, over, or out of the
6396 * bounds of a view and the view is not currently being touched.
6397 * Hover events are represented as pointer events with action
6398 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6399 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6400 * </p>
6401 * <ul>
6402 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6403 * when the pointer enters the bounds of the view.</li>
6404 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6405 * when the pointer has already entered the bounds of the view and has moved.</li>
6406 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6407 * when the pointer has exited the bounds of the view or when the pointer is
6408 * about to go down due to a button click, tap, or similar user action that
6409 * causes the view to be touched.</li>
6410 * </ul>
6411 * <p>
6412 * The view should implement this method to return true to indicate that it is
6413 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006414 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006415 * The default implementation calls {@link #setHovered} to update the hovered state
6416 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006417 * is enabled and is clickable. The default implementation also sends hover
6418 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006419 * </p>
6420 *
6421 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006422 * @return True if the view handled the hover event.
6423 *
6424 * @see #isHovered
6425 * @see #setHovered
6426 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006427 */
6428 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006429 // The root view may receive hover (or touch) events that are outside the bounds of
6430 // the window. This code ensures that we only send accessibility events for
6431 // hovers that are actually within the bounds of the root view.
6432 final int action = event.getAction();
6433 if (!mSendingHoverAccessibilityEvents) {
6434 if ((action == MotionEvent.ACTION_HOVER_ENTER
6435 || action == MotionEvent.ACTION_HOVER_MOVE)
6436 && !hasHoveredChild()
6437 && pointInView(event.getX(), event.getY())) {
6438 mSendingHoverAccessibilityEvents = true;
6439 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6440 }
6441 } else {
6442 if (action == MotionEvent.ACTION_HOVER_EXIT
6443 || (action == MotionEvent.ACTION_HOVER_MOVE
6444 && !pointInView(event.getX(), event.getY()))) {
6445 mSendingHoverAccessibilityEvents = false;
6446 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6447 }
Jeff Browna1b24182011-07-28 13:38:24 -07006448 }
6449
Jeff Brown87b7f802011-06-21 18:35:45 -07006450 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006451 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006452 case MotionEvent.ACTION_HOVER_ENTER:
6453 setHovered(true);
6454 break;
6455 case MotionEvent.ACTION_HOVER_EXIT:
6456 setHovered(false);
6457 break;
6458 }
Jeff Browna1b24182011-07-28 13:38:24 -07006459
6460 // Dispatch the event to onGenericMotionEvent before returning true.
6461 // This is to provide compatibility with existing applications that
6462 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6463 // break because of the new default handling for hoverable views
6464 // in onHoverEvent.
6465 // Note that onGenericMotionEvent will be called by default when
6466 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6467 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006468 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006469 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006470 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006471 }
6472
6473 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006474 * Returns true if the view should handle {@link #onHoverEvent}
6475 * by calling {@link #setHovered} to change its hovered state.
6476 *
6477 * @return True if the view is hoverable.
6478 */
6479 private boolean isHoverable() {
6480 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006481 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006482 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6483 return false;
6484 }
6485
6486 return (viewFlags & CLICKABLE) == CLICKABLE
6487 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6488 }
6489
6490 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006491 * Returns true if the view is currently hovered.
6492 *
6493 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006494 *
6495 * @see #setHovered
6496 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006497 */
Jeff Brown10b62902011-06-20 16:40:37 -07006498 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006499 public boolean isHovered() {
6500 return (mPrivateFlags & HOVERED) != 0;
6501 }
6502
6503 /**
6504 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006505 * <p>
6506 * Calling this method also changes the drawable state of the view. This
6507 * enables the view to react to hover by using different drawable resources
6508 * to change its appearance.
6509 * </p><p>
6510 * The {@link #onHoverChanged} method is called when the hovered state changes.
6511 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006512 *
6513 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006514 *
6515 * @see #isHovered
6516 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006517 */
6518 public void setHovered(boolean hovered) {
6519 if (hovered) {
6520 if ((mPrivateFlags & HOVERED) == 0) {
6521 mPrivateFlags |= HOVERED;
6522 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006523 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006524 }
6525 } else {
6526 if ((mPrivateFlags & HOVERED) != 0) {
6527 mPrivateFlags &= ~HOVERED;
6528 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006529 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006530 }
6531 }
6532 }
6533
6534 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006535 * Implement this method to handle hover state changes.
6536 * <p>
6537 * This method is called whenever the hover state changes as a result of a
6538 * call to {@link #setHovered}.
6539 * </p>
6540 *
6541 * @param hovered The current hover state, as returned by {@link #isHovered}.
6542 *
6543 * @see #isHovered
6544 * @see #setHovered
6545 */
6546 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006547 }
6548
6549 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006550 * Implement this method to handle touch screen motion events.
6551 *
6552 * @param event The motion event.
6553 * @return True if the event was handled, false otherwise.
6554 */
6555 public boolean onTouchEvent(MotionEvent event) {
6556 final int viewFlags = mViewFlags;
6557
6558 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006559 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08006560 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006562 // A disabled view that is clickable still consumes the touch
6563 // events, it just doesn't respond to them.
6564 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6565 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6566 }
6567
6568 if (mTouchDelegate != null) {
6569 if (mTouchDelegate.onTouchEvent(event)) {
6570 return true;
6571 }
6572 }
6573
6574 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6575 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6576 switch (event.getAction()) {
6577 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006578 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6579 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006580 // take focus if we don't have it already and we should in
6581 // touch mode.
6582 boolean focusTaken = false;
6583 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6584 focusTaken = requestFocus();
6585 }
6586
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006587 if (prepressed) {
6588 // The button is being released before we actually
6589 // showed it as pressed. Make it show the pressed
6590 // state now (before scheduling the click) to ensure
6591 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08006592 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006593 }
Joe Malin32736f02011-01-19 16:14:20 -08006594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006595 if (!mHasPerformedLongPress) {
6596 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006597 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006598
6599 // Only perform take click actions if we were in the pressed state
6600 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006601 // Use a Runnable and post this rather than calling
6602 // performClick directly. This lets other visual state
6603 // of the view update before click actions start.
6604 if (mPerformClick == null) {
6605 mPerformClick = new PerformClick();
6606 }
6607 if (!post(mPerformClick)) {
6608 performClick();
6609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006610 }
6611 }
6612
6613 if (mUnsetPressedState == null) {
6614 mUnsetPressedState = new UnsetPressedState();
6615 }
6616
Adam Powelle14579b2009-12-16 18:39:52 -08006617 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006618 postDelayed(mUnsetPressedState,
6619 ViewConfiguration.getPressedStateDuration());
6620 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006621 // If the post failed, unpress right now
6622 mUnsetPressedState.run();
6623 }
Adam Powelle14579b2009-12-16 18:39:52 -08006624 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006625 }
6626 break;
6627
6628 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006629 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006630
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006631 if (performButtonActionOnTouchDown(event)) {
6632 break;
6633 }
6634
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006635 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006636 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006637
6638 // For views inside a scrolling container, delay the pressed feedback for
6639 // a short period in case this is a scroll.
6640 if (isInScrollingContainer) {
6641 mPrivateFlags |= PREPRESSED;
6642 if (mPendingCheckForTap == null) {
6643 mPendingCheckForTap = new CheckForTap();
6644 }
6645 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6646 } else {
6647 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08006648 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006649 checkForLongClick(0);
6650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006651 break;
6652
6653 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08006654 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08006655 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006656 break;
6657
6658 case MotionEvent.ACTION_MOVE:
6659 final int x = (int) event.getX();
6660 final int y = (int) event.getY();
6661
6662 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006663 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006664 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006665 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006666 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006667 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006668 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006669
Adam Powell4d6f0662012-02-21 15:11:11 -08006670 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006672 }
6673 break;
6674 }
6675 return true;
6676 }
6677
6678 return false;
6679 }
6680
6681 /**
Adam Powell10298662011-08-14 18:26:30 -07006682 * @hide
6683 */
6684 public boolean isInScrollingContainer() {
6685 ViewParent p = getParent();
6686 while (p != null && p instanceof ViewGroup) {
6687 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6688 return true;
6689 }
6690 p = p.getParent();
6691 }
6692 return false;
6693 }
6694
6695 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006696 * Remove the longpress detection timer.
6697 */
6698 private void removeLongPressCallback() {
6699 if (mPendingCheckForLongPress != null) {
6700 removeCallbacks(mPendingCheckForLongPress);
6701 }
6702 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006703
6704 /**
6705 * Remove the pending click action
6706 */
6707 private void removePerformClickCallback() {
6708 if (mPerformClick != null) {
6709 removeCallbacks(mPerformClick);
6710 }
6711 }
6712
Adam Powelle14579b2009-12-16 18:39:52 -08006713 /**
Romain Guya440b002010-02-24 15:57:54 -08006714 * Remove the prepress detection timer.
6715 */
6716 private void removeUnsetPressCallback() {
6717 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6718 setPressed(false);
6719 removeCallbacks(mUnsetPressedState);
6720 }
6721 }
6722
6723 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006724 * Remove the tap detection timer.
6725 */
6726 private void removeTapCallback() {
6727 if (mPendingCheckForTap != null) {
6728 mPrivateFlags &= ~PREPRESSED;
6729 removeCallbacks(mPendingCheckForTap);
6730 }
6731 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006732
6733 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006734 * Cancels a pending long press. Your subclass can use this if you
6735 * want the context menu to come up if the user presses and holds
6736 * at the same place, but you don't want it to come up if they press
6737 * and then move around enough to cause scrolling.
6738 */
6739 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006740 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006741
6742 /*
6743 * The prepressed state handled by the tap callback is a display
6744 * construct, but the tap callback will post a long press callback
6745 * less its own timeout. Remove it here.
6746 */
6747 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006748 }
6749
6750 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006751 * Remove the pending callback for sending a
6752 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6753 */
6754 private void removeSendViewScrolledAccessibilityEventCallback() {
6755 if (mSendViewScrolledAccessibilityEvent != null) {
6756 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6757 }
6758 }
6759
6760 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006761 * Sets the TouchDelegate for this View.
6762 */
6763 public void setTouchDelegate(TouchDelegate delegate) {
6764 mTouchDelegate = delegate;
6765 }
6766
6767 /**
6768 * Gets the TouchDelegate for this View.
6769 */
6770 public TouchDelegate getTouchDelegate() {
6771 return mTouchDelegate;
6772 }
6773
6774 /**
6775 * Set flags controlling behavior of this view.
6776 *
6777 * @param flags Constant indicating the value which should be set
6778 * @param mask Constant indicating the bit range that should be changed
6779 */
6780 void setFlags(int flags, int mask) {
6781 int old = mViewFlags;
6782 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6783
6784 int changed = mViewFlags ^ old;
6785 if (changed == 0) {
6786 return;
6787 }
6788 int privateFlags = mPrivateFlags;
6789
6790 /* Check if the FOCUSABLE bit has changed */
6791 if (((changed & FOCUSABLE_MASK) != 0) &&
6792 ((privateFlags & HAS_BOUNDS) !=0)) {
6793 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6794 && ((privateFlags & FOCUSED) != 0)) {
6795 /* Give up focus if we are no longer focusable */
6796 clearFocus();
6797 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6798 && ((privateFlags & FOCUSED) == 0)) {
6799 /*
6800 * Tell the view system that we are now available to take focus
6801 * if no one else already has it.
6802 */
6803 if (mParent != null) mParent.focusableViewAvailable(this);
6804 }
6805 }
6806
6807 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6808 if ((changed & VISIBILITY_MASK) != 0) {
6809 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006810 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006811 * it was not visible. Marking it drawn ensures that the invalidation will
6812 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006813 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006814 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006815 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006816
6817 needGlobalAttributesUpdate(true);
6818
6819 // a view becoming visible is worth notifying the parent
6820 // about in case nothing has focus. even if this specific view
6821 // isn't focusable, it may contain something that is, so let
6822 // the root view try to give this focus if nothing else does.
6823 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6824 mParent.focusableViewAvailable(this);
6825 }
6826 }
6827 }
6828
6829 /* Check if the GONE bit has changed */
6830 if ((changed & GONE) != 0) {
6831 needGlobalAttributesUpdate(false);
6832 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006833
Romain Guyecd80ee2009-12-03 17:13:02 -08006834 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6835 if (hasFocus()) clearFocus();
6836 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006837 if (mParent instanceof View) {
6838 // GONE views noop invalidation, so invalidate the parent
6839 ((View) mParent).invalidate(true);
6840 }
6841 // Mark the view drawn to ensure that it gets invalidated properly the next
6842 // time it is visible and gets invalidated
6843 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006844 }
6845 if (mAttachInfo != null) {
6846 mAttachInfo.mViewVisibilityChanged = true;
6847 }
6848 }
6849
6850 /* Check if the VISIBLE bit has changed */
6851 if ((changed & INVISIBLE) != 0) {
6852 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006853 /*
6854 * If this view is becoming invisible, set the DRAWN flag so that
6855 * the next invalidate() will not be skipped.
6856 */
6857 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006858
6859 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6860 // root view becoming invisible shouldn't clear focus
6861 if (getRootView() != this) {
6862 clearFocus();
6863 }
6864 }
6865 if (mAttachInfo != null) {
6866 mAttachInfo.mViewVisibilityChanged = true;
6867 }
6868 }
6869
Adam Powell326d8082009-12-09 15:10:07 -08006870 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006871 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08006872 ((ViewGroup) mParent).onChildVisibilityChanged(this,
6873 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08006874 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006875 } else if (mParent != null) {
6876 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006877 }
Adam Powell326d8082009-12-09 15:10:07 -08006878 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6879 }
6880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006881 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6882 destroyDrawingCache();
6883 }
6884
6885 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6886 destroyDrawingCache();
6887 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006888 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006889 }
6890
6891 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6892 destroyDrawingCache();
6893 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6894 }
6895
6896 if ((changed & DRAW_MASK) != 0) {
6897 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6898 if (mBGDrawable != null) {
6899 mPrivateFlags &= ~SKIP_DRAW;
6900 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6901 } else {
6902 mPrivateFlags |= SKIP_DRAW;
6903 }
6904 } else {
6905 mPrivateFlags &= ~SKIP_DRAW;
6906 }
6907 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006908 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006909 }
6910
6911 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006912 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006913 mParent.recomputeViewAttributes(this);
6914 }
6915 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006916
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006917 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006918 requestLayout();
6919 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006920 }
6921
6922 /**
6923 * Change the view's z order in the tree, so it's on top of other sibling
6924 * views
6925 */
6926 public void bringToFront() {
6927 if (mParent != null) {
6928 mParent.bringChildToFront(this);
6929 }
6930 }
6931
6932 /**
6933 * This is called in response to an internal scroll in this view (i.e., the
6934 * view scrolled its own contents). This is typically as a result of
6935 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6936 * called.
6937 *
6938 * @param l Current horizontal scroll origin.
6939 * @param t Current vertical scroll origin.
6940 * @param oldl Previous horizontal scroll origin.
6941 * @param oldt Previous vertical scroll origin.
6942 */
6943 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006944 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6945 postSendViewScrolledAccessibilityEventCallback();
6946 }
6947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006948 mBackgroundSizeChanged = true;
6949
6950 final AttachInfo ai = mAttachInfo;
6951 if (ai != null) {
6952 ai.mViewScrollChanged = true;
6953 }
6954 }
6955
6956 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006957 * Interface definition for a callback to be invoked when the layout bounds of a view
6958 * changes due to layout processing.
6959 */
6960 public interface OnLayoutChangeListener {
6961 /**
6962 * Called when the focus state of a view has changed.
6963 *
6964 * @param v The view whose state has changed.
6965 * @param left The new value of the view's left property.
6966 * @param top The new value of the view's top property.
6967 * @param right The new value of the view's right property.
6968 * @param bottom The new value of the view's bottom property.
6969 * @param oldLeft The previous value of the view's left property.
6970 * @param oldTop The previous value of the view's top property.
6971 * @param oldRight The previous value of the view's right property.
6972 * @param oldBottom The previous value of the view's bottom property.
6973 */
6974 void onLayoutChange(View v, int left, int top, int right, int bottom,
6975 int oldLeft, int oldTop, int oldRight, int oldBottom);
6976 }
6977
6978 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006979 * This is called during layout when the size of this view has changed. If
6980 * you were just added to the view hierarchy, you're called with the old
6981 * values of 0.
6982 *
6983 * @param w Current width of this view.
6984 * @param h Current height of this view.
6985 * @param oldw Old width of this view.
6986 * @param oldh Old height of this view.
6987 */
6988 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6989 }
6990
6991 /**
6992 * Called by draw to draw the child views. This may be overridden
6993 * by derived classes to gain control just before its children are drawn
6994 * (but after its own view has been drawn).
6995 * @param canvas the canvas on which to draw the view
6996 */
6997 protected void dispatchDraw(Canvas canvas) {
6998 }
6999
7000 /**
7001 * Gets the parent of this view. Note that the parent is a
7002 * ViewParent and not necessarily a View.
7003 *
7004 * @return Parent of this view.
7005 */
7006 public final ViewParent getParent() {
7007 return mParent;
7008 }
7009
7010 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007011 * Set the horizontal scrolled position of your view. This will cause a call to
7012 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7013 * invalidated.
7014 * @param value the x position to scroll to
7015 */
7016 public void setScrollX(int value) {
7017 scrollTo(value, mScrollY);
7018 }
7019
7020 /**
7021 * Set the vertical scrolled position of your view. This will cause a call to
7022 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7023 * invalidated.
7024 * @param value the y position to scroll to
7025 */
7026 public void setScrollY(int value) {
7027 scrollTo(mScrollX, value);
7028 }
7029
7030 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007031 * Return the scrolled left position of this view. This is the left edge of
7032 * the displayed part of your view. You do not need to draw any pixels
7033 * farther left, since those are outside of the frame of your view on
7034 * screen.
7035 *
7036 * @return The left edge of the displayed part of your view, in pixels.
7037 */
7038 public final int getScrollX() {
7039 return mScrollX;
7040 }
7041
7042 /**
7043 * Return the scrolled top position of this view. This is the top edge of
7044 * the displayed part of your view. You do not need to draw any pixels above
7045 * it, since those are outside of the frame of your view on screen.
7046 *
7047 * @return The top edge of the displayed part of your view, in pixels.
7048 */
7049 public final int getScrollY() {
7050 return mScrollY;
7051 }
7052
7053 /**
7054 * Return the width of the your view.
7055 *
7056 * @return The width of your view, in pixels.
7057 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007058 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007059 public final int getWidth() {
7060 return mRight - mLeft;
7061 }
7062
7063 /**
7064 * Return the height of your view.
7065 *
7066 * @return The height of your view, in pixels.
7067 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007068 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007069 public final int getHeight() {
7070 return mBottom - mTop;
7071 }
7072
7073 /**
7074 * Return the visible drawing bounds of your view. Fills in the output
7075 * rectangle with the values from getScrollX(), getScrollY(),
7076 * getWidth(), and getHeight().
7077 *
7078 * @param outRect The (scrolled) drawing bounds of the view.
7079 */
7080 public void getDrawingRect(Rect outRect) {
7081 outRect.left = mScrollX;
7082 outRect.top = mScrollY;
7083 outRect.right = mScrollX + (mRight - mLeft);
7084 outRect.bottom = mScrollY + (mBottom - mTop);
7085 }
7086
7087 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007088 * Like {@link #getMeasuredWidthAndState()}, but only returns the
7089 * raw width component (that is the result is masked by
7090 * {@link #MEASURED_SIZE_MASK}).
7091 *
7092 * @return The raw measured width of this view.
7093 */
7094 public final int getMeasuredWidth() {
7095 return mMeasuredWidth & MEASURED_SIZE_MASK;
7096 }
7097
7098 /**
7099 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007100 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007101 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007102 * This should be used during measurement and layout calculations only. Use
7103 * {@link #getWidth()} to see how wide a view is after layout.
7104 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007105 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007106 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08007107 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007108 return mMeasuredWidth;
7109 }
7110
7111 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007112 * Like {@link #getMeasuredHeightAndState()}, but only returns the
7113 * raw width component (that is the result is masked by
7114 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007115 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007116 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007117 */
7118 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08007119 return mMeasuredHeight & MEASURED_SIZE_MASK;
7120 }
7121
7122 /**
7123 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007124 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007125 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
7126 * This should be used during measurement and layout calculations only. Use
7127 * {@link #getHeight()} to see how wide a view is after layout.
7128 *
7129 * @return The measured width of this view as a bit mask.
7130 */
7131 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007132 return mMeasuredHeight;
7133 }
7134
7135 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007136 * Return only the state bits of {@link #getMeasuredWidthAndState()}
7137 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
7138 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
7139 * and the height component is at the shifted bits
7140 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
7141 */
7142 public final int getMeasuredState() {
7143 return (mMeasuredWidth&MEASURED_STATE_MASK)
7144 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
7145 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
7146 }
7147
7148 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007149 * The transform matrix of this view, which is calculated based on the current
7150 * roation, scale, and pivot properties.
7151 *
7152 * @see #getRotation()
7153 * @see #getScaleX()
7154 * @see #getScaleY()
7155 * @see #getPivotX()
7156 * @see #getPivotY()
7157 * @return The current transform matrix for the view
7158 */
7159 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007160 if (mTransformationInfo != null) {
7161 updateMatrix();
7162 return mTransformationInfo.mMatrix;
7163 }
7164 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07007165 }
7166
7167 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007168 * Utility function to determine if the value is far enough away from zero to be
7169 * considered non-zero.
7170 * @param value A floating point value to check for zero-ness
7171 * @return whether the passed-in value is far enough away from zero to be considered non-zero
7172 */
7173 private static boolean nonzero(float value) {
7174 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
7175 }
7176
7177 /**
Jeff Brown86671742010-09-30 20:00:15 -07007178 * Returns true if the transform matrix is the identity matrix.
7179 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08007180 *
Romain Guy33e72ae2010-07-17 12:40:29 -07007181 * @return True if the transform matrix is the identity matrix, false otherwise.
7182 */
Jeff Brown86671742010-09-30 20:00:15 -07007183 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007184 if (mTransformationInfo != null) {
7185 updateMatrix();
7186 return mTransformationInfo.mMatrixIsIdentity;
7187 }
7188 return true;
7189 }
7190
7191 void ensureTransformationInfo() {
7192 if (mTransformationInfo == null) {
7193 mTransformationInfo = new TransformationInfo();
7194 }
Jeff Brown86671742010-09-30 20:00:15 -07007195 }
7196
7197 /**
7198 * Recomputes the transform matrix if necessary.
7199 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007200 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007201 final TransformationInfo info = mTransformationInfo;
7202 if (info == null) {
7203 return;
7204 }
7205 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007206 // transform-related properties have changed since the last time someone
7207 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007208
7209 // Figure out if we need to update the pivot point
7210 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007211 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7212 info.mPrevWidth = mRight - mLeft;
7213 info.mPrevHeight = mBottom - mTop;
7214 info.mPivotX = info.mPrevWidth / 2f;
7215 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007216 }
7217 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007218 info.mMatrix.reset();
7219 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7220 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7221 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7222 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007223 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007224 if (info.mCamera == null) {
7225 info.mCamera = new Camera();
7226 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007227 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007228 info.mCamera.save();
7229 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7230 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7231 info.mCamera.getMatrix(info.matrix3D);
7232 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7233 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7234 info.mPivotY + info.mTranslationY);
7235 info.mMatrix.postConcat(info.matrix3D);
7236 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007237 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007238 info.mMatrixDirty = false;
7239 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7240 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007241 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007242 }
7243
7244 /**
7245 * Utility method to retrieve the inverse of the current mMatrix property.
7246 * We cache the matrix to avoid recalculating it when transform properties
7247 * have not changed.
7248 *
7249 * @return The inverse of the current matrix of this view.
7250 */
Jeff Brown86671742010-09-30 20:00:15 -07007251 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007252 final TransformationInfo info = mTransformationInfo;
7253 if (info != null) {
7254 updateMatrix();
7255 if (info.mInverseMatrixDirty) {
7256 if (info.mInverseMatrix == null) {
7257 info.mInverseMatrix = new Matrix();
7258 }
7259 info.mMatrix.invert(info.mInverseMatrix);
7260 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007261 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007262 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007263 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007264 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007265 }
7266
7267 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007268 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7269 * views are drawn) from the camera to this view. The camera's distance
7270 * affects 3D transformations, for instance rotations around the X and Y
7271 * axis. If the rotationX or rotationY properties are changed and this view is
7272 * large (more than half the size of the screen), it is recommended to always
7273 * use a camera distance that's greater than the height (X axis rotation) or
7274 * the width (Y axis rotation) of this view.</p>
7275 *
7276 * <p>The distance of the camera from the view plane can have an affect on the
7277 * perspective distortion of the view when it is rotated around the x or y axis.
7278 * For example, a large distance will result in a large viewing angle, and there
7279 * will not be much perspective distortion of the view as it rotates. A short
7280 * distance may cause much more perspective distortion upon rotation, and can
7281 * also result in some drawing artifacts if the rotated view ends up partially
7282 * behind the camera (which is why the recommendation is to use a distance at
7283 * least as far as the size of the view, if the view is to be rotated.)</p>
7284 *
7285 * <p>The distance is expressed in "depth pixels." The default distance depends
7286 * on the screen density. For instance, on a medium density display, the
7287 * default distance is 1280. On a high density display, the default distance
7288 * is 1920.</p>
7289 *
7290 * <p>If you want to specify a distance that leads to visually consistent
7291 * results across various densities, use the following formula:</p>
7292 * <pre>
7293 * float scale = context.getResources().getDisplayMetrics().density;
7294 * view.setCameraDistance(distance * scale);
7295 * </pre>
7296 *
7297 * <p>The density scale factor of a high density display is 1.5,
7298 * and 1920 = 1280 * 1.5.</p>
7299 *
7300 * @param distance The distance in "depth pixels", if negative the opposite
7301 * value is used
7302 *
7303 * @see #setRotationX(float)
7304 * @see #setRotationY(float)
7305 */
7306 public void setCameraDistance(float distance) {
7307 invalidateParentCaches();
7308 invalidate(false);
7309
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007310 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007311 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007312 final TransformationInfo info = mTransformationInfo;
7313 if (info.mCamera == null) {
7314 info.mCamera = new Camera();
7315 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007316 }
7317
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007318 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7319 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007320
7321 invalidate(false);
7322 }
7323
7324 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007325 * The degrees that the view is rotated around the pivot point.
7326 *
Romain Guya5364ee2011-02-24 14:46:04 -08007327 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007328 * @see #getPivotX()
7329 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007330 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007331 * @return The degrees of rotation.
7332 */
Chet Haasea5531132012-02-02 13:41:44 -08007333 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007334 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007335 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007336 }
7337
7338 /**
Chet Haase897247b2010-09-09 14:54:47 -07007339 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7340 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007341 *
7342 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007343 *
7344 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007345 * @see #getPivotX()
7346 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007347 * @see #setRotationX(float)
7348 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007349 *
7350 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007351 */
7352 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007353 ensureTransformationInfo();
7354 final TransformationInfo info = mTransformationInfo;
7355 if (info.mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007356 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007357 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007358 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007359 info.mRotation = rotation;
7360 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007361 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007362 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007363 }
7364 }
7365
7366 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007367 * The degrees that the view is rotated around the vertical axis through the pivot point.
7368 *
7369 * @see #getPivotX()
7370 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007371 * @see #setRotationY(float)
7372 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007373 * @return The degrees of Y rotation.
7374 */
Chet Haasea5531132012-02-02 13:41:44 -08007375 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007376 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007377 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007378 }
7379
7380 /**
Chet Haase897247b2010-09-09 14:54:47 -07007381 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7382 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7383 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007384 *
7385 * When rotating large views, it is recommended to adjust the camera distance
7386 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007387 *
7388 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007389 *
7390 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007391 * @see #getPivotX()
7392 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007393 * @see #setRotation(float)
7394 * @see #setRotationX(float)
7395 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007396 *
7397 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007398 */
7399 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007400 ensureTransformationInfo();
7401 final TransformationInfo info = mTransformationInfo;
7402 if (info.mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007403 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007404 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007405 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007406 info.mRotationY = rotationY;
7407 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007408 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007409 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007410 }
7411 }
7412
7413 /**
7414 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7415 *
7416 * @see #getPivotX()
7417 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007418 * @see #setRotationX(float)
7419 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007420 * @return The degrees of X rotation.
7421 */
Chet Haasea5531132012-02-02 13:41:44 -08007422 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007423 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007424 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007425 }
7426
7427 /**
Chet Haase897247b2010-09-09 14:54:47 -07007428 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7429 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7430 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007431 *
7432 * When rotating large views, it is recommended to adjust the camera distance
7433 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007434 *
7435 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007436 *
7437 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007438 * @see #getPivotX()
7439 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007440 * @see #setRotation(float)
7441 * @see #setRotationY(float)
7442 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007443 *
7444 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007445 */
7446 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007447 ensureTransformationInfo();
7448 final TransformationInfo info = mTransformationInfo;
7449 if (info.mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007450 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007451 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007452 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007453 info.mRotationX = rotationX;
7454 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007455 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007456 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007457 }
7458 }
7459
7460 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007461 * The amount that the view is scaled in x around the pivot point, as a proportion of
7462 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7463 *
Joe Onorato93162322010-09-16 15:42:01 -04007464 * <p>By default, this is 1.0f.
7465 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007466 * @see #getPivotX()
7467 * @see #getPivotY()
7468 * @return The scaling factor.
7469 */
Chet Haasea5531132012-02-02 13:41:44 -08007470 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007471 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007472 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007473 }
7474
7475 /**
7476 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7477 * the view's unscaled width. A value of 1 means that no scaling is applied.
7478 *
7479 * @param scaleX The scaling factor.
7480 * @see #getPivotX()
7481 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007482 *
7483 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007484 */
7485 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007486 ensureTransformationInfo();
7487 final TransformationInfo info = mTransformationInfo;
7488 if (info.mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007489 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007490 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007491 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007492 info.mScaleX = scaleX;
7493 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007494 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007495 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007496 }
7497 }
7498
7499 /**
7500 * The amount that the view is scaled in y around the pivot point, as a proportion of
7501 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7502 *
Joe Onorato93162322010-09-16 15:42:01 -04007503 * <p>By default, this is 1.0f.
7504 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007505 * @see #getPivotX()
7506 * @see #getPivotY()
7507 * @return The scaling factor.
7508 */
Chet Haasea5531132012-02-02 13:41:44 -08007509 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007510 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007511 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007512 }
7513
7514 /**
7515 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7516 * the view's unscaled width. A value of 1 means that no scaling is applied.
7517 *
7518 * @param scaleY The scaling factor.
7519 * @see #getPivotX()
7520 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007521 *
7522 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007523 */
7524 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007525 ensureTransformationInfo();
7526 final TransformationInfo info = mTransformationInfo;
7527 if (info.mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007528 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007529 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007530 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007531 info.mScaleY = scaleY;
7532 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007533 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007534 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007535 }
7536 }
7537
7538 /**
7539 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7540 * and {@link #setScaleX(float) scaled}.
7541 *
7542 * @see #getRotation()
7543 * @see #getScaleX()
7544 * @see #getScaleY()
7545 * @see #getPivotY()
7546 * @return The x location of the pivot point.
7547 */
Chet Haasea5531132012-02-02 13:41:44 -08007548 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007549 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007550 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007551 }
7552
7553 /**
7554 * Sets the x location of the point around which the view is
7555 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007556 * By default, the pivot point is centered on the object.
7557 * Setting this property disables this behavior and causes the view to use only the
7558 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007559 *
7560 * @param pivotX The x location of the pivot point.
7561 * @see #getRotation()
7562 * @see #getScaleX()
7563 * @see #getScaleY()
7564 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007565 *
7566 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007567 */
7568 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007569 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007570 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007571 final TransformationInfo info = mTransformationInfo;
7572 if (info.mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007573 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007574 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007575 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007576 info.mPivotX = pivotX;
7577 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007578 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007579 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007580 }
7581 }
7582
7583 /**
7584 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7585 * and {@link #setScaleY(float) scaled}.
7586 *
7587 * @see #getRotation()
7588 * @see #getScaleX()
7589 * @see #getScaleY()
7590 * @see #getPivotY()
7591 * @return The y location of the pivot point.
7592 */
Chet Haasea5531132012-02-02 13:41:44 -08007593 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007594 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007595 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007596 }
7597
7598 /**
7599 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007600 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7601 * Setting this property disables this behavior and causes the view to use only the
7602 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007603 *
7604 * @param pivotY The y location of the pivot point.
7605 * @see #getRotation()
7606 * @see #getScaleX()
7607 * @see #getScaleY()
7608 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007609 *
7610 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007611 */
7612 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007613 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007614 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007615 final TransformationInfo info = mTransformationInfo;
7616 if (info.mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007617 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007618 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007619 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007620 info.mPivotY = pivotY;
7621 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007622 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007623 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007624 }
7625 }
7626
7627 /**
7628 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7629 * completely transparent and 1 means the view is completely opaque.
7630 *
Joe Onorato93162322010-09-16 15:42:01 -04007631 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007632 * @return The opacity of the view.
7633 */
Chet Haasea5531132012-02-02 13:41:44 -08007634 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007635 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007636 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007637 }
7638
7639 /**
Romain Guy171c5922011-01-06 10:04:23 -08007640 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7641 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007642 *
Romain Guy171c5922011-01-06 10:04:23 -08007643 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7644 * responsible for applying the opacity itself. Otherwise, calling this method is
7645 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007646 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007647 *
Chet Haasea5531132012-02-02 13:41:44 -08007648 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
7649 * performance implications. It is generally best to use the alpha property sparingly and
7650 * transiently, as in the case of fading animations.</p>
7651 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007652 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007653 *
Joe Malin32736f02011-01-19 16:14:20 -08007654 * @see #setLayerType(int, android.graphics.Paint)
7655 *
Chet Haase73066682010-11-29 15:55:32 -08007656 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007657 */
7658 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007659 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007660 if (mTransformationInfo.mAlpha != alpha) {
7661 mTransformationInfo.mAlpha = alpha;
7662 invalidateParentCaches();
7663 if (onSetAlpha((int) (alpha * 255))) {
7664 mPrivateFlags |= ALPHA_SET;
7665 // subclass is handling alpha - don't optimize rendering cache invalidation
7666 invalidate(true);
7667 } else {
7668 mPrivateFlags &= ~ALPHA_SET;
7669 invalidate(false);
7670 }
Chet Haaseed032702010-10-01 14:05:54 -07007671 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007672 }
7673
7674 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007675 * Faster version of setAlpha() which performs the same steps except there are
7676 * no calls to invalidate(). The caller of this function should perform proper invalidation
7677 * on the parent and this object. The return value indicates whether the subclass handles
7678 * alpha (the return value for onSetAlpha()).
7679 *
7680 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007681 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
7682 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08007683 */
7684 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007685 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007686 if (mTransformationInfo.mAlpha != alpha) {
7687 mTransformationInfo.mAlpha = alpha;
7688 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7689 if (subclassHandlesAlpha) {
7690 mPrivateFlags |= ALPHA_SET;
7691 return true;
7692 } else {
7693 mPrivateFlags &= ~ALPHA_SET;
7694 }
Chet Haasea00f3862011-02-22 06:34:40 -08007695 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007696 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08007697 }
7698
7699 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007700 * Top position of this view relative to its parent.
7701 *
7702 * @return The top of this view, in pixels.
7703 */
7704 @ViewDebug.CapturedViewProperty
7705 public final int getTop() {
7706 return mTop;
7707 }
7708
7709 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007710 * Sets the top position of this view relative to its parent. This method is meant to be called
7711 * by the layout system and should not generally be called otherwise, because the property
7712 * may be changed at any time by the layout.
7713 *
7714 * @param top The top of this view, in pixels.
7715 */
7716 public final void setTop(int top) {
7717 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007718 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007719 final boolean matrixIsIdentity = mTransformationInfo == null
7720 || mTransformationInfo.mMatrixIsIdentity;
7721 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007722 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007723 int minTop;
7724 int yLoc;
7725 if (top < mTop) {
7726 minTop = top;
7727 yLoc = top - mTop;
7728 } else {
7729 minTop = mTop;
7730 yLoc = 0;
7731 }
Chet Haasee9140a72011-02-16 16:23:29 -08007732 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007733 }
7734 } else {
7735 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007736 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007737 }
7738
Chet Haaseed032702010-10-01 14:05:54 -07007739 int width = mRight - mLeft;
7740 int oldHeight = mBottom - mTop;
7741
Chet Haase21cd1382010-09-01 17:42:29 -07007742 mTop = top;
7743
Chet Haaseed032702010-10-01 14:05:54 -07007744 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7745
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007746 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007747 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7748 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007749 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007750 }
Chet Haase21cd1382010-09-01 17:42:29 -07007751 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007752 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007753 }
Chet Haase55dbb652010-12-21 20:15:08 -08007754 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007755 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007756 }
7757 }
7758
7759 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007760 * Bottom position of this view relative to its parent.
7761 *
7762 * @return The bottom of this view, in pixels.
7763 */
7764 @ViewDebug.CapturedViewProperty
7765 public final int getBottom() {
7766 return mBottom;
7767 }
7768
7769 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007770 * True if this view has changed since the last time being drawn.
7771 *
7772 * @return The dirty state of this view.
7773 */
7774 public boolean isDirty() {
7775 return (mPrivateFlags & DIRTY_MASK) != 0;
7776 }
7777
7778 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007779 * Sets the bottom position of this view relative to its parent. This method is meant to be
7780 * called by the layout system and should not generally be called otherwise, because the
7781 * property may be changed at any time by the layout.
7782 *
7783 * @param bottom The bottom of this view, in pixels.
7784 */
7785 public final void setBottom(int bottom) {
7786 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007787 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007788 final boolean matrixIsIdentity = mTransformationInfo == null
7789 || mTransformationInfo.mMatrixIsIdentity;
7790 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007791 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007792 int maxBottom;
7793 if (bottom < mBottom) {
7794 maxBottom = mBottom;
7795 } else {
7796 maxBottom = bottom;
7797 }
Chet Haasee9140a72011-02-16 16:23:29 -08007798 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007799 }
7800 } else {
7801 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007802 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007803 }
7804
Chet Haaseed032702010-10-01 14:05:54 -07007805 int width = mRight - mLeft;
7806 int oldHeight = mBottom - mTop;
7807
Chet Haase21cd1382010-09-01 17:42:29 -07007808 mBottom = bottom;
7809
Chet Haaseed032702010-10-01 14:05:54 -07007810 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7811
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007812 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007813 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7814 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007815 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007816 }
Chet Haase21cd1382010-09-01 17:42:29 -07007817 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007818 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007819 }
Chet Haase55dbb652010-12-21 20:15:08 -08007820 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007821 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007822 }
7823 }
7824
7825 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007826 * Left position of this view relative to its parent.
7827 *
7828 * @return The left edge of this view, in pixels.
7829 */
7830 @ViewDebug.CapturedViewProperty
7831 public final int getLeft() {
7832 return mLeft;
7833 }
7834
7835 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007836 * Sets the left position of this view relative to its parent. This method is meant to be called
7837 * by the layout system and should not generally be called otherwise, because the property
7838 * may be changed at any time by the layout.
7839 *
7840 * @param left The bottom of this view, in pixels.
7841 */
7842 public final void setLeft(int left) {
7843 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007844 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007845 final boolean matrixIsIdentity = mTransformationInfo == null
7846 || mTransformationInfo.mMatrixIsIdentity;
7847 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007848 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007849 int minLeft;
7850 int xLoc;
7851 if (left < mLeft) {
7852 minLeft = left;
7853 xLoc = left - mLeft;
7854 } else {
7855 minLeft = mLeft;
7856 xLoc = 0;
7857 }
Chet Haasee9140a72011-02-16 16:23:29 -08007858 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007859 }
7860 } else {
7861 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007862 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007863 }
7864
Chet Haaseed032702010-10-01 14:05:54 -07007865 int oldWidth = mRight - mLeft;
7866 int height = mBottom - mTop;
7867
Chet Haase21cd1382010-09-01 17:42:29 -07007868 mLeft = left;
7869
Chet Haaseed032702010-10-01 14:05:54 -07007870 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7871
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007872 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007873 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7874 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007875 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007876 }
Chet Haase21cd1382010-09-01 17:42:29 -07007877 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007878 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007879 }
Chet Haase55dbb652010-12-21 20:15:08 -08007880 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007881 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007882 }
7883 }
7884
7885 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007886 * Right position of this view relative to its parent.
7887 *
7888 * @return The right edge of this view, in pixels.
7889 */
7890 @ViewDebug.CapturedViewProperty
7891 public final int getRight() {
7892 return mRight;
7893 }
7894
7895 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007896 * Sets the right position of this view relative to its parent. This method is meant to be called
7897 * by the layout system and should not generally be called otherwise, because the property
7898 * may be changed at any time by the layout.
7899 *
7900 * @param right The bottom of this view, in pixels.
7901 */
7902 public final void setRight(int right) {
7903 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007904 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007905 final boolean matrixIsIdentity = mTransformationInfo == null
7906 || mTransformationInfo.mMatrixIsIdentity;
7907 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007908 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007909 int maxRight;
7910 if (right < mRight) {
7911 maxRight = mRight;
7912 } else {
7913 maxRight = right;
7914 }
Chet Haasee9140a72011-02-16 16:23:29 -08007915 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007916 }
7917 } else {
7918 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007919 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007920 }
7921
Chet Haaseed032702010-10-01 14:05:54 -07007922 int oldWidth = mRight - mLeft;
7923 int height = mBottom - mTop;
7924
Chet Haase21cd1382010-09-01 17:42:29 -07007925 mRight = right;
7926
Chet Haaseed032702010-10-01 14:05:54 -07007927 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7928
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007929 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007930 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7931 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007932 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007933 }
Chet Haase21cd1382010-09-01 17:42:29 -07007934 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007935 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007936 }
Chet Haase55dbb652010-12-21 20:15:08 -08007937 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007938 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007939 }
7940 }
7941
7942 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007943 * The visual x position of this view, in pixels. This is equivalent to the
7944 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007945 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007946 *
Chet Haasedf030d22010-07-30 17:22:38 -07007947 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007948 */
Chet Haasea5531132012-02-02 13:41:44 -08007949 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007950 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007951 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007952 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007953
Chet Haasedf030d22010-07-30 17:22:38 -07007954 /**
7955 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7956 * {@link #setTranslationX(float) translationX} property to be the difference between
7957 * the x value passed in and the current {@link #getLeft() left} property.
7958 *
7959 * @param x The visual x position of this view, in pixels.
7960 */
7961 public void setX(float x) {
7962 setTranslationX(x - mLeft);
7963 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007964
Chet Haasedf030d22010-07-30 17:22:38 -07007965 /**
7966 * The visual y position of this view, in pixels. This is equivalent to the
7967 * {@link #setTranslationY(float) translationY} property plus the current
7968 * {@link #getTop() top} property.
7969 *
7970 * @return The visual y position of this view, in pixels.
7971 */
Chet Haasea5531132012-02-02 13:41:44 -08007972 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007973 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007974 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007975 }
7976
7977 /**
7978 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7979 * {@link #setTranslationY(float) translationY} property to be the difference between
7980 * the y value passed in and the current {@link #getTop() top} property.
7981 *
7982 * @param y The visual y position of this view, in pixels.
7983 */
7984 public void setY(float y) {
7985 setTranslationY(y - mTop);
7986 }
7987
7988
7989 /**
7990 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7991 * This position is post-layout, in addition to wherever the object's
7992 * layout placed it.
7993 *
7994 * @return The horizontal position of this view relative to its left position, in pixels.
7995 */
Chet Haasea5531132012-02-02 13:41:44 -08007996 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007997 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007998 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07007999 }
8000
8001 /**
8002 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
8003 * This effectively positions the object post-layout, in addition to wherever the object's
8004 * layout placed it.
8005 *
8006 * @param translationX The horizontal position of this view relative to its left position,
8007 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008008 *
8009 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07008010 */
8011 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008012 ensureTransformationInfo();
8013 final TransformationInfo info = mTransformationInfo;
8014 if (info.mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008015 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07008016 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07008017 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008018 info.mTranslationX = translationX;
8019 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07008020 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008021 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008022 }
8023 }
8024
8025 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008026 * The horizontal location of this view relative to its {@link #getTop() top} position.
8027 * This position is post-layout, in addition to wherever the object's
8028 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008029 *
Chet Haasedf030d22010-07-30 17:22:38 -07008030 * @return The vertical position of this view relative to its top position,
8031 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008032 */
Chet Haasea5531132012-02-02 13:41:44 -08008033 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008034 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008035 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008036 }
8037
8038 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008039 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
8040 * This effectively positions the object post-layout, in addition to wherever the object's
8041 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008042 *
Chet Haasedf030d22010-07-30 17:22:38 -07008043 * @param translationY The vertical position of this view relative to its top position,
8044 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008045 *
8046 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07008047 */
Chet Haasedf030d22010-07-30 17:22:38 -07008048 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008049 ensureTransformationInfo();
8050 final TransformationInfo info = mTransformationInfo;
8051 if (info.mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008052 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07008053 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07008054 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008055 info.mTranslationY = translationY;
8056 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07008057 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008058 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07008059 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008060 }
8061
8062 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008063 * Hit rectangle in parent's coordinates
8064 *
8065 * @param outRect The hit rectangle of the view.
8066 */
8067 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07008068 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008069 final TransformationInfo info = mTransformationInfo;
8070 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008071 outRect.set(mLeft, mTop, mRight, mBottom);
8072 } else {
8073 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008074 tmpRect.set(-info.mPivotX, -info.mPivotY,
8075 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
8076 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07008077 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
8078 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07008079 }
8080 }
8081
8082 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07008083 * Determines whether the given point, in local coordinates is inside the view.
8084 */
8085 /*package*/ final boolean pointInView(float localX, float localY) {
8086 return localX >= 0 && localX < (mRight - mLeft)
8087 && localY >= 0 && localY < (mBottom - mTop);
8088 }
8089
8090 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008091 * Utility method to determine whether the given point, in local coordinates,
8092 * is inside the view, where the area of the view is expanded by the slop factor.
8093 * This method is called while processing touch-move events to determine if the event
8094 * is still within the view.
8095 */
8096 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07008097 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07008098 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008099 }
8100
8101 /**
8102 * When a view has focus and the user navigates away from it, the next view is searched for
8103 * starting from the rectangle filled in by this method.
8104 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008105 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
8106 * of the view. However, if your view maintains some idea of internal selection,
8107 * such as a cursor, or a selected row or column, you should override this method and
8108 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008109 *
8110 * @param r The rectangle to fill in, in this view's coordinates.
8111 */
8112 public void getFocusedRect(Rect r) {
8113 getDrawingRect(r);
8114 }
8115
8116 /**
8117 * If some part of this view is not clipped by any of its parents, then
8118 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01008119 * coordinates (without taking possible View rotations into account), offset
8120 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
8121 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008122 *
8123 * @param r If true is returned, r holds the global coordinates of the
8124 * visible portion of this view.
8125 * @param globalOffset If true is returned, globalOffset holds the dx,dy
8126 * between this view and its root. globalOffet may be null.
8127 * @return true if r is non-empty (i.e. part of the view is visible at the
8128 * root level.
8129 */
8130 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
8131 int width = mRight - mLeft;
8132 int height = mBottom - mTop;
8133 if (width > 0 && height > 0) {
8134 r.set(0, 0, width, height);
8135 if (globalOffset != null) {
8136 globalOffset.set(-mScrollX, -mScrollY);
8137 }
8138 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
8139 }
8140 return false;
8141 }
8142
8143 public final boolean getGlobalVisibleRect(Rect r) {
8144 return getGlobalVisibleRect(r, null);
8145 }
8146
8147 public final boolean getLocalVisibleRect(Rect r) {
8148 Point offset = new Point();
8149 if (getGlobalVisibleRect(r, offset)) {
8150 r.offset(-offset.x, -offset.y); // make r local
8151 return true;
8152 }
8153 return false;
8154 }
8155
8156 /**
8157 * Offset this view's vertical location by the specified number of pixels.
8158 *
8159 * @param offset the number of pixels to offset the view by
8160 */
8161 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008162 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008163 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008164 final boolean matrixIsIdentity = mTransformationInfo == null
8165 || mTransformationInfo.mMatrixIsIdentity;
8166 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008167 final ViewParent p = mParent;
8168 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008169 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008170 int minTop;
8171 int maxBottom;
8172 int yLoc;
8173 if (offset < 0) {
8174 minTop = mTop + offset;
8175 maxBottom = mBottom;
8176 yLoc = offset;
8177 } else {
8178 minTop = mTop;
8179 maxBottom = mBottom + offset;
8180 yLoc = 0;
8181 }
8182 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8183 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008184 }
8185 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008186 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008187 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008188
Chet Haasec3aa3612010-06-17 08:50:37 -07008189 mTop += offset;
8190 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008191
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008192 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008193 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008194 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008195 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008196 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008198 }
8199
8200 /**
8201 * Offset this view's horizontal location by the specified amount of pixels.
8202 *
8203 * @param offset the numer of pixels to offset the view by
8204 */
8205 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008206 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008207 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008208 final boolean matrixIsIdentity = mTransformationInfo == null
8209 || mTransformationInfo.mMatrixIsIdentity;
8210 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008211 final ViewParent p = mParent;
8212 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008213 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008214 int minLeft;
8215 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008216 if (offset < 0) {
8217 minLeft = mLeft + offset;
8218 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008219 } else {
8220 minLeft = mLeft;
8221 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008222 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008223 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008224 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008225 }
8226 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008227 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008228 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008229
Chet Haasec3aa3612010-06-17 08:50:37 -07008230 mLeft += offset;
8231 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008232
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008233 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008234 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008235 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008236 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008237 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008239 }
8240
8241 /**
8242 * Get the LayoutParams associated with this view. All views should have
8243 * layout parameters. These supply parameters to the <i>parent</i> of this
8244 * view specifying how it should be arranged. There are many subclasses of
8245 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8246 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008247 *
8248 * This method may return null if this View is not attached to a parent
8249 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8250 * was not invoked successfully. When a View is attached to a parent
8251 * ViewGroup, this method must not return null.
8252 *
8253 * @return The LayoutParams associated with this view, or null if no
8254 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008255 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008256 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008257 public ViewGroup.LayoutParams getLayoutParams() {
8258 return mLayoutParams;
8259 }
8260
8261 /**
8262 * Set the layout parameters associated with this view. These supply
8263 * parameters to the <i>parent</i> of this view specifying how it should be
8264 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8265 * correspond to the different subclasses of ViewGroup that are responsible
8266 * for arranging their children.
8267 *
Romain Guy01c174b2011-02-22 11:51:06 -08008268 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008269 */
8270 public void setLayoutParams(ViewGroup.LayoutParams params) {
8271 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008272 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008273 }
8274 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08008275 if (mParent instanceof ViewGroup) {
8276 ((ViewGroup) mParent).onSetLayoutParams(this, params);
8277 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008278 requestLayout();
8279 }
8280
8281 /**
8282 * Set the scrolled position of your view. This will cause a call to
8283 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8284 * invalidated.
8285 * @param x the x position to scroll to
8286 * @param y the y position to scroll to
8287 */
8288 public void scrollTo(int x, int y) {
8289 if (mScrollX != x || mScrollY != y) {
8290 int oldX = mScrollX;
8291 int oldY = mScrollY;
8292 mScrollX = x;
8293 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008294 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008295 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008296 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008297 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008298 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008299 }
8300 }
8301
8302 /**
8303 * Move the scrolled position of your view. This will cause a call to
8304 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8305 * invalidated.
8306 * @param x the amount of pixels to scroll by horizontally
8307 * @param y the amount of pixels to scroll by vertically
8308 */
8309 public void scrollBy(int x, int y) {
8310 scrollTo(mScrollX + x, mScrollY + y);
8311 }
8312
8313 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008314 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8315 * animation to fade the scrollbars out after a default delay. If a subclass
8316 * provides animated scrolling, the start delay should equal the duration
8317 * of the scrolling animation.</p>
8318 *
8319 * <p>The animation starts only if at least one of the scrollbars is
8320 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8321 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8322 * this method returns true, and false otherwise. If the animation is
8323 * started, this method calls {@link #invalidate()}; in that case the
8324 * caller should not call {@link #invalidate()}.</p>
8325 *
8326 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008327 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008328 *
8329 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8330 * and {@link #scrollTo(int, int)}.</p>
8331 *
8332 * @return true if the animation is played, false otherwise
8333 *
8334 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008335 * @see #scrollBy(int, int)
8336 * @see #scrollTo(int, int)
8337 * @see #isHorizontalScrollBarEnabled()
8338 * @see #isVerticalScrollBarEnabled()
8339 * @see #setHorizontalScrollBarEnabled(boolean)
8340 * @see #setVerticalScrollBarEnabled(boolean)
8341 */
8342 protected boolean awakenScrollBars() {
8343 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008344 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008345 }
8346
8347 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008348 * Trigger the scrollbars to draw.
8349 * This method differs from awakenScrollBars() only in its default duration.
8350 * initialAwakenScrollBars() will show the scroll bars for longer than
8351 * usual to give the user more of a chance to notice them.
8352 *
8353 * @return true if the animation is played, false otherwise.
8354 */
8355 private boolean initialAwakenScrollBars() {
8356 return mScrollCache != null &&
8357 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8358 }
8359
8360 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008361 * <p>
8362 * Trigger the scrollbars to draw. When invoked this method starts an
8363 * animation to fade the scrollbars out after a fixed delay. If a subclass
8364 * provides animated scrolling, the start delay should equal the duration of
8365 * the scrolling animation.
8366 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008367 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008368 * <p>
8369 * The animation starts only if at least one of the scrollbars is enabled,
8370 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8371 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8372 * this method returns true, and false otherwise. If the animation is
8373 * started, this method calls {@link #invalidate()}; in that case the caller
8374 * should not call {@link #invalidate()}.
8375 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008376 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008377 * <p>
8378 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008379 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008380 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008381 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008382 * @param startDelay the delay, in milliseconds, after which the animation
8383 * should start; when the delay is 0, the animation starts
8384 * immediately
8385 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008386 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008387 * @see #scrollBy(int, int)
8388 * @see #scrollTo(int, int)
8389 * @see #isHorizontalScrollBarEnabled()
8390 * @see #isVerticalScrollBarEnabled()
8391 * @see #setHorizontalScrollBarEnabled(boolean)
8392 * @see #setVerticalScrollBarEnabled(boolean)
8393 */
8394 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008395 return awakenScrollBars(startDelay, true);
8396 }
Joe Malin32736f02011-01-19 16:14:20 -08008397
Mike Cleron290947b2009-09-29 18:34:32 -07008398 /**
8399 * <p>
8400 * Trigger the scrollbars to draw. When invoked this method starts an
8401 * animation to fade the scrollbars out after a fixed delay. If a subclass
8402 * provides animated scrolling, the start delay should equal the duration of
8403 * the scrolling animation.
8404 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008405 *
Mike Cleron290947b2009-09-29 18:34:32 -07008406 * <p>
8407 * The animation starts only if at least one of the scrollbars is enabled,
8408 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8409 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8410 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008411 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008412 * is set to true; in that case the caller
8413 * should not call {@link #invalidate()}.
8414 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008415 *
Mike Cleron290947b2009-09-29 18:34:32 -07008416 * <p>
8417 * This method should be invoked everytime a subclass directly updates the
8418 * scroll parameters.
8419 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008420 *
Mike Cleron290947b2009-09-29 18:34:32 -07008421 * @param startDelay the delay, in milliseconds, after which the animation
8422 * should start; when the delay is 0, the animation starts
8423 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008424 *
Mike Cleron290947b2009-09-29 18:34:32 -07008425 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008426 *
Mike Cleron290947b2009-09-29 18:34:32 -07008427 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008428 *
Mike Cleron290947b2009-09-29 18:34:32 -07008429 * @see #scrollBy(int, int)
8430 * @see #scrollTo(int, int)
8431 * @see #isHorizontalScrollBarEnabled()
8432 * @see #isVerticalScrollBarEnabled()
8433 * @see #setHorizontalScrollBarEnabled(boolean)
8434 * @see #setVerticalScrollBarEnabled(boolean)
8435 */
8436 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008437 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008438
Mike Cleronf116bf82009-09-27 19:14:12 -07008439 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8440 return false;
8441 }
8442
8443 if (scrollCache.scrollBar == null) {
8444 scrollCache.scrollBar = new ScrollBarDrawable();
8445 }
8446
8447 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8448
Mike Cleron290947b2009-09-29 18:34:32 -07008449 if (invalidate) {
8450 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008451 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008452 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008453
8454 if (scrollCache.state == ScrollabilityCache.OFF) {
8455 // FIXME: this is copied from WindowManagerService.
8456 // We should get this value from the system when it
8457 // is possible to do so.
8458 final int KEY_REPEAT_FIRST_DELAY = 750;
8459 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8460 }
8461
8462 // Tell mScrollCache when we should start fading. This may
8463 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008464 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008465 scrollCache.fadeStartTime = fadeStartTime;
8466 scrollCache.state = ScrollabilityCache.ON;
8467
8468 // Schedule our fader to run, unscheduling any old ones first
8469 if (mAttachInfo != null) {
8470 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8471 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8472 }
8473
8474 return true;
8475 }
8476
8477 return false;
8478 }
8479
8480 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008481 * Do not invalidate views which are not visible and which are not running an animation. They
8482 * will not get drawn and they should not set dirty flags as if they will be drawn
8483 */
8484 private boolean skipInvalidate() {
8485 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8486 (!(mParent instanceof ViewGroup) ||
8487 !((ViewGroup) mParent).isViewTransitioning(this));
8488 }
8489 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008490 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008491 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8492 * in the future. This must be called from a UI thread. To call from a non-UI
8493 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008494 *
8495 * WARNING: This method is destructive to dirty.
8496 * @param dirty the rectangle representing the bounds of the dirty region
8497 */
8498 public void invalidate(Rect dirty) {
8499 if (ViewDebug.TRACE_HIERARCHY) {
8500 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8501 }
8502
Chet Haaseaceafe62011-08-26 15:44:33 -07008503 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008504 return;
8505 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008506 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008507 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8508 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008509 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008510 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008511 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008512 final ViewParent p = mParent;
8513 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008514 //noinspection PointlessBooleanExpression,ConstantConditions
8515 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8516 if (p != null && ai != null && ai.mHardwareAccelerated) {
8517 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008518 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008519 p.invalidateChild(this, null);
8520 return;
8521 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008522 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008523 if (p != null && ai != null) {
8524 final int scrollX = mScrollX;
8525 final int scrollY = mScrollY;
8526 final Rect r = ai.mTmpInvalRect;
8527 r.set(dirty.left - scrollX, dirty.top - scrollY,
8528 dirty.right - scrollX, dirty.bottom - scrollY);
8529 mParent.invalidateChild(this, r);
8530 }
8531 }
8532 }
8533
8534 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008535 * 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 -08008536 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008537 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8538 * will be called at some point in the future. This must be called from
8539 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008540 * @param l the left position of the dirty region
8541 * @param t the top position of the dirty region
8542 * @param r the right position of the dirty region
8543 * @param b the bottom position of the dirty region
8544 */
8545 public void invalidate(int l, int t, int r, int b) {
8546 if (ViewDebug.TRACE_HIERARCHY) {
8547 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8548 }
8549
Chet Haaseaceafe62011-08-26 15:44:33 -07008550 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008551 return;
8552 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008553 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008554 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8555 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008556 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008557 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008558 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008559 final ViewParent p = mParent;
8560 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008561 //noinspection PointlessBooleanExpression,ConstantConditions
8562 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8563 if (p != null && ai != null && ai.mHardwareAccelerated) {
8564 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008565 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008566 p.invalidateChild(this, null);
8567 return;
8568 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008570 if (p != null && ai != null && l < r && t < b) {
8571 final int scrollX = mScrollX;
8572 final int scrollY = mScrollY;
8573 final Rect tmpr = ai.mTmpInvalRect;
8574 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8575 p.invalidateChild(this, tmpr);
8576 }
8577 }
8578 }
8579
8580 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008581 * Invalidate the whole view. If the view is visible,
8582 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8583 * the future. This must be called from a UI thread. To call from a non-UI thread,
8584 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008585 */
8586 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008587 invalidate(true);
8588 }
Joe Malin32736f02011-01-19 16:14:20 -08008589
Chet Haaseed032702010-10-01 14:05:54 -07008590 /**
8591 * This is where the invalidate() work actually happens. A full invalidate()
8592 * causes the drawing cache to be invalidated, but this function can be called with
8593 * invalidateCache set to false to skip that invalidation step for cases that do not
8594 * need it (for example, a component that remains at the same dimensions with the same
8595 * content).
8596 *
8597 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8598 * well. This is usually true for a full invalidate, but may be set to false if the
8599 * View's contents or dimensions have not changed.
8600 */
Romain Guy849d0a32011-02-01 17:20:48 -08008601 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008602 if (ViewDebug.TRACE_HIERARCHY) {
8603 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8604 }
8605
Chet Haaseaceafe62011-08-26 15:44:33 -07008606 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008607 return;
8608 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008609 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008610 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008611 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8612 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008613 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008614 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008615 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008616 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008617 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008619 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008620 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008621 //noinspection PointlessBooleanExpression,ConstantConditions
8622 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8623 if (p != null && ai != null && ai.mHardwareAccelerated) {
8624 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008625 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008626 p.invalidateChild(this, null);
8627 return;
8628 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008629 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008631 if (p != null && ai != null) {
8632 final Rect r = ai.mTmpInvalRect;
8633 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8634 // Don't call invalidate -- we don't want to internally scroll
8635 // our own bounds
8636 p.invalidateChild(this, r);
8637 }
8638 }
8639 }
8640
8641 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008642 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008643 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8644 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008645 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8646 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008647 *
8648 * @hide
8649 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008650 protected void invalidateParentCaches() {
8651 if (mParent instanceof View) {
8652 ((View) mParent).mPrivateFlags |= INVALIDATED;
8653 }
8654 }
Joe Malin32736f02011-01-19 16:14:20 -08008655
Romain Guy0fd89bf2011-01-26 15:41:30 -08008656 /**
8657 * Used to indicate that the parent of this view should be invalidated. This functionality
8658 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8659 * which is necessary when various parent-managed properties of the view change, such as
8660 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8661 * an invalidation event to the parent.
8662 *
8663 * @hide
8664 */
8665 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008666 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008667 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008668 }
8669 }
8670
8671 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008672 * Indicates whether this View is opaque. An opaque View guarantees that it will
8673 * draw all the pixels overlapping its bounds using a fully opaque color.
8674 *
8675 * Subclasses of View should override this method whenever possible to indicate
8676 * whether an instance is opaque. Opaque Views are treated in a special way by
8677 * the View hierarchy, possibly allowing it to perform optimizations during
8678 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008679 *
Romain Guy24443ea2009-05-11 11:56:30 -07008680 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008681 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008682 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008683 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008684 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008685 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8686 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008687 }
8688
Adam Powell20232d02010-12-08 21:08:53 -08008689 /**
8690 * @hide
8691 */
8692 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008693 // Opaque if:
8694 // - Has a background
8695 // - Background is opaque
8696 // - Doesn't have scrollbars or scrollbars are inside overlay
8697
8698 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8699 mPrivateFlags |= OPAQUE_BACKGROUND;
8700 } else {
8701 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8702 }
8703
8704 final int flags = mViewFlags;
8705 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8706 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8707 mPrivateFlags |= OPAQUE_SCROLLBARS;
8708 } else {
8709 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8710 }
8711 }
8712
8713 /**
8714 * @hide
8715 */
8716 protected boolean hasOpaqueScrollbars() {
8717 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008718 }
8719
8720 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008721 * @return A handler associated with the thread running the View. This
8722 * handler can be used to pump events in the UI events queue.
8723 */
8724 public Handler getHandler() {
8725 if (mAttachInfo != null) {
8726 return mAttachInfo.mHandler;
8727 }
8728 return null;
8729 }
8730
8731 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08008732 * Gets the view root associated with the View.
8733 * @return The view root, or null if none.
8734 * @hide
8735 */
8736 public ViewRootImpl getViewRootImpl() {
8737 if (mAttachInfo != null) {
8738 return mAttachInfo.mViewRootImpl;
8739 }
8740 return null;
8741 }
8742
8743 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008744 * <p>Causes the Runnable to be added to the message queue.
8745 * The runnable will be run on the user interface thread.</p>
8746 *
8747 * <p>This method can be invoked from outside of the UI thread
8748 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008749 *
8750 * @param action The Runnable that will be executed.
8751 *
8752 * @return Returns true if the Runnable was successfully placed in to the
8753 * message queue. Returns false on failure, usually because the
8754 * looper processing the message queue is exiting.
8755 */
8756 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008757 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008758 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008759 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008760 }
Jeff Browna175a5b2012-02-15 19:18:31 -08008761 // Assume that post will succeed later
8762 ViewRootImpl.getRunQueue().post(action);
8763 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008764 }
8765
8766 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008767 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008768 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008769 * The runnable will be run on the user interface thread.</p>
8770 *
8771 * <p>This method can be invoked from outside of the UI thread
8772 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008773 *
8774 * @param action The Runnable that will be executed.
8775 * @param delayMillis The delay (in milliseconds) until the Runnable
8776 * will be executed.
8777 *
8778 * @return true if the Runnable was successfully placed in to the
8779 * message queue. Returns false on failure, usually because the
8780 * looper processing the message queue is exiting. Note that a
8781 * result of true does not mean the Runnable will be processed --
8782 * if the looper is quit before the delivery time of the message
8783 * occurs then the message will be dropped.
8784 */
8785 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008786 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008787 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008788 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008789 }
Jeff Browna175a5b2012-02-15 19:18:31 -08008790 // Assume that post will succeed later
8791 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
8792 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008793 }
8794
8795 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08008796 * <p>Causes the Runnable to execute on the next animation time step.
8797 * The runnable will be run on the user interface thread.</p>
8798 *
8799 * <p>This method can be invoked from outside of the UI thread
8800 * only when this View is attached to a window.</p>
8801 *
8802 * @param action The Runnable that will be executed.
8803 *
8804 * @hide
8805 */
8806 public void postOnAnimation(Runnable action) {
8807 final AttachInfo attachInfo = mAttachInfo;
8808 if (attachInfo != null) {
8809 attachInfo.mViewRootImpl.mChoreographer.postAnimationCallback(action, null);
8810 } else {
8811 // Assume that post will succeed later
8812 ViewRootImpl.getRunQueue().post(action);
8813 }
8814 }
8815
8816 /**
8817 * <p>Causes the Runnable to execute on the next animation time step,
8818 * after the specified amount of time elapses.
8819 * The runnable will be run on the user interface thread.</p>
8820 *
8821 * <p>This method can be invoked from outside of the UI thread
8822 * only when this View is attached to a window.</p>
8823 *
8824 * @param action The Runnable that will be executed.
8825 * @param delayMillis The delay (in milliseconds) until the Runnable
8826 * will be executed.
8827 *
8828 * @hide
8829 */
8830 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
8831 final AttachInfo attachInfo = mAttachInfo;
8832 if (attachInfo != null) {
8833 attachInfo.mViewRootImpl.mChoreographer.postAnimationCallbackDelayed(
8834 action, null, delayMillis);
8835 } else {
8836 // Assume that post will succeed later
8837 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
8838 }
8839 }
8840
8841 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008842 * <p>Removes the specified Runnable from the message queue.</p>
8843 *
8844 * <p>This method can be invoked from outside of the UI thread
8845 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008846 *
8847 * @param action The Runnable to remove from the message handling queue
8848 *
8849 * @return true if this view could ask the Handler to remove the Runnable,
8850 * false otherwise. When the returned value is true, the Runnable
8851 * may or may not have been actually removed from the message queue
8852 * (for instance, if the Runnable was not in the queue already.)
8853 */
8854 public boolean removeCallbacks(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008855 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008856 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008857 attachInfo.mHandler.removeCallbacks(action);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08008858 attachInfo.mViewRootImpl.mChoreographer.removeAnimationCallbacks(action, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008859 } else {
8860 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008861 ViewRootImpl.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008862 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008863 return true;
8864 }
8865
8866 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008867 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
8868 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008869 *
Romain Guye63a4f32011-08-11 11:33:31 -07008870 * <p>This method can be invoked from outside of the UI thread
8871 * only when this View is attached to a window.</p>
8872 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008873 * @see #invalidate()
8874 */
8875 public void postInvalidate() {
8876 postInvalidateDelayed(0);
8877 }
8878
8879 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008880 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8881 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
8882 *
8883 * <p>This method can be invoked from outside of the UI thread
8884 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008885 *
8886 * @param left The left coordinate of the rectangle to invalidate.
8887 * @param top The top coordinate of the rectangle to invalidate.
8888 * @param right The right coordinate of the rectangle to invalidate.
8889 * @param bottom The bottom coordinate of the rectangle to invalidate.
8890 *
8891 * @see #invalidate(int, int, int, int)
8892 * @see #invalidate(Rect)
8893 */
8894 public void postInvalidate(int left, int top, int right, int bottom) {
8895 postInvalidateDelayed(0, left, top, right, bottom);
8896 }
8897
8898 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008899 * <p>Cause an invalidate to happen on a subsequent cycle through the event
8900 * loop. Waits for the specified amount of time.</p>
8901 *
8902 * <p>This method can be invoked from outside of the UI thread
8903 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008904 *
8905 * @param delayMilliseconds the duration in milliseconds to delay the
8906 * invalidation by
8907 */
8908 public void postInvalidateDelayed(long delayMilliseconds) {
8909 // We try only with the AttachInfo because there's no point in invalidating
8910 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08008911 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008912 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008913 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008914 }
8915 }
8916
8917 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008918 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8919 * through the event loop. Waits for the specified amount of time.</p>
8920 *
8921 * <p>This method can be invoked from outside of the UI thread
8922 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008923 *
8924 * @param delayMilliseconds the duration in milliseconds to delay the
8925 * invalidation by
8926 * @param left The left coordinate of the rectangle to invalidate.
8927 * @param top The top coordinate of the rectangle to invalidate.
8928 * @param right The right coordinate of the rectangle to invalidate.
8929 * @param bottom The bottom coordinate of the rectangle to invalidate.
8930 */
8931 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8932 int right, int bottom) {
8933
8934 // We try only with the AttachInfo because there's no point in invalidating
8935 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08008936 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008937 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008938 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8939 info.target = this;
8940 info.left = left;
8941 info.top = top;
8942 info.right = right;
8943 info.bottom = bottom;
8944
Jeff Browna175a5b2012-02-15 19:18:31 -08008945 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008946 }
8947 }
8948
8949 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -08008950 * <p>Cause an invalidate to happen on the next animation time step, typically the
8951 * next display frame.</p>
8952 *
8953 * <p>This method can be invoked from outside of the UI thread
8954 * only when this View is attached to a window.</p>
8955 *
8956 * @hide
8957 */
8958 public void postInvalidateOnAnimation() {
8959 // We try only with the AttachInfo because there's no point in invalidating
8960 // if we are not attached to our window
8961 final AttachInfo attachInfo = mAttachInfo;
8962 if (attachInfo != null) {
8963 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
8964 }
8965 }
8966
8967 /**
8968 * <p>Cause an invalidate of the specified area to happen on the next animation
8969 * time step, typically the next display frame.</p>
8970 *
8971 * <p>This method can be invoked from outside of the UI thread
8972 * only when this View is attached to a window.</p>
8973 *
8974 * @param left The left coordinate of the rectangle to invalidate.
8975 * @param top The top coordinate of the rectangle to invalidate.
8976 * @param right The right coordinate of the rectangle to invalidate.
8977 * @param bottom The bottom coordinate of the rectangle to invalidate.
8978 *
8979 * @hide
8980 */
8981 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
8982 // We try only with the AttachInfo because there's no point in invalidating
8983 // if we are not attached to our window
8984 final AttachInfo attachInfo = mAttachInfo;
8985 if (attachInfo != null) {
8986 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8987 info.target = this;
8988 info.left = left;
8989 info.top = top;
8990 info.right = right;
8991 info.bottom = bottom;
8992
8993 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
8994 }
8995 }
8996
8997 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008998 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8999 * This event is sent at most once every
9000 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
9001 */
9002 private void postSendViewScrolledAccessibilityEventCallback() {
9003 if (mSendViewScrolledAccessibilityEvent == null) {
9004 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
9005 }
9006 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
9007 mSendViewScrolledAccessibilityEvent.mIsPending = true;
9008 postDelayed(mSendViewScrolledAccessibilityEvent,
9009 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
9010 }
9011 }
9012
9013 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009014 * Called by a parent to request that a child update its values for mScrollX
9015 * and mScrollY if necessary. This will typically be done if the child is
9016 * animating a scroll using a {@link android.widget.Scroller Scroller}
9017 * object.
9018 */
9019 public void computeScroll() {
9020 }
9021
9022 /**
9023 * <p>Indicate whether the horizontal edges are faded when the view is
9024 * scrolled horizontally.</p>
9025 *
9026 * @return true if the horizontal edges should are faded on scroll, false
9027 * otherwise
9028 *
9029 * @see #setHorizontalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009030 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009031 */
9032 public boolean isHorizontalFadingEdgeEnabled() {
9033 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
9034 }
9035
9036 /**
9037 * <p>Define whether the horizontal edges should be faded when this view
9038 * is scrolled horizontally.</p>
9039 *
9040 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
9041 * be faded when the view is scrolled
9042 * horizontally
9043 *
9044 * @see #isHorizontalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009045 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009046 */
9047 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
9048 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
9049 if (horizontalFadingEdgeEnabled) {
9050 initScrollCache();
9051 }
9052
9053 mViewFlags ^= FADING_EDGE_HORIZONTAL;
9054 }
9055 }
9056
9057 /**
9058 * <p>Indicate whether the vertical edges are faded when the view is
9059 * scrolled horizontally.</p>
9060 *
9061 * @return true if the vertical edges should are faded on scroll, false
9062 * otherwise
9063 *
9064 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009065 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009066 */
9067 public boolean isVerticalFadingEdgeEnabled() {
9068 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
9069 }
9070
9071 /**
9072 * <p>Define whether the vertical edges should be faded when this view
9073 * is scrolled vertically.</p>
9074 *
9075 * @param verticalFadingEdgeEnabled true if the vertical edges should
9076 * be faded when the view is scrolled
9077 * vertically
9078 *
9079 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009080 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009081 */
9082 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
9083 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
9084 if (verticalFadingEdgeEnabled) {
9085 initScrollCache();
9086 }
9087
9088 mViewFlags ^= FADING_EDGE_VERTICAL;
9089 }
9090 }
9091
9092 /**
9093 * Returns the strength, or intensity, of the top faded edge. The strength is
9094 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9095 * returns 0.0 or 1.0 but no value in between.
9096 *
9097 * Subclasses should override this method to provide a smoother fade transition
9098 * when scrolling occurs.
9099 *
9100 * @return the intensity of the top fade as a float between 0.0f and 1.0f
9101 */
9102 protected float getTopFadingEdgeStrength() {
9103 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
9104 }
9105
9106 /**
9107 * Returns the strength, or intensity, of the bottom faded edge. The strength is
9108 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9109 * returns 0.0 or 1.0 but no value in between.
9110 *
9111 * Subclasses should override this method to provide a smoother fade transition
9112 * when scrolling occurs.
9113 *
9114 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
9115 */
9116 protected float getBottomFadingEdgeStrength() {
9117 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
9118 computeVerticalScrollRange() ? 1.0f : 0.0f;
9119 }
9120
9121 /**
9122 * Returns the strength, or intensity, of the left faded edge. The strength is
9123 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9124 * returns 0.0 or 1.0 but no value in between.
9125 *
9126 * Subclasses should override this method to provide a smoother fade transition
9127 * when scrolling occurs.
9128 *
9129 * @return the intensity of the left fade as a float between 0.0f and 1.0f
9130 */
9131 protected float getLeftFadingEdgeStrength() {
9132 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
9133 }
9134
9135 /**
9136 * Returns the strength, or intensity, of the right faded edge. The strength is
9137 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9138 * returns 0.0 or 1.0 but no value in between.
9139 *
9140 * Subclasses should override this method to provide a smoother fade transition
9141 * when scrolling occurs.
9142 *
9143 * @return the intensity of the right fade as a float between 0.0f and 1.0f
9144 */
9145 protected float getRightFadingEdgeStrength() {
9146 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
9147 computeHorizontalScrollRange() ? 1.0f : 0.0f;
9148 }
9149
9150 /**
9151 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
9152 * scrollbar is not drawn by default.</p>
9153 *
9154 * @return true if the horizontal scrollbar should be painted, false
9155 * otherwise
9156 *
9157 * @see #setHorizontalScrollBarEnabled(boolean)
9158 */
9159 public boolean isHorizontalScrollBarEnabled() {
9160 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9161 }
9162
9163 /**
9164 * <p>Define whether the horizontal scrollbar should be drawn or not. The
9165 * scrollbar is not drawn by default.</p>
9166 *
9167 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
9168 * be painted
9169 *
9170 * @see #isHorizontalScrollBarEnabled()
9171 */
9172 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
9173 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
9174 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009175 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009176 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009177 }
9178 }
9179
9180 /**
9181 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
9182 * scrollbar is not drawn by default.</p>
9183 *
9184 * @return true if the vertical scrollbar should be painted, false
9185 * otherwise
9186 *
9187 * @see #setVerticalScrollBarEnabled(boolean)
9188 */
9189 public boolean isVerticalScrollBarEnabled() {
9190 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
9191 }
9192
9193 /**
9194 * <p>Define whether the vertical scrollbar should be drawn or not. The
9195 * scrollbar is not drawn by default.</p>
9196 *
9197 * @param verticalScrollBarEnabled true if the vertical scrollbar should
9198 * be painted
9199 *
9200 * @see #isVerticalScrollBarEnabled()
9201 */
9202 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9203 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9204 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009205 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009206 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009207 }
9208 }
9209
Adam Powell20232d02010-12-08 21:08:53 -08009210 /**
9211 * @hide
9212 */
9213 protected void recomputePadding() {
9214 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009215 }
Joe Malin32736f02011-01-19 16:14:20 -08009216
Mike Cleronfe81d382009-09-28 14:22:16 -07009217 /**
9218 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009219 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009220 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009221 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009222 */
9223 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9224 initScrollCache();
9225 final ScrollabilityCache scrollabilityCache = mScrollCache;
9226 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009227 if (fadeScrollbars) {
9228 scrollabilityCache.state = ScrollabilityCache.OFF;
9229 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009230 scrollabilityCache.state = ScrollabilityCache.ON;
9231 }
9232 }
Joe Malin32736f02011-01-19 16:14:20 -08009233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009234 /**
Joe Malin32736f02011-01-19 16:14:20 -08009235 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009236 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009237 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009238 * @return true if scrollbar fading is enabled
9239 */
9240 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009241 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009242 }
Joe Malin32736f02011-01-19 16:14:20 -08009243
Mike Cleron52f0a642009-09-28 18:21:37 -07009244 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009245 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9246 * inset. When inset, they add to the padding of the view. And the scrollbars
9247 * can be drawn inside the padding area or on the edge of the view. For example,
9248 * if a view has a background drawable and you want to draw the scrollbars
9249 * inside the padding specified by the drawable, you can use
9250 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9251 * appear at the edge of the view, ignoring the padding, then you can use
9252 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9253 * @param style the style of the scrollbars. Should be one of
9254 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9255 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9256 * @see #SCROLLBARS_INSIDE_OVERLAY
9257 * @see #SCROLLBARS_INSIDE_INSET
9258 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9259 * @see #SCROLLBARS_OUTSIDE_INSET
9260 */
9261 public void setScrollBarStyle(int style) {
9262 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9263 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009264 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009265 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009266 }
9267 }
9268
9269 /**
9270 * <p>Returns the current scrollbar style.</p>
9271 * @return the current scrollbar style
9272 * @see #SCROLLBARS_INSIDE_OVERLAY
9273 * @see #SCROLLBARS_INSIDE_INSET
9274 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9275 * @see #SCROLLBARS_OUTSIDE_INSET
9276 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009277 @ViewDebug.ExportedProperty(mapping = {
9278 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9279 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9280 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9281 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9282 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009283 public int getScrollBarStyle() {
9284 return mViewFlags & SCROLLBARS_STYLE_MASK;
9285 }
9286
9287 /**
9288 * <p>Compute the horizontal range that the horizontal scrollbar
9289 * represents.</p>
9290 *
9291 * <p>The range is expressed in arbitrary units that must be the same as the
9292 * units used by {@link #computeHorizontalScrollExtent()} and
9293 * {@link #computeHorizontalScrollOffset()}.</p>
9294 *
9295 * <p>The default range is the drawing width of this view.</p>
9296 *
9297 * @return the total horizontal range represented by the horizontal
9298 * scrollbar
9299 *
9300 * @see #computeHorizontalScrollExtent()
9301 * @see #computeHorizontalScrollOffset()
9302 * @see android.widget.ScrollBarDrawable
9303 */
9304 protected int computeHorizontalScrollRange() {
9305 return getWidth();
9306 }
9307
9308 /**
9309 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9310 * within the horizontal range. This value is used to compute the position
9311 * of the thumb within the scrollbar's track.</p>
9312 *
9313 * <p>The range is expressed in arbitrary units that must be the same as the
9314 * units used by {@link #computeHorizontalScrollRange()} and
9315 * {@link #computeHorizontalScrollExtent()}.</p>
9316 *
9317 * <p>The default offset is the scroll offset of this view.</p>
9318 *
9319 * @return the horizontal offset of the scrollbar's thumb
9320 *
9321 * @see #computeHorizontalScrollRange()
9322 * @see #computeHorizontalScrollExtent()
9323 * @see android.widget.ScrollBarDrawable
9324 */
9325 protected int computeHorizontalScrollOffset() {
9326 return mScrollX;
9327 }
9328
9329 /**
9330 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9331 * within the horizontal range. This value is used to compute the length
9332 * of the thumb within the scrollbar's track.</p>
9333 *
9334 * <p>The range is expressed in arbitrary units that must be the same as the
9335 * units used by {@link #computeHorizontalScrollRange()} and
9336 * {@link #computeHorizontalScrollOffset()}.</p>
9337 *
9338 * <p>The default extent is the drawing width of this view.</p>
9339 *
9340 * @return the horizontal extent of the scrollbar's thumb
9341 *
9342 * @see #computeHorizontalScrollRange()
9343 * @see #computeHorizontalScrollOffset()
9344 * @see android.widget.ScrollBarDrawable
9345 */
9346 protected int computeHorizontalScrollExtent() {
9347 return getWidth();
9348 }
9349
9350 /**
9351 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9352 *
9353 * <p>The range is expressed in arbitrary units that must be the same as the
9354 * units used by {@link #computeVerticalScrollExtent()} and
9355 * {@link #computeVerticalScrollOffset()}.</p>
9356 *
9357 * @return the total vertical range represented by the vertical scrollbar
9358 *
9359 * <p>The default range is the drawing height of this view.</p>
9360 *
9361 * @see #computeVerticalScrollExtent()
9362 * @see #computeVerticalScrollOffset()
9363 * @see android.widget.ScrollBarDrawable
9364 */
9365 protected int computeVerticalScrollRange() {
9366 return getHeight();
9367 }
9368
9369 /**
9370 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9371 * within the horizontal range. This value is used to compute the position
9372 * of the thumb within the scrollbar's track.</p>
9373 *
9374 * <p>The range is expressed in arbitrary units that must be the same as the
9375 * units used by {@link #computeVerticalScrollRange()} and
9376 * {@link #computeVerticalScrollExtent()}.</p>
9377 *
9378 * <p>The default offset is the scroll offset of this view.</p>
9379 *
9380 * @return the vertical offset of the scrollbar's thumb
9381 *
9382 * @see #computeVerticalScrollRange()
9383 * @see #computeVerticalScrollExtent()
9384 * @see android.widget.ScrollBarDrawable
9385 */
9386 protected int computeVerticalScrollOffset() {
9387 return mScrollY;
9388 }
9389
9390 /**
9391 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9392 * within the vertical range. This value is used to compute the length
9393 * of the thumb within the scrollbar's track.</p>
9394 *
9395 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009396 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009397 * {@link #computeVerticalScrollOffset()}.</p>
9398 *
9399 * <p>The default extent is the drawing height of this view.</p>
9400 *
9401 * @return the vertical extent of the scrollbar's thumb
9402 *
9403 * @see #computeVerticalScrollRange()
9404 * @see #computeVerticalScrollOffset()
9405 * @see android.widget.ScrollBarDrawable
9406 */
9407 protected int computeVerticalScrollExtent() {
9408 return getHeight();
9409 }
9410
9411 /**
Adam Powell69159442011-06-13 17:53:06 -07009412 * Check if this view can be scrolled horizontally in a certain direction.
9413 *
9414 * @param direction Negative to check scrolling left, positive to check scrolling right.
9415 * @return true if this view can be scrolled in the specified direction, false otherwise.
9416 */
9417 public boolean canScrollHorizontally(int direction) {
9418 final int offset = computeHorizontalScrollOffset();
9419 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9420 if (range == 0) return false;
9421 if (direction < 0) {
9422 return offset > 0;
9423 } else {
9424 return offset < range - 1;
9425 }
9426 }
9427
9428 /**
9429 * Check if this view can be scrolled vertically in a certain direction.
9430 *
9431 * @param direction Negative to check scrolling up, positive to check scrolling down.
9432 * @return true if this view can be scrolled in the specified direction, false otherwise.
9433 */
9434 public boolean canScrollVertically(int direction) {
9435 final int offset = computeVerticalScrollOffset();
9436 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9437 if (range == 0) return false;
9438 if (direction < 0) {
9439 return offset > 0;
9440 } else {
9441 return offset < range - 1;
9442 }
9443 }
9444
9445 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009446 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9447 * scrollbars are painted only if they have been awakened first.</p>
9448 *
9449 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009450 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009451 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009452 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009453 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009454 // scrollbars are drawn only when the animation is running
9455 final ScrollabilityCache cache = mScrollCache;
9456 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009457
Mike Cleronf116bf82009-09-27 19:14:12 -07009458 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009459
Mike Cleronf116bf82009-09-27 19:14:12 -07009460 if (state == ScrollabilityCache.OFF) {
9461 return;
9462 }
Joe Malin32736f02011-01-19 16:14:20 -08009463
Mike Cleronf116bf82009-09-27 19:14:12 -07009464 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009465
Mike Cleronf116bf82009-09-27 19:14:12 -07009466 if (state == ScrollabilityCache.FADING) {
9467 // We're fading -- get our fade interpolation
9468 if (cache.interpolatorValues == null) {
9469 cache.interpolatorValues = new float[1];
9470 }
Joe Malin32736f02011-01-19 16:14:20 -08009471
Mike Cleronf116bf82009-09-27 19:14:12 -07009472 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009473
Mike Cleronf116bf82009-09-27 19:14:12 -07009474 // Stops the animation if we're done
9475 if (cache.scrollBarInterpolator.timeToValues(values) ==
9476 Interpolator.Result.FREEZE_END) {
9477 cache.state = ScrollabilityCache.OFF;
9478 } else {
9479 cache.scrollBar.setAlpha(Math.round(values[0]));
9480 }
Joe Malin32736f02011-01-19 16:14:20 -08009481
9482 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009483 // drawing. We only want this when we're fading so that
9484 // we prevent excessive redraws
9485 invalidate = true;
9486 } else {
9487 // We're just on -- but we may have been fading before so
9488 // reset alpha
9489 cache.scrollBar.setAlpha(255);
9490 }
9491
Joe Malin32736f02011-01-19 16:14:20 -08009492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009493 final int viewFlags = mViewFlags;
9494
9495 final boolean drawHorizontalScrollBar =
9496 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9497 final boolean drawVerticalScrollBar =
9498 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9499 && !isVerticalScrollBarHidden();
9500
9501 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9502 final int width = mRight - mLeft;
9503 final int height = mBottom - mTop;
9504
9505 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009506
Mike Reede8853fc2009-09-04 14:01:48 -04009507 final int scrollX = mScrollX;
9508 final int scrollY = mScrollY;
9509 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9510
Mike Cleronf116bf82009-09-27 19:14:12 -07009511 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009513 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009514 int size = scrollBar.getSize(false);
9515 if (size <= 0) {
9516 size = cache.scrollBarSize;
9517 }
9518
Mike Cleronf116bf82009-09-27 19:14:12 -07009519 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009520 computeHorizontalScrollOffset(),
9521 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009522 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009523 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009524 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009525 left = scrollX + (mPaddingLeft & inside);
9526 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9527 bottom = top + size;
9528 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9529 if (invalidate) {
9530 invalidate(left, top, right, bottom);
9531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009532 }
9533
9534 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009535 int size = scrollBar.getSize(true);
9536 if (size <= 0) {
9537 size = cache.scrollBarSize;
9538 }
9539
Mike Reede8853fc2009-09-04 14:01:48 -04009540 scrollBar.setParameters(computeVerticalScrollRange(),
9541 computeVerticalScrollOffset(),
9542 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009543 switch (mVerticalScrollbarPosition) {
9544 default:
9545 case SCROLLBAR_POSITION_DEFAULT:
9546 case SCROLLBAR_POSITION_RIGHT:
9547 left = scrollX + width - size - (mUserPaddingRight & inside);
9548 break;
9549 case SCROLLBAR_POSITION_LEFT:
9550 left = scrollX + (mUserPaddingLeft & inside);
9551 break;
9552 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009553 top = scrollY + (mPaddingTop & inside);
9554 right = left + size;
9555 bottom = scrollY + height - (mUserPaddingBottom & inside);
9556 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9557 if (invalidate) {
9558 invalidate(left, top, right, bottom);
9559 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009560 }
9561 }
9562 }
9563 }
Romain Guy8506ab42009-06-11 17:35:47 -07009564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009565 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009566 * 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 -08009567 * FastScroller is visible.
9568 * @return whether to temporarily hide the vertical scrollbar
9569 * @hide
9570 */
9571 protected boolean isVerticalScrollBarHidden() {
9572 return false;
9573 }
9574
9575 /**
9576 * <p>Draw the horizontal scrollbar if
9577 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9578 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009579 * @param canvas the canvas on which to draw the scrollbar
9580 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009581 *
9582 * @see #isHorizontalScrollBarEnabled()
9583 * @see #computeHorizontalScrollRange()
9584 * @see #computeHorizontalScrollExtent()
9585 * @see #computeHorizontalScrollOffset()
9586 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009587 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009588 */
Romain Guy8fb95422010-08-17 18:38:51 -07009589 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9590 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009591 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009592 scrollBar.draw(canvas);
9593 }
Mike Reede8853fc2009-09-04 14:01:48 -04009594
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009595 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009596 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9597 * returns true.</p>
9598 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009599 * @param canvas the canvas on which to draw the scrollbar
9600 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009601 *
9602 * @see #isVerticalScrollBarEnabled()
9603 * @see #computeVerticalScrollRange()
9604 * @see #computeVerticalScrollExtent()
9605 * @see #computeVerticalScrollOffset()
9606 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009607 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009608 */
Romain Guy8fb95422010-08-17 18:38:51 -07009609 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9610 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009611 scrollBar.setBounds(l, t, r, b);
9612 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009613 }
9614
9615 /**
9616 * Implement this to do your drawing.
9617 *
9618 * @param canvas the canvas on which the background will be drawn
9619 */
9620 protected void onDraw(Canvas canvas) {
9621 }
9622
9623 /*
9624 * Caller is responsible for calling requestLayout if necessary.
9625 * (This allows addViewInLayout to not request a new layout.)
9626 */
9627 void assignParent(ViewParent parent) {
9628 if (mParent == null) {
9629 mParent = parent;
9630 } else if (parent == null) {
9631 mParent = null;
9632 } else {
9633 throw new RuntimeException("view " + this + " being added, but"
9634 + " it already has a parent");
9635 }
9636 }
9637
9638 /**
9639 * This is called when the view is attached to a window. At this point it
9640 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009641 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9642 * however it may be called any time before the first onDraw -- including
9643 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009644 *
9645 * @see #onDetachedFromWindow()
9646 */
9647 protected void onAttachedToWindow() {
9648 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9649 mParent.requestTransparentRegion(this);
9650 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009651 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9652 initialAwakenScrollBars();
9653 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9654 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009655 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009656 // Order is important here: LayoutDirection MUST be resolved before Padding
9657 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009658 resolveLayoutDirectionIfNeeded();
9659 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009660 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009661 if (isFocused()) {
9662 InputMethodManager imm = InputMethodManager.peekInstance();
9663 imm.focusIn(this);
9664 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009665 }
Cibu Johny86666632010-02-22 13:01:02 -08009666
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009667 /**
Romain Guybb9908b2012-03-08 11:14:07 -08009668 * @see #onScreenStateChanged(int)
9669 */
9670 void dispatchScreenStateChanged(int screenState) {
9671 onScreenStateChanged(screenState);
9672 }
9673
9674 /**
9675 * This method is called whenever the state of the screen this view is
9676 * attached to changes. A state change will usually occurs when the screen
9677 * turns on or off (whether it happens automatically or the user does it
9678 * manually.)
9679 *
9680 * @param screenState The new state of the screen. Can be either
9681 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
9682 */
9683 public void onScreenStateChanged(int screenState) {
9684 }
9685
9686 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009687 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9688 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009689 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009690 private void resolveLayoutDirectionIfNeeded() {
9691 // Do not resolve if it is not needed
9692 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9693
9694 // Clear any previous layout direction resolution
9695 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9696
9697 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009698 switch (getLayoutDirection()) {
9699 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009700 // We cannot do the resolution if there is no parent
9701 if (mParent == null) return;
9702
Cibu Johny86666632010-02-22 13:01:02 -08009703 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009704 if (mParent instanceof ViewGroup) {
9705 ViewGroup viewGroup = ((ViewGroup) mParent);
9706
9707 // Check if the parent view group can resolve
9708 if (! viewGroup.canResolveLayoutDirection()) {
9709 return;
9710 }
9711 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9712 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9713 }
Cibu Johny86666632010-02-22 13:01:02 -08009714 }
9715 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009716 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009717 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009718 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009719 case LAYOUT_DIRECTION_LOCALE:
9720 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009721 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009722 }
9723 break;
9724 default:
9725 // Nothing to do, LTR by default
9726 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009727
9728 // Set to resolved
9729 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009730 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08009731 // Resolve padding
9732 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009733 }
9734
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009735 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009736 * Called when layout direction has been resolved.
9737 *
9738 * The default implementation does nothing.
9739 */
9740 public void onResolvedLayoutDirectionChanged() {
9741 }
9742
9743 /**
9744 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009745 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009746 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009747 // If the user specified the absolute padding (either with android:padding or
9748 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9749 // use the default padding or the padding from the background drawable
9750 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009751 int resolvedLayoutDirection = getResolvedLayoutDirection();
9752 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009753 case LAYOUT_DIRECTION_RTL:
9754 // Start user padding override Right user padding. Otherwise, if Right user
9755 // padding is not defined, use the default Right padding. If Right user padding
9756 // is defined, just use it.
9757 if (mUserPaddingStart >= 0) {
9758 mUserPaddingRight = mUserPaddingStart;
9759 } else if (mUserPaddingRight < 0) {
9760 mUserPaddingRight = mPaddingRight;
9761 }
9762 if (mUserPaddingEnd >= 0) {
9763 mUserPaddingLeft = mUserPaddingEnd;
9764 } else if (mUserPaddingLeft < 0) {
9765 mUserPaddingLeft = mPaddingLeft;
9766 }
9767 break;
9768 case LAYOUT_DIRECTION_LTR:
9769 default:
9770 // Start user padding override Left user padding. Otherwise, if Left user
9771 // padding is not defined, use the default left padding. If Left user padding
9772 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009773 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009774 mUserPaddingLeft = mUserPaddingStart;
9775 } else if (mUserPaddingLeft < 0) {
9776 mUserPaddingLeft = mPaddingLeft;
9777 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009778 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009779 mUserPaddingRight = mUserPaddingEnd;
9780 } else if (mUserPaddingRight < 0) {
9781 mUserPaddingRight = mPaddingRight;
9782 }
9783 }
9784
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009785 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9786
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08009787 if(isPaddingRelative()) {
9788 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
9789 } else {
9790 recomputePadding();
9791 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009792 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009793 }
9794
9795 /**
9796 * Resolve padding depending on the layout direction. Subclasses that care about
9797 * padding resolution should override this method. The default implementation does
9798 * nothing.
9799 *
9800 * @param layoutDirection the direction of the layout
9801 *
Fabrice Di Meglio085585f2012-03-05 13:33:57 -08009802 * {@link #LAYOUT_DIRECTION_LTR}
9803 * {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009804 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009805 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009806 }
9807
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009808 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009809 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009810 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009811 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009812 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009813 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009814 switch (getLayoutDirection()) {
9815 case LAYOUT_DIRECTION_INHERIT:
9816 return (mParent != null);
9817 default:
9818 return true;
9819 }
9820 }
9821
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009822 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009823 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
9824 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009825 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009826 public void resetResolvedLayoutDirection() {
9827 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009828 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009829 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -08009830 // Reset also the text direction
9831 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009832 }
9833
9834 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009835 * Called during reset of resolved layout direction.
9836 *
9837 * Subclasses need to override this method to clear cached information that depends on the
9838 * resolved layout direction, or to inform child views that inherit their layout direction.
9839 *
9840 * The default implementation does nothing.
9841 */
9842 public void onResolvedLayoutDirectionReset() {
9843 }
9844
9845 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009846 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009847 *
9848 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009849 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009850 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009851 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -08009852 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009853 }
9854
9855 /**
9856 * This is called when the view is detached from a window. At this point it
9857 * no longer has a surface for drawing.
9858 *
9859 * @see #onAttachedToWindow()
9860 */
9861 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009862 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009863
Romain Guya440b002010-02-24 15:57:54 -08009864 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009865 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009866 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009867 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009869 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009870
Romain Guy6d7475d2011-07-27 16:28:21 -07009871 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009872
Chet Haasedaf98e92011-01-10 14:10:36 -08009873 if (mDisplayList != null) {
9874 mDisplayList.invalidate();
9875 }
9876
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009877 if (mAttachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009878 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009879 }
9880
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009881 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009882
9883 resetResolvedLayoutDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009884 }
9885
9886 /**
9887 * @return The number of times this view has been attached to a window
9888 */
9889 protected int getWindowAttachCount() {
9890 return mWindowAttachCount;
9891 }
9892
9893 /**
9894 * Retrieve a unique token identifying the window this view is attached to.
9895 * @return Return the window's token for use in
9896 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9897 */
9898 public IBinder getWindowToken() {
9899 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9900 }
9901
9902 /**
9903 * Retrieve a unique token identifying the top-level "real" window of
9904 * the window that this view is attached to. That is, this is like
9905 * {@link #getWindowToken}, except if the window this view in is a panel
9906 * window (attached to another containing window), then the token of
9907 * the containing window is returned instead.
9908 *
9909 * @return Returns the associated window token, either
9910 * {@link #getWindowToken()} or the containing window's token.
9911 */
9912 public IBinder getApplicationWindowToken() {
9913 AttachInfo ai = mAttachInfo;
9914 if (ai != null) {
9915 IBinder appWindowToken = ai.mPanelParentWindowToken;
9916 if (appWindowToken == null) {
9917 appWindowToken = ai.mWindowToken;
9918 }
9919 return appWindowToken;
9920 }
9921 return null;
9922 }
9923
9924 /**
9925 * Retrieve private session object this view hierarchy is using to
9926 * communicate with the window manager.
9927 * @return the session object to communicate with the window manager
9928 */
9929 /*package*/ IWindowSession getWindowSession() {
9930 return mAttachInfo != null ? mAttachInfo.mSession : null;
9931 }
9932
9933 /**
9934 * @param info the {@link android.view.View.AttachInfo} to associated with
9935 * this view
9936 */
9937 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9938 //System.out.println("Attached! " + this);
9939 mAttachInfo = info;
9940 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009941 // We will need to evaluate the drawable state at least once.
9942 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009943 if (mFloatingTreeObserver != null) {
9944 info.mTreeObserver.merge(mFloatingTreeObserver);
9945 mFloatingTreeObserver = null;
9946 }
9947 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9948 mAttachInfo.mScrollContainers.add(this);
9949 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9950 }
9951 performCollectViewAttributes(visibility);
9952 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009953
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009954 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009955 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009956 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009957 if (listeners != null && listeners.size() > 0) {
9958 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9959 // perform the dispatching. The iterator is a safe guard against listeners that
9960 // could mutate the list by calling the various add/remove methods. This prevents
9961 // the array from being modified while we iterate it.
9962 for (OnAttachStateChangeListener listener : listeners) {
9963 listener.onViewAttachedToWindow(this);
9964 }
9965 }
9966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009967 int vis = info.mWindowVisibility;
9968 if (vis != GONE) {
9969 onWindowVisibilityChanged(vis);
9970 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009971 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9972 // If nobody has evaluated the drawable state yet, then do it now.
9973 refreshDrawableState();
9974 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009975 }
9976
9977 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009978 AttachInfo info = mAttachInfo;
9979 if (info != null) {
9980 int vis = info.mWindowVisibility;
9981 if (vis != GONE) {
9982 onWindowVisibilityChanged(GONE);
9983 }
9984 }
9985
9986 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009987
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009988 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009989 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009990 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009991 if (listeners != null && listeners.size() > 0) {
9992 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9993 // perform the dispatching. The iterator is a safe guard against listeners that
9994 // could mutate the list by calling the various add/remove methods. This prevents
9995 // the array from being modified while we iterate it.
9996 for (OnAttachStateChangeListener listener : listeners) {
9997 listener.onViewDetachedFromWindow(this);
9998 }
9999 }
10000
Romain Guy01d5edc2011-01-28 11:28:53 -080010001 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010002 mAttachInfo.mScrollContainers.remove(this);
10003 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
10004 }
Romain Guy01d5edc2011-01-28 11:28:53 -080010005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010006 mAttachInfo = null;
10007 }
10008
10009 /**
10010 * Store this view hierarchy's frozen state into the given container.
10011 *
10012 * @param container The SparseArray in which to save the view's state.
10013 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010014 * @see #restoreHierarchyState(android.util.SparseArray)
10015 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10016 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010017 */
10018 public void saveHierarchyState(SparseArray<Parcelable> container) {
10019 dispatchSaveInstanceState(container);
10020 }
10021
10022 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010023 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
10024 * this view and its children. May be overridden to modify how freezing happens to a
10025 * 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 -080010026 *
10027 * @param container The SparseArray in which to save the view's state.
10028 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010029 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
10030 * @see #saveHierarchyState(android.util.SparseArray)
10031 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010032 */
10033 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
10034 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
10035 mPrivateFlags &= ~SAVE_STATE_CALLED;
10036 Parcelable state = onSaveInstanceState();
10037 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10038 throw new IllegalStateException(
10039 "Derived class did not call super.onSaveInstanceState()");
10040 }
10041 if (state != null) {
10042 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
10043 // + ": " + state);
10044 container.put(mID, state);
10045 }
10046 }
10047 }
10048
10049 /**
10050 * Hook allowing a view to generate a representation of its internal state
10051 * that can later be used to create a new instance with that same state.
10052 * This state should only contain information that is not persistent or can
10053 * not be reconstructed later. For example, you will never store your
10054 * current position on screen because that will be computed again when a
10055 * new instance of the view is placed in its view hierarchy.
10056 * <p>
10057 * Some examples of things you may store here: the current cursor position
10058 * in a text view (but usually not the text itself since that is stored in a
10059 * content provider or other persistent storage), the currently selected
10060 * item in a list view.
10061 *
10062 * @return Returns a Parcelable object containing the view's current dynamic
10063 * state, or null if there is nothing interesting to save. The
10064 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010065 * @see #onRestoreInstanceState(android.os.Parcelable)
10066 * @see #saveHierarchyState(android.util.SparseArray)
10067 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010068 * @see #setSaveEnabled(boolean)
10069 */
10070 protected Parcelable onSaveInstanceState() {
10071 mPrivateFlags |= SAVE_STATE_CALLED;
10072 return BaseSavedState.EMPTY_STATE;
10073 }
10074
10075 /**
10076 * Restore this view hierarchy's frozen state from the given container.
10077 *
10078 * @param container The SparseArray which holds previously frozen states.
10079 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010080 * @see #saveHierarchyState(android.util.SparseArray)
10081 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
10082 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010083 */
10084 public void restoreHierarchyState(SparseArray<Parcelable> container) {
10085 dispatchRestoreInstanceState(container);
10086 }
10087
10088 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010089 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
10090 * state for this view and its children. May be overridden to modify how restoring
10091 * happens to a view's children; for example, some views may want to not store state
10092 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010093 *
10094 * @param container The SparseArray which holds previously saved state.
10095 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010096 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10097 * @see #restoreHierarchyState(android.util.SparseArray)
10098 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010099 */
10100 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
10101 if (mID != NO_ID) {
10102 Parcelable state = container.get(mID);
10103 if (state != null) {
10104 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
10105 // + ": " + state);
10106 mPrivateFlags &= ~SAVE_STATE_CALLED;
10107 onRestoreInstanceState(state);
10108 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10109 throw new IllegalStateException(
10110 "Derived class did not call super.onRestoreInstanceState()");
10111 }
10112 }
10113 }
10114 }
10115
10116 /**
10117 * Hook allowing a view to re-apply a representation of its internal state that had previously
10118 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
10119 * null state.
10120 *
10121 * @param state The frozen state that had previously been returned by
10122 * {@link #onSaveInstanceState}.
10123 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010124 * @see #onSaveInstanceState()
10125 * @see #restoreHierarchyState(android.util.SparseArray)
10126 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010127 */
10128 protected void onRestoreInstanceState(Parcelable state) {
10129 mPrivateFlags |= SAVE_STATE_CALLED;
10130 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080010131 throw new IllegalArgumentException("Wrong state class, expecting View State but "
10132 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080010133 + "when two views of different type have the same id in the same hierarchy. "
10134 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080010135 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010136 }
10137 }
10138
10139 /**
10140 * <p>Return the time at which the drawing of the view hierarchy started.</p>
10141 *
10142 * @return the drawing start time in milliseconds
10143 */
10144 public long getDrawingTime() {
10145 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
10146 }
10147
10148 /**
10149 * <p>Enables or disables the duplication of the parent's state into this view. When
10150 * duplication is enabled, this view gets its drawable state from its parent rather
10151 * than from its own internal properties.</p>
10152 *
10153 * <p>Note: in the current implementation, setting this property to true after the
10154 * view was added to a ViewGroup might have no effect at all. This property should
10155 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
10156 *
10157 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
10158 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010159 *
Gilles Debunnefb817032011-01-13 13:52:49 -080010160 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
10161 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010162 *
10163 * @param enabled True to enable duplication of the parent's drawable state, false
10164 * to disable it.
10165 *
10166 * @see #getDrawableState()
10167 * @see #isDuplicateParentStateEnabled()
10168 */
10169 public void setDuplicateParentStateEnabled(boolean enabled) {
10170 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
10171 }
10172
10173 /**
10174 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
10175 *
10176 * @return True if this view's drawable state is duplicated from the parent,
10177 * false otherwise
10178 *
10179 * @see #getDrawableState()
10180 * @see #setDuplicateParentStateEnabled(boolean)
10181 */
10182 public boolean isDuplicateParentStateEnabled() {
10183 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
10184 }
10185
10186 /**
Romain Guy171c5922011-01-06 10:04:23 -080010187 * <p>Specifies the type of layer backing this view. The layer can be
10188 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
10189 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010190 *
Romain Guy171c5922011-01-06 10:04:23 -080010191 * <p>A layer is associated with an optional {@link android.graphics.Paint}
10192 * instance that controls how the layer is composed on screen. The following
10193 * properties of the paint are taken into account when composing the layer:</p>
10194 * <ul>
10195 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
10196 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
10197 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
10198 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080010199 *
Romain Guy171c5922011-01-06 10:04:23 -080010200 * <p>If this view has an alpha value set to < 1.0 by calling
10201 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
10202 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
10203 * equivalent to setting a hardware layer on this view and providing a paint with
10204 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080010205 *
Romain Guy171c5922011-01-06 10:04:23 -080010206 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
10207 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
10208 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010209 *
Romain Guy171c5922011-01-06 10:04:23 -080010210 * @param layerType The ype of layer to use with this view, must be one of
10211 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10212 * {@link #LAYER_TYPE_HARDWARE}
10213 * @param paint The paint used to compose the layer. This argument is optional
10214 * and can be null. It is ignored when the layer type is
10215 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080010216 *
10217 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080010218 * @see #LAYER_TYPE_NONE
10219 * @see #LAYER_TYPE_SOFTWARE
10220 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080010221 * @see #setAlpha(float)
10222 *
Romain Guy171c5922011-01-06 10:04:23 -080010223 * @attr ref android.R.styleable#View_layerType
10224 */
10225 public void setLayerType(int layerType, Paint paint) {
10226 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080010227 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080010228 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
10229 }
Chet Haasedaf98e92011-01-10 14:10:36 -080010230
Romain Guyd6cd5722011-01-17 14:42:41 -080010231 if (layerType == mLayerType) {
10232 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
10233 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010234 invalidateParentCaches();
10235 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080010236 }
10237 return;
10238 }
Romain Guy171c5922011-01-06 10:04:23 -080010239
10240 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080010241 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070010242 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010243 destroyLayer();
Romain Guy31f2c2e2011-11-21 10:55:41 -080010244 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080010245 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010246 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010247 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080010248 default:
10249 break;
Romain Guy171c5922011-01-06 10:04:23 -080010250 }
10251
10252 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010253 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10254 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10255 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010256
Romain Guy0fd89bf2011-01-26 15:41:30 -080010257 invalidateParentCaches();
10258 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010259 }
10260
10261 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010262 * Indicates whether this view has a static layer. A view with layer type
10263 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10264 * dynamic.
10265 */
10266 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080010267 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070010268 }
10269
10270 /**
Romain Guy171c5922011-01-06 10:04:23 -080010271 * Indicates what type of layer is currently associated with this view. By default
10272 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10273 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10274 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010275 *
Romain Guy171c5922011-01-06 10:04:23 -080010276 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10277 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010278 *
10279 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010280 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010281 * @see #LAYER_TYPE_NONE
10282 * @see #LAYER_TYPE_SOFTWARE
10283 * @see #LAYER_TYPE_HARDWARE
10284 */
10285 public int getLayerType() {
10286 return mLayerType;
10287 }
Joe Malin32736f02011-01-19 16:14:20 -080010288
Romain Guy6c319ca2011-01-11 14:29:25 -080010289 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010290 * Forces this view's layer to be created and this view to be rendered
10291 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10292 * invoking this method will have no effect.
10293 *
10294 * This method can for instance be used to render a view into its layer before
10295 * starting an animation. If this view is complex, rendering into the layer
10296 * before starting the animation will avoid skipping frames.
10297 *
10298 * @throws IllegalStateException If this view is not attached to a window
10299 *
10300 * @see #setLayerType(int, android.graphics.Paint)
10301 */
10302 public void buildLayer() {
10303 if (mLayerType == LAYER_TYPE_NONE) return;
10304
10305 if (mAttachInfo == null) {
10306 throw new IllegalStateException("This view must be attached to a window first");
10307 }
10308
10309 switch (mLayerType) {
10310 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080010311 if (mAttachInfo.mHardwareRenderer != null &&
10312 mAttachInfo.mHardwareRenderer.isEnabled() &&
10313 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080010314 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080010315 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010316 break;
10317 case LAYER_TYPE_SOFTWARE:
10318 buildDrawingCache(true);
10319 break;
10320 }
10321 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010322
10323 // Make sure the HardwareRenderer.validate() was invoked before calling this method
10324 void flushLayer() {
10325 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
10326 mHardwareLayer.flush();
10327 }
10328 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010329
10330 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010331 * <p>Returns a hardware layer that can be used to draw this view again
10332 * without executing its draw method.</p>
10333 *
10334 * @return A HardwareLayer ready to render, or null if an error occurred.
10335 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080010336 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010337 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10338 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010339 return null;
10340 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010341
10342 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080010343
10344 final int width = mRight - mLeft;
10345 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010346
Romain Guy6c319ca2011-01-11 14:29:25 -080010347 if (width == 0 || height == 0) {
10348 return null;
10349 }
10350
10351 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10352 if (mHardwareLayer == null) {
10353 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10354 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -080010355 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010356 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10357 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -080010358 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010359 }
10360
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010361 // The layer is not valid if the underlying GPU resources cannot be allocated
10362 if (!mHardwareLayer.isValid()) {
10363 return null;
10364 }
10365
Michael Jurka7e52caf2012-03-06 15:57:06 -080010366 mHardwareLayer.redraw(getDisplayList(), mLocalDirtyRect);
10367 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010368 }
10369
10370 return mHardwareLayer;
10371 }
Romain Guy171c5922011-01-06 10:04:23 -080010372
Romain Guy589b0bb2011-10-10 13:57:47 -070010373 /**
10374 * Destroys this View's hardware layer if possible.
10375 *
10376 * @return True if the layer was destroyed, false otherwise.
10377 *
10378 * @see #setLayerType(int, android.graphics.Paint)
10379 * @see #LAYER_TYPE_HARDWARE
10380 */
Romain Guy65b345f2011-07-27 18:51:50 -070010381 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -070010382 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010383 AttachInfo info = mAttachInfo;
10384 if (info != null && info.mHardwareRenderer != null &&
10385 info.mHardwareRenderer.isEnabled() && info.mHardwareRenderer.validate()) {
10386 mHardwareLayer.destroy();
10387 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080010388
Romain Guy9c4b79a2011-11-10 19:23:58 -080010389 invalidate(true);
10390 invalidateParentCaches();
10391 }
Romain Guy65b345f2011-07-27 18:51:50 -070010392 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010393 }
Romain Guy65b345f2011-07-27 18:51:50 -070010394 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010395 }
10396
Romain Guy171c5922011-01-06 10:04:23 -080010397 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080010398 * Destroys all hardware rendering resources. This method is invoked
10399 * when the system needs to reclaim resources. Upon execution of this
10400 * method, you should free any OpenGL resources created by the view.
10401 *
10402 * Note: you <strong>must</strong> call
10403 * <code>super.destroyHardwareResources()</code> when overriding
10404 * this method.
10405 *
10406 * @hide
10407 */
10408 protected void destroyHardwareResources() {
10409 destroyLayer();
10410 }
10411
10412 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010413 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10414 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10415 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10416 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10417 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10418 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010419 *
Romain Guy171c5922011-01-06 10:04:23 -080010420 * <p>Enabling the drawing cache is similar to
10421 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010422 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10423 * drawing cache has no effect on rendering because the system uses a different mechanism
10424 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10425 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10426 * for information on how to enable software and hardware layers.</p>
10427 *
10428 * <p>This API can be used to manually generate
10429 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10430 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010431 *
10432 * @param enabled true to enable the drawing cache, false otherwise
10433 *
10434 * @see #isDrawingCacheEnabled()
10435 * @see #getDrawingCache()
10436 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010437 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010438 */
10439 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010440 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010441 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10442 }
10443
10444 /**
10445 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10446 *
10447 * @return true if the drawing cache is enabled
10448 *
10449 * @see #setDrawingCacheEnabled(boolean)
10450 * @see #getDrawingCache()
10451 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010452 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010453 public boolean isDrawingCacheEnabled() {
10454 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10455 }
10456
10457 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010458 * Debugging utility which recursively outputs the dirty state of a view and its
10459 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010460 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010461 * @hide
10462 */
Romain Guy676b1732011-02-14 14:45:33 -080010463 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010464 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10465 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10466 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10467 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10468 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10469 if (clear) {
10470 mPrivateFlags &= clearMask;
10471 }
10472 if (this instanceof ViewGroup) {
10473 ViewGroup parent = (ViewGroup) this;
10474 final int count = parent.getChildCount();
10475 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010476 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010477 child.outputDirtyFlags(indent + " ", clear, clearMask);
10478 }
10479 }
10480 }
10481
10482 /**
10483 * This method is used by ViewGroup to cause its children to restore or recreate their
10484 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10485 * to recreate its own display list, which would happen if it went through the normal
10486 * draw/dispatchDraw mechanisms.
10487 *
10488 * @hide
10489 */
10490 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010491
10492 /**
10493 * A view that is not attached or hardware accelerated cannot create a display list.
10494 * This method checks these conditions and returns the appropriate result.
10495 *
10496 * @return true if view has the ability to create a display list, false otherwise.
10497 *
10498 * @hide
10499 */
10500 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010501 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010502 }
Joe Malin32736f02011-01-19 16:14:20 -080010503
Chet Haasedaf98e92011-01-10 14:10:36 -080010504 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080010505 * @return The HardwareRenderer associated with that view or null if hardware rendering
10506 * is not supported or this this has not been attached to a window.
10507 *
10508 * @hide
10509 */
10510 public HardwareRenderer getHardwareRenderer() {
10511 if (mAttachInfo != null) {
10512 return mAttachInfo.mHardwareRenderer;
10513 }
10514 return null;
10515 }
10516
10517 /**
Romain Guyb051e892010-09-28 19:09:36 -070010518 * <p>Returns a display list that can be used to draw this view again
10519 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010520 *
Romain Guyb051e892010-09-28 19:09:36 -070010521 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010522 *
10523 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010524 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010525 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -080010526 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -070010527 return null;
10528 }
10529
Chet Haasedaf98e92011-01-10 14:10:36 -080010530 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10531 mDisplayList == null || !mDisplayList.isValid() ||
10532 mRecreateDisplayList)) {
10533 // Don't need to recreate the display list, just need to tell our
10534 // children to restore/recreate theirs
10535 if (mDisplayList != null && mDisplayList.isValid() &&
10536 !mRecreateDisplayList) {
10537 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10538 mPrivateFlags &= ~DIRTY_MASK;
10539 dispatchGetDisplayList();
10540
10541 return mDisplayList;
10542 }
10543
10544 // If we got here, we're recreating it. Mark it as such to ensure that
10545 // we copy in child display lists into ours in drawChild()
10546 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -080010547 if (mDisplayList == null) {
Romain Guy13631f32012-01-30 17:41:55 -080010548 final String name = getClass().getSimpleName();
10549 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
Chet Haasedaf98e92011-01-10 14:10:36 -080010550 // If we're creating a new display list, make sure our parent gets invalidated
10551 // since they will need to recreate their display list to account for this
10552 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -080010553 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -080010554 }
Romain Guyb051e892010-09-28 19:09:36 -070010555
10556 final HardwareCanvas canvas = mDisplayList.start();
Romain Guye080af32011-09-08 15:03:39 -070010557 int restoreCount = 0;
Romain Guyb051e892010-09-28 19:09:36 -070010558 try {
10559 int width = mRight - mLeft;
10560 int height = mBottom - mTop;
10561
10562 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -080010563 // The dirty rect should always be null for a display list
10564 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -070010565
Chet Haasedaf98e92011-01-10 14:10:36 -080010566 computeScroll();
Romain Guye080af32011-09-08 15:03:39 -070010567
10568 restoreCount = canvas.save();
Chet Haasedaf98e92011-01-10 14:10:36 -080010569 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010570 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -070010571 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -080010572
Romain Guyb051e892010-09-28 19:09:36 -070010573 // Fast path for layouts with no backgrounds
10574 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -070010575 dispatchDraw(canvas);
10576 } else {
10577 draw(canvas);
10578 }
Romain Guyb051e892010-09-28 19:09:36 -070010579 } finally {
Romain Guye080af32011-09-08 15:03:39 -070010580 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -070010581 canvas.onPostDraw();
10582
10583 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -070010584 }
Chet Haase77785f92011-01-25 23:22:09 -080010585 } else {
10586 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10587 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -070010588 }
10589
10590 return mDisplayList;
10591 }
10592
10593 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010594 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010595 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010596 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010597 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010598 * @see #getDrawingCache(boolean)
10599 */
10600 public Bitmap getDrawingCache() {
10601 return getDrawingCache(false);
10602 }
10603
10604 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010605 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10606 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10607 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10608 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10609 * request the drawing cache by calling this method and draw it on screen if the
10610 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010611 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010612 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10613 * this method will create a bitmap of the same size as this view. Because this bitmap
10614 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10615 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10616 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10617 * size than the view. This implies that your application must be able to handle this
10618 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010619 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010620 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10621 * the current density of the screen when the application is in compatibility
10622 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010623 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010624 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010625 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010626 * @see #setDrawingCacheEnabled(boolean)
10627 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010628 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010629 * @see #destroyDrawingCache()
10630 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010631 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010632 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10633 return null;
10634 }
10635 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010636 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010637 }
Romain Guy02890fd2010-08-06 17:58:44 -070010638 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010639 }
10640
10641 /**
10642 * <p>Frees the resources used by the drawing cache. If you call
10643 * {@link #buildDrawingCache()} manually without calling
10644 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10645 * should cleanup the cache with this method afterwards.</p>
10646 *
10647 * @see #setDrawingCacheEnabled(boolean)
10648 * @see #buildDrawingCache()
10649 * @see #getDrawingCache()
10650 */
10651 public void destroyDrawingCache() {
10652 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010653 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010654 mDrawingCache = null;
10655 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010656 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010657 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010658 mUnscaledDrawingCache = null;
10659 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010660 }
10661
10662 /**
10663 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070010664 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010665 * view will always be drawn on top of a solid color.
10666 *
10667 * @param color The background color to use for the drawing cache's bitmap
10668 *
10669 * @see #setDrawingCacheEnabled(boolean)
10670 * @see #buildDrawingCache()
10671 * @see #getDrawingCache()
10672 */
10673 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010674 if (color != mDrawingCacheBackgroundColor) {
10675 mDrawingCacheBackgroundColor = color;
10676 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010678 }
10679
10680 /**
10681 * @see #setDrawingCacheBackgroundColor(int)
10682 *
10683 * @return The background color to used for the drawing cache's bitmap
10684 */
10685 public int getDrawingCacheBackgroundColor() {
10686 return mDrawingCacheBackgroundColor;
10687 }
10688
10689 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010690 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010691 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010692 * @see #buildDrawingCache(boolean)
10693 */
10694 public void buildDrawingCache() {
10695 buildDrawingCache(false);
10696 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010697
Romain Guyfbd8f692009-06-26 14:51:58 -070010698 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010699 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10700 *
10701 * <p>If you call {@link #buildDrawingCache()} manually without calling
10702 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10703 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010704 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010705 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10706 * this method will create a bitmap of the same size as this view. Because this bitmap
10707 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10708 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10709 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10710 * size than the view. This implies that your application must be able to handle this
10711 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010712 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010713 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10714 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010715 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010716 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010717 *
10718 * @see #getDrawingCache()
10719 * @see #destroyDrawingCache()
10720 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010721 public void buildDrawingCache(boolean autoScale) {
10722 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010723 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010724 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010725
10726 if (ViewDebug.TRACE_HIERARCHY) {
10727 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010729
Romain Guy8506ab42009-06-11 17:35:47 -070010730 int width = mRight - mLeft;
10731 int height = mBottom - mTop;
10732
10733 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010734 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010735
Romain Guye1123222009-06-29 14:24:56 -070010736 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010737 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10738 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010740
10741 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010742 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010743 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010744
10745 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010746 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010747 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010748 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10749 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010750 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010751 return;
10752 }
10753
10754 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010755 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010756
10757 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010758 Bitmap.Config quality;
10759 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010760 // Never pick ARGB_4444 because it looks awful
10761 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010762 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10763 case DRAWING_CACHE_QUALITY_AUTO:
10764 quality = Bitmap.Config.ARGB_8888;
10765 break;
10766 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010767 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010768 break;
10769 case DRAWING_CACHE_QUALITY_HIGH:
10770 quality = Bitmap.Config.ARGB_8888;
10771 break;
10772 default:
10773 quality = Bitmap.Config.ARGB_8888;
10774 break;
10775 }
10776 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010777 // Optimization for translucent windows
10778 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010779 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010780 }
10781
10782 // Try to cleanup memory
10783 if (bitmap != null) bitmap.recycle();
10784
10785 try {
10786 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010787 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010788 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010789 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010790 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010791 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010792 }
Adam Powell26153a32010-11-08 15:22:27 -080010793 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010794 } catch (OutOfMemoryError e) {
10795 // If there is not enough memory to create the bitmap cache, just
10796 // ignore the issue as bitmap caches are not required to draw the
10797 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010798 if (autoScale) {
10799 mDrawingCache = null;
10800 } else {
10801 mUnscaledDrawingCache = null;
10802 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010803 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010804 return;
10805 }
10806
10807 clear = drawingCacheBackgroundColor != 0;
10808 }
10809
10810 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010811 if (attachInfo != null) {
10812 canvas = attachInfo.mCanvas;
10813 if (canvas == null) {
10814 canvas = new Canvas();
10815 }
10816 canvas.setBitmap(bitmap);
10817 // Temporarily clobber the cached Canvas in case one of our children
10818 // is also using a drawing cache. Without this, the children would
10819 // steal the canvas by attaching their own bitmap to it and bad, bad
10820 // thing would happen (invisible views, corrupted drawings, etc.)
10821 attachInfo.mCanvas = null;
10822 } else {
10823 // This case should hopefully never or seldom happen
10824 canvas = new Canvas(bitmap);
10825 }
10826
10827 if (clear) {
10828 bitmap.eraseColor(drawingCacheBackgroundColor);
10829 }
10830
10831 computeScroll();
10832 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010833
Romain Guye1123222009-06-29 14:24:56 -070010834 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010835 final float scale = attachInfo.mApplicationScale;
10836 canvas.scale(scale, scale);
10837 }
Joe Malin32736f02011-01-19 16:14:20 -080010838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010839 canvas.translate(-mScrollX, -mScrollY);
10840
Romain Guy5bcdff42009-05-14 21:27:18 -070010841 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010842 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10843 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010844 mPrivateFlags |= DRAWING_CACHE_VALID;
10845 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010846
10847 // Fast path for layouts with no backgrounds
10848 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10849 if (ViewDebug.TRACE_HIERARCHY) {
10850 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10851 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010852 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010853 dispatchDraw(canvas);
10854 } else {
10855 draw(canvas);
10856 }
10857
10858 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010859 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010860
10861 if (attachInfo != null) {
10862 // Restore the cached Canvas for our siblings
10863 attachInfo.mCanvas = canvas;
10864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010865 }
10866 }
10867
10868 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010869 * Create a snapshot of the view into a bitmap. We should probably make
10870 * some form of this public, but should think about the API.
10871 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010872 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010873 int width = mRight - mLeft;
10874 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010875
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010876 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010877 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010878 width = (int) ((width * scale) + 0.5f);
10879 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010880
Romain Guy8c11e312009-09-14 15:15:30 -070010881 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010882 if (bitmap == null) {
10883 throw new OutOfMemoryError();
10884 }
10885
Romain Guyc529d8d2011-09-06 15:01:39 -070010886 Resources resources = getResources();
10887 if (resources != null) {
10888 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
10889 }
Joe Malin32736f02011-01-19 16:14:20 -080010890
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010891 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010892 if (attachInfo != null) {
10893 canvas = attachInfo.mCanvas;
10894 if (canvas == null) {
10895 canvas = new Canvas();
10896 }
10897 canvas.setBitmap(bitmap);
10898 // Temporarily clobber the cached Canvas in case one of our children
10899 // is also using a drawing cache. Without this, the children would
10900 // steal the canvas by attaching their own bitmap to it and bad, bad
10901 // things would happen (invisible views, corrupted drawings, etc.)
10902 attachInfo.mCanvas = null;
10903 } else {
10904 // This case should hopefully never or seldom happen
10905 canvas = new Canvas(bitmap);
10906 }
10907
Romain Guy5bcdff42009-05-14 21:27:18 -070010908 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010909 bitmap.eraseColor(backgroundColor);
10910 }
10911
10912 computeScroll();
10913 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010914 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010915 canvas.translate(-mScrollX, -mScrollY);
10916
Romain Guy5bcdff42009-05-14 21:27:18 -070010917 // Temporarily remove the dirty mask
10918 int flags = mPrivateFlags;
10919 mPrivateFlags &= ~DIRTY_MASK;
10920
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010921 // Fast path for layouts with no backgrounds
10922 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10923 dispatchDraw(canvas);
10924 } else {
10925 draw(canvas);
10926 }
10927
Romain Guy5bcdff42009-05-14 21:27:18 -070010928 mPrivateFlags = flags;
10929
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010930 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010931 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010932
10933 if (attachInfo != null) {
10934 // Restore the cached Canvas for our siblings
10935 attachInfo.mCanvas = canvas;
10936 }
Romain Guy8506ab42009-06-11 17:35:47 -070010937
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010938 return bitmap;
10939 }
10940
10941 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010942 * Indicates whether this View is currently in edit mode. A View is usually
10943 * in edit mode when displayed within a developer tool. For instance, if
10944 * this View is being drawn by a visual user interface builder, this method
10945 * should return true.
10946 *
10947 * Subclasses should check the return value of this method to provide
10948 * different behaviors if their normal behavior might interfere with the
10949 * host environment. For instance: the class spawns a thread in its
10950 * constructor, the drawing code relies on device-specific features, etc.
10951 *
10952 * This method is usually checked in the drawing code of custom widgets.
10953 *
10954 * @return True if this View is in edit mode, false otherwise.
10955 */
10956 public boolean isInEditMode() {
10957 return false;
10958 }
10959
10960 /**
10961 * If the View draws content inside its padding and enables fading edges,
10962 * it needs to support padding offsets. Padding offsets are added to the
10963 * fading edges to extend the length of the fade so that it covers pixels
10964 * drawn inside the padding.
10965 *
10966 * Subclasses of this class should override this method if they need
10967 * to draw content inside the padding.
10968 *
10969 * @return True if padding offset must be applied, false otherwise.
10970 *
10971 * @see #getLeftPaddingOffset()
10972 * @see #getRightPaddingOffset()
10973 * @see #getTopPaddingOffset()
10974 * @see #getBottomPaddingOffset()
10975 *
10976 * @since CURRENT
10977 */
10978 protected boolean isPaddingOffsetRequired() {
10979 return false;
10980 }
10981
10982 /**
10983 * Amount by which to extend the left fading region. Called only when
10984 * {@link #isPaddingOffsetRequired()} returns true.
10985 *
10986 * @return The left padding offset in pixels.
10987 *
10988 * @see #isPaddingOffsetRequired()
10989 *
10990 * @since CURRENT
10991 */
10992 protected int getLeftPaddingOffset() {
10993 return 0;
10994 }
10995
10996 /**
10997 * Amount by which to extend the right fading region. Called only when
10998 * {@link #isPaddingOffsetRequired()} returns true.
10999 *
11000 * @return The right padding offset in pixels.
11001 *
11002 * @see #isPaddingOffsetRequired()
11003 *
11004 * @since CURRENT
11005 */
11006 protected int getRightPaddingOffset() {
11007 return 0;
11008 }
11009
11010 /**
11011 * Amount by which to extend the top fading region. Called only when
11012 * {@link #isPaddingOffsetRequired()} returns true.
11013 *
11014 * @return The top padding offset in pixels.
11015 *
11016 * @see #isPaddingOffsetRequired()
11017 *
11018 * @since CURRENT
11019 */
11020 protected int getTopPaddingOffset() {
11021 return 0;
11022 }
11023
11024 /**
11025 * Amount by which to extend the bottom fading region. Called only when
11026 * {@link #isPaddingOffsetRequired()} returns true.
11027 *
11028 * @return The bottom padding offset in pixels.
11029 *
11030 * @see #isPaddingOffsetRequired()
11031 *
11032 * @since CURRENT
11033 */
11034 protected int getBottomPaddingOffset() {
11035 return 0;
11036 }
11037
11038 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070011039 * @hide
11040 * @param offsetRequired
11041 */
11042 protected int getFadeTop(boolean offsetRequired) {
11043 int top = mPaddingTop;
11044 if (offsetRequired) top += getTopPaddingOffset();
11045 return top;
11046 }
11047
11048 /**
11049 * @hide
11050 * @param offsetRequired
11051 */
11052 protected int getFadeHeight(boolean offsetRequired) {
11053 int padding = mPaddingTop;
11054 if (offsetRequired) padding += getTopPaddingOffset();
11055 return mBottom - mTop - mPaddingBottom - padding;
11056 }
11057
11058 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090011059 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070011060 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011061 *
Romain Guy2bffd262010-09-12 17:40:02 -070011062 * <p>Even if this method returns true, it does not mean that every call
11063 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
11064 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090011065 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070011066 * window is hardware accelerated,
11067 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
11068 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011069 *
Romain Guy2bffd262010-09-12 17:40:02 -070011070 * @return True if the view is attached to a window and the window is
11071 * hardware accelerated; false in any other case.
11072 */
11073 public boolean isHardwareAccelerated() {
11074 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
11075 }
Joe Malin32736f02011-01-19 16:14:20 -080011076
Romain Guy2bffd262010-09-12 17:40:02 -070011077 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080011078 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
11079 * case of an active Animation being run on the view.
11080 */
11081 private boolean drawAnimation(ViewGroup parent, long drawingTime,
11082 Animation a, boolean scalingRequired) {
11083 Transformation invalidationTransform;
11084 final int flags = parent.mGroupFlags;
11085 final boolean initialized = a.isInitialized();
11086 if (!initialized) {
11087 a.initialize(mRight - mLeft, mBottom - mTop, getWidth(), getHeight());
11088 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
11089 onAnimationStart();
11090 }
11091
11092 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
11093 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
11094 if (parent.mInvalidationTransformation == null) {
11095 parent.mInvalidationTransformation = new Transformation();
11096 }
11097 invalidationTransform = parent.mInvalidationTransformation;
11098 a.getTransformation(drawingTime, invalidationTransform, 1f);
11099 } else {
11100 invalidationTransform = parent.mChildTransformation;
11101 }
11102 if (more) {
11103 if (!a.willChangeBounds()) {
11104 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
11105 parent.FLAG_OPTIMIZE_INVALIDATE) {
11106 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
11107 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
11108 // The child need to draw an animation, potentially offscreen, so
11109 // make sure we do not cancel invalidate requests
11110 parent.mPrivateFlags |= DRAW_ANIMATION;
11111 parent.invalidate(mLeft, mTop, mRight, mBottom);
11112 }
11113 } else {
11114 if (parent.mInvalidateRegion == null) {
11115 parent.mInvalidateRegion = new RectF();
11116 }
11117 final RectF region = parent.mInvalidateRegion;
11118 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
11119 invalidationTransform);
11120
11121 // The child need to draw an animation, potentially offscreen, so
11122 // make sure we do not cancel invalidate requests
11123 parent.mPrivateFlags |= DRAW_ANIMATION;
11124
11125 final int left = mLeft + (int) region.left;
11126 final int top = mTop + (int) region.top;
11127 parent.invalidate(left, top, left + (int) (region.width() + .5f),
11128 top + (int) (region.height() + .5f));
11129 }
11130 }
11131 return more;
11132 }
11133
11134 /**
Chet Haase64a48c12012-02-13 16:33:29 -080011135 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
11136 * This draw() method is an implementation detail and is not intended to be overridden or
11137 * to be called from anywhere else other than ViewGroup.drawChild().
11138 */
11139 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
11140 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080011141 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080011142 final int flags = parent.mGroupFlags;
11143
11144 if ((flags & parent.FLAG_CLEAR_TRANSFORMATION) == parent.FLAG_CLEAR_TRANSFORMATION) {
11145 parent.mChildTransformation.clear();
11146 parent.mGroupFlags &= ~parent.FLAG_CLEAR_TRANSFORMATION;
11147 }
11148
11149 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080011150 boolean concatMatrix = false;
11151
11152 boolean scalingRequired = false;
11153 boolean caching;
11154 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
11155
11156 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
11157 if ((flags & parent.FLAG_CHILDREN_DRAWN_WITH_CACHE) == parent.FLAG_CHILDREN_DRAWN_WITH_CACHE ||
11158 (flags & parent.FLAG_ALWAYS_DRAWN_WITH_CACHE) == parent.FLAG_ALWAYS_DRAWN_WITH_CACHE) {
11159 caching = true;
11160 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
11161 } else {
11162 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
11163 }
11164
Chet Haasebcca79a2012-02-14 08:45:14 -080011165 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080011166 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011167 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080011168 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080011169 transformToApply = parent.mChildTransformation;
Chet Haase64a48c12012-02-13 16:33:29 -080011170 } else if ((flags & parent.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) ==
11171 parent.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011172 final boolean hasTransform =
11173 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080011174 if (hasTransform) {
11175 final int transformType = parent.mChildTransformation.getTransformationType();
11176 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
11177 parent.mChildTransformation : null;
11178 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
11179 }
11180 }
11181
11182 concatMatrix |= !childHasIdentityMatrix;
11183
11184 // Sets the flag as early as possible to allow draw() implementations
11185 // to call invalidate() successfully when doing animations
11186 mPrivateFlags |= DRAWN;
11187
Chet Haasebcca79a2012-02-14 08:45:14 -080011188 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080011189 (mPrivateFlags & DRAW_ANIMATION) == 0) {
11190 return more;
11191 }
11192
11193 if (hardwareAccelerated) {
11194 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
11195 // retain the flag's value temporarily in the mRecreateDisplayList flag
11196 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
11197 mPrivateFlags &= ~INVALIDATED;
11198 }
11199
11200 computeScroll();
11201
11202 final int sx = mScrollX;
11203 final int sy = mScrollY;
11204
11205 DisplayList displayList = null;
11206 Bitmap cache = null;
11207 boolean hasDisplayList = false;
11208 if (caching) {
11209 if (!hardwareAccelerated) {
11210 if (layerType != LAYER_TYPE_NONE) {
11211 layerType = LAYER_TYPE_SOFTWARE;
11212 buildDrawingCache(true);
11213 }
11214 cache = getDrawingCache(true);
11215 } else {
11216 switch (layerType) {
11217 case LAYER_TYPE_SOFTWARE:
11218 buildDrawingCache(true);
11219 cache = getDrawingCache(true);
11220 break;
11221 case LAYER_TYPE_NONE:
11222 // Delay getting the display list until animation-driven alpha values are
11223 // set up and possibly passed on to the view
11224 hasDisplayList = canHaveDisplayList();
11225 break;
11226 }
11227 }
11228 }
11229
11230 final boolean hasNoCache = cache == null || hasDisplayList;
11231 final boolean offsetForScroll = cache == null && !hasDisplayList &&
11232 layerType != LAYER_TYPE_HARDWARE;
11233
11234 final int restoreTo = canvas.save();
11235 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011236 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080011237 } else {
Chet Haasebcca79a2012-02-14 08:45:14 -080011238 canvas.translate(mLeft, mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080011239 if (scalingRequired) {
11240 // mAttachInfo cannot be null, otherwise scalingRequired == false
11241 final float scale = 1.0f / mAttachInfo.mApplicationScale;
11242 canvas.scale(scale, scale);
11243 }
11244 }
11245
11246 float alpha = getAlpha();
11247 if (transformToApply != null || alpha < 1.0f || !hasIdentityMatrix()) {
11248 if (transformToApply != null || !childHasIdentityMatrix) {
11249 int transX = 0;
11250 int transY = 0;
11251
11252 if (offsetForScroll) {
11253 transX = -sx;
11254 transY = -sy;
11255 }
11256
11257 if (transformToApply != null) {
11258 if (concatMatrix) {
11259 // Undo the scroll translation, apply the transformation matrix,
11260 // then redo the scroll translate to get the correct result.
11261 canvas.translate(-transX, -transY);
11262 canvas.concat(transformToApply.getMatrix());
11263 canvas.translate(transX, transY);
11264 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11265 }
11266
11267 float transformAlpha = transformToApply.getAlpha();
11268 if (transformAlpha < 1.0f) {
11269 alpha *= transformToApply.getAlpha();
11270 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11271 }
11272 }
11273
11274 if (!childHasIdentityMatrix) {
11275 canvas.translate(-transX, -transY);
11276 canvas.concat(getMatrix());
11277 canvas.translate(transX, transY);
11278 }
11279 }
11280
11281 if (alpha < 1.0f) {
11282 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11283 if (hasNoCache) {
11284 final int multipliedAlpha = (int) (255 * alpha);
11285 if (!onSetAlpha(multipliedAlpha)) {
11286 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11287 if ((flags & parent.FLAG_CLIP_CHILDREN) == parent.FLAG_CLIP_CHILDREN ||
11288 layerType != LAYER_TYPE_NONE) {
11289 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
11290 }
11291 if (layerType == LAYER_TYPE_NONE) {
11292 final int scrollX = hasDisplayList ? 0 : sx;
11293 final int scrollY = hasDisplayList ? 0 : sy;
Chet Haasebcca79a2012-02-14 08:45:14 -080011294 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
11295 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080011296 }
11297 } else {
11298 // Alpha is handled by the child directly, clobber the layer's alpha
11299 mPrivateFlags |= ALPHA_SET;
11300 }
11301 }
11302 }
11303 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11304 onSetAlpha(255);
11305 mPrivateFlags &= ~ALPHA_SET;
11306 }
11307
11308 if ((flags & parent.FLAG_CLIP_CHILDREN) == parent.FLAG_CLIP_CHILDREN) {
11309 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011310 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080011311 } else {
11312 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011313 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080011314 } else {
11315 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
11316 }
11317 }
11318 }
11319
11320 if (hasDisplayList) {
11321 displayList = getDisplayList();
11322 if (!displayList.isValid()) {
11323 // Uncommon, but possible. If a view is removed from the hierarchy during the call
11324 // to getDisplayList(), the display list will be marked invalid and we should not
11325 // try to use it again.
11326 displayList = null;
11327 hasDisplayList = false;
11328 }
11329 }
11330
11331 if (hasNoCache) {
11332 boolean layerRendered = false;
11333 if (layerType == LAYER_TYPE_HARDWARE) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011334 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080011335 if (layer != null && layer.isValid()) {
11336 mLayerPaint.setAlpha((int) (alpha * 255));
11337 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
11338 layerRendered = true;
11339 } else {
11340 final int scrollX = hasDisplayList ? 0 : sx;
11341 final int scrollY = hasDisplayList ? 0 : sy;
11342 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080011343 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080011344 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11345 }
11346 }
11347
11348 if (!layerRendered) {
11349 if (!hasDisplayList) {
11350 // Fast path for layouts with no backgrounds
11351 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11352 if (ViewDebug.TRACE_HIERARCHY) {
11353 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
11354 }
11355 mPrivateFlags &= ~DIRTY_MASK;
11356 dispatchDraw(canvas);
11357 } else {
11358 draw(canvas);
11359 }
11360 } else {
11361 mPrivateFlags &= ~DIRTY_MASK;
Chet Haasebcca79a2012-02-14 08:45:14 -080011362 ((HardwareCanvas) canvas).drawDisplayList(displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -080011363 mRight - mLeft, mBottom - mTop, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080011364 }
11365 }
11366 } else if (cache != null) {
11367 mPrivateFlags &= ~DIRTY_MASK;
11368 Paint cachePaint;
11369
11370 if (layerType == LAYER_TYPE_NONE) {
11371 cachePaint = parent.mCachePaint;
11372 if (cachePaint == null) {
11373 cachePaint = new Paint();
11374 cachePaint.setDither(false);
11375 parent.mCachePaint = cachePaint;
11376 }
11377 if (alpha < 1.0f) {
11378 cachePaint.setAlpha((int) (alpha * 255));
11379 parent.mGroupFlags |= parent.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haasebcca79a2012-02-14 08:45:14 -080011380 } else if ((flags & parent.FLAG_ALPHA_LOWER_THAN_ONE) ==
11381 parent.FLAG_ALPHA_LOWER_THAN_ONE) {
Chet Haase64a48c12012-02-13 16:33:29 -080011382 cachePaint.setAlpha(255);
11383 parent.mGroupFlags &= ~parent.FLAG_ALPHA_LOWER_THAN_ONE;
11384 }
11385 } else {
11386 cachePaint = mLayerPaint;
11387 cachePaint.setAlpha((int) (alpha * 255));
11388 }
11389 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
11390 }
11391
11392 canvas.restoreToCount(restoreTo);
11393
11394 if (a != null && !more) {
11395 if (!hardwareAccelerated && !a.getFillAfter()) {
11396 onSetAlpha(255);
11397 }
11398 parent.finishAnimatingView(this, a);
11399 }
11400
11401 if (more && hardwareAccelerated) {
11402 // invalidation is the trigger to recreate display lists, so if we're using
11403 // display lists to render, force an invalidate to allow the animation to
11404 // continue drawing another frame
11405 parent.invalidate(true);
11406 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11407 // alpha animations should cause the child to recreate its display list
11408 invalidate(true);
11409 }
11410 }
11411
11412 mRecreateDisplayList = false;
11413
11414 return more;
11415 }
11416
11417 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011418 * Manually render this view (and all of its children) to the given Canvas.
11419 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011420 * called. When implementing a view, implement
11421 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
11422 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011423 *
11424 * @param canvas The Canvas to which the View is rendered.
11425 */
11426 public void draw(Canvas canvas) {
11427 if (ViewDebug.TRACE_HIERARCHY) {
11428 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
11429 }
11430
Romain Guy5bcdff42009-05-14 21:27:18 -070011431 final int privateFlags = mPrivateFlags;
11432 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
11433 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
11434 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070011435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011436 /*
11437 * Draw traversal performs several drawing steps which must be executed
11438 * in the appropriate order:
11439 *
11440 * 1. Draw the background
11441 * 2. If necessary, save the canvas' layers to prepare for fading
11442 * 3. Draw view's content
11443 * 4. Draw children
11444 * 5. If necessary, draw the fading edges and restore layers
11445 * 6. Draw decorations (scrollbars for instance)
11446 */
11447
11448 // Step 1, draw the background, if needed
11449 int saveCount;
11450
Romain Guy24443ea2009-05-11 11:56:30 -070011451 if (!dirtyOpaque) {
11452 final Drawable background = mBGDrawable;
11453 if (background != null) {
11454 final int scrollX = mScrollX;
11455 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011456
Romain Guy24443ea2009-05-11 11:56:30 -070011457 if (mBackgroundSizeChanged) {
11458 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
11459 mBackgroundSizeChanged = false;
11460 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011461
Romain Guy24443ea2009-05-11 11:56:30 -070011462 if ((scrollX | scrollY) == 0) {
11463 background.draw(canvas);
11464 } else {
11465 canvas.translate(scrollX, scrollY);
11466 background.draw(canvas);
11467 canvas.translate(-scrollX, -scrollY);
11468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011469 }
11470 }
11471
11472 // skip step 2 & 5 if possible (common case)
11473 final int viewFlags = mViewFlags;
11474 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
11475 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
11476 if (!verticalEdges && !horizontalEdges) {
11477 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011478 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011479
11480 // Step 4, draw the children
11481 dispatchDraw(canvas);
11482
11483 // Step 6, draw decorations (scrollbars)
11484 onDrawScrollBars(canvas);
11485
11486 // we're done...
11487 return;
11488 }
11489
11490 /*
11491 * Here we do the full fledged routine...
11492 * (this is an uncommon case where speed matters less,
11493 * this is why we repeat some of the tests that have been
11494 * done above)
11495 */
11496
11497 boolean drawTop = false;
11498 boolean drawBottom = false;
11499 boolean drawLeft = false;
11500 boolean drawRight = false;
11501
11502 float topFadeStrength = 0.0f;
11503 float bottomFadeStrength = 0.0f;
11504 float leftFadeStrength = 0.0f;
11505 float rightFadeStrength = 0.0f;
11506
11507 // Step 2, save the canvas' layers
11508 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011509
11510 final boolean offsetRequired = isPaddingOffsetRequired();
11511 if (offsetRequired) {
11512 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011513 }
11514
11515 int left = mScrollX + paddingLeft;
11516 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070011517 int top = mScrollY + getFadeTop(offsetRequired);
11518 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011519
11520 if (offsetRequired) {
11521 right += getRightPaddingOffset();
11522 bottom += getBottomPaddingOffset();
11523 }
11524
11525 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011526 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
11527 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011528
11529 // clip the fade length if top and bottom fades overlap
11530 // overlapping fades produce odd-looking artifacts
11531 if (verticalEdges && (top + length > bottom - length)) {
11532 length = (bottom - top) / 2;
11533 }
11534
11535 // also clip horizontal fades if necessary
11536 if (horizontalEdges && (left + length > right - length)) {
11537 length = (right - left) / 2;
11538 }
11539
11540 if (verticalEdges) {
11541 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011542 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011543 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011544 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011545 }
11546
11547 if (horizontalEdges) {
11548 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011549 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011550 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011551 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011552 }
11553
11554 saveCount = canvas.getSaveCount();
11555
11556 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070011557 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011558 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11559
11560 if (drawTop) {
11561 canvas.saveLayer(left, top, right, top + length, null, flags);
11562 }
11563
11564 if (drawBottom) {
11565 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
11566 }
11567
11568 if (drawLeft) {
11569 canvas.saveLayer(left, top, left + length, bottom, null, flags);
11570 }
11571
11572 if (drawRight) {
11573 canvas.saveLayer(right - length, top, right, bottom, null, flags);
11574 }
11575 } else {
11576 scrollabilityCache.setFadeColor(solidColor);
11577 }
11578
11579 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011580 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011581
11582 // Step 4, draw the children
11583 dispatchDraw(canvas);
11584
11585 // Step 5, draw the fade effect and restore layers
11586 final Paint p = scrollabilityCache.paint;
11587 final Matrix matrix = scrollabilityCache.matrix;
11588 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011589
11590 if (drawTop) {
11591 matrix.setScale(1, fadeHeight * topFadeStrength);
11592 matrix.postTranslate(left, top);
11593 fade.setLocalMatrix(matrix);
11594 canvas.drawRect(left, top, right, top + length, p);
11595 }
11596
11597 if (drawBottom) {
11598 matrix.setScale(1, fadeHeight * bottomFadeStrength);
11599 matrix.postRotate(180);
11600 matrix.postTranslate(left, bottom);
11601 fade.setLocalMatrix(matrix);
11602 canvas.drawRect(left, bottom - length, right, bottom, p);
11603 }
11604
11605 if (drawLeft) {
11606 matrix.setScale(1, fadeHeight * leftFadeStrength);
11607 matrix.postRotate(-90);
11608 matrix.postTranslate(left, top);
11609 fade.setLocalMatrix(matrix);
11610 canvas.drawRect(left, top, left + length, bottom, p);
11611 }
11612
11613 if (drawRight) {
11614 matrix.setScale(1, fadeHeight * rightFadeStrength);
11615 matrix.postRotate(90);
11616 matrix.postTranslate(right, top);
11617 fade.setLocalMatrix(matrix);
11618 canvas.drawRect(right - length, top, right, bottom, p);
11619 }
11620
11621 canvas.restoreToCount(saveCount);
11622
11623 // Step 6, draw decorations (scrollbars)
11624 onDrawScrollBars(canvas);
11625 }
11626
11627 /**
11628 * Override this if your view is known to always be drawn on top of a solid color background,
11629 * and needs to draw fading edges. Returning a non-zero color enables the view system to
11630 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
11631 * should be set to 0xFF.
11632 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011633 * @see #setVerticalFadingEdgeEnabled(boolean)
11634 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011635 *
11636 * @return The known solid color background for this view, or 0 if the color may vary
11637 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011638 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011639 public int getSolidColor() {
11640 return 0;
11641 }
11642
11643 /**
11644 * Build a human readable string representation of the specified view flags.
11645 *
11646 * @param flags the view flags to convert to a string
11647 * @return a String representing the supplied flags
11648 */
11649 private static String printFlags(int flags) {
11650 String output = "";
11651 int numFlags = 0;
11652 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
11653 output += "TAKES_FOCUS";
11654 numFlags++;
11655 }
11656
11657 switch (flags & VISIBILITY_MASK) {
11658 case INVISIBLE:
11659 if (numFlags > 0) {
11660 output += " ";
11661 }
11662 output += "INVISIBLE";
11663 // USELESS HERE numFlags++;
11664 break;
11665 case GONE:
11666 if (numFlags > 0) {
11667 output += " ";
11668 }
11669 output += "GONE";
11670 // USELESS HERE numFlags++;
11671 break;
11672 default:
11673 break;
11674 }
11675 return output;
11676 }
11677
11678 /**
11679 * Build a human readable string representation of the specified private
11680 * view flags.
11681 *
11682 * @param privateFlags the private view flags to convert to a string
11683 * @return a String representing the supplied flags
11684 */
11685 private static String printPrivateFlags(int privateFlags) {
11686 String output = "";
11687 int numFlags = 0;
11688
11689 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
11690 output += "WANTS_FOCUS";
11691 numFlags++;
11692 }
11693
11694 if ((privateFlags & FOCUSED) == FOCUSED) {
11695 if (numFlags > 0) {
11696 output += " ";
11697 }
11698 output += "FOCUSED";
11699 numFlags++;
11700 }
11701
11702 if ((privateFlags & SELECTED) == SELECTED) {
11703 if (numFlags > 0) {
11704 output += " ";
11705 }
11706 output += "SELECTED";
11707 numFlags++;
11708 }
11709
11710 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
11711 if (numFlags > 0) {
11712 output += " ";
11713 }
11714 output += "IS_ROOT_NAMESPACE";
11715 numFlags++;
11716 }
11717
11718 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
11719 if (numFlags > 0) {
11720 output += " ";
11721 }
11722 output += "HAS_BOUNDS";
11723 numFlags++;
11724 }
11725
11726 if ((privateFlags & DRAWN) == DRAWN) {
11727 if (numFlags > 0) {
11728 output += " ";
11729 }
11730 output += "DRAWN";
11731 // USELESS HERE numFlags++;
11732 }
11733 return output;
11734 }
11735
11736 /**
11737 * <p>Indicates whether or not this view's layout will be requested during
11738 * the next hierarchy layout pass.</p>
11739 *
11740 * @return true if the layout will be forced during next layout pass
11741 */
11742 public boolean isLayoutRequested() {
11743 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
11744 }
11745
11746 /**
11747 * Assign a size and position to a view and all of its
11748 * descendants
11749 *
11750 * <p>This is the second phase of the layout mechanism.
11751 * (The first is measuring). In this phase, each parent calls
11752 * layout on all of its children to position them.
11753 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080011754 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011755 *
Chet Haase9c087442011-01-12 16:20:16 -080011756 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011757 * Derived classes with children should override
11758 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080011759 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011760 *
11761 * @param l Left position, relative to parent
11762 * @param t Top position, relative to parent
11763 * @param r Right position, relative to parent
11764 * @param b Bottom position, relative to parent
11765 */
Romain Guy5429e1d2010-09-07 12:38:00 -070011766 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080011767 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070011768 int oldL = mLeft;
11769 int oldT = mTop;
11770 int oldB = mBottom;
11771 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011772 boolean changed = setFrame(l, t, r, b);
11773 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
11774 if (ViewDebug.TRACE_HIERARCHY) {
11775 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
11776 }
11777
11778 onLayout(changed, l, t, r, b);
11779 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070011780
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011781 ListenerInfo li = mListenerInfo;
11782 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070011783 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011784 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070011785 int numListeners = listenersCopy.size();
11786 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070011787 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070011788 }
11789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011790 }
11791 mPrivateFlags &= ~FORCE_LAYOUT;
11792 }
11793
11794 /**
11795 * Called from layout when this view should
11796 * assign a size and position to each of its children.
11797 *
11798 * Derived classes with children should override
11799 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070011800 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011801 * @param changed This is a new size or position for this view
11802 * @param left Left position, relative to parent
11803 * @param top Top position, relative to parent
11804 * @param right Right position, relative to parent
11805 * @param bottom Bottom position, relative to parent
11806 */
11807 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
11808 }
11809
11810 /**
11811 * Assign a size and position to this view.
11812 *
11813 * This is called from layout.
11814 *
11815 * @param left Left position, relative to parent
11816 * @param top Top position, relative to parent
11817 * @param right Right position, relative to parent
11818 * @param bottom Bottom position, relative to parent
11819 * @return true if the new size and position are different than the
11820 * previous ones
11821 * {@hide}
11822 */
11823 protected boolean setFrame(int left, int top, int right, int bottom) {
11824 boolean changed = false;
11825
11826 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070011827 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011828 + right + "," + bottom + ")");
11829 }
11830
11831 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
11832 changed = true;
11833
11834 // Remember our drawn bit
11835 int drawn = mPrivateFlags & DRAWN;
11836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011837 int oldWidth = mRight - mLeft;
11838 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070011839 int newWidth = right - left;
11840 int newHeight = bottom - top;
11841 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
11842
11843 // Invalidate our old position
11844 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011845
11846 mLeft = left;
11847 mTop = top;
11848 mRight = right;
11849 mBottom = bottom;
11850
11851 mPrivateFlags |= HAS_BOUNDS;
11852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011853
Chet Haase75755e22011-07-18 17:48:25 -070011854 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011855 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
11856 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070011857 if (mTransformationInfo != null) {
11858 mTransformationInfo.mMatrixDirty = true;
11859 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011861 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
11862 }
11863
11864 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
11865 // If we are visible, force the DRAWN bit to on so that
11866 // this invalidate will go through (at least to our parent).
11867 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011868 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011869 // the DRAWN bit.
11870 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070011871 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080011872 // parent display list may need to be recreated based on a change in the bounds
11873 // of any child
11874 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011875 }
11876
11877 // Reset drawn bit to original value (invalidate turns it off)
11878 mPrivateFlags |= drawn;
11879
11880 mBackgroundSizeChanged = true;
11881 }
11882 return changed;
11883 }
11884
11885 /**
11886 * Finalize inflating a view from XML. This is called as the last phase
11887 * of inflation, after all child views have been added.
11888 *
11889 * <p>Even if the subclass overrides onFinishInflate, they should always be
11890 * sure to call the super method, so that we get called.
11891 */
11892 protected void onFinishInflate() {
11893 }
11894
11895 /**
11896 * Returns the resources associated with this view.
11897 *
11898 * @return Resources object.
11899 */
11900 public Resources getResources() {
11901 return mResources;
11902 }
11903
11904 /**
11905 * Invalidates the specified Drawable.
11906 *
11907 * @param drawable the drawable to invalidate
11908 */
11909 public void invalidateDrawable(Drawable drawable) {
11910 if (verifyDrawable(drawable)) {
11911 final Rect dirty = drawable.getBounds();
11912 final int scrollX = mScrollX;
11913 final int scrollY = mScrollY;
11914
11915 invalidate(dirty.left + scrollX, dirty.top + scrollY,
11916 dirty.right + scrollX, dirty.bottom + scrollY);
11917 }
11918 }
11919
11920 /**
11921 * Schedules an action on a drawable to occur at a specified time.
11922 *
11923 * @param who the recipient of the action
11924 * @param what the action to run on the drawable
11925 * @param when the time at which the action must occur. Uses the
11926 * {@link SystemClock#uptimeMillis} timebase.
11927 */
11928 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080011929 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080011930 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080011931 if (mAttachInfo != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080011932 mAttachInfo.mViewRootImpl.mChoreographer.postAnimationCallbackDelayed(
11933 what, who, Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080011934 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080011935 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080011936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011937 }
11938 }
11939
11940 /**
11941 * Cancels a scheduled action on a drawable.
11942 *
11943 * @param who the recipient of the action
11944 * @param what the action to cancel
11945 */
11946 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080011947 if (verifyDrawable(who) && what != null) {
11948 if (mAttachInfo != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080011949 mAttachInfo.mViewRootImpl.mChoreographer.removeAnimationCallbacks(what, who);
Adam Powell37419d72011-11-10 11:32:09 -080011950 } else {
11951 ViewRootImpl.getRunQueue().removeCallbacks(what);
11952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011953 }
11954 }
11955
11956 /**
11957 * Unschedule any events associated with the given Drawable. This can be
11958 * used when selecting a new Drawable into a view, so that the previous
11959 * one is completely unscheduled.
11960 *
11961 * @param who The Drawable to unschedule.
11962 *
11963 * @see #drawableStateChanged
11964 */
11965 public void unscheduleDrawable(Drawable who) {
11966 if (mAttachInfo != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080011967 mAttachInfo.mViewRootImpl.mChoreographer.removeAnimationCallbacks(null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011968 }
11969 }
11970
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011971 /**
11972 * Return the layout direction of a given Drawable.
11973 *
11974 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011975 */
11976 public int getResolvedLayoutDirection(Drawable who) {
11977 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070011978 }
11979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011980 /**
11981 * If your view subclass is displaying its own Drawable objects, it should
11982 * override this function and return true for any Drawable it is
11983 * displaying. This allows animations for those drawables to be
11984 * scheduled.
11985 *
11986 * <p>Be sure to call through to the super class when overriding this
11987 * function.
11988 *
11989 * @param who The Drawable to verify. Return true if it is one you are
11990 * displaying, else return the result of calling through to the
11991 * super class.
11992 *
11993 * @return boolean If true than the Drawable is being displayed in the
11994 * view; else false and it is not allowed to animate.
11995 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011996 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
11997 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011998 */
11999 protected boolean verifyDrawable(Drawable who) {
12000 return who == mBGDrawable;
12001 }
12002
12003 /**
12004 * This function is called whenever the state of the view changes in such
12005 * a way that it impacts the state of drawables being shown.
12006 *
12007 * <p>Be sure to call through to the superclass when overriding this
12008 * function.
12009 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012010 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012011 */
12012 protected void drawableStateChanged() {
12013 Drawable d = mBGDrawable;
12014 if (d != null && d.isStateful()) {
12015 d.setState(getDrawableState());
12016 }
12017 }
12018
12019 /**
12020 * Call this to force a view to update its drawable state. This will cause
12021 * drawableStateChanged to be called on this view. Views that are interested
12022 * in the new state should call getDrawableState.
12023 *
12024 * @see #drawableStateChanged
12025 * @see #getDrawableState
12026 */
12027 public void refreshDrawableState() {
12028 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
12029 drawableStateChanged();
12030
12031 ViewParent parent = mParent;
12032 if (parent != null) {
12033 parent.childDrawableStateChanged(this);
12034 }
12035 }
12036
12037 /**
12038 * Return an array of resource IDs of the drawable states representing the
12039 * current state of the view.
12040 *
12041 * @return The current drawable state
12042 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012043 * @see Drawable#setState(int[])
12044 * @see #drawableStateChanged()
12045 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012046 */
12047 public final int[] getDrawableState() {
12048 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
12049 return mDrawableState;
12050 } else {
12051 mDrawableState = onCreateDrawableState(0);
12052 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
12053 return mDrawableState;
12054 }
12055 }
12056
12057 /**
12058 * Generate the new {@link android.graphics.drawable.Drawable} state for
12059 * this view. This is called by the view
12060 * system when the cached Drawable state is determined to be invalid. To
12061 * retrieve the current state, you should use {@link #getDrawableState}.
12062 *
12063 * @param extraSpace if non-zero, this is the number of extra entries you
12064 * would like in the returned array in which you can place your own
12065 * states.
12066 *
12067 * @return Returns an array holding the current {@link Drawable} state of
12068 * the view.
12069 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012070 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012071 */
12072 protected int[] onCreateDrawableState(int extraSpace) {
12073 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
12074 mParent instanceof View) {
12075 return ((View) mParent).onCreateDrawableState(extraSpace);
12076 }
12077
12078 int[] drawableState;
12079
12080 int privateFlags = mPrivateFlags;
12081
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012082 int viewStateIndex = 0;
12083 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
12084 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
12085 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070012086 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012087 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
12088 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070012089 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
12090 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012091 // This is set if HW acceleration is requested, even if the current
12092 // process doesn't allow it. This is just to allow app preview
12093 // windows to better match their app.
12094 viewStateIndex |= VIEW_STATE_ACCELERATED;
12095 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070012096 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012097
Christopher Tate3d4bf172011-03-28 16:16:46 -070012098 final int privateFlags2 = mPrivateFlags2;
12099 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
12100 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
12101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012102 drawableState = VIEW_STATE_SETS[viewStateIndex];
12103
12104 //noinspection ConstantIfStatement
12105 if (false) {
12106 Log.i("View", "drawableStateIndex=" + viewStateIndex);
12107 Log.i("View", toString()
12108 + " pressed=" + ((privateFlags & PRESSED) != 0)
12109 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
12110 + " fo=" + hasFocus()
12111 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012112 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012113 + ": " + Arrays.toString(drawableState));
12114 }
12115
12116 if (extraSpace == 0) {
12117 return drawableState;
12118 }
12119
12120 final int[] fullState;
12121 if (drawableState != null) {
12122 fullState = new int[drawableState.length + extraSpace];
12123 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
12124 } else {
12125 fullState = new int[extraSpace];
12126 }
12127
12128 return fullState;
12129 }
12130
12131 /**
12132 * Merge your own state values in <var>additionalState</var> into the base
12133 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012134 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012135 *
12136 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012137 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012138 * own additional state values.
12139 *
12140 * @param additionalState The additional state values you would like
12141 * added to <var>baseState</var>; this array is not modified.
12142 *
12143 * @return As a convenience, the <var>baseState</var> array you originally
12144 * passed into the function is returned.
12145 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012146 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012147 */
12148 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
12149 final int N = baseState.length;
12150 int i = N - 1;
12151 while (i >= 0 && baseState[i] == 0) {
12152 i--;
12153 }
12154 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
12155 return baseState;
12156 }
12157
12158 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070012159 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
12160 * on all Drawable objects associated with this view.
12161 */
12162 public void jumpDrawablesToCurrentState() {
12163 if (mBGDrawable != null) {
12164 mBGDrawable.jumpToCurrentState();
12165 }
12166 }
12167
12168 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012169 * Sets the background color for this view.
12170 * @param color the color of the background
12171 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012172 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012173 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070012174 if (mBGDrawable instanceof ColorDrawable) {
12175 ((ColorDrawable) mBGDrawable).setColor(color);
12176 } else {
12177 setBackgroundDrawable(new ColorDrawable(color));
12178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012179 }
12180
12181 /**
12182 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070012183 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012184 * @param resid The identifier of the resource.
12185 * @attr ref android.R.styleable#View_background
12186 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012187 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012188 public void setBackgroundResource(int resid) {
12189 if (resid != 0 && resid == mBackgroundResource) {
12190 return;
12191 }
12192
12193 Drawable d= null;
12194 if (resid != 0) {
12195 d = mResources.getDrawable(resid);
12196 }
12197 setBackgroundDrawable(d);
12198
12199 mBackgroundResource = resid;
12200 }
12201
12202 /**
12203 * Set the background to a given Drawable, or remove the background. If the
12204 * background has padding, this View's padding is set to the background's
12205 * padding. However, when a background is removed, this View's padding isn't
12206 * touched. If setting the padding is desired, please use
12207 * {@link #setPadding(int, int, int, int)}.
12208 *
12209 * @param d The Drawable to use as the background, or null to remove the
12210 * background
12211 */
12212 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070012213 if (d == mBGDrawable) {
12214 return;
12215 }
12216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012217 boolean requestLayout = false;
12218
12219 mBackgroundResource = 0;
12220
12221 /*
12222 * Regardless of whether we're setting a new background or not, we want
12223 * to clear the previous drawable.
12224 */
12225 if (mBGDrawable != null) {
12226 mBGDrawable.setCallback(null);
12227 unscheduleDrawable(mBGDrawable);
12228 }
12229
12230 if (d != null) {
12231 Rect padding = sThreadLocal.get();
12232 if (padding == null) {
12233 padding = new Rect();
12234 sThreadLocal.set(padding);
12235 }
12236 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012237 switch (d.getResolvedLayoutDirectionSelf()) {
12238 case LAYOUT_DIRECTION_RTL:
12239 setPadding(padding.right, padding.top, padding.left, padding.bottom);
12240 break;
12241 case LAYOUT_DIRECTION_LTR:
12242 default:
12243 setPadding(padding.left, padding.top, padding.right, padding.bottom);
12244 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012245 }
12246
12247 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
12248 // if it has a different minimum size, we should layout again
12249 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
12250 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
12251 requestLayout = true;
12252 }
12253
12254 d.setCallback(this);
12255 if (d.isStateful()) {
12256 d.setState(getDrawableState());
12257 }
12258 d.setVisible(getVisibility() == VISIBLE, false);
12259 mBGDrawable = d;
12260
12261 if ((mPrivateFlags & SKIP_DRAW) != 0) {
12262 mPrivateFlags &= ~SKIP_DRAW;
12263 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
12264 requestLayout = true;
12265 }
12266 } else {
12267 /* Remove the background */
12268 mBGDrawable = null;
12269
12270 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
12271 /*
12272 * This view ONLY drew the background before and we're removing
12273 * the background, so now it won't draw anything
12274 * (hence we SKIP_DRAW)
12275 */
12276 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
12277 mPrivateFlags |= SKIP_DRAW;
12278 }
12279
12280 /*
12281 * When the background is set, we try to apply its padding to this
12282 * View. When the background is removed, we don't touch this View's
12283 * padding. This is noted in the Javadocs. Hence, we don't need to
12284 * requestLayout(), the invalidate() below is sufficient.
12285 */
12286
12287 // The old background's minimum size could have affected this
12288 // View's layout, so let's requestLayout
12289 requestLayout = true;
12290 }
12291
Romain Guy8f1344f52009-05-15 16:03:59 -070012292 computeOpaqueFlags();
12293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012294 if (requestLayout) {
12295 requestLayout();
12296 }
12297
12298 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012299 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012300 }
12301
12302 /**
12303 * Gets the background drawable
12304 * @return The drawable used as the background for this view, if any.
12305 */
12306 public Drawable getBackground() {
12307 return mBGDrawable;
12308 }
12309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012310 /**
12311 * Sets the padding. The view may add on the space required to display
12312 * the scrollbars, depending on the style and visibility of the scrollbars.
12313 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
12314 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
12315 * from the values set in this call.
12316 *
12317 * @attr ref android.R.styleable#View_padding
12318 * @attr ref android.R.styleable#View_paddingBottom
12319 * @attr ref android.R.styleable#View_paddingLeft
12320 * @attr ref android.R.styleable#View_paddingRight
12321 * @attr ref android.R.styleable#View_paddingTop
12322 * @param left the left padding in pixels
12323 * @param top the top padding in pixels
12324 * @param right the right padding in pixels
12325 * @param bottom the bottom padding in pixels
12326 */
12327 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012328 mUserPaddingStart = -1;
12329 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012330 mUserPaddingRelative = false;
12331
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012332 internalSetPadding(left, top, right, bottom);
12333 }
12334
12335 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080012336 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012337 mUserPaddingRight = right;
12338 mUserPaddingBottom = bottom;
12339
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012340 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012341 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070012342
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012343 // Common case is there are no scroll bars.
12344 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012345 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080012346 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012347 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080012348 switch (mVerticalScrollbarPosition) {
12349 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012350 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
12351 left += offset;
12352 } else {
12353 right += offset;
12354 }
12355 break;
Adam Powell20232d02010-12-08 21:08:53 -080012356 case SCROLLBAR_POSITION_RIGHT:
12357 right += offset;
12358 break;
12359 case SCROLLBAR_POSITION_LEFT:
12360 left += offset;
12361 break;
12362 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012363 }
Adam Powell20232d02010-12-08 21:08:53 -080012364 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012365 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
12366 ? 0 : getHorizontalScrollbarHeight();
12367 }
12368 }
Romain Guy8506ab42009-06-11 17:35:47 -070012369
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012370 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012371 changed = true;
12372 mPaddingLeft = left;
12373 }
12374 if (mPaddingTop != top) {
12375 changed = true;
12376 mPaddingTop = top;
12377 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012378 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012379 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012380 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012381 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012382 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012383 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012384 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012385 }
12386
12387 if (changed) {
12388 requestLayout();
12389 }
12390 }
12391
12392 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012393 * Sets the relative padding. The view may add on the space required to display
12394 * the scrollbars, depending on the style and visibility of the scrollbars.
12395 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
12396 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
12397 * from the values set in this call.
12398 *
12399 * @attr ref android.R.styleable#View_padding
12400 * @attr ref android.R.styleable#View_paddingBottom
12401 * @attr ref android.R.styleable#View_paddingStart
12402 * @attr ref android.R.styleable#View_paddingEnd
12403 * @attr ref android.R.styleable#View_paddingTop
12404 * @param start the start padding in pixels
12405 * @param top the top padding in pixels
12406 * @param end the end padding in pixels
12407 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012408 */
12409 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070012410 mUserPaddingStart = start;
12411 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012412 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070012413
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012414 switch(getResolvedLayoutDirection()) {
12415 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012416 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012417 break;
12418 case LAYOUT_DIRECTION_LTR:
12419 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012420 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012421 }
12422 }
12423
12424 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012425 * Returns the top padding of this view.
12426 *
12427 * @return the top padding in pixels
12428 */
12429 public int getPaddingTop() {
12430 return mPaddingTop;
12431 }
12432
12433 /**
12434 * Returns the bottom padding of this view. If there are inset and enabled
12435 * scrollbars, this value may include the space required to display the
12436 * scrollbars as well.
12437 *
12438 * @return the bottom padding in pixels
12439 */
12440 public int getPaddingBottom() {
12441 return mPaddingBottom;
12442 }
12443
12444 /**
12445 * Returns the left padding of this view. If there are inset and enabled
12446 * scrollbars, this value may include the space required to display the
12447 * scrollbars as well.
12448 *
12449 * @return the left padding in pixels
12450 */
12451 public int getPaddingLeft() {
12452 return mPaddingLeft;
12453 }
12454
12455 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012456 * Returns the start padding of this view. If there are inset and enabled
12457 * scrollbars, this value may include the space required to display the
12458 * scrollbars as well.
12459 *
12460 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012461 */
12462 public int getPaddingStart() {
12463 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12464 mPaddingRight : mPaddingLeft;
12465 }
12466
12467 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012468 * Returns the right padding of this view. If there are inset and enabled
12469 * scrollbars, this value may include the space required to display the
12470 * scrollbars as well.
12471 *
12472 * @return the right padding in pixels
12473 */
12474 public int getPaddingRight() {
12475 return mPaddingRight;
12476 }
12477
12478 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012479 * Returns the end padding of this view. If there are inset and enabled
12480 * scrollbars, this value may include the space required to display the
12481 * scrollbars as well.
12482 *
12483 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012484 */
12485 public int getPaddingEnd() {
12486 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12487 mPaddingLeft : mPaddingRight;
12488 }
12489
12490 /**
12491 * Return if the padding as been set thru relative values
12492 * {@link #setPaddingRelative(int, int, int, int)} or thru
12493 * @attr ref android.R.styleable#View_paddingStart or
12494 * @attr ref android.R.styleable#View_paddingEnd
12495 *
12496 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012497 */
12498 public boolean isPaddingRelative() {
12499 return mUserPaddingRelative;
12500 }
12501
12502 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012503 * Changes the selection state of this view. A view can be selected or not.
12504 * Note that selection is not the same as focus. Views are typically
12505 * selected in the context of an AdapterView like ListView or GridView;
12506 * the selected view is the view that is highlighted.
12507 *
12508 * @param selected true if the view must be selected, false otherwise
12509 */
12510 public void setSelected(boolean selected) {
12511 if (((mPrivateFlags & SELECTED) != 0) != selected) {
12512 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070012513 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080012514 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012515 refreshDrawableState();
12516 dispatchSetSelected(selected);
12517 }
12518 }
12519
12520 /**
12521 * Dispatch setSelected to all of this View's children.
12522 *
12523 * @see #setSelected(boolean)
12524 *
12525 * @param selected The new selected state
12526 */
12527 protected void dispatchSetSelected(boolean selected) {
12528 }
12529
12530 /**
12531 * Indicates the selection state of this view.
12532 *
12533 * @return true if the view is selected, false otherwise
12534 */
12535 @ViewDebug.ExportedProperty
12536 public boolean isSelected() {
12537 return (mPrivateFlags & SELECTED) != 0;
12538 }
12539
12540 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012541 * Changes the activated state of this view. A view can be activated or not.
12542 * Note that activation is not the same as selection. Selection is
12543 * a transient property, representing the view (hierarchy) the user is
12544 * currently interacting with. Activation is a longer-term state that the
12545 * user can move views in and out of. For example, in a list view with
12546 * single or multiple selection enabled, the views in the current selection
12547 * set are activated. (Um, yeah, we are deeply sorry about the terminology
12548 * here.) The activated state is propagated down to children of the view it
12549 * is set on.
12550 *
12551 * @param activated true if the view must be activated, false otherwise
12552 */
12553 public void setActivated(boolean activated) {
12554 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
12555 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012556 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012557 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070012558 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012559 }
12560 }
12561
12562 /**
12563 * Dispatch setActivated to all of this View's children.
12564 *
12565 * @see #setActivated(boolean)
12566 *
12567 * @param activated The new activated state
12568 */
12569 protected void dispatchSetActivated(boolean activated) {
12570 }
12571
12572 /**
12573 * Indicates the activation state of this view.
12574 *
12575 * @return true if the view is activated, false otherwise
12576 */
12577 @ViewDebug.ExportedProperty
12578 public boolean isActivated() {
12579 return (mPrivateFlags & ACTIVATED) != 0;
12580 }
12581
12582 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012583 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
12584 * observer can be used to get notifications when global events, like
12585 * layout, happen.
12586 *
12587 * The returned ViewTreeObserver observer is not guaranteed to remain
12588 * valid for the lifetime of this View. If the caller of this method keeps
12589 * a long-lived reference to ViewTreeObserver, it should always check for
12590 * the return value of {@link ViewTreeObserver#isAlive()}.
12591 *
12592 * @return The ViewTreeObserver for this view's hierarchy.
12593 */
12594 public ViewTreeObserver getViewTreeObserver() {
12595 if (mAttachInfo != null) {
12596 return mAttachInfo.mTreeObserver;
12597 }
12598 if (mFloatingTreeObserver == null) {
12599 mFloatingTreeObserver = new ViewTreeObserver();
12600 }
12601 return mFloatingTreeObserver;
12602 }
12603
12604 /**
12605 * <p>Finds the topmost view in the current view hierarchy.</p>
12606 *
12607 * @return the topmost view containing this view
12608 */
12609 public View getRootView() {
12610 if (mAttachInfo != null) {
12611 final View v = mAttachInfo.mRootView;
12612 if (v != null) {
12613 return v;
12614 }
12615 }
Romain Guy8506ab42009-06-11 17:35:47 -070012616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012617 View parent = this;
12618
12619 while (parent.mParent != null && parent.mParent instanceof View) {
12620 parent = (View) parent.mParent;
12621 }
12622
12623 return parent;
12624 }
12625
12626 /**
12627 * <p>Computes the coordinates of this view on the screen. The argument
12628 * must be an array of two integers. After the method returns, the array
12629 * contains the x and y location in that order.</p>
12630 *
12631 * @param location an array of two integers in which to hold the coordinates
12632 */
12633 public void getLocationOnScreen(int[] location) {
12634 getLocationInWindow(location);
12635
12636 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070012637 if (info != null) {
12638 location[0] += info.mWindowLeft;
12639 location[1] += info.mWindowTop;
12640 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012641 }
12642
12643 /**
12644 * <p>Computes the coordinates of this view in its window. The argument
12645 * must be an array of two integers. After the method returns, the array
12646 * contains the x and y location in that order.</p>
12647 *
12648 * @param location an array of two integers in which to hold the coordinates
12649 */
12650 public void getLocationInWindow(int[] location) {
12651 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012652 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012653 }
12654
Gilles Debunne6583ce52011-12-06 18:09:02 -080012655 if (mAttachInfo == null) {
12656 // When the view is not attached to a window, this method does not make sense
12657 location[0] = location[1] = 0;
12658 return;
12659 }
12660
Gilles Debunnecea45132011-11-24 02:19:27 +010012661 float[] position = mAttachInfo.mTmpTransformLocation;
12662 position[0] = position[1] = 0.0f;
12663
12664 if (!hasIdentityMatrix()) {
12665 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012666 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012667
Gilles Debunnecea45132011-11-24 02:19:27 +010012668 position[0] += mLeft;
12669 position[1] += mTop;
12670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012671 ViewParent viewParent = mParent;
12672 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012673 final View view = (View) viewParent;
12674
12675 position[0] -= view.mScrollX;
12676 position[1] -= view.mScrollY;
12677
12678 if (!view.hasIdentityMatrix()) {
12679 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012680 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012681
12682 position[0] += view.mLeft;
12683 position[1] += view.mTop;
12684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012685 viewParent = view.mParent;
12686 }
Romain Guy8506ab42009-06-11 17:35:47 -070012687
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012688 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012689 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010012690 final ViewRootImpl vr = (ViewRootImpl) viewParent;
12691 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012692 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012693
12694 location[0] = (int) (position[0] + 0.5f);
12695 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012696 }
12697
12698 /**
12699 * {@hide}
12700 * @param id the id of the view to be found
12701 * @return the view of the specified id, null if cannot be found
12702 */
12703 protected View findViewTraversal(int id) {
12704 if (id == mID) {
12705 return this;
12706 }
12707 return null;
12708 }
12709
12710 /**
12711 * {@hide}
12712 * @param tag the tag of the view to be found
12713 * @return the view of specified tag, null if cannot be found
12714 */
12715 protected View findViewWithTagTraversal(Object tag) {
12716 if (tag != null && tag.equals(mTag)) {
12717 return this;
12718 }
12719 return null;
12720 }
12721
12722 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012723 * {@hide}
12724 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070012725 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080012726 * @return The first view that matches the predicate or null.
12727 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070012728 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080012729 if (predicate.apply(this)) {
12730 return this;
12731 }
12732 return null;
12733 }
12734
12735 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012736 * Look for a child view with the given id. If this view has the given
12737 * id, return this view.
12738 *
12739 * @param id The id to search for.
12740 * @return The view that has the given id in the hierarchy or null
12741 */
12742 public final View findViewById(int id) {
12743 if (id < 0) {
12744 return null;
12745 }
12746 return findViewTraversal(id);
12747 }
12748
12749 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070012750 * Finds a view by its unuque and stable accessibility id.
12751 *
12752 * @param accessibilityId The searched accessibility id.
12753 * @return The found view.
12754 */
12755 final View findViewByAccessibilityId(int accessibilityId) {
12756 if (accessibilityId < 0) {
12757 return null;
12758 }
12759 return findViewByAccessibilityIdTraversal(accessibilityId);
12760 }
12761
12762 /**
12763 * Performs the traversal to find a view by its unuque and stable accessibility id.
12764 *
12765 * <strong>Note:</strong>This method does not stop at the root namespace
12766 * boundary since the user can touch the screen at an arbitrary location
12767 * potentially crossing the root namespace bounday which will send an
12768 * accessibility event to accessibility services and they should be able
12769 * to obtain the event source. Also accessibility ids are guaranteed to be
12770 * unique in the window.
12771 *
12772 * @param accessibilityId The accessibility id.
12773 * @return The found view.
12774 */
12775 View findViewByAccessibilityIdTraversal(int accessibilityId) {
12776 if (getAccessibilityViewId() == accessibilityId) {
12777 return this;
12778 }
12779 return null;
12780 }
12781
12782 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012783 * Look for a child view with the given tag. If this view has the given
12784 * tag, return this view.
12785 *
12786 * @param tag The tag to search for, using "tag.equals(getTag())".
12787 * @return The View that has the given tag in the hierarchy or null
12788 */
12789 public final View findViewWithTag(Object tag) {
12790 if (tag == null) {
12791 return null;
12792 }
12793 return findViewWithTagTraversal(tag);
12794 }
12795
12796 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012797 * {@hide}
12798 * Look for a child view that matches the specified predicate.
12799 * If this view matches the predicate, return this view.
12800 *
12801 * @param predicate The predicate to evaluate.
12802 * @return The first view that matches the predicate or null.
12803 */
12804 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070012805 return findViewByPredicateTraversal(predicate, null);
12806 }
12807
12808 /**
12809 * {@hide}
12810 * Look for a child view that matches the specified predicate,
12811 * starting with the specified view and its descendents and then
12812 * recusively searching the ancestors and siblings of that view
12813 * until this view is reached.
12814 *
12815 * This method is useful in cases where the predicate does not match
12816 * a single unique view (perhaps multiple views use the same id)
12817 * and we are trying to find the view that is "closest" in scope to the
12818 * starting view.
12819 *
12820 * @param start The view to start from.
12821 * @param predicate The predicate to evaluate.
12822 * @return The first view that matches the predicate or null.
12823 */
12824 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
12825 View childToSkip = null;
12826 for (;;) {
12827 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
12828 if (view != null || start == this) {
12829 return view;
12830 }
12831
12832 ViewParent parent = start.getParent();
12833 if (parent == null || !(parent instanceof View)) {
12834 return null;
12835 }
12836
12837 childToSkip = start;
12838 start = (View) parent;
12839 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080012840 }
12841
12842 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012843 * Sets the identifier for this view. The identifier does not have to be
12844 * unique in this view's hierarchy. The identifier should be a positive
12845 * number.
12846 *
12847 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070012848 * @see #getId()
12849 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012850 *
12851 * @param id a number used to identify the view
12852 *
12853 * @attr ref android.R.styleable#View_id
12854 */
12855 public void setId(int id) {
12856 mID = id;
12857 }
12858
12859 /**
12860 * {@hide}
12861 *
12862 * @param isRoot true if the view belongs to the root namespace, false
12863 * otherwise
12864 */
12865 public void setIsRootNamespace(boolean isRoot) {
12866 if (isRoot) {
12867 mPrivateFlags |= IS_ROOT_NAMESPACE;
12868 } else {
12869 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
12870 }
12871 }
12872
12873 /**
12874 * {@hide}
12875 *
12876 * @return true if the view belongs to the root namespace, false otherwise
12877 */
12878 public boolean isRootNamespace() {
12879 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
12880 }
12881
12882 /**
12883 * Returns this view's identifier.
12884 *
12885 * @return a positive integer used to identify the view or {@link #NO_ID}
12886 * if the view has no ID
12887 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012888 * @see #setId(int)
12889 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012890 * @attr ref android.R.styleable#View_id
12891 */
12892 @ViewDebug.CapturedViewProperty
12893 public int getId() {
12894 return mID;
12895 }
12896
12897 /**
12898 * Returns this view's tag.
12899 *
12900 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070012901 *
12902 * @see #setTag(Object)
12903 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012904 */
12905 @ViewDebug.ExportedProperty
12906 public Object getTag() {
12907 return mTag;
12908 }
12909
12910 /**
12911 * Sets the tag associated with this view. A tag can be used to mark
12912 * a view in its hierarchy and does not have to be unique within the
12913 * hierarchy. Tags can also be used to store data within a view without
12914 * resorting to another data structure.
12915 *
12916 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070012917 *
12918 * @see #getTag()
12919 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012920 */
12921 public void setTag(final Object tag) {
12922 mTag = tag;
12923 }
12924
12925 /**
Romain Guyd90a3312009-05-06 14:54:28 -070012926 * Returns the tag associated with this view and the specified key.
12927 *
12928 * @param key The key identifying the tag
12929 *
12930 * @return the Object stored in this view as a tag
12931 *
12932 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070012933 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070012934 */
12935 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012936 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070012937 return null;
12938 }
12939
12940 /**
12941 * Sets a tag associated with this view and a key. A tag can be used
12942 * to mark a view in its hierarchy and does not have to be unique within
12943 * the hierarchy. Tags can also be used to store data within a view
12944 * without resorting to another data structure.
12945 *
12946 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070012947 * application to ensure it is unique (see the <a
12948 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
12949 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070012950 * the Android framework or not associated with any package will cause
12951 * an {@link IllegalArgumentException} to be thrown.
12952 *
12953 * @param key The key identifying the tag
12954 * @param tag An Object to tag the view with
12955 *
12956 * @throws IllegalArgumentException If they specified key is not valid
12957 *
12958 * @see #setTag(Object)
12959 * @see #getTag(int)
12960 */
12961 public void setTag(int key, final Object tag) {
12962 // If the package id is 0x00 or 0x01, it's either an undefined package
12963 // or a framework id
12964 if ((key >>> 24) < 2) {
12965 throw new IllegalArgumentException("The key must be an application-specific "
12966 + "resource id.");
12967 }
12968
Adam Powell2b2f6d62011-09-23 11:15:39 -070012969 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012970 }
12971
12972 /**
12973 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
12974 * framework id.
12975 *
12976 * @hide
12977 */
12978 public void setTagInternal(int key, Object tag) {
12979 if ((key >>> 24) != 0x1) {
12980 throw new IllegalArgumentException("The key must be a framework-specific "
12981 + "resource id.");
12982 }
12983
Adam Powell2b2f6d62011-09-23 11:15:39 -070012984 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012985 }
12986
Adam Powell2b2f6d62011-09-23 11:15:39 -070012987 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012988 if (mKeyedTags == null) {
12989 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070012990 }
12991
Adam Powell7db82ac2011-09-22 19:44:04 -070012992 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012993 }
12994
12995 /**
Romain Guy13922e02009-05-12 17:56:14 -070012996 * @param consistency The type of consistency. See ViewDebug for more information.
12997 *
12998 * @hide
12999 */
13000 protected boolean dispatchConsistencyCheck(int consistency) {
13001 return onConsistencyCheck(consistency);
13002 }
13003
13004 /**
13005 * Method that subclasses should implement to check their consistency. The type of
13006 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070013007 *
Romain Guy13922e02009-05-12 17:56:14 -070013008 * @param consistency The type of consistency. See ViewDebug for more information.
13009 *
13010 * @throws IllegalStateException if the view is in an inconsistent state.
13011 *
13012 * @hide
13013 */
13014 protected boolean onConsistencyCheck(int consistency) {
13015 boolean result = true;
13016
13017 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
13018 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
13019
13020 if (checkLayout) {
13021 if (getParent() == null) {
13022 result = false;
13023 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13024 "View " + this + " does not have a parent.");
13025 }
13026
13027 if (mAttachInfo == null) {
13028 result = false;
13029 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13030 "View " + this + " is not attached to a window.");
13031 }
13032 }
13033
13034 if (checkDrawing) {
13035 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
13036 // from their draw() method
13037
13038 if ((mPrivateFlags & DRAWN) != DRAWN &&
13039 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
13040 result = false;
13041 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13042 "View " + this + " was invalidated but its drawing cache is valid.");
13043 }
13044 }
13045
13046 return result;
13047 }
13048
13049 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013050 * Prints information about this view in the log output, with the tag
13051 * {@link #VIEW_LOG_TAG}.
13052 *
13053 * @hide
13054 */
13055 public void debug() {
13056 debug(0);
13057 }
13058
13059 /**
13060 * Prints information about this view in the log output, with the tag
13061 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
13062 * indentation defined by the <code>depth</code>.
13063 *
13064 * @param depth the indentation level
13065 *
13066 * @hide
13067 */
13068 protected void debug(int depth) {
13069 String output = debugIndent(depth - 1);
13070
13071 output += "+ " + this;
13072 int id = getId();
13073 if (id != -1) {
13074 output += " (id=" + id + ")";
13075 }
13076 Object tag = getTag();
13077 if (tag != null) {
13078 output += " (tag=" + tag + ")";
13079 }
13080 Log.d(VIEW_LOG_TAG, output);
13081
13082 if ((mPrivateFlags & FOCUSED) != 0) {
13083 output = debugIndent(depth) + " FOCUSED";
13084 Log.d(VIEW_LOG_TAG, output);
13085 }
13086
13087 output = debugIndent(depth);
13088 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
13089 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
13090 + "} ";
13091 Log.d(VIEW_LOG_TAG, output);
13092
13093 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
13094 || mPaddingBottom != 0) {
13095 output = debugIndent(depth);
13096 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
13097 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
13098 Log.d(VIEW_LOG_TAG, output);
13099 }
13100
13101 output = debugIndent(depth);
13102 output += "mMeasureWidth=" + mMeasuredWidth +
13103 " mMeasureHeight=" + mMeasuredHeight;
13104 Log.d(VIEW_LOG_TAG, output);
13105
13106 output = debugIndent(depth);
13107 if (mLayoutParams == null) {
13108 output += "BAD! no layout params";
13109 } else {
13110 output = mLayoutParams.debug(output);
13111 }
13112 Log.d(VIEW_LOG_TAG, output);
13113
13114 output = debugIndent(depth);
13115 output += "flags={";
13116 output += View.printFlags(mViewFlags);
13117 output += "}";
13118 Log.d(VIEW_LOG_TAG, output);
13119
13120 output = debugIndent(depth);
13121 output += "privateFlags={";
13122 output += View.printPrivateFlags(mPrivateFlags);
13123 output += "}";
13124 Log.d(VIEW_LOG_TAG, output);
13125 }
13126
13127 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013128 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013129 *
13130 * @param depth the indentation level
13131 * @return a String containing (depth * 2 + 3) * 2 white spaces
13132 *
13133 * @hide
13134 */
13135 protected static String debugIndent(int depth) {
13136 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
13137 for (int i = 0; i < (depth * 2) + 3; i++) {
13138 spaces.append(' ').append(' ');
13139 }
13140 return spaces.toString();
13141 }
13142
13143 /**
13144 * <p>Return the offset of the widget's text baseline from the widget's top
13145 * boundary. If this widget does not support baseline alignment, this
13146 * method returns -1. </p>
13147 *
13148 * @return the offset of the baseline within the widget's bounds or -1
13149 * if baseline alignment is not supported
13150 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070013151 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013152 public int getBaseline() {
13153 return -1;
13154 }
13155
13156 /**
13157 * Call this when something has changed which has invalidated the
13158 * layout of this view. This will schedule a layout pass of the view
13159 * tree.
13160 */
13161 public void requestLayout() {
13162 if (ViewDebug.TRACE_HIERARCHY) {
13163 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
13164 }
13165
13166 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013167 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013168
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070013169 if (mParent != null) {
13170 if (mLayoutParams != null) {
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080013171 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070013172 }
13173 if (!mParent.isLayoutRequested()) {
13174 mParent.requestLayout();
13175 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013176 }
13177 }
13178
13179 /**
13180 * Forces this view to be laid out during the next layout pass.
13181 * This method does not call requestLayout() or forceLayout()
13182 * on the parent.
13183 */
13184 public void forceLayout() {
13185 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013186 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013187 }
13188
13189 /**
13190 * <p>
13191 * This is called to find out how big a view should be. The parent
13192 * supplies constraint information in the width and height parameters.
13193 * </p>
13194 *
13195 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080013196 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013197 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080013198 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013199 * </p>
13200 *
13201 *
13202 * @param widthMeasureSpec Horizontal space requirements as imposed by the
13203 * parent
13204 * @param heightMeasureSpec Vertical space requirements as imposed by the
13205 * parent
13206 *
13207 * @see #onMeasure(int, int)
13208 */
13209 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
13210 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
13211 widthMeasureSpec != mOldWidthMeasureSpec ||
13212 heightMeasureSpec != mOldHeightMeasureSpec) {
13213
13214 // first clears the measured dimension flag
13215 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
13216
13217 if (ViewDebug.TRACE_HIERARCHY) {
13218 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
13219 }
13220
13221 // measure ourselves, this should set the measured dimension flag back
13222 onMeasure(widthMeasureSpec, heightMeasureSpec);
13223
13224 // flag not set, setMeasuredDimension() was not invoked, we raise
13225 // an exception to warn the developer
13226 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
13227 throw new IllegalStateException("onMeasure() did not set the"
13228 + " measured dimension by calling"
13229 + " setMeasuredDimension()");
13230 }
13231
13232 mPrivateFlags |= LAYOUT_REQUIRED;
13233 }
13234
13235 mOldWidthMeasureSpec = widthMeasureSpec;
13236 mOldHeightMeasureSpec = heightMeasureSpec;
13237 }
13238
13239 /**
13240 * <p>
13241 * Measure the view and its content to determine the measured width and the
13242 * measured height. This method is invoked by {@link #measure(int, int)} and
13243 * should be overriden by subclasses to provide accurate and efficient
13244 * measurement of their contents.
13245 * </p>
13246 *
13247 * <p>
13248 * <strong>CONTRACT:</strong> When overriding this method, you
13249 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
13250 * measured width and height of this view. Failure to do so will trigger an
13251 * <code>IllegalStateException</code>, thrown by
13252 * {@link #measure(int, int)}. Calling the superclass'
13253 * {@link #onMeasure(int, int)} is a valid use.
13254 * </p>
13255 *
13256 * <p>
13257 * The base class implementation of measure defaults to the background size,
13258 * unless a larger size is allowed by the MeasureSpec. Subclasses should
13259 * override {@link #onMeasure(int, int)} to provide better measurements of
13260 * their content.
13261 * </p>
13262 *
13263 * <p>
13264 * If this method is overridden, it is the subclass's responsibility to make
13265 * sure the measured height and width are at least the view's minimum height
13266 * and width ({@link #getSuggestedMinimumHeight()} and
13267 * {@link #getSuggestedMinimumWidth()}).
13268 * </p>
13269 *
13270 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
13271 * The requirements are encoded with
13272 * {@link android.view.View.MeasureSpec}.
13273 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
13274 * The requirements are encoded with
13275 * {@link android.view.View.MeasureSpec}.
13276 *
13277 * @see #getMeasuredWidth()
13278 * @see #getMeasuredHeight()
13279 * @see #setMeasuredDimension(int, int)
13280 * @see #getSuggestedMinimumHeight()
13281 * @see #getSuggestedMinimumWidth()
13282 * @see android.view.View.MeasureSpec#getMode(int)
13283 * @see android.view.View.MeasureSpec#getSize(int)
13284 */
13285 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
13286 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
13287 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
13288 }
13289
13290 /**
13291 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
13292 * measured width and measured height. Failing to do so will trigger an
13293 * exception at measurement time.</p>
13294 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080013295 * @param measuredWidth The measured width of this view. May be a complex
13296 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13297 * {@link #MEASURED_STATE_TOO_SMALL}.
13298 * @param measuredHeight The measured height of this view. May be a complex
13299 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13300 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013301 */
13302 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
13303 mMeasuredWidth = measuredWidth;
13304 mMeasuredHeight = measuredHeight;
13305
13306 mPrivateFlags |= MEASURED_DIMENSION_SET;
13307 }
13308
13309 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080013310 * Merge two states as returned by {@link #getMeasuredState()}.
13311 * @param curState The current state as returned from a view or the result
13312 * of combining multiple views.
13313 * @param newState The new view state to combine.
13314 * @return Returns a new integer reflecting the combination of the two
13315 * states.
13316 */
13317 public static int combineMeasuredStates(int curState, int newState) {
13318 return curState | newState;
13319 }
13320
13321 /**
13322 * Version of {@link #resolveSizeAndState(int, int, int)}
13323 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
13324 */
13325 public static int resolveSize(int size, int measureSpec) {
13326 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
13327 }
13328
13329 /**
13330 * Utility to reconcile a desired size and state, with constraints imposed
13331 * by a MeasureSpec. Will take the desired size, unless a different size
13332 * is imposed by the constraints. The returned value is a compound integer,
13333 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
13334 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
13335 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013336 *
13337 * @param size How big the view wants to be
13338 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080013339 * @return Size information bit mask as defined by
13340 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013341 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080013342 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013343 int result = size;
13344 int specMode = MeasureSpec.getMode(measureSpec);
13345 int specSize = MeasureSpec.getSize(measureSpec);
13346 switch (specMode) {
13347 case MeasureSpec.UNSPECIFIED:
13348 result = size;
13349 break;
13350 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080013351 if (specSize < size) {
13352 result = specSize | MEASURED_STATE_TOO_SMALL;
13353 } else {
13354 result = size;
13355 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013356 break;
13357 case MeasureSpec.EXACTLY:
13358 result = specSize;
13359 break;
13360 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080013361 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013362 }
13363
13364 /**
13365 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070013366 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013367 * by the MeasureSpec.
13368 *
13369 * @param size Default size for this view
13370 * @param measureSpec Constraints imposed by the parent
13371 * @return The size this view should be.
13372 */
13373 public static int getDefaultSize(int size, int measureSpec) {
13374 int result = size;
13375 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070013376 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013377
13378 switch (specMode) {
13379 case MeasureSpec.UNSPECIFIED:
13380 result = size;
13381 break;
13382 case MeasureSpec.AT_MOST:
13383 case MeasureSpec.EXACTLY:
13384 result = specSize;
13385 break;
13386 }
13387 return result;
13388 }
13389
13390 /**
13391 * Returns the suggested minimum height that the view should use. This
13392 * returns the maximum of the view's minimum height
13393 * and the background's minimum height
13394 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
13395 * <p>
13396 * When being used in {@link #onMeasure(int, int)}, the caller should still
13397 * ensure the returned height is within the requirements of the parent.
13398 *
13399 * @return The suggested minimum height of the view.
13400 */
13401 protected int getSuggestedMinimumHeight() {
13402 int suggestedMinHeight = mMinHeight;
13403
13404 if (mBGDrawable != null) {
13405 final int bgMinHeight = mBGDrawable.getMinimumHeight();
13406 if (suggestedMinHeight < bgMinHeight) {
13407 suggestedMinHeight = bgMinHeight;
13408 }
13409 }
13410
13411 return suggestedMinHeight;
13412 }
13413
13414 /**
13415 * Returns the suggested minimum width that the view should use. This
13416 * returns the maximum of the view's minimum width)
13417 * and the background's minimum width
13418 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
13419 * <p>
13420 * When being used in {@link #onMeasure(int, int)}, the caller should still
13421 * ensure the returned width is within the requirements of the parent.
13422 *
13423 * @return The suggested minimum width of the view.
13424 */
13425 protected int getSuggestedMinimumWidth() {
13426 int suggestedMinWidth = mMinWidth;
13427
13428 if (mBGDrawable != null) {
13429 final int bgMinWidth = mBGDrawable.getMinimumWidth();
13430 if (suggestedMinWidth < bgMinWidth) {
13431 suggestedMinWidth = bgMinWidth;
13432 }
13433 }
13434
13435 return suggestedMinWidth;
13436 }
13437
13438 /**
13439 * Sets the minimum height of the view. It is not guaranteed the view will
13440 * be able to achieve this minimum height (for example, if its parent layout
13441 * constrains it with less available height).
13442 *
13443 * @param minHeight The minimum height the view will try to be.
13444 */
13445 public void setMinimumHeight(int minHeight) {
13446 mMinHeight = minHeight;
13447 }
13448
13449 /**
13450 * Sets the minimum width of the view. It is not guaranteed the view will
13451 * be able to achieve this minimum width (for example, if its parent layout
13452 * constrains it with less available width).
13453 *
13454 * @param minWidth The minimum width the view will try to be.
13455 */
13456 public void setMinimumWidth(int minWidth) {
13457 mMinWidth = minWidth;
13458 }
13459
13460 /**
13461 * Get the animation currently associated with this view.
13462 *
13463 * @return The animation that is currently playing or
13464 * scheduled to play for this view.
13465 */
13466 public Animation getAnimation() {
13467 return mCurrentAnimation;
13468 }
13469
13470 /**
13471 * Start the specified animation now.
13472 *
13473 * @param animation the animation to start now
13474 */
13475 public void startAnimation(Animation animation) {
13476 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
13477 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013478 invalidateParentCaches();
13479 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013480 }
13481
13482 /**
13483 * Cancels any animations for this view.
13484 */
13485 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080013486 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080013487 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080013488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013489 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013490 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013491 }
13492
13493 /**
13494 * Sets the next animation to play for this view.
13495 * If you want the animation to play immediately, use
13496 * startAnimation. This method provides allows fine-grained
13497 * control over the start time and invalidation, but you
13498 * must make sure that 1) the animation has a start time set, and
13499 * 2) the view will be invalidated when the animation is supposed to
13500 * start.
13501 *
13502 * @param animation The next animation, or null.
13503 */
13504 public void setAnimation(Animation animation) {
13505 mCurrentAnimation = animation;
13506 if (animation != null) {
13507 animation.reset();
13508 }
13509 }
13510
13511 /**
13512 * Invoked by a parent ViewGroup to notify the start of the animation
13513 * currently associated with this view. If you override this method,
13514 * always call super.onAnimationStart();
13515 *
13516 * @see #setAnimation(android.view.animation.Animation)
13517 * @see #getAnimation()
13518 */
13519 protected void onAnimationStart() {
13520 mPrivateFlags |= ANIMATION_STARTED;
13521 }
13522
13523 /**
13524 * Invoked by a parent ViewGroup to notify the end of the animation
13525 * currently associated with this view. If you override this method,
13526 * always call super.onAnimationEnd();
13527 *
13528 * @see #setAnimation(android.view.animation.Animation)
13529 * @see #getAnimation()
13530 */
13531 protected void onAnimationEnd() {
13532 mPrivateFlags &= ~ANIMATION_STARTED;
13533 }
13534
13535 /**
13536 * Invoked if there is a Transform that involves alpha. Subclass that can
13537 * draw themselves with the specified alpha should return true, and then
13538 * respect that alpha when their onDraw() is called. If this returns false
13539 * then the view may be redirected to draw into an offscreen buffer to
13540 * fulfill the request, which will look fine, but may be slower than if the
13541 * subclass handles it internally. The default implementation returns false.
13542 *
13543 * @param alpha The alpha (0..255) to apply to the view's drawing
13544 * @return true if the view can draw with the specified alpha.
13545 */
13546 protected boolean onSetAlpha(int alpha) {
13547 return false;
13548 }
13549
13550 /**
13551 * This is used by the RootView to perform an optimization when
13552 * the view hierarchy contains one or several SurfaceView.
13553 * SurfaceView is always considered transparent, but its children are not,
13554 * therefore all View objects remove themselves from the global transparent
13555 * region (passed as a parameter to this function).
13556 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013557 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013558 *
13559 * @return Returns true if the effective visibility of the view at this
13560 * point is opaque, regardless of the transparent region; returns false
13561 * if it is possible for underlying windows to be seen behind the view.
13562 *
13563 * {@hide}
13564 */
13565 public boolean gatherTransparentRegion(Region region) {
13566 final AttachInfo attachInfo = mAttachInfo;
13567 if (region != null && attachInfo != null) {
13568 final int pflags = mPrivateFlags;
13569 if ((pflags & SKIP_DRAW) == 0) {
13570 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
13571 // remove it from the transparent region.
13572 final int[] location = attachInfo.mTransparentLocation;
13573 getLocationInWindow(location);
13574 region.op(location[0], location[1], location[0] + mRight - mLeft,
13575 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
13576 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
13577 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
13578 // exists, so we remove the background drawable's non-transparent
13579 // parts from this transparent region.
13580 applyDrawableToTransparentRegion(mBGDrawable, region);
13581 }
13582 }
13583 return true;
13584 }
13585
13586 /**
13587 * Play a sound effect for this view.
13588 *
13589 * <p>The framework will play sound effects for some built in actions, such as
13590 * clicking, but you may wish to play these effects in your widget,
13591 * for instance, for internal navigation.
13592 *
13593 * <p>The sound effect will only be played if sound effects are enabled by the user, and
13594 * {@link #isSoundEffectsEnabled()} is true.
13595 *
13596 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
13597 */
13598 public void playSoundEffect(int soundConstant) {
13599 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
13600 return;
13601 }
13602 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
13603 }
13604
13605 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013606 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013607 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013608 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013609 *
13610 * <p>The framework will provide haptic feedback for some built in actions,
13611 * such as long presses, but you may wish to provide feedback for your
13612 * own widget.
13613 *
13614 * <p>The feedback will only be performed if
13615 * {@link #isHapticFeedbackEnabled()} is true.
13616 *
13617 * @param feedbackConstant One of the constants defined in
13618 * {@link HapticFeedbackConstants}
13619 */
13620 public boolean performHapticFeedback(int feedbackConstant) {
13621 return performHapticFeedback(feedbackConstant, 0);
13622 }
13623
13624 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013625 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013626 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013627 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013628 *
13629 * @param feedbackConstant One of the constants defined in
13630 * {@link HapticFeedbackConstants}
13631 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
13632 */
13633 public boolean performHapticFeedback(int feedbackConstant, int flags) {
13634 if (mAttachInfo == null) {
13635 return false;
13636 }
Romain Guyf607bdc2010-09-10 19:20:06 -070013637 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070013638 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013639 && !isHapticFeedbackEnabled()) {
13640 return false;
13641 }
Romain Guy812ccbe2010-06-01 14:07:24 -070013642 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
13643 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013644 }
13645
13646 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013647 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013648 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13649 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013650 */
13651 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040013652 if (visibility != mSystemUiVisibility) {
13653 mSystemUiVisibility = visibility;
13654 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13655 mParent.recomputeViewAttributes(this);
13656 }
Joe Onorato664644d2011-01-23 17:53:23 -080013657 }
13658 }
13659
13660 /**
13661 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013662 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13663 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013664 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080013665 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080013666 return mSystemUiVisibility;
13667 }
13668
Scott Mainec6331b2011-05-24 16:55:56 -070013669 /**
13670 * Set a listener to receive callbacks when the visibility of the system bar changes.
13671 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
13672 */
Joe Onorato664644d2011-01-23 17:53:23 -080013673 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013674 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080013675 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13676 mParent.recomputeViewAttributes(this);
13677 }
13678 }
13679
13680 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013681 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
13682 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080013683 */
13684 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013685 ListenerInfo li = mListenerInfo;
13686 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
13687 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080013688 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080013689 }
13690 }
13691
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013692 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
13693 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
13694 if (val != mSystemUiVisibility) {
13695 setSystemUiVisibility(val);
13696 }
13697 }
13698
Joe Onorato664644d2011-01-23 17:53:23 -080013699 /**
Joe Malin32736f02011-01-19 16:14:20 -080013700 * Creates an image that the system displays during the drag and drop
13701 * operation. This is called a &quot;drag shadow&quot;. The default implementation
13702 * for a DragShadowBuilder based on a View returns an image that has exactly the same
13703 * appearance as the given View. The default also positions the center of the drag shadow
13704 * directly under the touch point. If no View is provided (the constructor with no parameters
13705 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
13706 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
13707 * default is an invisible drag shadow.
13708 * <p>
13709 * You are not required to use the View you provide to the constructor as the basis of the
13710 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
13711 * anything you want as the drag shadow.
13712 * </p>
13713 * <p>
13714 * You pass a DragShadowBuilder object to the system when you start the drag. The system
13715 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
13716 * size and position of the drag shadow. It uses this data to construct a
13717 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
13718 * so that your application can draw the shadow image in the Canvas.
13719 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070013720 *
13721 * <div class="special reference">
13722 * <h3>Developer Guides</h3>
13723 * <p>For a guide to implementing drag and drop features, read the
13724 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
13725 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070013726 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013727 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070013728 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070013729
13730 /**
Joe Malin32736f02011-01-19 16:14:20 -080013731 * Constructs a shadow image builder based on a View. By default, the resulting drag
13732 * shadow will have the same appearance and dimensions as the View, with the touch point
13733 * over the center of the View.
13734 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070013735 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013736 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070013737 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070013738 }
13739
Christopher Tate17ed60c2011-01-18 12:50:26 -080013740 /**
13741 * Construct a shadow builder object with no associated View. This
13742 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
13743 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
13744 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080013745 * reference to any View object. If they are not overridden, then the result is an
13746 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080013747 */
13748 public DragShadowBuilder() {
13749 mView = new WeakReference<View>(null);
13750 }
13751
13752 /**
13753 * Returns the View object that had been passed to the
13754 * {@link #View.DragShadowBuilder(View)}
13755 * constructor. If that View parameter was {@code null} or if the
13756 * {@link #View.DragShadowBuilder()}
13757 * constructor was used to instantiate the builder object, this method will return
13758 * null.
13759 *
13760 * @return The View object associate with this builder object.
13761 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070013762 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070013763 final public View getView() {
13764 return mView.get();
13765 }
13766
Christopher Tate2c095f32010-10-04 14:13:40 -070013767 /**
Joe Malin32736f02011-01-19 16:14:20 -080013768 * Provides the metrics for the shadow image. These include the dimensions of
13769 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070013770 * be centered under the touch location while dragging.
13771 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013772 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080013773 * same as the dimensions of the View itself and centers the shadow under
13774 * the touch point.
13775 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013776 *
Joe Malin32736f02011-01-19 16:14:20 -080013777 * @param shadowSize A {@link android.graphics.Point} containing the width and height
13778 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
13779 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
13780 * image.
13781 *
13782 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
13783 * shadow image that should be underneath the touch point during the drag and drop
13784 * operation. Your application must set {@link android.graphics.Point#x} to the
13785 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070013786 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013787 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070013788 final View view = mView.get();
13789 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013790 shadowSize.set(view.getWidth(), view.getHeight());
13791 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070013792 } else {
13793 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
13794 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013795 }
13796
13797 /**
Joe Malin32736f02011-01-19 16:14:20 -080013798 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
13799 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013800 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070013801 *
Joe Malin32736f02011-01-19 16:14:20 -080013802 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070013803 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013804 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070013805 final View view = mView.get();
13806 if (view != null) {
13807 view.draw(canvas);
13808 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013809 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070013810 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013811 }
13812 }
13813
13814 /**
Joe Malin32736f02011-01-19 16:14:20 -080013815 * Starts a drag and drop operation. When your application calls this method, it passes a
13816 * {@link android.view.View.DragShadowBuilder} object to the system. The
13817 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
13818 * to get metrics for the drag shadow, and then calls the object's
13819 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
13820 * <p>
13821 * Once the system has the drag shadow, it begins the drag and drop operation by sending
13822 * drag events to all the View objects in your application that are currently visible. It does
13823 * this either by calling the View object's drag listener (an implementation of
13824 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
13825 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
13826 * Both are passed a {@link android.view.DragEvent} object that has a
13827 * {@link android.view.DragEvent#getAction()} value of
13828 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
13829 * </p>
13830 * <p>
13831 * Your application can invoke startDrag() on any attached View object. The View object does not
13832 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
13833 * be related to the View the user selected for dragging.
13834 * </p>
13835 * @param data A {@link android.content.ClipData} object pointing to the data to be
13836 * transferred by the drag and drop operation.
13837 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
13838 * drag shadow.
13839 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
13840 * drop operation. This Object is put into every DragEvent object sent by the system during the
13841 * current drag.
13842 * <p>
13843 * myLocalState is a lightweight mechanism for the sending information from the dragged View
13844 * to the target Views. For example, it can contain flags that differentiate between a
13845 * a copy operation and a move operation.
13846 * </p>
13847 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
13848 * so the parameter should be set to 0.
13849 * @return {@code true} if the method completes successfully, or
13850 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
13851 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070013852 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013853 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013854 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070013855 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013856 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070013857 }
13858 boolean okay = false;
13859
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013860 Point shadowSize = new Point();
13861 Point shadowTouchPoint = new Point();
13862 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070013863
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013864 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
13865 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
13866 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070013867 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013868
Chris Tatea32dcf72010-10-14 12:13:50 -070013869 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013870 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
13871 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070013872 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013873 Surface surface = new Surface();
13874 try {
13875 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013876 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070013877 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070013878 + " surface=" + surface);
13879 if (token != null) {
13880 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070013881 try {
Chris Tate6b391282010-10-14 15:48:59 -070013882 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013883 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070013884 } finally {
13885 surface.unlockCanvasAndPost(canvas);
13886 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013887
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013888 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080013889
13890 // Cache the local state object for delivery with DragEvents
13891 root.setLocalDragState(myLocalState);
13892
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013893 // repurpose 'shadowSize' for the last touch point
13894 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070013895
Christopher Tatea53146c2010-09-07 11:57:52 -070013896 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013897 shadowSize.x, shadowSize.y,
13898 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070013899 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070013900
13901 // Off and running! Release our local surface instance; the drag
13902 // shadow surface is now managed by the system process.
13903 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070013904 }
13905 } catch (Exception e) {
13906 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
13907 surface.destroy();
13908 }
13909
13910 return okay;
13911 }
13912
Christopher Tatea53146c2010-09-07 11:57:52 -070013913 /**
Joe Malin32736f02011-01-19 16:14:20 -080013914 * Handles drag events sent by the system following a call to
13915 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
13916 *<p>
13917 * When the system calls this method, it passes a
13918 * {@link android.view.DragEvent} object. A call to
13919 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
13920 * in DragEvent. The method uses these to determine what is happening in the drag and drop
13921 * operation.
13922 * @param event The {@link android.view.DragEvent} sent by the system.
13923 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
13924 * in DragEvent, indicating the type of drag event represented by this object.
13925 * @return {@code true} if the method was successful, otherwise {@code false}.
13926 * <p>
13927 * The method should return {@code true} in response to an action type of
13928 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
13929 * operation.
13930 * </p>
13931 * <p>
13932 * The method should also return {@code true} in response to an action type of
13933 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
13934 * {@code false} if it didn't.
13935 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013936 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070013937 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070013938 return false;
13939 }
13940
13941 /**
Joe Malin32736f02011-01-19 16:14:20 -080013942 * Detects if this View is enabled and has a drag event listener.
13943 * If both are true, then it calls the drag event listener with the
13944 * {@link android.view.DragEvent} it received. If the drag event listener returns
13945 * {@code true}, then dispatchDragEvent() returns {@code true}.
13946 * <p>
13947 * For all other cases, the method calls the
13948 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
13949 * method and returns its result.
13950 * </p>
13951 * <p>
13952 * This ensures that a drag event is always consumed, even if the View does not have a drag
13953 * event listener. However, if the View has a listener and the listener returns true, then
13954 * onDragEvent() is not called.
13955 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013956 */
13957 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080013958 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013959 ListenerInfo li = mListenerInfo;
13960 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
13961 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070013962 return true;
13963 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013964 return onDragEvent(event);
13965 }
13966
Christopher Tate3d4bf172011-03-28 16:16:46 -070013967 boolean canAcceptDrag() {
13968 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
13969 }
13970
Christopher Tatea53146c2010-09-07 11:57:52 -070013971 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070013972 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
13973 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070013974 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070013975 */
13976 public void onCloseSystemDialogs(String reason) {
13977 }
Joe Malin32736f02011-01-19 16:14:20 -080013978
Dianne Hackbornffa42482009-09-23 22:20:11 -070013979 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013980 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070013981 * update a Region being computed for
13982 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013983 * that any non-transparent parts of the Drawable are removed from the
13984 * given transparent region.
13985 *
13986 * @param dr The Drawable whose transparency is to be applied to the region.
13987 * @param region A Region holding the current transparency information,
13988 * where any parts of the region that are set are considered to be
13989 * transparent. On return, this region will be modified to have the
13990 * transparency information reduced by the corresponding parts of the
13991 * Drawable that are not transparent.
13992 * {@hide}
13993 */
13994 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
13995 if (DBG) {
13996 Log.i("View", "Getting transparent region for: " + this);
13997 }
13998 final Region r = dr.getTransparentRegion();
13999 final Rect db = dr.getBounds();
14000 final AttachInfo attachInfo = mAttachInfo;
14001 if (r != null && attachInfo != null) {
14002 final int w = getRight()-getLeft();
14003 final int h = getBottom()-getTop();
14004 if (db.left > 0) {
14005 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
14006 r.op(0, 0, db.left, h, Region.Op.UNION);
14007 }
14008 if (db.right < w) {
14009 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
14010 r.op(db.right, 0, w, h, Region.Op.UNION);
14011 }
14012 if (db.top > 0) {
14013 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
14014 r.op(0, 0, w, db.top, Region.Op.UNION);
14015 }
14016 if (db.bottom < h) {
14017 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
14018 r.op(0, db.bottom, w, h, Region.Op.UNION);
14019 }
14020 final int[] location = attachInfo.mTransparentLocation;
14021 getLocationInWindow(location);
14022 r.translate(location[0], location[1]);
14023 region.op(r, Region.Op.INTERSECT);
14024 } else {
14025 region.op(db, Region.Op.DIFFERENCE);
14026 }
14027 }
14028
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014029 private void checkForLongClick(int delayOffset) {
14030 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
14031 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014032
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014033 if (mPendingCheckForLongPress == null) {
14034 mPendingCheckForLongPress = new CheckForLongPress();
14035 }
14036 mPendingCheckForLongPress.rememberWindowAttachCount();
14037 postDelayed(mPendingCheckForLongPress,
14038 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014040 }
14041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014042 /**
14043 * Inflate a view from an XML resource. This convenience method wraps the {@link
14044 * LayoutInflater} class, which provides a full range of options for view inflation.
14045 *
14046 * @param context The Context object for your activity or application.
14047 * @param resource The resource ID to inflate
14048 * @param root A view group that will be the parent. Used to properly inflate the
14049 * layout_* parameters.
14050 * @see LayoutInflater
14051 */
14052 public static View inflate(Context context, int resource, ViewGroup root) {
14053 LayoutInflater factory = LayoutInflater.from(context);
14054 return factory.inflate(resource, root);
14055 }
Romain Guy33e72ae2010-07-17 12:40:29 -070014056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014057 /**
Adam Powell637d3372010-08-25 14:37:03 -070014058 * Scroll the view with standard behavior for scrolling beyond the normal
14059 * content boundaries. Views that call this method should override
14060 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
14061 * results of an over-scroll operation.
14062 *
14063 * Views can use this method to handle any touch or fling-based scrolling.
14064 *
14065 * @param deltaX Change in X in pixels
14066 * @param deltaY Change in Y in pixels
14067 * @param scrollX Current X scroll value in pixels before applying deltaX
14068 * @param scrollY Current Y scroll value in pixels before applying deltaY
14069 * @param scrollRangeX Maximum content scroll range along the X axis
14070 * @param scrollRangeY Maximum content scroll range along the Y axis
14071 * @param maxOverScrollX Number of pixels to overscroll by in either direction
14072 * along the X axis.
14073 * @param maxOverScrollY Number of pixels to overscroll by in either direction
14074 * along the Y axis.
14075 * @param isTouchEvent true if this scroll operation is the result of a touch event.
14076 * @return true if scrolling was clamped to an over-scroll boundary along either
14077 * axis, false otherwise.
14078 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070014079 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070014080 protected boolean overScrollBy(int deltaX, int deltaY,
14081 int scrollX, int scrollY,
14082 int scrollRangeX, int scrollRangeY,
14083 int maxOverScrollX, int maxOverScrollY,
14084 boolean isTouchEvent) {
14085 final int overScrollMode = mOverScrollMode;
14086 final boolean canScrollHorizontal =
14087 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
14088 final boolean canScrollVertical =
14089 computeVerticalScrollRange() > computeVerticalScrollExtent();
14090 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
14091 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
14092 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
14093 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
14094
14095 int newScrollX = scrollX + deltaX;
14096 if (!overScrollHorizontal) {
14097 maxOverScrollX = 0;
14098 }
14099
14100 int newScrollY = scrollY + deltaY;
14101 if (!overScrollVertical) {
14102 maxOverScrollY = 0;
14103 }
14104
14105 // Clamp values if at the limits and record
14106 final int left = -maxOverScrollX;
14107 final int right = maxOverScrollX + scrollRangeX;
14108 final int top = -maxOverScrollY;
14109 final int bottom = maxOverScrollY + scrollRangeY;
14110
14111 boolean clampedX = false;
14112 if (newScrollX > right) {
14113 newScrollX = right;
14114 clampedX = true;
14115 } else if (newScrollX < left) {
14116 newScrollX = left;
14117 clampedX = true;
14118 }
14119
14120 boolean clampedY = false;
14121 if (newScrollY > bottom) {
14122 newScrollY = bottom;
14123 clampedY = true;
14124 } else if (newScrollY < top) {
14125 newScrollY = top;
14126 clampedY = true;
14127 }
14128
14129 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
14130
14131 return clampedX || clampedY;
14132 }
14133
14134 /**
14135 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
14136 * respond to the results of an over-scroll operation.
14137 *
14138 * @param scrollX New X scroll value in pixels
14139 * @param scrollY New Y scroll value in pixels
14140 * @param clampedX True if scrollX was clamped to an over-scroll boundary
14141 * @param clampedY True if scrollY was clamped to an over-scroll boundary
14142 */
14143 protected void onOverScrolled(int scrollX, int scrollY,
14144 boolean clampedX, boolean clampedY) {
14145 // Intentionally empty.
14146 }
14147
14148 /**
14149 * Returns the over-scroll mode for this view. The result will be
14150 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14151 * (allow over-scrolling only if the view content is larger than the container),
14152 * or {@link #OVER_SCROLL_NEVER}.
14153 *
14154 * @return This view's over-scroll mode.
14155 */
14156 public int getOverScrollMode() {
14157 return mOverScrollMode;
14158 }
14159
14160 /**
14161 * Set the over-scroll mode for this view. Valid over-scroll modes are
14162 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14163 * (allow over-scrolling only if the view content is larger than the container),
14164 * or {@link #OVER_SCROLL_NEVER}.
14165 *
14166 * Setting the over-scroll mode of a view will have an effect only if the
14167 * view is capable of scrolling.
14168 *
14169 * @param overScrollMode The new over-scroll mode for this view.
14170 */
14171 public void setOverScrollMode(int overScrollMode) {
14172 if (overScrollMode != OVER_SCROLL_ALWAYS &&
14173 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
14174 overScrollMode != OVER_SCROLL_NEVER) {
14175 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
14176 }
14177 mOverScrollMode = overScrollMode;
14178 }
14179
14180 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014181 * Gets a scale factor that determines the distance the view should scroll
14182 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
14183 * @return The vertical scroll scale factor.
14184 * @hide
14185 */
14186 protected float getVerticalScrollFactor() {
14187 if (mVerticalScrollFactor == 0) {
14188 TypedValue outValue = new TypedValue();
14189 if (!mContext.getTheme().resolveAttribute(
14190 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
14191 throw new IllegalStateException(
14192 "Expected theme to define listPreferredItemHeight.");
14193 }
14194 mVerticalScrollFactor = outValue.getDimension(
14195 mContext.getResources().getDisplayMetrics());
14196 }
14197 return mVerticalScrollFactor;
14198 }
14199
14200 /**
14201 * Gets a scale factor that determines the distance the view should scroll
14202 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
14203 * @return The horizontal scroll scale factor.
14204 * @hide
14205 */
14206 protected float getHorizontalScrollFactor() {
14207 // TODO: Should use something else.
14208 return getVerticalScrollFactor();
14209 }
14210
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014211 /**
14212 * Return the value specifying the text direction or policy that was set with
14213 * {@link #setTextDirection(int)}.
14214 *
14215 * @return the defined text direction. It can be one of:
14216 *
14217 * {@link #TEXT_DIRECTION_INHERIT},
14218 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14219 * {@link #TEXT_DIRECTION_ANY_RTL},
14220 * {@link #TEXT_DIRECTION_LTR},
14221 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014222 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014223 */
14224 public int getTextDirection() {
14225 return mTextDirection;
14226 }
14227
14228 /**
14229 * Set the text direction.
14230 *
14231 * @param textDirection the direction to set. Should be one of:
14232 *
14233 * {@link #TEXT_DIRECTION_INHERIT},
14234 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14235 * {@link #TEXT_DIRECTION_ANY_RTL},
14236 * {@link #TEXT_DIRECTION_LTR},
14237 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014238 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014239 */
14240 public void setTextDirection(int textDirection) {
14241 if (textDirection != mTextDirection) {
14242 mTextDirection = textDirection;
14243 resetResolvedTextDirection();
14244 requestLayout();
14245 }
14246 }
14247
14248 /**
14249 * Return the resolved text direction.
14250 *
14251 * @return the resolved text direction. Return one of:
14252 *
Doug Feltcb3791202011-07-07 11:57:48 -070014253 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14254 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014255 * {@link #TEXT_DIRECTION_LTR},
14256 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014257 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014258 */
14259 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070014260 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014261 resolveTextDirection();
14262 }
14263 return mResolvedTextDirection;
14264 }
14265
14266 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014267 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
14268 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014269 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014270 public void resolveTextDirection() {
14271 if (mResolvedTextDirection != TEXT_DIRECTION_INHERIT) {
14272 // Resolution has already been done.
14273 return;
14274 }
Doug Feltcb3791202011-07-07 11:57:48 -070014275 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
14276 mResolvedTextDirection = mTextDirection;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014277 } else if (mParent != null && mParent instanceof ViewGroup) {
Doug Feltcb3791202011-07-07 11:57:48 -070014278 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014279 } else {
14280 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Doug Feltcb3791202011-07-07 11:57:48 -070014281 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014282 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014283 }
14284
14285 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014286 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014287 * resolution should override this method.
14288 *
14289 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014290 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014291 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014292 }
14293
14294 /**
14295 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014296 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014297 * reset is done.
14298 */
14299 public void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070014300 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014301 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014302 }
14303
14304 /**
14305 * Called when text direction is reset. Subclasses that care about text direction reset should
14306 * override this method and do a reset of the text direction of their children. The default
14307 * implementation does nothing.
14308 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014309 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014310 }
14311
Chet Haaseb39f0512011-05-24 14:36:40 -070014312 //
14313 // Properties
14314 //
14315 /**
14316 * A Property wrapper around the <code>alpha</code> functionality handled by the
14317 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
14318 */
Chet Haased47f1532011-12-16 11:18:52 -080014319 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014320 @Override
14321 public void setValue(View object, float value) {
14322 object.setAlpha(value);
14323 }
14324
14325 @Override
14326 public Float get(View object) {
14327 return object.getAlpha();
14328 }
14329 };
14330
14331 /**
14332 * A Property wrapper around the <code>translationX</code> functionality handled by the
14333 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
14334 */
Chet Haased47f1532011-12-16 11:18:52 -080014335 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014336 @Override
14337 public void setValue(View object, float value) {
14338 object.setTranslationX(value);
14339 }
14340
14341 @Override
14342 public Float get(View object) {
14343 return object.getTranslationX();
14344 }
14345 };
14346
14347 /**
14348 * A Property wrapper around the <code>translationY</code> functionality handled by the
14349 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
14350 */
Chet Haased47f1532011-12-16 11:18:52 -080014351 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014352 @Override
14353 public void setValue(View object, float value) {
14354 object.setTranslationY(value);
14355 }
14356
14357 @Override
14358 public Float get(View object) {
14359 return object.getTranslationY();
14360 }
14361 };
14362
14363 /**
14364 * A Property wrapper around the <code>x</code> functionality handled by the
14365 * {@link View#setX(float)} and {@link View#getX()} methods.
14366 */
Chet Haased47f1532011-12-16 11:18:52 -080014367 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014368 @Override
14369 public void setValue(View object, float value) {
14370 object.setX(value);
14371 }
14372
14373 @Override
14374 public Float get(View object) {
14375 return object.getX();
14376 }
14377 };
14378
14379 /**
14380 * A Property wrapper around the <code>y</code> functionality handled by the
14381 * {@link View#setY(float)} and {@link View#getY()} methods.
14382 */
Chet Haased47f1532011-12-16 11:18:52 -080014383 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014384 @Override
14385 public void setValue(View object, float value) {
14386 object.setY(value);
14387 }
14388
14389 @Override
14390 public Float get(View object) {
14391 return object.getY();
14392 }
14393 };
14394
14395 /**
14396 * A Property wrapper around the <code>rotation</code> functionality handled by the
14397 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
14398 */
Chet Haased47f1532011-12-16 11:18:52 -080014399 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014400 @Override
14401 public void setValue(View object, float value) {
14402 object.setRotation(value);
14403 }
14404
14405 @Override
14406 public Float get(View object) {
14407 return object.getRotation();
14408 }
14409 };
14410
14411 /**
14412 * A Property wrapper around the <code>rotationX</code> functionality handled by the
14413 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
14414 */
Chet Haased47f1532011-12-16 11:18:52 -080014415 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014416 @Override
14417 public void setValue(View object, float value) {
14418 object.setRotationX(value);
14419 }
14420
14421 @Override
14422 public Float get(View object) {
14423 return object.getRotationX();
14424 }
14425 };
14426
14427 /**
14428 * A Property wrapper around the <code>rotationY</code> functionality handled by the
14429 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
14430 */
Chet Haased47f1532011-12-16 11:18:52 -080014431 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014432 @Override
14433 public void setValue(View object, float value) {
14434 object.setRotationY(value);
14435 }
14436
14437 @Override
14438 public Float get(View object) {
14439 return object.getRotationY();
14440 }
14441 };
14442
14443 /**
14444 * A Property wrapper around the <code>scaleX</code> functionality handled by the
14445 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
14446 */
Chet Haased47f1532011-12-16 11:18:52 -080014447 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014448 @Override
14449 public void setValue(View object, float value) {
14450 object.setScaleX(value);
14451 }
14452
14453 @Override
14454 public Float get(View object) {
14455 return object.getScaleX();
14456 }
14457 };
14458
14459 /**
14460 * A Property wrapper around the <code>scaleY</code> functionality handled by the
14461 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
14462 */
Chet Haased47f1532011-12-16 11:18:52 -080014463 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014464 @Override
14465 public void setValue(View object, float value) {
14466 object.setScaleY(value);
14467 }
14468
14469 @Override
14470 public Float get(View object) {
14471 return object.getScaleY();
14472 }
14473 };
14474
Jeff Brown33bbfd22011-02-24 20:55:35 -080014475 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014476 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
14477 * Each MeasureSpec represents a requirement for either the width or the height.
14478 * A MeasureSpec is comprised of a size and a mode. There are three possible
14479 * modes:
14480 * <dl>
14481 * <dt>UNSPECIFIED</dt>
14482 * <dd>
14483 * The parent has not imposed any constraint on the child. It can be whatever size
14484 * it wants.
14485 * </dd>
14486 *
14487 * <dt>EXACTLY</dt>
14488 * <dd>
14489 * The parent has determined an exact size for the child. The child is going to be
14490 * given those bounds regardless of how big it wants to be.
14491 * </dd>
14492 *
14493 * <dt>AT_MOST</dt>
14494 * <dd>
14495 * The child can be as large as it wants up to the specified size.
14496 * </dd>
14497 * </dl>
14498 *
14499 * MeasureSpecs are implemented as ints to reduce object allocation. This class
14500 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
14501 */
14502 public static class MeasureSpec {
14503 private static final int MODE_SHIFT = 30;
14504 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
14505
14506 /**
14507 * Measure specification mode: The parent has not imposed any constraint
14508 * on the child. It can be whatever size it wants.
14509 */
14510 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
14511
14512 /**
14513 * Measure specification mode: The parent has determined an exact size
14514 * for the child. The child is going to be given those bounds regardless
14515 * of how big it wants to be.
14516 */
14517 public static final int EXACTLY = 1 << MODE_SHIFT;
14518
14519 /**
14520 * Measure specification mode: The child can be as large as it wants up
14521 * to the specified size.
14522 */
14523 public static final int AT_MOST = 2 << MODE_SHIFT;
14524
14525 /**
14526 * Creates a measure specification based on the supplied size and mode.
14527 *
14528 * The mode must always be one of the following:
14529 * <ul>
14530 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
14531 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
14532 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
14533 * </ul>
14534 *
14535 * @param size the size of the measure specification
14536 * @param mode the mode of the measure specification
14537 * @return the measure specification based on size and mode
14538 */
14539 public static int makeMeasureSpec(int size, int mode) {
14540 return size + mode;
14541 }
14542
14543 /**
14544 * Extracts the mode from the supplied measure specification.
14545 *
14546 * @param measureSpec the measure specification to extract the mode from
14547 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
14548 * {@link android.view.View.MeasureSpec#AT_MOST} or
14549 * {@link android.view.View.MeasureSpec#EXACTLY}
14550 */
14551 public static int getMode(int measureSpec) {
14552 return (measureSpec & MODE_MASK);
14553 }
14554
14555 /**
14556 * Extracts the size from the supplied measure specification.
14557 *
14558 * @param measureSpec the measure specification to extract the size from
14559 * @return the size in pixels defined in the supplied measure specification
14560 */
14561 public static int getSize(int measureSpec) {
14562 return (measureSpec & ~MODE_MASK);
14563 }
14564
14565 /**
14566 * Returns a String representation of the specified measure
14567 * specification.
14568 *
14569 * @param measureSpec the measure specification to convert to a String
14570 * @return a String with the following format: "MeasureSpec: MODE SIZE"
14571 */
14572 public static String toString(int measureSpec) {
14573 int mode = getMode(measureSpec);
14574 int size = getSize(measureSpec);
14575
14576 StringBuilder sb = new StringBuilder("MeasureSpec: ");
14577
14578 if (mode == UNSPECIFIED)
14579 sb.append("UNSPECIFIED ");
14580 else if (mode == EXACTLY)
14581 sb.append("EXACTLY ");
14582 else if (mode == AT_MOST)
14583 sb.append("AT_MOST ");
14584 else
14585 sb.append(mode).append(" ");
14586
14587 sb.append(size);
14588 return sb.toString();
14589 }
14590 }
14591
14592 class CheckForLongPress implements Runnable {
14593
14594 private int mOriginalWindowAttachCount;
14595
14596 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070014597 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014598 && mOriginalWindowAttachCount == mWindowAttachCount) {
14599 if (performLongClick()) {
14600 mHasPerformedLongPress = true;
14601 }
14602 }
14603 }
14604
14605 public void rememberWindowAttachCount() {
14606 mOriginalWindowAttachCount = mWindowAttachCount;
14607 }
14608 }
Joe Malin32736f02011-01-19 16:14:20 -080014609
Adam Powelle14579b2009-12-16 18:39:52 -080014610 private final class CheckForTap implements Runnable {
14611 public void run() {
14612 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080014613 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014614 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080014615 }
14616 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014617
Adam Powella35d7682010-03-12 14:48:13 -080014618 private final class PerformClick implements Runnable {
14619 public void run() {
14620 performClick();
14621 }
14622 }
14623
Dianne Hackborn63042d62011-01-26 18:56:29 -080014624 /** @hide */
14625 public void hackTurnOffWindowResizeAnim(boolean off) {
14626 mAttachInfo.mTurnOffWindowResizeAnim = off;
14627 }
Joe Malin32736f02011-01-19 16:14:20 -080014628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014629 /**
Chet Haasea00f3862011-02-22 06:34:40 -080014630 * This method returns a ViewPropertyAnimator object, which can be used to animate
14631 * specific properties on this View.
14632 *
14633 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
14634 */
14635 public ViewPropertyAnimator animate() {
14636 if (mAnimator == null) {
14637 mAnimator = new ViewPropertyAnimator(this);
14638 }
14639 return mAnimator;
14640 }
14641
14642 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014643 * Interface definition for a callback to be invoked when a key event is
14644 * dispatched to this view. The callback will be invoked before the key
14645 * event is given to the view.
14646 */
14647 public interface OnKeyListener {
14648 /**
14649 * Called when a key is dispatched to a view. This allows listeners to
14650 * get a chance to respond before the target view.
14651 *
14652 * @param v The view the key has been dispatched to.
14653 * @param keyCode The code for the physical key that was pressed
14654 * @param event The KeyEvent object containing full information about
14655 * the event.
14656 * @return True if the listener has consumed the event, false otherwise.
14657 */
14658 boolean onKey(View v, int keyCode, KeyEvent event);
14659 }
14660
14661 /**
14662 * Interface definition for a callback to be invoked when a touch event is
14663 * dispatched to this view. The callback will be invoked before the touch
14664 * event is given to the view.
14665 */
14666 public interface OnTouchListener {
14667 /**
14668 * Called when a touch event is dispatched to a view. This allows listeners to
14669 * get a chance to respond before the target view.
14670 *
14671 * @param v The view the touch event has been dispatched to.
14672 * @param event The MotionEvent object containing full information about
14673 * the event.
14674 * @return True if the listener has consumed the event, false otherwise.
14675 */
14676 boolean onTouch(View v, MotionEvent event);
14677 }
14678
14679 /**
Jeff Brown10b62902011-06-20 16:40:37 -070014680 * Interface definition for a callback to be invoked when a hover event is
14681 * dispatched to this view. The callback will be invoked before the hover
14682 * event is given to the view.
14683 */
14684 public interface OnHoverListener {
14685 /**
14686 * Called when a hover event is dispatched to a view. This allows listeners to
14687 * get a chance to respond before the target view.
14688 *
14689 * @param v The view the hover event has been dispatched to.
14690 * @param event The MotionEvent object containing full information about
14691 * the event.
14692 * @return True if the listener has consumed the event, false otherwise.
14693 */
14694 boolean onHover(View v, MotionEvent event);
14695 }
14696
14697 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014698 * Interface definition for a callback to be invoked when a generic motion event is
14699 * dispatched to this view. The callback will be invoked before the generic motion
14700 * event is given to the view.
14701 */
14702 public interface OnGenericMotionListener {
14703 /**
14704 * Called when a generic motion event is dispatched to a view. This allows listeners to
14705 * get a chance to respond before the target view.
14706 *
14707 * @param v The view the generic motion event has been dispatched to.
14708 * @param event The MotionEvent object containing full information about
14709 * the event.
14710 * @return True if the listener has consumed the event, false otherwise.
14711 */
14712 boolean onGenericMotion(View v, MotionEvent event);
14713 }
14714
14715 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014716 * Interface definition for a callback to be invoked when a view has been clicked and held.
14717 */
14718 public interface OnLongClickListener {
14719 /**
14720 * Called when a view has been clicked and held.
14721 *
14722 * @param v The view that was clicked and held.
14723 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080014724 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014725 */
14726 boolean onLongClick(View v);
14727 }
14728
14729 /**
Chris Tate32affef2010-10-18 15:29:21 -070014730 * Interface definition for a callback to be invoked when a drag is being dispatched
14731 * to this view. The callback will be invoked before the hosting view's own
14732 * onDrag(event) method. If the listener wants to fall back to the hosting view's
14733 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070014734 *
14735 * <div class="special reference">
14736 * <h3>Developer Guides</h3>
14737 * <p>For a guide to implementing drag and drop features, read the
14738 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
14739 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070014740 */
14741 public interface OnDragListener {
14742 /**
14743 * Called when a drag event is dispatched to a view. This allows listeners
14744 * to get a chance to override base View behavior.
14745 *
Joe Malin32736f02011-01-19 16:14:20 -080014746 * @param v The View that received the drag event.
14747 * @param event The {@link android.view.DragEvent} object for the drag event.
14748 * @return {@code true} if the drag event was handled successfully, or {@code false}
14749 * if the drag event was not handled. Note that {@code false} will trigger the View
14750 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070014751 */
14752 boolean onDrag(View v, DragEvent event);
14753 }
14754
14755 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014756 * Interface definition for a callback to be invoked when the focus state of
14757 * a view changed.
14758 */
14759 public interface OnFocusChangeListener {
14760 /**
14761 * Called when the focus state of a view has changed.
14762 *
14763 * @param v The view whose state has changed.
14764 * @param hasFocus The new focus state of v.
14765 */
14766 void onFocusChange(View v, boolean hasFocus);
14767 }
14768
14769 /**
14770 * Interface definition for a callback to be invoked when a view is clicked.
14771 */
14772 public interface OnClickListener {
14773 /**
14774 * Called when a view has been clicked.
14775 *
14776 * @param v The view that was clicked.
14777 */
14778 void onClick(View v);
14779 }
14780
14781 /**
14782 * Interface definition for a callback to be invoked when the context menu
14783 * for this view is being built.
14784 */
14785 public interface OnCreateContextMenuListener {
14786 /**
14787 * Called when the context menu for this view is being built. It is not
14788 * safe to hold onto the menu after this method returns.
14789 *
14790 * @param menu The context menu that is being built
14791 * @param v The view for which the context menu is being built
14792 * @param menuInfo Extra information about the item for which the
14793 * context menu should be shown. This information will vary
14794 * depending on the class of v.
14795 */
14796 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
14797 }
14798
Joe Onorato664644d2011-01-23 17:53:23 -080014799 /**
14800 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014801 * visibility. This reports <strong>global</strong> changes to the system UI
14802 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080014803 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070014804 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080014805 */
14806 public interface OnSystemUiVisibilityChangeListener {
14807 /**
14808 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070014809 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080014810 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040014811 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014812 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
14813 * <strong>global</strong> state of the UI visibility flags, not what your
14814 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080014815 */
14816 public void onSystemUiVisibilityChange(int visibility);
14817 }
14818
Adam Powell4afd62b2011-02-18 15:02:18 -080014819 /**
14820 * Interface definition for a callback to be invoked when this view is attached
14821 * or detached from its window.
14822 */
14823 public interface OnAttachStateChangeListener {
14824 /**
14825 * Called when the view is attached to a window.
14826 * @param v The view that was attached
14827 */
14828 public void onViewAttachedToWindow(View v);
14829 /**
14830 * Called when the view is detached from a window.
14831 * @param v The view that was detached
14832 */
14833 public void onViewDetachedFromWindow(View v);
14834 }
14835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014836 private final class UnsetPressedState implements Runnable {
14837 public void run() {
14838 setPressed(false);
14839 }
14840 }
14841
14842 /**
14843 * Base class for derived classes that want to save and restore their own
14844 * state in {@link android.view.View#onSaveInstanceState()}.
14845 */
14846 public static class BaseSavedState extends AbsSavedState {
14847 /**
14848 * Constructor used when reading from a parcel. Reads the state of the superclass.
14849 *
14850 * @param source
14851 */
14852 public BaseSavedState(Parcel source) {
14853 super(source);
14854 }
14855
14856 /**
14857 * Constructor called by derived classes when creating their SavedState objects
14858 *
14859 * @param superState The state of the superclass of this view
14860 */
14861 public BaseSavedState(Parcelable superState) {
14862 super(superState);
14863 }
14864
14865 public static final Parcelable.Creator<BaseSavedState> CREATOR =
14866 new Parcelable.Creator<BaseSavedState>() {
14867 public BaseSavedState createFromParcel(Parcel in) {
14868 return new BaseSavedState(in);
14869 }
14870
14871 public BaseSavedState[] newArray(int size) {
14872 return new BaseSavedState[size];
14873 }
14874 };
14875 }
14876
14877 /**
14878 * A set of information given to a view when it is attached to its parent
14879 * window.
14880 */
14881 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014882 interface Callbacks {
14883 void playSoundEffect(int effectId);
14884 boolean performHapticFeedback(int effectId, boolean always);
14885 }
14886
14887 /**
14888 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
14889 * to a Handler. This class contains the target (View) to invalidate and
14890 * the coordinates of the dirty rectangle.
14891 *
14892 * For performance purposes, this class also implements a pool of up to
14893 * POOL_LIMIT objects that get reused. This reduces memory allocations
14894 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014895 */
Romain Guyd928d682009-03-31 17:52:16 -070014896 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014897 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070014898 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
14899 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070014900 public InvalidateInfo newInstance() {
14901 return new InvalidateInfo();
14902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014903
Romain Guyd928d682009-03-31 17:52:16 -070014904 public void onAcquired(InvalidateInfo element) {
14905 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014906
Romain Guyd928d682009-03-31 17:52:16 -070014907 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070014908 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070014909 }
14910 }, POOL_LIMIT)
14911 );
14912
14913 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014914 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014915
14916 View target;
14917
14918 int left;
14919 int top;
14920 int right;
14921 int bottom;
14922
Romain Guyd928d682009-03-31 17:52:16 -070014923 public void setNextPoolable(InvalidateInfo element) {
14924 mNext = element;
14925 }
14926
14927 public InvalidateInfo getNextPoolable() {
14928 return mNext;
14929 }
14930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014931 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070014932 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014933 }
14934
14935 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070014936 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014937 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014938
14939 public boolean isPooled() {
14940 return mIsPooled;
14941 }
14942
14943 public void setPooled(boolean isPooled) {
14944 mIsPooled = isPooled;
14945 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014946 }
14947
14948 final IWindowSession mSession;
14949
14950 final IWindow mWindow;
14951
14952 final IBinder mWindowToken;
14953
14954 final Callbacks mRootCallbacks;
14955
Romain Guy59a12ca2011-06-09 17:48:21 -070014956 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080014957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014958 /**
14959 * The top view of the hierarchy.
14960 */
14961 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070014962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014963 IBinder mPanelParentWindowToken;
14964 Surface mSurface;
14965
Romain Guyb051e892010-09-28 19:09:36 -070014966 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014967 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070014968 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080014969
Romain Guy7e4e5612012-03-05 14:37:29 -080014970 boolean mScreenOn;
14971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014972 /**
Romain Guy8506ab42009-06-11 17:35:47 -070014973 * Scale factor used by the compatibility mode
14974 */
14975 float mApplicationScale;
14976
14977 /**
14978 * Indicates whether the application is in compatibility mode
14979 */
14980 boolean mScalingRequired;
14981
14982 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014983 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080014984 */
14985 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080014986
Dianne Hackborn63042d62011-01-26 18:56:29 -080014987 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014988 * Left position of this view's window
14989 */
14990 int mWindowLeft;
14991
14992 /**
14993 * Top position of this view's window
14994 */
14995 int mWindowTop;
14996
14997 /**
Adam Powell26153a32010-11-08 15:22:27 -080014998 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070014999 */
Adam Powell26153a32010-11-08 15:22:27 -080015000 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070015001
15002 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015003 * For windows that are full-screen but using insets to layout inside
15004 * of the screen decorations, these are the current insets for the
15005 * content of the window.
15006 */
15007 final Rect mContentInsets = new Rect();
15008
15009 /**
15010 * For windows that are full-screen but using insets to layout inside
15011 * of the screen decorations, these are the current insets for the
15012 * actual visible parts of the window.
15013 */
15014 final Rect mVisibleInsets = new Rect();
15015
15016 /**
15017 * The internal insets given by this window. This value is
15018 * supplied by the client (through
15019 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
15020 * be given to the window manager when changed to be used in laying
15021 * out windows behind it.
15022 */
15023 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
15024 = new ViewTreeObserver.InternalInsetsInfo();
15025
15026 /**
15027 * All views in the window's hierarchy that serve as scroll containers,
15028 * used to determine if the window can be resized or must be panned
15029 * to adjust for a soft input area.
15030 */
15031 final ArrayList<View> mScrollContainers = new ArrayList<View>();
15032
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070015033 final KeyEvent.DispatcherState mKeyDispatchState
15034 = new KeyEvent.DispatcherState();
15035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015036 /**
15037 * Indicates whether the view's window currently has the focus.
15038 */
15039 boolean mHasWindowFocus;
15040
15041 /**
15042 * The current visibility of the window.
15043 */
15044 int mWindowVisibility;
15045
15046 /**
15047 * Indicates the time at which drawing started to occur.
15048 */
15049 long mDrawingTime;
15050
15051 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070015052 * Indicates whether or not ignoring the DIRTY_MASK flags.
15053 */
15054 boolean mIgnoreDirtyState;
15055
15056 /**
Romain Guy02ccac62011-06-24 13:20:23 -070015057 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
15058 * to avoid clearing that flag prematurely.
15059 */
15060 boolean mSetIgnoreDirtyState = false;
15061
15062 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015063 * Indicates whether the view's window is currently in touch mode.
15064 */
15065 boolean mInTouchMode;
15066
15067 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015068 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015069 * the next time it performs a traversal
15070 */
15071 boolean mRecomputeGlobalAttributes;
15072
15073 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015074 * Always report new attributes at next traversal.
15075 */
15076 boolean mForceReportNewAttributes;
15077
15078 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015079 * Set during a traveral if any views want to keep the screen on.
15080 */
15081 boolean mKeepScreenOn;
15082
15083 /**
Joe Onorato664644d2011-01-23 17:53:23 -080015084 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
15085 */
15086 int mSystemUiVisibility;
15087
15088 /**
15089 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
15090 * attached.
15091 */
15092 boolean mHasSystemUiListeners;
15093
15094 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015095 * Set if the visibility of any views has changed.
15096 */
15097 boolean mViewVisibilityChanged;
15098
15099 /**
15100 * Set to true if a view has been scrolled.
15101 */
15102 boolean mViewScrollChanged;
15103
15104 /**
15105 * Global to the view hierarchy used as a temporary for dealing with
15106 * x/y points in the transparent region computations.
15107 */
15108 final int[] mTransparentLocation = new int[2];
15109
15110 /**
15111 * Global to the view hierarchy used as a temporary for dealing with
15112 * x/y points in the ViewGroup.invalidateChild implementation.
15113 */
15114 final int[] mInvalidateChildLocation = new int[2];
15115
Chet Haasec3aa3612010-06-17 08:50:37 -070015116
15117 /**
15118 * Global to the view hierarchy used as a temporary for dealing with
15119 * x/y location when view is transformed.
15120 */
15121 final float[] mTmpTransformLocation = new float[2];
15122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015123 /**
15124 * The view tree observer used to dispatch global events like
15125 * layout, pre-draw, touch mode change, etc.
15126 */
15127 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
15128
15129 /**
15130 * A Canvas used by the view hierarchy to perform bitmap caching.
15131 */
15132 Canvas mCanvas;
15133
15134 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080015135 * The view root impl.
15136 */
15137 final ViewRootImpl mViewRootImpl;
15138
15139 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015140 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015141 * handler can be used to pump events in the UI events queue.
15142 */
15143 final Handler mHandler;
15144
15145 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015146 * Temporary for use in computing invalidate rectangles while
15147 * calling up the hierarchy.
15148 */
15149 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070015150
15151 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070015152 * Temporary for use in computing hit areas with transformed views
15153 */
15154 final RectF mTmpTransformRect = new RectF();
15155
15156 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070015157 * Temporary list for use in collecting focusable descendents of a view.
15158 */
15159 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
15160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015161 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070015162 * The id of the window for accessibility purposes.
15163 */
15164 int mAccessibilityWindowId = View.NO_ID;
15165
15166 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015167 * Creates a new set of attachment information with the specified
15168 * events handler and thread.
15169 *
15170 * @param handler the events handler the view must use
15171 */
15172 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080015173 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015174 mSession = session;
15175 mWindow = window;
15176 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080015177 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015178 mHandler = handler;
15179 mRootCallbacks = effectPlayer;
15180 }
15181 }
15182
15183 /**
15184 * <p>ScrollabilityCache holds various fields used by a View when scrolling
15185 * is supported. This avoids keeping too many unused fields in most
15186 * instances of View.</p>
15187 */
Mike Cleronf116bf82009-09-27 19:14:12 -070015188 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080015189
Mike Cleronf116bf82009-09-27 19:14:12 -070015190 /**
15191 * Scrollbars are not visible
15192 */
15193 public static final int OFF = 0;
15194
15195 /**
15196 * Scrollbars are visible
15197 */
15198 public static final int ON = 1;
15199
15200 /**
15201 * Scrollbars are fading away
15202 */
15203 public static final int FADING = 2;
15204
15205 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080015206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015207 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070015208 public int scrollBarDefaultDelayBeforeFade;
15209 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015210
15211 public int scrollBarSize;
15212 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070015213 public float[] interpolatorValues;
15214 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015215
15216 public final Paint paint;
15217 public final Matrix matrix;
15218 public Shader shader;
15219
Mike Cleronf116bf82009-09-27 19:14:12 -070015220 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
15221
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015222 private static final float[] OPAQUE = { 255 };
15223 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080015224
Mike Cleronf116bf82009-09-27 19:14:12 -070015225 /**
15226 * When fading should start. This time moves into the future every time
15227 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
15228 */
15229 public long fadeStartTime;
15230
15231
15232 /**
15233 * The current state of the scrollbars: ON, OFF, or FADING
15234 */
15235 public int state = OFF;
15236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015237 private int mLastColor;
15238
Mike Cleronf116bf82009-09-27 19:14:12 -070015239 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015240 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
15241 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070015242 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
15243 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015244
15245 paint = new Paint();
15246 matrix = new Matrix();
15247 // use use a height of 1, and then wack the matrix each time we
15248 // actually use it.
15249 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070015250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015251 paint.setShader(shader);
15252 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070015253 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015254 }
Romain Guy8506ab42009-06-11 17:35:47 -070015255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015256 public void setFadeColor(int color) {
15257 if (color != 0 && color != mLastColor) {
15258 mLastColor = color;
15259 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070015260
Romain Guye55e1a72009-08-27 10:42:26 -070015261 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
15262 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070015263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015264 paint.setShader(shader);
15265 // Restore the default transfer mode (src_over)
15266 paint.setXfermode(null);
15267 }
15268 }
Joe Malin32736f02011-01-19 16:14:20 -080015269
Mike Cleronf116bf82009-09-27 19:14:12 -070015270 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070015271 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070015272 if (now >= fadeStartTime) {
15273
15274 // the animation fades the scrollbars out by changing
15275 // the opacity (alpha) from fully opaque to fully
15276 // transparent
15277 int nextFrame = (int) now;
15278 int framesCount = 0;
15279
15280 Interpolator interpolator = scrollBarInterpolator;
15281
15282 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015283 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070015284
15285 // End transparent
15286 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015287 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070015288
15289 state = FADING;
15290
15291 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080015292 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070015293 }
15294 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070015295 }
Mike Cleronf116bf82009-09-27 19:14:12 -070015296
Svetoslav Ganova0156172011-06-26 17:55:44 -070015297 /**
15298 * Resuable callback for sending
15299 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
15300 */
15301 private class SendViewScrolledAccessibilityEvent implements Runnable {
15302 public volatile boolean mIsPending;
15303
15304 public void run() {
15305 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
15306 mIsPending = false;
15307 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015308 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015309
15310 /**
15311 * <p>
15312 * This class represents a delegate that can be registered in a {@link View}
15313 * to enhance accessibility support via composition rather via inheritance.
15314 * It is specifically targeted to widget developers that extend basic View
15315 * classes i.e. classes in package android.view, that would like their
15316 * applications to be backwards compatible.
15317 * </p>
15318 * <p>
15319 * A scenario in which a developer would like to use an accessibility delegate
15320 * is overriding a method introduced in a later API version then the minimal API
15321 * version supported by the application. For example, the method
15322 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
15323 * in API version 4 when the accessibility APIs were first introduced. If a
15324 * developer would like his application to run on API version 4 devices (assuming
15325 * all other APIs used by the application are version 4 or lower) and take advantage
15326 * of this method, instead of overriding the method which would break the application's
15327 * backwards compatibility, he can override the corresponding method in this
15328 * delegate and register the delegate in the target View if the API version of
15329 * the system is high enough i.e. the API version is same or higher to the API
15330 * version that introduced
15331 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
15332 * </p>
15333 * <p>
15334 * Here is an example implementation:
15335 * </p>
15336 * <code><pre><p>
15337 * if (Build.VERSION.SDK_INT >= 14) {
15338 * // If the API version is equal of higher than the version in
15339 * // which onInitializeAccessibilityNodeInfo was introduced we
15340 * // register a delegate with a customized implementation.
15341 * View view = findViewById(R.id.view_id);
15342 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
15343 * public void onInitializeAccessibilityNodeInfo(View host,
15344 * AccessibilityNodeInfo info) {
15345 * // Let the default implementation populate the info.
15346 * super.onInitializeAccessibilityNodeInfo(host, info);
15347 * // Set some other information.
15348 * info.setEnabled(host.isEnabled());
15349 * }
15350 * });
15351 * }
15352 * </code></pre></p>
15353 * <p>
15354 * This delegate contains methods that correspond to the accessibility methods
15355 * in View. If a delegate has been specified the implementation in View hands
15356 * off handling to the corresponding method in this delegate. The default
15357 * implementation the delegate methods behaves exactly as the corresponding
15358 * method in View for the case of no accessibility delegate been set. Hence,
15359 * to customize the behavior of a View method, clients can override only the
15360 * corresponding delegate method without altering the behavior of the rest
15361 * accessibility related methods of the host view.
15362 * </p>
15363 */
15364 public static class AccessibilityDelegate {
15365
15366 /**
15367 * Sends an accessibility event of the given type. If accessibility is not
15368 * enabled this method has no effect.
15369 * <p>
15370 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
15371 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
15372 * been set.
15373 * </p>
15374 *
15375 * @param host The View hosting the delegate.
15376 * @param eventType The type of the event to send.
15377 *
15378 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
15379 */
15380 public void sendAccessibilityEvent(View host, int eventType) {
15381 host.sendAccessibilityEventInternal(eventType);
15382 }
15383
15384 /**
15385 * Sends an accessibility event. This method behaves exactly as
15386 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
15387 * empty {@link AccessibilityEvent} and does not perform a check whether
15388 * accessibility is enabled.
15389 * <p>
15390 * The default implementation behaves as
15391 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15392 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
15393 * the case of no accessibility delegate been set.
15394 * </p>
15395 *
15396 * @param host The View hosting the delegate.
15397 * @param event The event to send.
15398 *
15399 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15400 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15401 */
15402 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
15403 host.sendAccessibilityEventUncheckedInternal(event);
15404 }
15405
15406 /**
15407 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
15408 * to its children for adding their text content to the event.
15409 * <p>
15410 * The default implementation behaves as
15411 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15412 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
15413 * the case of no accessibility delegate been set.
15414 * </p>
15415 *
15416 * @param host The View hosting the delegate.
15417 * @param event The event.
15418 * @return True if the event population was completed.
15419 *
15420 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15421 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15422 */
15423 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15424 return host.dispatchPopulateAccessibilityEventInternal(event);
15425 }
15426
15427 /**
15428 * Gives a chance to the host View to populate the accessibility event with its
15429 * text content.
15430 * <p>
15431 * The default implementation behaves as
15432 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
15433 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
15434 * the case of no accessibility delegate been set.
15435 * </p>
15436 *
15437 * @param host The View hosting the delegate.
15438 * @param event The accessibility event which to populate.
15439 *
15440 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
15441 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
15442 */
15443 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15444 host.onPopulateAccessibilityEventInternal(event);
15445 }
15446
15447 /**
15448 * Initializes an {@link AccessibilityEvent} with information about the
15449 * the host View which is the event source.
15450 * <p>
15451 * The default implementation behaves as
15452 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
15453 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
15454 * the case of no accessibility delegate been set.
15455 * </p>
15456 *
15457 * @param host The View hosting the delegate.
15458 * @param event The event to initialize.
15459 *
15460 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
15461 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
15462 */
15463 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
15464 host.onInitializeAccessibilityEventInternal(event);
15465 }
15466
15467 /**
15468 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
15469 * <p>
15470 * The default implementation behaves as
15471 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15472 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
15473 * the case of no accessibility delegate been set.
15474 * </p>
15475 *
15476 * @param host The View hosting the delegate.
15477 * @param info The instance to initialize.
15478 *
15479 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15480 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15481 */
15482 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
15483 host.onInitializeAccessibilityNodeInfoInternal(info);
15484 }
15485
15486 /**
15487 * Called when a child of the host View has requested sending an
15488 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
15489 * to augment the event.
15490 * <p>
15491 * The default implementation behaves as
15492 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15493 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
15494 * the case of no accessibility delegate been set.
15495 * </p>
15496 *
15497 * @param host The View hosting the delegate.
15498 * @param child The child which requests sending the event.
15499 * @param event The event to be sent.
15500 * @return True if the event should be sent
15501 *
15502 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15503 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15504 */
15505 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
15506 AccessibilityEvent event) {
15507 return host.onRequestSendAccessibilityEventInternal(child, event);
15508 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070015509
15510 /**
15511 * Gets the provider for managing a virtual view hierarchy rooted at this View
15512 * and reported to {@link android.accessibilityservice.AccessibilityService}s
15513 * that explore the window content.
15514 * <p>
15515 * The default implementation behaves as
15516 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
15517 * the case of no accessibility delegate been set.
15518 * </p>
15519 *
15520 * @return The provider.
15521 *
15522 * @see AccessibilityNodeProvider
15523 */
15524 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
15525 return null;
15526 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015528}