blob: aea7c302bfdf7a2584c4ec6e578216b352351486 [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 /**
Adam Powell637d3372010-08-25 14:37:03 -07001997 * Controls the over-scroll mode for this view.
1998 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1999 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2000 * and {@link #OVER_SCROLL_NEVER}.
2001 */
2002 private int mOverScrollMode;
2003
2004 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 * The parent this view is attached to.
2006 * {@hide}
2007 *
2008 * @see #getParent()
2009 */
2010 protected ViewParent mParent;
2011
2012 /**
2013 * {@hide}
2014 */
2015 AttachInfo mAttachInfo;
2016
2017 /**
2018 * {@hide}
2019 */
Romain Guy809a7f62009-05-14 15:44:42 -07002020 @ViewDebug.ExportedProperty(flagMapping = {
2021 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2022 name = "FORCE_LAYOUT"),
2023 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2024 name = "LAYOUT_REQUIRED"),
2025 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002026 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002027 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2028 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2029 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2030 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2031 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002033 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034
2035 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002036 * This view's request for the visibility of the status bar.
2037 * @hide
2038 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002039 @ViewDebug.ExportedProperty(flagMapping = {
2040 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2041 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2042 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2043 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2044 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2045 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2046 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2047 equals = SYSTEM_UI_FLAG_VISIBLE,
2048 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2049 })
Joe Onorato664644d2011-01-23 17:53:23 -08002050 int mSystemUiVisibility;
2051
2052 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 * Count of how many windows this view has been attached to.
2054 */
2055 int mWindowAttachCount;
2056
2057 /**
2058 * The layout parameters associated with this view and used by the parent
2059 * {@link android.view.ViewGroup} to determine how this view should be
2060 * laid out.
2061 * {@hide}
2062 */
2063 protected ViewGroup.LayoutParams mLayoutParams;
2064
2065 /**
2066 * The view flags hold various views states.
2067 * {@hide}
2068 */
2069 @ViewDebug.ExportedProperty
2070 int mViewFlags;
2071
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002072 static class TransformationInfo {
2073 /**
2074 * The transform matrix for the View. This transform is calculated internally
2075 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2076 * is used by default. Do *not* use this variable directly; instead call
2077 * getMatrix(), which will automatically recalculate the matrix if necessary
2078 * to get the correct matrix based on the latest rotation and scale properties.
2079 */
2080 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002081
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002082 /**
2083 * The transform matrix for the View. This transform is calculated internally
2084 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2085 * is used by default. Do *not* use this variable directly; instead call
2086 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2087 * to get the correct matrix based on the latest rotation and scale properties.
2088 */
2089 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002090
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002091 /**
2092 * An internal variable that tracks whether we need to recalculate the
2093 * transform matrix, based on whether the rotation or scaleX/Y properties
2094 * have changed since the matrix was last calculated.
2095 */
2096 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002097
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002098 /**
2099 * An internal variable that tracks whether we need to recalculate the
2100 * transform matrix, based on whether the rotation or scaleX/Y properties
2101 * have changed since the matrix was last calculated.
2102 */
2103 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002104
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002105 /**
2106 * A 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. This variable
2109 * is only valid after a call to updateMatrix() or to a function that
2110 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2111 */
2112 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002113
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002114 /**
2115 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2116 */
2117 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002118
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002119 /**
2120 * This matrix is used when computing the matrix for 3D rotations.
2121 */
2122 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002123
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002124 /**
2125 * These prev values are used to recalculate a centered pivot point when necessary. The
2126 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2127 * set), so thes values are only used then as well.
2128 */
2129 private int mPrevWidth = -1;
2130 private int mPrevHeight = -1;
2131
2132 /**
2133 * The degrees rotation around the vertical axis through the pivot point.
2134 */
2135 @ViewDebug.ExportedProperty
2136 float mRotationY = 0f;
2137
2138 /**
2139 * The degrees rotation around the horizontal axis through the pivot point.
2140 */
2141 @ViewDebug.ExportedProperty
2142 float mRotationX = 0f;
2143
2144 /**
2145 * The degrees rotation around the pivot point.
2146 */
2147 @ViewDebug.ExportedProperty
2148 float mRotation = 0f;
2149
2150 /**
2151 * The amount of translation of the object away from its left property (post-layout).
2152 */
2153 @ViewDebug.ExportedProperty
2154 float mTranslationX = 0f;
2155
2156 /**
2157 * The amount of translation of the object away from its top property (post-layout).
2158 */
2159 @ViewDebug.ExportedProperty
2160 float mTranslationY = 0f;
2161
2162 /**
2163 * The amount of scale in the x direction around the pivot point. A
2164 * value of 1 means no scaling is applied.
2165 */
2166 @ViewDebug.ExportedProperty
2167 float mScaleX = 1f;
2168
2169 /**
2170 * The amount of scale in the y direction around the pivot point. A
2171 * value of 1 means no scaling is applied.
2172 */
2173 @ViewDebug.ExportedProperty
2174 float mScaleY = 1f;
2175
2176 /**
Chet Haasea33de552012-02-03 16:28:24 -08002177 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002178 */
2179 @ViewDebug.ExportedProperty
2180 float mPivotX = 0f;
2181
2182 /**
Chet Haasea33de552012-02-03 16:28:24 -08002183 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002184 */
2185 @ViewDebug.ExportedProperty
2186 float mPivotY = 0f;
2187
2188 /**
2189 * The opacity of the View. This is a value from 0 to 1, where 0 means
2190 * completely transparent and 1 means completely opaque.
2191 */
2192 @ViewDebug.ExportedProperty
2193 float mAlpha = 1f;
2194 }
2195
2196 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002197
Joe Malin32736f02011-01-19 16:14:20 -08002198 private boolean mLastIsOpaque;
2199
Chet Haasefd2b0022010-08-06 13:08:56 -07002200 /**
2201 * Convenience value to check for float values that are close enough to zero to be considered
2202 * zero.
2203 */
Romain Guy2542d192010-08-18 11:47:12 -07002204 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002205
2206 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 * The distance in pixels from the left edge of this view's parent
2208 * to the left edge of this view.
2209 * {@hide}
2210 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002211 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 protected int mLeft;
2213 /**
2214 * The distance in pixels from the left edge of this view's parent
2215 * to the right edge of this view.
2216 * {@hide}
2217 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002218 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 protected int mRight;
2220 /**
2221 * The distance in pixels from the top edge of this view's parent
2222 * to the top 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 mTop;
2227 /**
2228 * The distance in pixels from the top edge of this view's parent
2229 * to the bottom 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 mBottom;
2234
2235 /**
2236 * The offset, in pixels, by which the content of this view is scrolled
2237 * horizontally.
2238 * {@hide}
2239 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002240 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 protected int mScrollX;
2242 /**
2243 * The offset, in pixels, by which the content of this view is scrolled
2244 * vertically.
2245 * {@hide}
2246 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002247 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 protected int mScrollY;
2249
2250 /**
2251 * The left padding in pixels, that is the distance in pixels between the
2252 * left edge of this view and the left edge of its content.
2253 * {@hide}
2254 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002255 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 protected int mPaddingLeft;
2257 /**
2258 * The right padding in pixels, that is the distance in pixels between the
2259 * right edge of this view and the right edge of its content.
2260 * {@hide}
2261 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002262 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 protected int mPaddingRight;
2264 /**
2265 * The top padding in pixels, that is the distance in pixels between the
2266 * top edge of this view and the top 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 mPaddingTop;
2271 /**
2272 * The bottom padding in pixels, that is the distance in pixels between the
2273 * bottom edge of this view and the bottom 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 mPaddingBottom;
2278
2279 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002280 * Briefly describes the view and is primarily used for accessibility support.
2281 */
2282 private CharSequence mContentDescription;
2283
2284 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002286 *
2287 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002289 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002290 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291
2292 /**
2293 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002294 *
2295 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002297 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002298 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002300 /**
Adam Powell20232d02010-12-08 21:08:53 -08002301 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002302 *
2303 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002304 */
2305 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002306 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002307
2308 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002309 * Cache if the user padding is relative.
2310 *
2311 */
2312 @ViewDebug.ExportedProperty(category = "padding")
2313 boolean mUserPaddingRelative;
2314
2315 /**
2316 * Cache the paddingStart set by the user to append to the scrollbar's size.
2317 *
2318 */
2319 @ViewDebug.ExportedProperty(category = "padding")
2320 int mUserPaddingStart;
2321
2322 /**
2323 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2324 *
2325 */
2326 @ViewDebug.ExportedProperty(category = "padding")
2327 int mUserPaddingEnd;
2328
2329 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002330 * @hide
2331 */
2332 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2333 /**
2334 * @hide
2335 */
2336 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 private Drawable mBGDrawable;
2339
2340 private int mBackgroundResource;
2341 private boolean mBackgroundSizeChanged;
2342
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002343 static class ListenerInfo {
2344 /**
2345 * Listener used to dispatch focus change events.
2346 * This field should be made private, so it is hidden from the SDK.
2347 * {@hide}
2348 */
2349 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002351 /**
2352 * Listeners for layout change events.
2353 */
2354 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002355
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002356 /**
2357 * Listeners for attach events.
2358 */
2359 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002360
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002361 /**
2362 * Listener used to dispatch click events.
2363 * This field should be made private, so it is hidden from the SDK.
2364 * {@hide}
2365 */
2366 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002368 /**
2369 * Listener used to dispatch long click events.
2370 * This field should be made private, so it is hidden from the SDK.
2371 * {@hide}
2372 */
2373 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002375 /**
2376 * Listener used to build the context menu.
2377 * This field should be made private, so it is hidden from the SDK.
2378 * {@hide}
2379 */
2380 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002382 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002384 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002386 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002387
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002388 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002389
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002390 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002391
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002392 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2393 }
2394
2395 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 /**
2398 * The application environment this view lives in.
2399 * This field should be made private, so it is hidden from the SDK.
2400 * {@hide}
2401 */
2402 protected Context mContext;
2403
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002404 private final Resources mResources;
2405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 private ScrollabilityCache mScrollCache;
2407
2408 private int[] mDrawableState = null;
2409
Romain Guy0211a0a2011-02-14 16:34:59 -08002410 /**
2411 * Set to true when drawing cache is enabled and cannot be created.
2412 *
2413 * @hide
2414 */
2415 public boolean mCachingFailed;
2416
Romain Guy02890fd2010-08-06 17:58:44 -07002417 private Bitmap mDrawingCache;
2418 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002419 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002420 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421
2422 /**
2423 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2424 * the user may specify which view to go to next.
2425 */
2426 private int mNextFocusLeftId = View.NO_ID;
2427
2428 /**
2429 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2430 * the user may specify which view to go to next.
2431 */
2432 private int mNextFocusRightId = View.NO_ID;
2433
2434 /**
2435 * When this view has focus and the next focus is {@link #FOCUS_UP},
2436 * the user may specify which view to go to next.
2437 */
2438 private int mNextFocusUpId = View.NO_ID;
2439
2440 /**
2441 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2442 * the user may specify which view to go to next.
2443 */
2444 private int mNextFocusDownId = View.NO_ID;
2445
Jeff Brown4e6319b2010-12-13 10:36:51 -08002446 /**
2447 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2448 * the user may specify which view to go to next.
2449 */
2450 int mNextFocusForwardId = View.NO_ID;
2451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002453 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002454 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002455 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 private UnsetPressedState mUnsetPressedState;
2458
2459 /**
2460 * Whether the long press's action has been invoked. The tap's action is invoked on the
2461 * up event while a long press is invoked as soon as the long press duration is reached, so
2462 * a long press could be performed before the tap is checked, in which case the tap's action
2463 * should not be invoked.
2464 */
2465 private boolean mHasPerformedLongPress;
2466
2467 /**
2468 * The minimum height of the view. We'll try our best to have the height
2469 * of this view to at least this amount.
2470 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002471 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 private int mMinHeight;
2473
2474 /**
2475 * The minimum width of the view. We'll try our best to have the width
2476 * of this view to at least this amount.
2477 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002478 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 private int mMinWidth;
2480
2481 /**
2482 * The delegate to handle touch events that are physically in this view
2483 * but should be handled by another view.
2484 */
2485 private TouchDelegate mTouchDelegate = null;
2486
2487 /**
2488 * Solid color to use as a background when creating the drawing cache. Enables
2489 * the cache to use 16 bit bitmaps instead of 32 bit.
2490 */
2491 private int mDrawingCacheBackgroundColor = 0;
2492
2493 /**
2494 * Special tree observer used when mAttachInfo is null.
2495 */
2496 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002497
Adam Powelle14579b2009-12-16 18:39:52 -08002498 /**
2499 * Cache the touch slop from the context that created the view.
2500 */
2501 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002504 * Object that handles automatic animation of view properties.
2505 */
2506 private ViewPropertyAnimator mAnimator = null;
2507
2508 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002509 * Flag indicating that a drag can cross window boundaries. When
2510 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2511 * with this flag set, all visible applications will be able to participate
2512 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002513 *
2514 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002515 */
2516 public static final int DRAG_FLAG_GLOBAL = 1;
2517
2518 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002519 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2520 */
2521 private float mVerticalScrollFactor;
2522
2523 /**
Adam Powell20232d02010-12-08 21:08:53 -08002524 * Position of the vertical scroll bar.
2525 */
2526 private int mVerticalScrollbarPosition;
2527
2528 /**
2529 * Position the scroll bar at the default position as determined by the system.
2530 */
2531 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2532
2533 /**
2534 * Position the scroll bar along the left edge.
2535 */
2536 public static final int SCROLLBAR_POSITION_LEFT = 1;
2537
2538 /**
2539 * Position the scroll bar along the right edge.
2540 */
2541 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2542
2543 /**
Romain Guy171c5922011-01-06 10:04:23 -08002544 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002545 *
2546 * @see #getLayerType()
2547 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002548 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002549 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002550 */
2551 public static final int LAYER_TYPE_NONE = 0;
2552
2553 /**
2554 * <p>Indicates that the view has a software layer. A software layer is backed
2555 * by a bitmap and causes the view to be rendered using Android's software
2556 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002557 *
Romain Guy171c5922011-01-06 10:04:23 -08002558 * <p>Software layers have various usages:</p>
2559 * <p>When the application is not using hardware acceleration, a software layer
2560 * is useful to apply a specific color filter and/or blending mode and/or
2561 * translucency to a view and all its children.</p>
2562 * <p>When the application is using hardware acceleration, a software layer
2563 * is useful to render drawing primitives not supported by the hardware
2564 * accelerated pipeline. It can also be used to cache a complex view tree
2565 * into a texture and reduce the complexity of drawing operations. For instance,
2566 * when animating a complex view tree with a translation, a software layer can
2567 * be used to render the view tree only once.</p>
2568 * <p>Software layers should be avoided when the affected view tree updates
2569 * often. Every update will require to re-render the software layer, which can
2570 * potentially be slow (particularly when hardware acceleration is turned on
2571 * since the layer will have to be uploaded into a hardware texture after every
2572 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002573 *
2574 * @see #getLayerType()
2575 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002576 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002577 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002578 */
2579 public static final int LAYER_TYPE_SOFTWARE = 1;
2580
2581 /**
2582 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2583 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2584 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2585 * rendering pipeline, but only if hardware acceleration is turned on for the
2586 * view hierarchy. When hardware acceleration is turned off, hardware layers
2587 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002588 *
Romain Guy171c5922011-01-06 10:04:23 -08002589 * <p>A hardware layer is useful to apply a specific color filter and/or
2590 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002591 * <p>A hardware layer can be used to cache a complex view tree into a
2592 * texture and reduce the complexity of drawing operations. For instance,
2593 * when animating a complex view tree with a translation, a hardware layer can
2594 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002595 * <p>A hardware layer can also be used to increase the rendering quality when
2596 * rotation transformations are applied on a view. It can also be used to
2597 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002598 *
2599 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002600 * @see #setLayerType(int, android.graphics.Paint)
2601 * @see #LAYER_TYPE_NONE
2602 * @see #LAYER_TYPE_SOFTWARE
2603 */
2604 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002605
Romain Guy3aaff3a2011-01-12 14:18:47 -08002606 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2607 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2608 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2609 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2610 })
Romain Guy171c5922011-01-06 10:04:23 -08002611 int mLayerType = LAYER_TYPE_NONE;
2612 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002613 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002614
2615 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002616 * Set to true when the view is sending hover accessibility events because it
2617 * is the innermost hovered view.
2618 */
2619 private boolean mSendingHoverAccessibilityEvents;
2620
2621 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002622 * Delegate for injecting accessiblity functionality.
2623 */
2624 AccessibilityDelegate mAccessibilityDelegate;
2625
2626 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002627 * Text direction is inherited thru {@link ViewGroup}
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002628 */
2629 public static final int TEXT_DIRECTION_INHERIT = 0;
2630
2631 /**
2632 * Text direction is using "first strong algorithm". The first strong directional character
2633 * determines the paragraph direction. If there is no strong directional character, the
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002634 * paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002635 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002636 */
2637 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2638
2639 /**
2640 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2641 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002642 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002643 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002644 */
2645 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2646
2647 /**
2648 * Text direction is forced to LTR.
2649 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002650 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002651 public static final int TEXT_DIRECTION_LTR = 3;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002652
2653 /**
2654 * Text direction is forced to RTL.
2655 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002656 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002657 public static final int TEXT_DIRECTION_RTL = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002658
2659 /**
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002660 * Text direction is coming from the system Locale.
2661 *
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002662 */
2663 public static final int TEXT_DIRECTION_LOCALE = 5;
2664
2665 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002666 * Default text direction is inherited
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07002667 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002668 */
2669 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2670
2671 /**
2672 * The text direction that has been defined by {@link #setTextDirection(int)}.
2673 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002674 */
2675 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002676 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2677 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2678 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2679 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002680 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
2681 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002682 })
Doug Feltcb3791202011-07-07 11:57:48 -07002683 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002684
2685 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002686 * The resolved text direction. This needs resolution if the value is
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002687 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if it is
Doug Feltcb3791202011-07-07 11:57:48 -07002688 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2689 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002690 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002691 */
2692 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002693 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2694 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2695 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002696 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002697 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
2698 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002699 })
Doug Feltcb3791202011-07-07 11:57:48 -07002700 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002701
2702 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002703 * Consistency verifier for debugging purposes.
2704 * @hide
2705 */
2706 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2707 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2708 new InputEventConsistencyVerifier(this, 0) : null;
2709
2710 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 * Simple constructor to use when creating a view from code.
2712 *
2713 * @param context The Context the view is running in, through which it can
2714 * access the current theme, resources, etc.
2715 */
2716 public View(Context context) {
2717 mContext = context;
2718 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002719 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002720 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002721 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002722 mUserPaddingStart = -1;
2723 mUserPaddingEnd = -1;
2724 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 }
2726
2727 /**
2728 * Constructor that is called when inflating a view from XML. This is called
2729 * when a view is being constructed from an XML file, supplying attributes
2730 * that were specified in the XML file. This version uses a default style of
2731 * 0, so the only attribute values applied are those in the Context's Theme
2732 * and the given AttributeSet.
2733 *
2734 * <p>
2735 * The method onFinishInflate() will be called after all children have been
2736 * added.
2737 *
2738 * @param context The Context the view is running in, through which it can
2739 * access the current theme, resources, etc.
2740 * @param attrs The attributes of the XML tag that is inflating the view.
2741 * @see #View(Context, AttributeSet, int)
2742 */
2743 public View(Context context, AttributeSet attrs) {
2744 this(context, attrs, 0);
2745 }
2746
2747 /**
2748 * Perform inflation from XML and apply a class-specific base style. This
2749 * constructor of View allows subclasses to use their own base style when
2750 * they are inflating. For example, a Button class's constructor would call
2751 * this version of the super class constructor and supply
2752 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2753 * the theme's button style to modify all of the base view attributes (in
2754 * particular its background) as well as the Button class's attributes.
2755 *
2756 * @param context The Context the view is running in, through which it can
2757 * access the current theme, resources, etc.
2758 * @param attrs The attributes of the XML tag that is inflating the view.
2759 * @param defStyle The default style to apply to this view. If 0, no style
2760 * will be applied (beyond what is included in the theme). This may
2761 * either be an attribute resource, whose value will be retrieved
2762 * from the current theme, or an explicit style resource.
2763 * @see #View(Context, AttributeSet)
2764 */
2765 public View(Context context, AttributeSet attrs, int defStyle) {
2766 this(context);
2767
2768 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2769 defStyle, 0);
2770
2771 Drawable background = null;
2772
2773 int leftPadding = -1;
2774 int topPadding = -1;
2775 int rightPadding = -1;
2776 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002777 int startPadding = -1;
2778 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779
2780 int padding = -1;
2781
2782 int viewFlagValues = 0;
2783 int viewFlagMasks = 0;
2784
2785 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 int x = 0;
2788 int y = 0;
2789
Chet Haase73066682010-11-29 15:55:32 -08002790 float tx = 0;
2791 float ty = 0;
2792 float rotation = 0;
2793 float rotationX = 0;
2794 float rotationY = 0;
2795 float sx = 1f;
2796 float sy = 1f;
2797 boolean transformSet = false;
2798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2800
Adam Powell637d3372010-08-25 14:37:03 -07002801 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 final int N = a.getIndexCount();
2803 for (int i = 0; i < N; i++) {
2804 int attr = a.getIndex(i);
2805 switch (attr) {
2806 case com.android.internal.R.styleable.View_background:
2807 background = a.getDrawable(attr);
2808 break;
2809 case com.android.internal.R.styleable.View_padding:
2810 padding = a.getDimensionPixelSize(attr, -1);
2811 break;
2812 case com.android.internal.R.styleable.View_paddingLeft:
2813 leftPadding = a.getDimensionPixelSize(attr, -1);
2814 break;
2815 case com.android.internal.R.styleable.View_paddingTop:
2816 topPadding = a.getDimensionPixelSize(attr, -1);
2817 break;
2818 case com.android.internal.R.styleable.View_paddingRight:
2819 rightPadding = a.getDimensionPixelSize(attr, -1);
2820 break;
2821 case com.android.internal.R.styleable.View_paddingBottom:
2822 bottomPadding = a.getDimensionPixelSize(attr, -1);
2823 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002824 case com.android.internal.R.styleable.View_paddingStart:
2825 startPadding = a.getDimensionPixelSize(attr, -1);
2826 break;
2827 case com.android.internal.R.styleable.View_paddingEnd:
2828 endPadding = a.getDimensionPixelSize(attr, -1);
2829 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 case com.android.internal.R.styleable.View_scrollX:
2831 x = a.getDimensionPixelOffset(attr, 0);
2832 break;
2833 case com.android.internal.R.styleable.View_scrollY:
2834 y = a.getDimensionPixelOffset(attr, 0);
2835 break;
Chet Haase73066682010-11-29 15:55:32 -08002836 case com.android.internal.R.styleable.View_alpha:
2837 setAlpha(a.getFloat(attr, 1f));
2838 break;
2839 case com.android.internal.R.styleable.View_transformPivotX:
2840 setPivotX(a.getDimensionPixelOffset(attr, 0));
2841 break;
2842 case com.android.internal.R.styleable.View_transformPivotY:
2843 setPivotY(a.getDimensionPixelOffset(attr, 0));
2844 break;
2845 case com.android.internal.R.styleable.View_translationX:
2846 tx = a.getDimensionPixelOffset(attr, 0);
2847 transformSet = true;
2848 break;
2849 case com.android.internal.R.styleable.View_translationY:
2850 ty = a.getDimensionPixelOffset(attr, 0);
2851 transformSet = true;
2852 break;
2853 case com.android.internal.R.styleable.View_rotation:
2854 rotation = a.getFloat(attr, 0);
2855 transformSet = true;
2856 break;
2857 case com.android.internal.R.styleable.View_rotationX:
2858 rotationX = a.getFloat(attr, 0);
2859 transformSet = true;
2860 break;
2861 case com.android.internal.R.styleable.View_rotationY:
2862 rotationY = a.getFloat(attr, 0);
2863 transformSet = true;
2864 break;
2865 case com.android.internal.R.styleable.View_scaleX:
2866 sx = a.getFloat(attr, 1f);
2867 transformSet = true;
2868 break;
2869 case com.android.internal.R.styleable.View_scaleY:
2870 sy = a.getFloat(attr, 1f);
2871 transformSet = true;
2872 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 case com.android.internal.R.styleable.View_id:
2874 mID = a.getResourceId(attr, NO_ID);
2875 break;
2876 case com.android.internal.R.styleable.View_tag:
2877 mTag = a.getText(attr);
2878 break;
2879 case com.android.internal.R.styleable.View_fitsSystemWindows:
2880 if (a.getBoolean(attr, false)) {
2881 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2882 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2883 }
2884 break;
2885 case com.android.internal.R.styleable.View_focusable:
2886 if (a.getBoolean(attr, false)) {
2887 viewFlagValues |= FOCUSABLE;
2888 viewFlagMasks |= FOCUSABLE_MASK;
2889 }
2890 break;
2891 case com.android.internal.R.styleable.View_focusableInTouchMode:
2892 if (a.getBoolean(attr, false)) {
2893 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2894 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2895 }
2896 break;
2897 case com.android.internal.R.styleable.View_clickable:
2898 if (a.getBoolean(attr, false)) {
2899 viewFlagValues |= CLICKABLE;
2900 viewFlagMasks |= CLICKABLE;
2901 }
2902 break;
2903 case com.android.internal.R.styleable.View_longClickable:
2904 if (a.getBoolean(attr, false)) {
2905 viewFlagValues |= LONG_CLICKABLE;
2906 viewFlagMasks |= LONG_CLICKABLE;
2907 }
2908 break;
2909 case com.android.internal.R.styleable.View_saveEnabled:
2910 if (!a.getBoolean(attr, true)) {
2911 viewFlagValues |= SAVE_DISABLED;
2912 viewFlagMasks |= SAVE_DISABLED_MASK;
2913 }
2914 break;
2915 case com.android.internal.R.styleable.View_duplicateParentState:
2916 if (a.getBoolean(attr, false)) {
2917 viewFlagValues |= DUPLICATE_PARENT_STATE;
2918 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2919 }
2920 break;
2921 case com.android.internal.R.styleable.View_visibility:
2922 final int visibility = a.getInt(attr, 0);
2923 if (visibility != 0) {
2924 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2925 viewFlagMasks |= VISIBILITY_MASK;
2926 }
2927 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002928 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002929 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002930 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002931 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002932 final int layoutDirection = a.getInt(attr, -1);
2933 if (layoutDirection != -1) {
2934 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002935 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002936 // Set to default (LAYOUT_DIRECTION_INHERIT)
2937 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002938 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002939 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002940 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 case com.android.internal.R.styleable.View_drawingCacheQuality:
2942 final int cacheQuality = a.getInt(attr, 0);
2943 if (cacheQuality != 0) {
2944 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2945 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2946 }
2947 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002948 case com.android.internal.R.styleable.View_contentDescription:
2949 mContentDescription = a.getString(attr);
2950 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2952 if (!a.getBoolean(attr, true)) {
2953 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2954 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2955 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002956 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2958 if (!a.getBoolean(attr, true)) {
2959 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2960 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2961 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002962 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 case R.styleable.View_scrollbars:
2964 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2965 if (scrollbars != SCROLLBARS_NONE) {
2966 viewFlagValues |= scrollbars;
2967 viewFlagMasks |= SCROLLBARS_MASK;
2968 initializeScrollbars(a);
2969 }
2970 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07002971 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07002973 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
2974 // Ignore the attribute starting with ICS
2975 break;
2976 }
2977 // With builds < ICS, fall through and apply fading edges
2978 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2980 if (fadingEdge != FADING_EDGE_NONE) {
2981 viewFlagValues |= fadingEdge;
2982 viewFlagMasks |= FADING_EDGE_MASK;
2983 initializeFadingEdge(a);
2984 }
2985 break;
2986 case R.styleable.View_scrollbarStyle:
2987 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2988 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2989 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2990 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2991 }
2992 break;
2993 case R.styleable.View_isScrollContainer:
2994 setScrollContainer = true;
2995 if (a.getBoolean(attr, false)) {
2996 setScrollContainer(true);
2997 }
2998 break;
2999 case com.android.internal.R.styleable.View_keepScreenOn:
3000 if (a.getBoolean(attr, false)) {
3001 viewFlagValues |= KEEP_SCREEN_ON;
3002 viewFlagMasks |= KEEP_SCREEN_ON;
3003 }
3004 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003005 case R.styleable.View_filterTouchesWhenObscured:
3006 if (a.getBoolean(attr, false)) {
3007 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3008 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3009 }
3010 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 case R.styleable.View_nextFocusLeft:
3012 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3013 break;
3014 case R.styleable.View_nextFocusRight:
3015 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3016 break;
3017 case R.styleable.View_nextFocusUp:
3018 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3019 break;
3020 case R.styleable.View_nextFocusDown:
3021 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3022 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003023 case R.styleable.View_nextFocusForward:
3024 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3025 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003026 case R.styleable.View_minWidth:
3027 mMinWidth = a.getDimensionPixelSize(attr, 0);
3028 break;
3029 case R.styleable.View_minHeight:
3030 mMinHeight = a.getDimensionPixelSize(attr, 0);
3031 break;
Romain Guy9a817362009-05-01 10:57:14 -07003032 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003033 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003034 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003035 + "be used within a restricted context");
3036 }
3037
Romain Guy9a817362009-05-01 10:57:14 -07003038 final String handlerName = a.getString(attr);
3039 if (handlerName != null) {
3040 setOnClickListener(new OnClickListener() {
3041 private Method mHandler;
3042
3043 public void onClick(View v) {
3044 if (mHandler == null) {
3045 try {
3046 mHandler = getContext().getClass().getMethod(handlerName,
3047 View.class);
3048 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003049 int id = getId();
3050 String idText = id == NO_ID ? "" : " with id '"
3051 + getContext().getResources().getResourceEntryName(
3052 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003053 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003054 handlerName + "(View) in the activity "
3055 + getContext().getClass() + " for onClick handler"
3056 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003057 }
3058 }
3059
3060 try {
3061 mHandler.invoke(getContext(), View.this);
3062 } catch (IllegalAccessException e) {
3063 throw new IllegalStateException("Could not execute non "
3064 + "public method of the activity", e);
3065 } catch (InvocationTargetException e) {
3066 throw new IllegalStateException("Could not execute "
3067 + "method of the activity", e);
3068 }
3069 }
3070 });
3071 }
3072 break;
Adam Powell637d3372010-08-25 14:37:03 -07003073 case R.styleable.View_overScrollMode:
3074 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3075 break;
Adam Powell20232d02010-12-08 21:08:53 -08003076 case R.styleable.View_verticalScrollbarPosition:
3077 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3078 break;
Romain Guy171c5922011-01-06 10:04:23 -08003079 case R.styleable.View_layerType:
3080 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3081 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003082 case R.styleable.View_textDirection:
3083 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
3084 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 }
3086 }
3087
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003088 a.recycle();
3089
Adam Powell637d3372010-08-25 14:37:03 -07003090 setOverScrollMode(overScrollMode);
3091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 if (background != null) {
3093 setBackgroundDrawable(background);
3094 }
3095
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003096 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
3097
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003098 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3099 // layout direction). Those cached values will be used later during padding resolution.
3100 mUserPaddingStart = startPadding;
3101 mUserPaddingEnd = endPadding;
3102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 if (padding >= 0) {
3104 leftPadding = padding;
3105 topPadding = padding;
3106 rightPadding = padding;
3107 bottomPadding = padding;
3108 }
3109
3110 // If the user specified the padding (either with android:padding or
3111 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3112 // use the default padding or the padding from the background drawable
3113 // (stored at this point in mPadding*)
3114 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3115 topPadding >= 0 ? topPadding : mPaddingTop,
3116 rightPadding >= 0 ? rightPadding : mPaddingRight,
3117 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3118
3119 if (viewFlagMasks != 0) {
3120 setFlags(viewFlagValues, viewFlagMasks);
3121 }
3122
3123 // Needs to be called after mViewFlags is set
3124 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3125 recomputePadding();
3126 }
3127
3128 if (x != 0 || y != 0) {
3129 scrollTo(x, y);
3130 }
3131
Chet Haase73066682010-11-29 15:55:32 -08003132 if (transformSet) {
3133 setTranslationX(tx);
3134 setTranslationY(ty);
3135 setRotation(rotation);
3136 setRotationX(rotationX);
3137 setRotationY(rotationY);
3138 setScaleX(sx);
3139 setScaleY(sy);
3140 }
3141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3143 setScrollContainer(true);
3144 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003145
3146 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 }
3148
3149 /**
3150 * Non-public constructor for use in testing
3151 */
3152 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003153 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154 }
3155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003156 /**
3157 * <p>
3158 * Initializes the fading edges from a given set of styled attributes. This
3159 * method should be called by subclasses that need fading edges and when an
3160 * instance of these subclasses is created programmatically rather than
3161 * being inflated from XML. This method is automatically called when the XML
3162 * is inflated.
3163 * </p>
3164 *
3165 * @param a the styled attributes set to initialize the fading edges from
3166 */
3167 protected void initializeFadingEdge(TypedArray a) {
3168 initScrollCache();
3169
3170 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3171 R.styleable.View_fadingEdgeLength,
3172 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3173 }
3174
3175 /**
3176 * Returns the size of the vertical faded edges used to indicate that more
3177 * content in this view is visible.
3178 *
3179 * @return The size in pixels of the vertical faded edge or 0 if vertical
3180 * faded edges are not enabled for this view.
3181 * @attr ref android.R.styleable#View_fadingEdgeLength
3182 */
3183 public int getVerticalFadingEdgeLength() {
3184 if (isVerticalFadingEdgeEnabled()) {
3185 ScrollabilityCache cache = mScrollCache;
3186 if (cache != null) {
3187 return cache.fadingEdgeLength;
3188 }
3189 }
3190 return 0;
3191 }
3192
3193 /**
3194 * Set the size of the faded edge used to indicate that more content in this
3195 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003196 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3197 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3198 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 *
3200 * @param length The size in pixels of the faded edge used to indicate that more
3201 * content in this view is visible.
3202 */
3203 public void setFadingEdgeLength(int length) {
3204 initScrollCache();
3205 mScrollCache.fadingEdgeLength = length;
3206 }
3207
3208 /**
3209 * Returns the size of the horizontal faded edges used to indicate that more
3210 * content in this view is visible.
3211 *
3212 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3213 * faded edges are not enabled for this view.
3214 * @attr ref android.R.styleable#View_fadingEdgeLength
3215 */
3216 public int getHorizontalFadingEdgeLength() {
3217 if (isHorizontalFadingEdgeEnabled()) {
3218 ScrollabilityCache cache = mScrollCache;
3219 if (cache != null) {
3220 return cache.fadingEdgeLength;
3221 }
3222 }
3223 return 0;
3224 }
3225
3226 /**
3227 * Returns the width of the vertical scrollbar.
3228 *
3229 * @return The width in pixels of the vertical scrollbar or 0 if there
3230 * is no vertical scrollbar.
3231 */
3232 public int getVerticalScrollbarWidth() {
3233 ScrollabilityCache cache = mScrollCache;
3234 if (cache != null) {
3235 ScrollBarDrawable scrollBar = cache.scrollBar;
3236 if (scrollBar != null) {
3237 int size = scrollBar.getSize(true);
3238 if (size <= 0) {
3239 size = cache.scrollBarSize;
3240 }
3241 return size;
3242 }
3243 return 0;
3244 }
3245 return 0;
3246 }
3247
3248 /**
3249 * Returns the height of the horizontal scrollbar.
3250 *
3251 * @return The height in pixels of the horizontal scrollbar or 0 if
3252 * there is no horizontal scrollbar.
3253 */
3254 protected int getHorizontalScrollbarHeight() {
3255 ScrollabilityCache cache = mScrollCache;
3256 if (cache != null) {
3257 ScrollBarDrawable scrollBar = cache.scrollBar;
3258 if (scrollBar != null) {
3259 int size = scrollBar.getSize(false);
3260 if (size <= 0) {
3261 size = cache.scrollBarSize;
3262 }
3263 return size;
3264 }
3265 return 0;
3266 }
3267 return 0;
3268 }
3269
3270 /**
3271 * <p>
3272 * Initializes the scrollbars from a given set of styled attributes. This
3273 * method should be called by subclasses that need scrollbars and when an
3274 * instance of these subclasses is created programmatically rather than
3275 * being inflated from XML. This method is automatically called when the XML
3276 * is inflated.
3277 * </p>
3278 *
3279 * @param a the styled attributes set to initialize the scrollbars from
3280 */
3281 protected void initializeScrollbars(TypedArray a) {
3282 initScrollCache();
3283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003285
Mike Cleronf116bf82009-09-27 19:14:12 -07003286 if (scrollabilityCache.scrollBar == null) {
3287 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3288 }
Joe Malin32736f02011-01-19 16:14:20 -08003289
Romain Guy8bda2482010-03-02 11:42:11 -08003290 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291
Mike Cleronf116bf82009-09-27 19:14:12 -07003292 if (!fadeScrollbars) {
3293 scrollabilityCache.state = ScrollabilityCache.ON;
3294 }
3295 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003296
3297
Mike Cleronf116bf82009-09-27 19:14:12 -07003298 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3299 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3300 .getScrollBarFadeDuration());
3301 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3302 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3303 ViewConfiguration.getScrollDefaultDelay());
3304
Joe Malin32736f02011-01-19 16:14:20 -08003305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3307 com.android.internal.R.styleable.View_scrollbarSize,
3308 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3309
3310 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3311 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3312
3313 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3314 if (thumb != null) {
3315 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3316 }
3317
3318 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3319 false);
3320 if (alwaysDraw) {
3321 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3322 }
3323
3324 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3325 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3326
3327 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3328 if (thumb != null) {
3329 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3330 }
3331
3332 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3333 false);
3334 if (alwaysDraw) {
3335 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3336 }
3337
3338 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003339 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 }
3341
3342 /**
3343 * <p>
3344 * Initalizes the scrollability cache if necessary.
3345 * </p>
3346 */
3347 private void initScrollCache() {
3348 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003349 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 }
3351 }
3352
3353 /**
Adam Powell20232d02010-12-08 21:08:53 -08003354 * Set the position of the vertical scroll bar. Should be one of
3355 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3356 * {@link #SCROLLBAR_POSITION_RIGHT}.
3357 *
3358 * @param position Where the vertical scroll bar should be positioned.
3359 */
3360 public void setVerticalScrollbarPosition(int position) {
3361 if (mVerticalScrollbarPosition != position) {
3362 mVerticalScrollbarPosition = position;
3363 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003364 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003365 }
3366 }
3367
3368 /**
3369 * @return The position where the vertical scroll bar will show, if applicable.
3370 * @see #setVerticalScrollbarPosition(int)
3371 */
3372 public int getVerticalScrollbarPosition() {
3373 return mVerticalScrollbarPosition;
3374 }
3375
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003376 ListenerInfo getListenerInfo() {
3377 if (mListenerInfo != null) {
3378 return mListenerInfo;
3379 }
3380 mListenerInfo = new ListenerInfo();
3381 return mListenerInfo;
3382 }
3383
Adam Powell20232d02010-12-08 21:08:53 -08003384 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003385 * Register a callback to be invoked when focus of this view changed.
3386 *
3387 * @param l The callback that will run.
3388 */
3389 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003390 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 }
3392
3393 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003394 * Add a listener that will be called when the bounds of the view change due to
3395 * layout processing.
3396 *
3397 * @param listener The listener that will be called when layout bounds change.
3398 */
3399 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003400 ListenerInfo li = getListenerInfo();
3401 if (li.mOnLayoutChangeListeners == null) {
3402 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003403 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003404 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3405 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003406 }
Chet Haase21cd1382010-09-01 17:42:29 -07003407 }
3408
3409 /**
3410 * Remove a listener for layout changes.
3411 *
3412 * @param listener The listener for layout bounds change.
3413 */
3414 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003415 ListenerInfo li = mListenerInfo;
3416 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003417 return;
3418 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003419 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003420 }
3421
3422 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003423 * Add a listener for attach state changes.
3424 *
3425 * This listener will be called whenever this view is attached or detached
3426 * from a window. Remove the listener using
3427 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3428 *
3429 * @param listener Listener to attach
3430 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3431 */
3432 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003433 ListenerInfo li = getListenerInfo();
3434 if (li.mOnAttachStateChangeListeners == null) {
3435 li.mOnAttachStateChangeListeners
3436 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003437 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003438 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003439 }
3440
3441 /**
3442 * Remove a listener for attach state changes. The listener will receive no further
3443 * notification of window attach/detach events.
3444 *
3445 * @param listener Listener to remove
3446 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3447 */
3448 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003449 ListenerInfo li = mListenerInfo;
3450 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003451 return;
3452 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003453 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003454 }
3455
3456 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003457 * Returns the focus-change callback registered for this view.
3458 *
3459 * @return The callback, or null if one is not registered.
3460 */
3461 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003462 ListenerInfo li = mListenerInfo;
3463 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003464 }
3465
3466 /**
3467 * Register a callback to be invoked when this view is clicked. If this view is not
3468 * clickable, it becomes clickable.
3469 *
3470 * @param l The callback that will run
3471 *
3472 * @see #setClickable(boolean)
3473 */
3474 public void setOnClickListener(OnClickListener l) {
3475 if (!isClickable()) {
3476 setClickable(true);
3477 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003478 getListenerInfo().mOnClickListener = l;
3479 }
3480
3481 /**
3482 * Return whether this view has an attached OnClickListener. Returns
3483 * true if there is a listener, false if there is none.
3484 */
3485 public boolean hasOnClickListeners() {
3486 ListenerInfo li = mListenerInfo;
3487 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 }
3489
3490 /**
3491 * Register a callback to be invoked when this view is clicked and held. If this view is not
3492 * long clickable, it becomes long clickable.
3493 *
3494 * @param l The callback that will run
3495 *
3496 * @see #setLongClickable(boolean)
3497 */
3498 public void setOnLongClickListener(OnLongClickListener l) {
3499 if (!isLongClickable()) {
3500 setLongClickable(true);
3501 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003502 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 }
3504
3505 /**
3506 * Register a callback to be invoked when the context menu for this view is
3507 * being built. If this view is not long clickable, it becomes long clickable.
3508 *
3509 * @param l The callback that will run
3510 *
3511 */
3512 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3513 if (!isLongClickable()) {
3514 setLongClickable(true);
3515 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003516 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 }
3518
3519 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003520 * Call this view's OnClickListener, if it is defined. Performs all normal
3521 * actions associated with clicking: reporting accessibility event, playing
3522 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 *
3524 * @return True there was an assigned OnClickListener that was called, false
3525 * otherwise is returned.
3526 */
3527 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003528 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3529
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003530 ListenerInfo li = mListenerInfo;
3531 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003533 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 return true;
3535 }
3536
3537 return false;
3538 }
3539
3540 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003541 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3542 * this only calls the listener, and does not do any associated clicking
3543 * actions like reporting an accessibility event.
3544 *
3545 * @return True there was an assigned OnClickListener that was called, false
3546 * otherwise is returned.
3547 */
3548 public boolean callOnClick() {
3549 ListenerInfo li = mListenerInfo;
3550 if (li != null && li.mOnClickListener != null) {
3551 li.mOnClickListener.onClick(this);
3552 return true;
3553 }
3554 return false;
3555 }
3556
3557 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003558 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3559 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003560 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003561 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 */
3563 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003564 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003566 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003567 ListenerInfo li = mListenerInfo;
3568 if (li != null && li.mOnLongClickListener != null) {
3569 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 }
3571 if (!handled) {
3572 handled = showContextMenu();
3573 }
3574 if (handled) {
3575 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3576 }
3577 return handled;
3578 }
3579
3580 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003581 * Performs button-related actions during a touch down event.
3582 *
3583 * @param event The event.
3584 * @return True if the down was consumed.
3585 *
3586 * @hide
3587 */
3588 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3589 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3590 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3591 return true;
3592 }
3593 }
3594 return false;
3595 }
3596
3597 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003598 * Bring up the context menu for this view.
3599 *
3600 * @return Whether a context menu was displayed.
3601 */
3602 public boolean showContextMenu() {
3603 return getParent().showContextMenuForChild(this);
3604 }
3605
3606 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003607 * Bring up the context menu for this view, referring to the item under the specified point.
3608 *
3609 * @param x The referenced x coordinate.
3610 * @param y The referenced y coordinate.
3611 * @param metaState The keyboard modifiers that were pressed.
3612 * @return Whether a context menu was displayed.
3613 *
3614 * @hide
3615 */
3616 public boolean showContextMenu(float x, float y, int metaState) {
3617 return showContextMenu();
3618 }
3619
3620 /**
Adam Powell6e346362010-07-23 10:18:23 -07003621 * Start an action mode.
3622 *
3623 * @param callback Callback that will control the lifecycle of the action mode
3624 * @return The new action mode if it is started, null otherwise
3625 *
3626 * @see ActionMode
3627 */
3628 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08003629 ViewParent parent = getParent();
3630 if (parent == null) return null;
3631 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07003632 }
3633
3634 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003635 * Register a callback to be invoked when a key is pressed in this view.
3636 * @param l the key listener to attach to this view
3637 */
3638 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003639 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003640 }
3641
3642 /**
3643 * Register a callback to be invoked when a touch event is sent to this view.
3644 * @param l the touch listener to attach to this view
3645 */
3646 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003647 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003648 }
3649
3650 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003651 * Register a callback to be invoked when a generic motion event is sent to this view.
3652 * @param l the generic motion listener to attach to this view
3653 */
3654 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003655 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08003656 }
3657
3658 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003659 * Register a callback to be invoked when a hover event is sent to this view.
3660 * @param l the hover listener to attach to this view
3661 */
3662 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003663 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07003664 }
3665
3666 /**
Joe Malin32736f02011-01-19 16:14:20 -08003667 * Register a drag event listener callback object for this View. The parameter is
3668 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3669 * View, the system calls the
3670 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3671 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003672 */
3673 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003674 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07003675 }
3676
3677 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003678 * Give this view focus. This will cause
3679 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003680 *
3681 * Note: this does not check whether this {@link View} should get focus, it just
3682 * gives it focus no matter what. It should only be called internally by framework
3683 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3684 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003685 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3686 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 * focus moved when requestFocus() is called. It may not always
3688 * apply, in which case use the default View.FOCUS_DOWN.
3689 * @param previouslyFocusedRect The rectangle of the view that had focus
3690 * prior in this View's coordinate system.
3691 */
3692 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3693 if (DBG) {
3694 System.out.println(this + " requestFocus()");
3695 }
3696
3697 if ((mPrivateFlags & FOCUSED) == 0) {
3698 mPrivateFlags |= FOCUSED;
3699
3700 if (mParent != null) {
3701 mParent.requestChildFocus(this, this);
3702 }
3703
3704 onFocusChanged(true, direction, previouslyFocusedRect);
3705 refreshDrawableState();
3706 }
3707 }
3708
3709 /**
3710 * Request that a rectangle of this view be visible on the screen,
3711 * scrolling if necessary just enough.
3712 *
3713 * <p>A View should call this if it maintains some notion of which part
3714 * of its content is interesting. For example, a text editing view
3715 * should call this when its cursor moves.
3716 *
3717 * @param rectangle The rectangle.
3718 * @return Whether any parent scrolled.
3719 */
3720 public boolean requestRectangleOnScreen(Rect rectangle) {
3721 return requestRectangleOnScreen(rectangle, false);
3722 }
3723
3724 /**
3725 * Request that a rectangle of this view be visible on the screen,
3726 * scrolling if necessary just enough.
3727 *
3728 * <p>A View should call this if it maintains some notion of which part
3729 * of its content is interesting. For example, a text editing view
3730 * should call this when its cursor moves.
3731 *
3732 * <p>When <code>immediate</code> is set to true, scrolling will not be
3733 * animated.
3734 *
3735 * @param rectangle The rectangle.
3736 * @param immediate True to forbid animated scrolling, false otherwise
3737 * @return Whether any parent scrolled.
3738 */
3739 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3740 View child = this;
3741 ViewParent parent = mParent;
3742 boolean scrolled = false;
3743 while (parent != null) {
3744 scrolled |= parent.requestChildRectangleOnScreen(child,
3745 rectangle, immediate);
3746
3747 // offset rect so next call has the rectangle in the
3748 // coordinate system of its direct child.
3749 rectangle.offset(child.getLeft(), child.getTop());
3750 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3751
3752 if (!(parent instanceof View)) {
3753 break;
3754 }
Romain Guy8506ab42009-06-11 17:35:47 -07003755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003756 child = (View) parent;
3757 parent = child.getParent();
3758 }
3759 return scrolled;
3760 }
3761
3762 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08003763 * Called when this view wants to give up focus. If focus is cleared
3764 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
3765 * <p>
3766 * <strong>Note:</strong> When a View clears focus the framework is trying
3767 * to give focus to the first focusable View from the top. Hence, if this
3768 * View is the first from the top that can take focus, then its focus will
3769 * not be cleared nor will the focus change callback be invoked.
3770 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003771 */
3772 public void clearFocus() {
3773 if (DBG) {
3774 System.out.println(this + " clearFocus()");
3775 }
3776
3777 if ((mPrivateFlags & FOCUSED) != 0) {
3778 mPrivateFlags &= ~FOCUSED;
3779
3780 if (mParent != null) {
3781 mParent.clearChildFocus(this);
3782 }
3783
3784 onFocusChanged(false, 0, null);
3785 refreshDrawableState();
3786 }
3787 }
3788
3789 /**
3790 * Called to clear the focus of a view that is about to be removed.
3791 * Doesn't call clearChildFocus, which prevents this view from taking
3792 * focus again before it has been removed from the parent
3793 */
3794 void clearFocusForRemoval() {
3795 if ((mPrivateFlags & FOCUSED) != 0) {
3796 mPrivateFlags &= ~FOCUSED;
3797
3798 onFocusChanged(false, 0, null);
3799 refreshDrawableState();
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08003800
3801 // The view cleared focus and invoked the callbacks, so now is the
3802 // time to give focus to the the first focusable from the top to
3803 // ensure that the gain focus is announced after clear focus.
3804 getRootView().requestFocus(FOCUS_FORWARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 }
3806 }
3807
3808 /**
3809 * Called internally by the view system when a new view is getting focus.
3810 * This is what clears the old focus.
3811 */
3812 void unFocus() {
3813 if (DBG) {
3814 System.out.println(this + " unFocus()");
3815 }
3816
3817 if ((mPrivateFlags & FOCUSED) != 0) {
3818 mPrivateFlags &= ~FOCUSED;
3819
3820 onFocusChanged(false, 0, null);
3821 refreshDrawableState();
3822 }
3823 }
3824
3825 /**
3826 * Returns true if this view has focus iteself, or is the ancestor of the
3827 * view that has focus.
3828 *
3829 * @return True if this view has or contains focus, false otherwise.
3830 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003831 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003832 public boolean hasFocus() {
3833 return (mPrivateFlags & FOCUSED) != 0;
3834 }
3835
3836 /**
3837 * Returns true if this view is focusable or if it contains a reachable View
3838 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3839 * is a View whose parents do not block descendants focus.
3840 *
3841 * Only {@link #VISIBLE} views are considered focusable.
3842 *
3843 * @return True if the view is focusable or if the view contains a focusable
3844 * View, false otherwise.
3845 *
3846 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3847 */
3848 public boolean hasFocusable() {
3849 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3850 }
3851
3852 /**
3853 * Called by the view system when the focus state of this view changes.
3854 * When the focus change event is caused by directional navigation, direction
3855 * and previouslyFocusedRect provide insight into where the focus is coming from.
3856 * When overriding, be sure to call up through to the super class so that
3857 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003858 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003859 * @param gainFocus True if the View has focus; false otherwise.
3860 * @param direction The direction focus has moved when requestFocus()
3861 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003862 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3863 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3864 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003865 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3866 * system, of the previously focused view. If applicable, this will be
3867 * passed in as finer grained information about where the focus is coming
3868 * from (in addition to direction). Will be <code>null</code> otherwise.
3869 */
3870 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003871 if (gainFocus) {
3872 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3873 }
3874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003875 InputMethodManager imm = InputMethodManager.peekInstance();
3876 if (!gainFocus) {
3877 if (isPressed()) {
3878 setPressed(false);
3879 }
3880 if (imm != null && mAttachInfo != null
3881 && mAttachInfo.mHasWindowFocus) {
3882 imm.focusOut(this);
3883 }
Romain Guya2431d02009-04-30 16:30:00 -07003884 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 } else if (imm != null && mAttachInfo != null
3886 && mAttachInfo.mHasWindowFocus) {
3887 imm.focusIn(this);
3888 }
Romain Guy8506ab42009-06-11 17:35:47 -07003889
Romain Guy0fd89bf2011-01-26 15:41:30 -08003890 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003891 ListenerInfo li = mListenerInfo;
3892 if (li != null && li.mOnFocusChangeListener != null) {
3893 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 }
Joe Malin32736f02011-01-19 16:14:20 -08003895
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003896 if (mAttachInfo != null) {
3897 mAttachInfo.mKeyDispatchState.reset(this);
3898 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 }
3900
3901 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003902 * Sends an accessibility event of the given type. If accessiiblity is
3903 * not enabled this method has no effect. The default implementation calls
3904 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3905 * to populate information about the event source (this View), then calls
3906 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3907 * populate the text content of the event source including its descendants,
3908 * and last calls
3909 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3910 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003911 * <p>
3912 * If an {@link AccessibilityDelegate} has been specified via calling
3913 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3914 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
3915 * responsible for handling this call.
3916 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003917 *
Scott Mainb303d832011-10-12 16:45:18 -07003918 * @param eventType The type of the event to send, as defined by several types from
3919 * {@link android.view.accessibility.AccessibilityEvent}, such as
3920 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
3921 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003922 *
3923 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3924 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3925 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003926 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07003927 */
3928 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003929 if (mAccessibilityDelegate != null) {
3930 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
3931 } else {
3932 sendAccessibilityEventInternal(eventType);
3933 }
3934 }
3935
3936 /**
3937 * @see #sendAccessibilityEvent(int)
3938 *
3939 * Note: Called from the default {@link AccessibilityDelegate}.
3940 */
3941 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003942 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3943 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3944 }
3945 }
3946
3947 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003948 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3949 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003950 * perform a check whether accessibility is enabled.
3951 * <p>
3952 * If an {@link AccessibilityDelegate} has been specified via calling
3953 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3954 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
3955 * is responsible for handling this call.
3956 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003957 *
3958 * @param event The event to send.
3959 *
3960 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003961 */
3962 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003963 if (mAccessibilityDelegate != null) {
3964 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
3965 } else {
3966 sendAccessibilityEventUncheckedInternal(event);
3967 }
3968 }
3969
3970 /**
3971 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
3972 *
3973 * Note: Called from the default {@link AccessibilityDelegate}.
3974 */
3975 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003976 if (!isShown()) {
3977 return;
3978 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003979 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07003980 // Only a subset of accessibility events populates text content.
3981 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
3982 dispatchPopulateAccessibilityEvent(event);
3983 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003984 // In the beginning we called #isShown(), so we know that getParent() is not null.
3985 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003986 }
3987
3988 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003989 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3990 * to its children for adding their text content to the event. Note that the
3991 * event text is populated in a separate dispatch path since we add to the
3992 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003993 * A typical implementation will call
3994 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3995 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3996 * on each child. Override this method if custom population of the event text
3997 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003998 * <p>
3999 * If an {@link AccessibilityDelegate} has been specified via calling
4000 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4001 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4002 * is responsible for handling this call.
4003 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004004 * <p>
4005 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4006 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4007 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004008 *
4009 * @param event The event.
4010 *
4011 * @return True if the event population was completed.
4012 */
4013 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004014 if (mAccessibilityDelegate != null) {
4015 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4016 } else {
4017 return dispatchPopulateAccessibilityEventInternal(event);
4018 }
4019 }
4020
4021 /**
4022 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4023 *
4024 * Note: Called from the default {@link AccessibilityDelegate}.
4025 */
4026 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004027 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004028 return false;
4029 }
4030
4031 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004032 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004033 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004034 * text content. While this method is free to modify event
4035 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004036 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4037 * <p>
4038 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004039 * to the text added by the super implementation:
4040 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004041 * super.onPopulateAccessibilityEvent(event);
4042 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4043 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4044 * mCurrentDate.getTimeInMillis(), flags);
4045 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004046 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004047 * <p>
4048 * If an {@link AccessibilityDelegate} has been specified via calling
4049 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4050 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4051 * is responsible for handling this call.
4052 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004053 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4054 * information to the event, in case the default implementation has basic information to add.
4055 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004056 *
4057 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004058 *
4059 * @see #sendAccessibilityEvent(int)
4060 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004061 */
4062 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004063 if (mAccessibilityDelegate != null) {
4064 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4065 } else {
4066 onPopulateAccessibilityEventInternal(event);
4067 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004068 }
4069
4070 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004071 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4072 *
4073 * Note: Called from the default {@link AccessibilityDelegate}.
4074 */
4075 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4076
4077 }
4078
4079 /**
4080 * Initializes an {@link AccessibilityEvent} with information about
4081 * this View which is the event source. In other words, the source of
4082 * an accessibility event is the view whose state change triggered firing
4083 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004084 * <p>
4085 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004086 * to properties set by the super implementation:
4087 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4088 * super.onInitializeAccessibilityEvent(event);
4089 * event.setPassword(true);
4090 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004091 * <p>
4092 * If an {@link AccessibilityDelegate} has been specified via calling
4093 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4094 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4095 * is responsible for handling this call.
4096 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004097 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4098 * information to the event, in case the default implementation has basic information to add.
4099 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004100 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004101 *
4102 * @see #sendAccessibilityEvent(int)
4103 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4104 */
4105 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004106 if (mAccessibilityDelegate != null) {
4107 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4108 } else {
4109 onInitializeAccessibilityEventInternal(event);
4110 }
4111 }
4112
4113 /**
4114 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4115 *
4116 * Note: Called from the default {@link AccessibilityDelegate}.
4117 */
4118 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004119 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004120 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004121 event.setPackageName(getContext().getPackageName());
4122 event.setEnabled(isEnabled());
4123 event.setContentDescription(mContentDescription);
4124
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004125 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
4126 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4127 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4128 FOCUSABLES_ALL);
4129 event.setItemCount(focusablesTempList.size());
4130 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4131 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004132 }
4133 }
4134
4135 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004136 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4137 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4138 * This method is responsible for obtaining an accessibility node info from a
4139 * pool of reusable instances and calling
4140 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4141 * initialize the former.
4142 * <p>
4143 * Note: The client is responsible for recycling the obtained instance by calling
4144 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4145 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004146 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004147 * @return A populated {@link AccessibilityNodeInfo}.
4148 *
4149 * @see AccessibilityNodeInfo
4150 */
4151 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004152 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4153 if (provider != null) {
4154 return provider.createAccessibilityNodeInfo(View.NO_ID);
4155 } else {
4156 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4157 onInitializeAccessibilityNodeInfo(info);
4158 return info;
4159 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004160 }
4161
4162 /**
4163 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4164 * The base implementation sets:
4165 * <ul>
4166 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004167 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4168 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004169 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4170 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4171 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4172 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4173 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4174 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4175 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4176 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4177 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4178 * </ul>
4179 * <p>
4180 * Subclasses should override this method, call the super implementation,
4181 * and set additional attributes.
4182 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004183 * <p>
4184 * If an {@link AccessibilityDelegate} has been specified via calling
4185 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4186 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4187 * is responsible for handling this call.
4188 * </p>
4189 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004190 * @param info The instance to initialize.
4191 */
4192 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004193 if (mAccessibilityDelegate != null) {
4194 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4195 } else {
4196 onInitializeAccessibilityNodeInfoInternal(info);
4197 }
4198 }
4199
4200 /**
4201 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4202 *
4203 * Note: Called from the default {@link AccessibilityDelegate}.
4204 */
4205 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004206 Rect bounds = mAttachInfo.mTmpInvalRect;
4207 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004208 info.setBoundsInParent(bounds);
4209
4210 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4211 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004212 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004213 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4214 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004215
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004216 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
4217 ViewParent parent = getParent();
4218 if (parent instanceof View) {
4219 View parentView = (View) parent;
4220 info.setParent(parentView);
4221 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004222 }
4223
4224 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004225 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004226 info.setContentDescription(getContentDescription());
4227
4228 info.setEnabled(isEnabled());
4229 info.setClickable(isClickable());
4230 info.setFocusable(isFocusable());
4231 info.setFocused(isFocused());
4232 info.setSelected(isSelected());
4233 info.setLongClickable(isLongClickable());
4234
4235 // TODO: These make sense only if we are in an AdapterView but all
4236 // views can be selected. Maybe from accessiiblity perspective
4237 // we should report as selectable view in an AdapterView.
4238 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4239 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4240
4241 if (isFocusable()) {
4242 if (isFocused()) {
4243 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4244 } else {
4245 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4246 }
4247 }
4248 }
4249
4250 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004251 * Sets a delegate for implementing accessibility support via compositon as
4252 * opposed to inheritance. The delegate's primary use is for implementing
4253 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4254 *
4255 * @param delegate The delegate instance.
4256 *
4257 * @see AccessibilityDelegate
4258 */
4259 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4260 mAccessibilityDelegate = delegate;
4261 }
4262
4263 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004264 * Gets the provider for managing a virtual view hierarchy rooted at this View
4265 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4266 * that explore the window content.
4267 * <p>
4268 * If this method returns an instance, this instance is responsible for managing
4269 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4270 * View including the one representing the View itself. Similarly the returned
4271 * instance is responsible for performing accessibility actions on any virtual
4272 * view or the root view itself.
4273 * </p>
4274 * <p>
4275 * If an {@link AccessibilityDelegate} has been specified via calling
4276 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4277 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4278 * is responsible for handling this call.
4279 * </p>
4280 *
4281 * @return The provider.
4282 *
4283 * @see AccessibilityNodeProvider
4284 */
4285 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4286 if (mAccessibilityDelegate != null) {
4287 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4288 } else {
4289 return null;
4290 }
4291 }
4292
4293 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004294 * Gets the unique identifier of this view on the screen for accessibility purposes.
4295 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4296 *
4297 * @return The view accessibility id.
4298 *
4299 * @hide
4300 */
4301 public int getAccessibilityViewId() {
4302 if (mAccessibilityViewId == NO_ID) {
4303 mAccessibilityViewId = sNextAccessibilityViewId++;
4304 }
4305 return mAccessibilityViewId;
4306 }
4307
4308 /**
4309 * Gets the unique identifier of the window in which this View reseides.
4310 *
4311 * @return The window accessibility id.
4312 *
4313 * @hide
4314 */
4315 public int getAccessibilityWindowId() {
4316 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4317 }
4318
4319 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004320 * Gets the {@link View} description. It briefly describes the view and is
4321 * primarily used for accessibility support. Set this property to enable
4322 * better accessibility support for your application. This is especially
4323 * true for views that do not have textual representation (For example,
4324 * ImageButton).
4325 *
4326 * @return The content descriptiopn.
4327 *
4328 * @attr ref android.R.styleable#View_contentDescription
4329 */
4330 public CharSequence getContentDescription() {
4331 return mContentDescription;
4332 }
4333
4334 /**
4335 * Sets the {@link View} description. It briefly describes the view and is
4336 * primarily used for accessibility support. Set this property to enable
4337 * better accessibility support for your application. This is especially
4338 * true for views that do not have textual representation (For example,
4339 * ImageButton).
4340 *
4341 * @param contentDescription The content description.
4342 *
4343 * @attr ref android.R.styleable#View_contentDescription
4344 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004345 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004346 public void setContentDescription(CharSequence contentDescription) {
4347 mContentDescription = contentDescription;
4348 }
4349
4350 /**
Romain Guya2431d02009-04-30 16:30:00 -07004351 * Invoked whenever this view loses focus, either by losing window focus or by losing
4352 * focus within its window. This method can be used to clear any state tied to the
4353 * focus. For instance, if a button is held pressed with the trackball and the window
4354 * loses focus, this method can be used to cancel the press.
4355 *
4356 * Subclasses of View overriding this method should always call super.onFocusLost().
4357 *
4358 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004359 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004360 *
4361 * @hide pending API council approval
4362 */
4363 protected void onFocusLost() {
4364 resetPressedState();
4365 }
4366
4367 private void resetPressedState() {
4368 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4369 return;
4370 }
4371
4372 if (isPressed()) {
4373 setPressed(false);
4374
4375 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004376 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004377 }
4378 }
4379 }
4380
4381 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004382 * Returns true if this view has focus
4383 *
4384 * @return True if this view has focus, false otherwise.
4385 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004386 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004387 public boolean isFocused() {
4388 return (mPrivateFlags & FOCUSED) != 0;
4389 }
4390
4391 /**
4392 * Find the view in the hierarchy rooted at this view that currently has
4393 * focus.
4394 *
4395 * @return The view that currently has focus, or null if no focused view can
4396 * be found.
4397 */
4398 public View findFocus() {
4399 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4400 }
4401
4402 /**
4403 * Change whether this view is one of the set of scrollable containers in
4404 * its window. This will be used to determine whether the window can
4405 * resize or must pan when a soft input area is open -- scrollable
4406 * containers allow the window to use resize mode since the container
4407 * will appropriately shrink.
4408 */
4409 public void setScrollContainer(boolean isScrollContainer) {
4410 if (isScrollContainer) {
4411 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4412 mAttachInfo.mScrollContainers.add(this);
4413 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4414 }
4415 mPrivateFlags |= SCROLL_CONTAINER;
4416 } else {
4417 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4418 mAttachInfo.mScrollContainers.remove(this);
4419 }
4420 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4421 }
4422 }
4423
4424 /**
4425 * Returns the quality of the drawing cache.
4426 *
4427 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4428 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4429 *
4430 * @see #setDrawingCacheQuality(int)
4431 * @see #setDrawingCacheEnabled(boolean)
4432 * @see #isDrawingCacheEnabled()
4433 *
4434 * @attr ref android.R.styleable#View_drawingCacheQuality
4435 */
4436 public int getDrawingCacheQuality() {
4437 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4438 }
4439
4440 /**
4441 * Set the drawing cache quality of this view. This value is used only when the
4442 * drawing cache is enabled
4443 *
4444 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4445 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4446 *
4447 * @see #getDrawingCacheQuality()
4448 * @see #setDrawingCacheEnabled(boolean)
4449 * @see #isDrawingCacheEnabled()
4450 *
4451 * @attr ref android.R.styleable#View_drawingCacheQuality
4452 */
4453 public void setDrawingCacheQuality(int quality) {
4454 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4455 }
4456
4457 /**
4458 * Returns whether the screen should remain on, corresponding to the current
4459 * value of {@link #KEEP_SCREEN_ON}.
4460 *
4461 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4462 *
4463 * @see #setKeepScreenOn(boolean)
4464 *
4465 * @attr ref android.R.styleable#View_keepScreenOn
4466 */
4467 public boolean getKeepScreenOn() {
4468 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4469 }
4470
4471 /**
4472 * Controls whether the screen should remain on, modifying the
4473 * value of {@link #KEEP_SCREEN_ON}.
4474 *
4475 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4476 *
4477 * @see #getKeepScreenOn()
4478 *
4479 * @attr ref android.R.styleable#View_keepScreenOn
4480 */
4481 public void setKeepScreenOn(boolean keepScreenOn) {
4482 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4483 }
4484
4485 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004486 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4487 * @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 -08004488 *
4489 * @attr ref android.R.styleable#View_nextFocusLeft
4490 */
4491 public int getNextFocusLeftId() {
4492 return mNextFocusLeftId;
4493 }
4494
4495 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004496 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4497 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4498 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004499 *
4500 * @attr ref android.R.styleable#View_nextFocusLeft
4501 */
4502 public void setNextFocusLeftId(int nextFocusLeftId) {
4503 mNextFocusLeftId = nextFocusLeftId;
4504 }
4505
4506 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004507 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4508 * @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 -08004509 *
4510 * @attr ref android.R.styleable#View_nextFocusRight
4511 */
4512 public int getNextFocusRightId() {
4513 return mNextFocusRightId;
4514 }
4515
4516 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004517 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4518 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4519 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004520 *
4521 * @attr ref android.R.styleable#View_nextFocusRight
4522 */
4523 public void setNextFocusRightId(int nextFocusRightId) {
4524 mNextFocusRightId = nextFocusRightId;
4525 }
4526
4527 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004528 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4529 * @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 -08004530 *
4531 * @attr ref android.R.styleable#View_nextFocusUp
4532 */
4533 public int getNextFocusUpId() {
4534 return mNextFocusUpId;
4535 }
4536
4537 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004538 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4539 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4540 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 *
4542 * @attr ref android.R.styleable#View_nextFocusUp
4543 */
4544 public void setNextFocusUpId(int nextFocusUpId) {
4545 mNextFocusUpId = nextFocusUpId;
4546 }
4547
4548 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004549 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4550 * @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 -08004551 *
4552 * @attr ref android.R.styleable#View_nextFocusDown
4553 */
4554 public int getNextFocusDownId() {
4555 return mNextFocusDownId;
4556 }
4557
4558 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004559 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4560 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4561 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004562 *
4563 * @attr ref android.R.styleable#View_nextFocusDown
4564 */
4565 public void setNextFocusDownId(int nextFocusDownId) {
4566 mNextFocusDownId = nextFocusDownId;
4567 }
4568
4569 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004570 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4571 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4572 *
4573 * @attr ref android.R.styleable#View_nextFocusForward
4574 */
4575 public int getNextFocusForwardId() {
4576 return mNextFocusForwardId;
4577 }
4578
4579 /**
4580 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4581 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4582 * decide automatically.
4583 *
4584 * @attr ref android.R.styleable#View_nextFocusForward
4585 */
4586 public void setNextFocusForwardId(int nextFocusForwardId) {
4587 mNextFocusForwardId = nextFocusForwardId;
4588 }
4589
4590 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004591 * Returns the visibility of this view and all of its ancestors
4592 *
4593 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4594 */
4595 public boolean isShown() {
4596 View current = this;
4597 //noinspection ConstantConditions
4598 do {
4599 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4600 return false;
4601 }
4602 ViewParent parent = current.mParent;
4603 if (parent == null) {
4604 return false; // We are not attached to the view root
4605 }
4606 if (!(parent instanceof View)) {
4607 return true;
4608 }
4609 current = (View) parent;
4610 } while (current != null);
4611
4612 return false;
4613 }
4614
4615 /**
4616 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4617 * is set
4618 *
4619 * @param insets Insets for system windows
4620 *
4621 * @return True if this view applied the insets, false otherwise
4622 */
4623 protected boolean fitSystemWindows(Rect insets) {
4624 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4625 mPaddingLeft = insets.left;
4626 mPaddingTop = insets.top;
4627 mPaddingRight = insets.right;
4628 mPaddingBottom = insets.bottom;
4629 requestLayout();
4630 return true;
4631 }
4632 return false;
4633 }
4634
4635 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004636 * Set whether or not this view should account for system screen decorations
4637 * such as the status bar and inset its content. This allows this view to be
4638 * positioned in absolute screen coordinates and remain visible to the user.
4639 *
4640 * <p>This should only be used by top-level window decor views.
4641 *
4642 * @param fitSystemWindows true to inset content for system screen decorations, false for
4643 * default behavior.
4644 *
4645 * @attr ref android.R.styleable#View_fitsSystemWindows
4646 */
4647 public void setFitsSystemWindows(boolean fitSystemWindows) {
4648 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4649 }
4650
4651 /**
4652 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4653 * will account for system screen decorations such as the status bar and inset its
4654 * content. This allows the view to be positioned in absolute screen coordinates
4655 * and remain visible to the user.
4656 *
4657 * @return true if this view will adjust its content bounds for system screen decorations.
4658 *
4659 * @attr ref android.R.styleable#View_fitsSystemWindows
4660 */
4661 public boolean fitsSystemWindows() {
4662 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4663 }
4664
4665 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004666 * Returns the visibility status for this view.
4667 *
4668 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4669 * @attr ref android.R.styleable#View_visibility
4670 */
4671 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004672 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4673 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4674 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004675 })
4676 public int getVisibility() {
4677 return mViewFlags & VISIBILITY_MASK;
4678 }
4679
4680 /**
4681 * Set the enabled state of this view.
4682 *
4683 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4684 * @attr ref android.R.styleable#View_visibility
4685 */
4686 @RemotableViewMethod
4687 public void setVisibility(int visibility) {
4688 setFlags(visibility, VISIBILITY_MASK);
4689 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4690 }
4691
4692 /**
4693 * Returns the enabled status for this view. The interpretation of the
4694 * enabled state varies by subclass.
4695 *
4696 * @return True if this view is enabled, false otherwise.
4697 */
4698 @ViewDebug.ExportedProperty
4699 public boolean isEnabled() {
4700 return (mViewFlags & ENABLED_MASK) == ENABLED;
4701 }
4702
4703 /**
4704 * Set the enabled state of this view. The interpretation of the enabled
4705 * state varies by subclass.
4706 *
4707 * @param enabled True if this view is enabled, false otherwise.
4708 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004709 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004710 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004711 if (enabled == isEnabled()) return;
4712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004713 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4714
4715 /*
4716 * The View most likely has to change its appearance, so refresh
4717 * the drawable state.
4718 */
4719 refreshDrawableState();
4720
4721 // Invalidate too, since the default behavior for views is to be
4722 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004723 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004724 }
4725
4726 /**
4727 * Set whether this view can receive the focus.
4728 *
4729 * Setting this to false will also ensure that this view is not focusable
4730 * in touch mode.
4731 *
4732 * @param focusable If true, this view can receive the focus.
4733 *
4734 * @see #setFocusableInTouchMode(boolean)
4735 * @attr ref android.R.styleable#View_focusable
4736 */
4737 public void setFocusable(boolean focusable) {
4738 if (!focusable) {
4739 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4740 }
4741 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4742 }
4743
4744 /**
4745 * Set whether this view can receive focus while in touch mode.
4746 *
4747 * Setting this to true will also ensure that this view is focusable.
4748 *
4749 * @param focusableInTouchMode If true, this view can receive the focus while
4750 * in touch mode.
4751 *
4752 * @see #setFocusable(boolean)
4753 * @attr ref android.R.styleable#View_focusableInTouchMode
4754 */
4755 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4756 // Focusable in touch mode should always be set before the focusable flag
4757 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4758 // which, in touch mode, will not successfully request focus on this view
4759 // because the focusable in touch mode flag is not set
4760 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4761 if (focusableInTouchMode) {
4762 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4763 }
4764 }
4765
4766 /**
4767 * Set whether this view should have sound effects enabled for events such as
4768 * clicking and touching.
4769 *
4770 * <p>You may wish to disable sound effects for a view if you already play sounds,
4771 * for instance, a dial key that plays dtmf tones.
4772 *
4773 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4774 * @see #isSoundEffectsEnabled()
4775 * @see #playSoundEffect(int)
4776 * @attr ref android.R.styleable#View_soundEffectsEnabled
4777 */
4778 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4779 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4780 }
4781
4782 /**
4783 * @return whether this view should have sound effects enabled for events such as
4784 * clicking and touching.
4785 *
4786 * @see #setSoundEffectsEnabled(boolean)
4787 * @see #playSoundEffect(int)
4788 * @attr ref android.R.styleable#View_soundEffectsEnabled
4789 */
4790 @ViewDebug.ExportedProperty
4791 public boolean isSoundEffectsEnabled() {
4792 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4793 }
4794
4795 /**
4796 * Set whether this view should have haptic feedback for events such as
4797 * long presses.
4798 *
4799 * <p>You may wish to disable haptic feedback if your view already controls
4800 * its own haptic feedback.
4801 *
4802 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4803 * @see #isHapticFeedbackEnabled()
4804 * @see #performHapticFeedback(int)
4805 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4806 */
4807 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4808 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4809 }
4810
4811 /**
4812 * @return whether this view should have haptic feedback enabled for events
4813 * long presses.
4814 *
4815 * @see #setHapticFeedbackEnabled(boolean)
4816 * @see #performHapticFeedback(int)
4817 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4818 */
4819 @ViewDebug.ExportedProperty
4820 public boolean isHapticFeedbackEnabled() {
4821 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4822 }
4823
4824 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004825 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004826 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004827 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4828 * {@link #LAYOUT_DIRECTION_RTL},
4829 * {@link #LAYOUT_DIRECTION_INHERIT} or
4830 * {@link #LAYOUT_DIRECTION_LOCALE}.
4831 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08004832 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004833 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004834 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4835 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4836 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4837 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004838 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004839 public int getLayoutDirection() {
4840 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004841 }
4842
4843 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004844 * Set the layout direction for this view. This will propagate a reset of layout direction
4845 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004846 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004847 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4848 * {@link #LAYOUT_DIRECTION_RTL},
4849 * {@link #LAYOUT_DIRECTION_INHERIT} or
4850 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004851 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004852 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08004853 */
4854 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004855 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004856 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004857 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004858 // Setting the flag will also request a layout.
4859 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4860 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004861 }
4862
4863 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004864 * Returns the resolved layout direction for this view.
4865 *
4866 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4867 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004868 */
4869 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4870 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4871 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4872 })
4873 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004874 resolveLayoutDirectionIfNeeded();
4875 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004876 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4877 }
4878
4879 /**
4880 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4881 * layout attribute and/or the inherited value from the parent.</p>
4882 *
4883 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004884 */
4885 @ViewDebug.ExportedProperty(category = "layout")
4886 public boolean isLayoutRtl() {
4887 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4888 }
4889
4890 /**
Adam Powell539ee872012-02-03 19:00:49 -08004891 * Indicates whether the view is currently tracking transient state that the
4892 * app should not need to concern itself with saving and restoring, but that
4893 * the framework should take special note to preserve when possible.
4894 *
4895 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08004896 */
4897 @ViewDebug.ExportedProperty(category = "layout")
4898 public boolean hasTransientState() {
4899 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
4900 }
4901
4902 /**
4903 * Set whether this view is currently tracking transient state that the
4904 * framework should attempt to preserve when possible.
4905 *
4906 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08004907 */
4908 public void setHasTransientState(boolean hasTransientState) {
4909 if (hasTransientState() == hasTransientState) return;
4910
4911 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
4912 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
4913 if (mParent != null) {
4914 try {
4915 mParent.childHasTransientStateChanged(this, hasTransientState);
4916 } catch (AbstractMethodError e) {
4917 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
4918 " does not fully implement ViewParent", e);
4919 }
4920 }
4921 }
4922
4923 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004924 * If this view doesn't do any drawing on its own, set this flag to
4925 * allow further optimizations. By default, this flag is not set on
4926 * View, but could be set on some View subclasses such as ViewGroup.
4927 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004928 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4929 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004930 *
4931 * @param willNotDraw whether or not this View draw on its own
4932 */
4933 public void setWillNotDraw(boolean willNotDraw) {
4934 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4935 }
4936
4937 /**
4938 * Returns whether or not this View draws on its own.
4939 *
4940 * @return true if this view has nothing to draw, false otherwise
4941 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004942 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004943 public boolean willNotDraw() {
4944 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4945 }
4946
4947 /**
4948 * When a View's drawing cache is enabled, drawing is redirected to an
4949 * offscreen bitmap. Some views, like an ImageView, must be able to
4950 * bypass this mechanism if they already draw a single bitmap, to avoid
4951 * unnecessary usage of the memory.
4952 *
4953 * @param willNotCacheDrawing true if this view does not cache its
4954 * drawing, false otherwise
4955 */
4956 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4957 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4958 }
4959
4960 /**
4961 * Returns whether or not this View can cache its drawing or not.
4962 *
4963 * @return true if this view does not cache its drawing, false otherwise
4964 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004965 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004966 public boolean willNotCacheDrawing() {
4967 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4968 }
4969
4970 /**
4971 * Indicates whether this view reacts to click events or not.
4972 *
4973 * @return true if the view is clickable, false otherwise
4974 *
4975 * @see #setClickable(boolean)
4976 * @attr ref android.R.styleable#View_clickable
4977 */
4978 @ViewDebug.ExportedProperty
4979 public boolean isClickable() {
4980 return (mViewFlags & CLICKABLE) == CLICKABLE;
4981 }
4982
4983 /**
4984 * Enables or disables click events for this view. When a view
4985 * is clickable it will change its state to "pressed" on every click.
4986 * Subclasses should set the view clickable to visually react to
4987 * user's clicks.
4988 *
4989 * @param clickable true to make the view clickable, false otherwise
4990 *
4991 * @see #isClickable()
4992 * @attr ref android.R.styleable#View_clickable
4993 */
4994 public void setClickable(boolean clickable) {
4995 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4996 }
4997
4998 /**
4999 * Indicates whether this view reacts to long click events or not.
5000 *
5001 * @return true if the view is long clickable, false otherwise
5002 *
5003 * @see #setLongClickable(boolean)
5004 * @attr ref android.R.styleable#View_longClickable
5005 */
5006 public boolean isLongClickable() {
5007 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5008 }
5009
5010 /**
5011 * Enables or disables long click events for this view. When a view is long
5012 * clickable it reacts to the user holding down the button for a longer
5013 * duration than a tap. This event can either launch the listener or a
5014 * context menu.
5015 *
5016 * @param longClickable true to make the view long clickable, false otherwise
5017 * @see #isLongClickable()
5018 * @attr ref android.R.styleable#View_longClickable
5019 */
5020 public void setLongClickable(boolean longClickable) {
5021 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5022 }
5023
5024 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005025 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005026 *
5027 * @see #isClickable()
5028 * @see #setClickable(boolean)
5029 *
5030 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5031 * the View's internal state from a previously set "pressed" state.
5032 */
5033 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005034 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005036 if (pressed) {
5037 mPrivateFlags |= PRESSED;
5038 } else {
5039 mPrivateFlags &= ~PRESSED;
5040 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005041
5042 if (needsRefresh) {
5043 refreshDrawableState();
5044 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005045 dispatchSetPressed(pressed);
5046 }
5047
5048 /**
5049 * Dispatch setPressed to all of this View's children.
5050 *
5051 * @see #setPressed(boolean)
5052 *
5053 * @param pressed The new pressed state
5054 */
5055 protected void dispatchSetPressed(boolean pressed) {
5056 }
5057
5058 /**
5059 * Indicates whether the view is currently in pressed state. Unless
5060 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5061 * the pressed state.
5062 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005063 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005064 * @see #isClickable()
5065 * @see #setClickable(boolean)
5066 *
5067 * @return true if the view is currently pressed, false otherwise
5068 */
5069 public boolean isPressed() {
5070 return (mPrivateFlags & PRESSED) == PRESSED;
5071 }
5072
5073 /**
5074 * Indicates whether this view will save its state (that is,
5075 * whether its {@link #onSaveInstanceState} method will be called).
5076 *
5077 * @return Returns true if the view state saving is enabled, else false.
5078 *
5079 * @see #setSaveEnabled(boolean)
5080 * @attr ref android.R.styleable#View_saveEnabled
5081 */
5082 public boolean isSaveEnabled() {
5083 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5084 }
5085
5086 /**
5087 * Controls whether the saving of this view's state is
5088 * enabled (that is, whether its {@link #onSaveInstanceState} method
5089 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005090 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005091 * for its state to be saved. This flag can only disable the
5092 * saving of this view; any child views may still have their state saved.
5093 *
5094 * @param enabled Set to false to <em>disable</em> state saving, or true
5095 * (the default) to allow it.
5096 *
5097 * @see #isSaveEnabled()
5098 * @see #setId(int)
5099 * @see #onSaveInstanceState()
5100 * @attr ref android.R.styleable#View_saveEnabled
5101 */
5102 public void setSaveEnabled(boolean enabled) {
5103 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5104 }
5105
Jeff Brown85a31762010-09-01 17:01:00 -07005106 /**
5107 * Gets whether the framework should discard touches when the view's
5108 * window is obscured by another visible window.
5109 * Refer to the {@link View} security documentation for more details.
5110 *
5111 * @return True if touch filtering is enabled.
5112 *
5113 * @see #setFilterTouchesWhenObscured(boolean)
5114 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5115 */
5116 @ViewDebug.ExportedProperty
5117 public boolean getFilterTouchesWhenObscured() {
5118 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5119 }
5120
5121 /**
5122 * Sets whether the framework should discard touches when the view's
5123 * window is obscured by another visible window.
5124 * Refer to the {@link View} security documentation for more details.
5125 *
5126 * @param enabled True if touch filtering should be enabled.
5127 *
5128 * @see #getFilterTouchesWhenObscured
5129 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5130 */
5131 public void setFilterTouchesWhenObscured(boolean enabled) {
5132 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5133 FILTER_TOUCHES_WHEN_OBSCURED);
5134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005135
5136 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005137 * Indicates whether the entire hierarchy under this view will save its
5138 * state when a state saving traversal occurs from its parent. The default
5139 * is true; if false, these views will not be saved unless
5140 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5141 *
5142 * @return Returns true if the view state saving from parent is enabled, else false.
5143 *
5144 * @see #setSaveFromParentEnabled(boolean)
5145 */
5146 public boolean isSaveFromParentEnabled() {
5147 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5148 }
5149
5150 /**
5151 * Controls whether the entire hierarchy under this view will save its
5152 * state when a state saving traversal occurs from its parent. The default
5153 * is true; if false, these views will not be saved unless
5154 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5155 *
5156 * @param enabled Set to false to <em>disable</em> state saving, or true
5157 * (the default) to allow it.
5158 *
5159 * @see #isSaveFromParentEnabled()
5160 * @see #setId(int)
5161 * @see #onSaveInstanceState()
5162 */
5163 public void setSaveFromParentEnabled(boolean enabled) {
5164 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5165 }
5166
5167
5168 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005169 * Returns whether this View is able to take focus.
5170 *
5171 * @return True if this view can take focus, or false otherwise.
5172 * @attr ref android.R.styleable#View_focusable
5173 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005174 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005175 public final boolean isFocusable() {
5176 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5177 }
5178
5179 /**
5180 * When a view is focusable, it may not want to take focus when in touch mode.
5181 * For example, a button would like focus when the user is navigating via a D-pad
5182 * so that the user can click on it, but once the user starts touching the screen,
5183 * the button shouldn't take focus
5184 * @return Whether the view is focusable in touch mode.
5185 * @attr ref android.R.styleable#View_focusableInTouchMode
5186 */
5187 @ViewDebug.ExportedProperty
5188 public final boolean isFocusableInTouchMode() {
5189 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5190 }
5191
5192 /**
5193 * Find the nearest view in the specified direction that can take focus.
5194 * This does not actually give focus to that view.
5195 *
5196 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5197 *
5198 * @return The nearest focusable in the specified direction, or null if none
5199 * can be found.
5200 */
5201 public View focusSearch(int direction) {
5202 if (mParent != null) {
5203 return mParent.focusSearch(this, direction);
5204 } else {
5205 return null;
5206 }
5207 }
5208
5209 /**
5210 * This method is the last chance for the focused view and its ancestors to
5211 * respond to an arrow key. This is called when the focused view did not
5212 * consume the key internally, nor could the view system find a new view in
5213 * the requested direction to give focus to.
5214 *
5215 * @param focused The currently focused view.
5216 * @param direction The direction focus wants to move. One of FOCUS_UP,
5217 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5218 * @return True if the this view consumed this unhandled move.
5219 */
5220 public boolean dispatchUnhandledMove(View focused, int direction) {
5221 return false;
5222 }
5223
5224 /**
5225 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005226 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005227 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005228 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5229 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005230 * @return The user specified next view, or null if there is none.
5231 */
5232 View findUserSetNextFocus(View root, int direction) {
5233 switch (direction) {
5234 case FOCUS_LEFT:
5235 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005236 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005237 case FOCUS_RIGHT:
5238 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005239 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005240 case FOCUS_UP:
5241 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005242 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005243 case FOCUS_DOWN:
5244 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005245 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005246 case FOCUS_FORWARD:
5247 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005248 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005249 case FOCUS_BACKWARD: {
5250 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005251 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005252 @Override
5253 public boolean apply(View t) {
5254 return t.mNextFocusForwardId == id;
5255 }
5256 });
5257 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005258 }
5259 return null;
5260 }
5261
Jeff Brown4dfbec22011-08-15 14:55:37 -07005262 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5263 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5264 @Override
5265 public boolean apply(View t) {
5266 return t.mID == childViewId;
5267 }
5268 });
5269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005270 if (result == null) {
5271 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5272 + "by user for id " + childViewId);
5273 }
5274 return result;
5275 }
5276
5277 /**
5278 * Find and return all focusable views that are descendants of this view,
5279 * possibly including this view if it is focusable itself.
5280 *
5281 * @param direction The direction of the focus
5282 * @return A list of focusable views
5283 */
5284 public ArrayList<View> getFocusables(int direction) {
5285 ArrayList<View> result = new ArrayList<View>(24);
5286 addFocusables(result, direction);
5287 return result;
5288 }
5289
5290 /**
5291 * Add any focusable views that are descendants of this view (possibly
5292 * including this view if it is focusable itself) to views. If we are in touch mode,
5293 * only add views that are also focusable in touch mode.
5294 *
5295 * @param views Focusable views found so far
5296 * @param direction The direction of the focus
5297 */
5298 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005299 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005301
svetoslavganov75986cf2009-05-14 22:28:01 -07005302 /**
5303 * Adds any focusable views that are descendants of this view (possibly
5304 * including this view if it is focusable itself) to views. This method
5305 * adds all focusable views regardless if we are in touch mode or
5306 * only views focusable in touch mode if we are in touch mode depending on
5307 * the focusable mode paramater.
5308 *
5309 * @param views Focusable views found so far or null if all we are interested is
5310 * the number of focusables.
5311 * @param direction The direction of the focus.
5312 * @param focusableMode The type of focusables to be added.
5313 *
5314 * @see #FOCUSABLES_ALL
5315 * @see #FOCUSABLES_TOUCH_MODE
5316 */
5317 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5318 if (!isFocusable()) {
5319 return;
5320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005321
svetoslavganov75986cf2009-05-14 22:28:01 -07005322 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5323 isInTouchMode() && !isFocusableInTouchMode()) {
5324 return;
5325 }
5326
5327 if (views != null) {
5328 views.add(this);
5329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005330 }
5331
5332 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005333 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005334 * The search is performed by either the text that the View renders or the content
5335 * description that describes the view for accessibility purposes and the view does
5336 * not render or both. Clients can specify how the search is to be performed via
5337 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5338 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005339 *
5340 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005341 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005342 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005343 * @see #FIND_VIEWS_WITH_TEXT
5344 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5345 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005346 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005347 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005348 if (getAccessibilityNodeProvider() != null) {
5349 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5350 outViews.add(this);
5351 }
5352 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5353 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005354 String searchedLowerCase = searched.toString().toLowerCase();
5355 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5356 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5357 outViews.add(this);
5358 }
5359 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005360 }
5361
5362 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 * Find and return all touchable views that are descendants of this view,
5364 * possibly including this view if it is touchable itself.
5365 *
5366 * @return A list of touchable views
5367 */
5368 public ArrayList<View> getTouchables() {
5369 ArrayList<View> result = new ArrayList<View>();
5370 addTouchables(result);
5371 return result;
5372 }
5373
5374 /**
5375 * Add any touchable views that are descendants of this view (possibly
5376 * including this view if it is touchable itself) to views.
5377 *
5378 * @param views Touchable views found so far
5379 */
5380 public void addTouchables(ArrayList<View> views) {
5381 final int viewFlags = mViewFlags;
5382
5383 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5384 && (viewFlags & ENABLED_MASK) == ENABLED) {
5385 views.add(this);
5386 }
5387 }
5388
5389 /**
5390 * Call this to try to give focus to a specific view or to one of its
5391 * descendants.
5392 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005393 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5394 * false), or if it is focusable and it is not focusable in touch mode
5395 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005396 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005397 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005398 * have focus, and you want your parent to look for the next one.
5399 *
5400 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5401 * {@link #FOCUS_DOWN} and <code>null</code>.
5402 *
5403 * @return Whether this view or one of its descendants actually took focus.
5404 */
5405 public final boolean requestFocus() {
5406 return requestFocus(View.FOCUS_DOWN);
5407 }
5408
5409
5410 /**
5411 * Call this to try to give focus to a specific view or to one of its
5412 * descendants and give it a hint about what direction focus is heading.
5413 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005414 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5415 * false), or if it is focusable and it is not focusable in touch mode
5416 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005417 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005418 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005419 * have focus, and you want your parent to look for the next one.
5420 *
5421 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5422 * <code>null</code> set for the previously focused rectangle.
5423 *
5424 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5425 * @return Whether this view or one of its descendants actually took focus.
5426 */
5427 public final boolean requestFocus(int direction) {
5428 return requestFocus(direction, null);
5429 }
5430
5431 /**
5432 * Call this to try to give focus to a specific view or to one of its descendants
5433 * and give it hints about the direction and a specific rectangle that the focus
5434 * is coming from. The rectangle can help give larger views a finer grained hint
5435 * about where focus is coming from, and therefore, where to show selection, or
5436 * forward focus change internally.
5437 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005438 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5439 * false), or if it is focusable and it is not focusable in touch mode
5440 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005441 *
5442 * A View will not take focus if it is not visible.
5443 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005444 * A View will not take focus if one of its parents has
5445 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5446 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005447 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005448 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005449 * have focus, and you want your parent to look for the next one.
5450 *
5451 * You may wish to override this method if your custom {@link View} has an internal
5452 * {@link View} that it wishes to forward the request to.
5453 *
5454 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5455 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5456 * to give a finer grained hint about where focus is coming from. May be null
5457 * if there is no hint.
5458 * @return Whether this view or one of its descendants actually took focus.
5459 */
5460 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5461 // need to be focusable
5462 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5463 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5464 return false;
5465 }
5466
5467 // need to be focusable in touch mode if in touch mode
5468 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005469 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5470 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005471 }
5472
5473 // need to not have any parents blocking us
5474 if (hasAncestorThatBlocksDescendantFocus()) {
5475 return false;
5476 }
5477
5478 handleFocusGainInternal(direction, previouslyFocusedRect);
5479 return true;
5480 }
5481
5482 /**
5483 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5484 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5485 * touch mode to request focus when they are touched.
5486 *
5487 * @return Whether this view or one of its descendants actually took focus.
5488 *
5489 * @see #isInTouchMode()
5490 *
5491 */
5492 public final boolean requestFocusFromTouch() {
5493 // Leave touch mode if we need to
5494 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005495 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005496 if (viewRoot != null) {
5497 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005498 }
5499 }
5500 return requestFocus(View.FOCUS_DOWN);
5501 }
5502
5503 /**
5504 * @return Whether any ancestor of this view blocks descendant focus.
5505 */
5506 private boolean hasAncestorThatBlocksDescendantFocus() {
5507 ViewParent ancestor = mParent;
5508 while (ancestor instanceof ViewGroup) {
5509 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5510 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5511 return true;
5512 } else {
5513 ancestor = vgAncestor.getParent();
5514 }
5515 }
5516 return false;
5517 }
5518
5519 /**
Romain Guya440b002010-02-24 15:57:54 -08005520 * @hide
5521 */
5522 public void dispatchStartTemporaryDetach() {
5523 onStartTemporaryDetach();
5524 }
5525
5526 /**
5527 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005528 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5529 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005530 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005531 */
5532 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005533 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005534 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005535 }
5536
5537 /**
5538 * @hide
5539 */
5540 public void dispatchFinishTemporaryDetach() {
5541 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005542 }
Romain Guy8506ab42009-06-11 17:35:47 -07005543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005544 /**
5545 * Called after {@link #onStartTemporaryDetach} when the container is done
5546 * changing the view.
5547 */
5548 public void onFinishTemporaryDetach() {
5549 }
Romain Guy8506ab42009-06-11 17:35:47 -07005550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005551 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005552 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5553 * for this view's window. Returns null if the view is not currently attached
5554 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005555 * just use the standard high-level event callbacks like
5556 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005557 */
5558 public KeyEvent.DispatcherState getKeyDispatcherState() {
5559 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5560 }
Joe Malin32736f02011-01-19 16:14:20 -08005561
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005562 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005563 * Dispatch a key event before it is processed by any input method
5564 * associated with the view hierarchy. This can be used to intercept
5565 * key events in special situations before the IME consumes them; a
5566 * typical example would be handling the BACK key to update the application's
5567 * UI instead of allowing the IME to see it and close itself.
5568 *
5569 * @param event The key event to be dispatched.
5570 * @return True if the event was handled, false otherwise.
5571 */
5572 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5573 return onKeyPreIme(event.getKeyCode(), event);
5574 }
5575
5576 /**
5577 * Dispatch a key event to the next view on the focus path. This path runs
5578 * from the top of the view tree down to the currently focused view. If this
5579 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5580 * the next node down the focus path. This method also fires any key
5581 * listeners.
5582 *
5583 * @param event The key event to be dispatched.
5584 * @return True if the event was handled, false otherwise.
5585 */
5586 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005587 if (mInputEventConsistencyVerifier != null) {
5588 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005590
Jeff Brown21bc5c92011-02-28 18:27:14 -08005591 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005592 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005593 ListenerInfo li = mListenerInfo;
5594 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5595 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005596 return true;
5597 }
5598
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005599 if (event.dispatch(this, mAttachInfo != null
5600 ? mAttachInfo.mKeyDispatchState : null, this)) {
5601 return true;
5602 }
5603
5604 if (mInputEventConsistencyVerifier != null) {
5605 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5606 }
5607 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005608 }
5609
5610 /**
5611 * Dispatches a key shortcut event.
5612 *
5613 * @param event The key event to be dispatched.
5614 * @return True if the event was handled by the view, false otherwise.
5615 */
5616 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5617 return onKeyShortcut(event.getKeyCode(), event);
5618 }
5619
5620 /**
5621 * Pass the touch screen motion event down to the target view, or this
5622 * view if it is the target.
5623 *
5624 * @param event The motion event to be dispatched.
5625 * @return True if the event was handled by the view, false otherwise.
5626 */
5627 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005628 if (mInputEventConsistencyVerifier != null) {
5629 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5630 }
5631
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005632 if (onFilterTouchEventForSecurity(event)) {
5633 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005634 ListenerInfo li = mListenerInfo;
5635 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5636 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005637 return true;
5638 }
5639
5640 if (onTouchEvent(event)) {
5641 return true;
5642 }
Jeff Brown85a31762010-09-01 17:01:00 -07005643 }
5644
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005645 if (mInputEventConsistencyVerifier != null) {
5646 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005647 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005648 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005649 }
5650
5651 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005652 * Filter the touch event to apply security policies.
5653 *
5654 * @param event The motion event to be filtered.
5655 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005656 *
Jeff Brown85a31762010-09-01 17:01:00 -07005657 * @see #getFilterTouchesWhenObscured
5658 */
5659 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005660 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005661 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5662 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5663 // Window is obscured, drop this touch.
5664 return false;
5665 }
5666 return true;
5667 }
5668
5669 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005670 * Pass a trackball motion event down to the focused view.
5671 *
5672 * @param event The motion event to be dispatched.
5673 * @return True if the event was handled by the view, false otherwise.
5674 */
5675 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005676 if (mInputEventConsistencyVerifier != null) {
5677 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5678 }
5679
Romain Guy02ccac62011-06-24 13:20:23 -07005680 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005681 }
5682
5683 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005684 * Dispatch a generic motion event.
5685 * <p>
5686 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5687 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005688 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005689 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005690 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005691 *
5692 * @param event The motion event to be dispatched.
5693 * @return True if the event was handled by the view, false otherwise.
5694 */
5695 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005696 if (mInputEventConsistencyVerifier != null) {
5697 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5698 }
5699
Jeff Browna032cc02011-03-07 16:56:21 -08005700 final int source = event.getSource();
5701 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5702 final int action = event.getAction();
5703 if (action == MotionEvent.ACTION_HOVER_ENTER
5704 || action == MotionEvent.ACTION_HOVER_MOVE
5705 || action == MotionEvent.ACTION_HOVER_EXIT) {
5706 if (dispatchHoverEvent(event)) {
5707 return true;
5708 }
5709 } else if (dispatchGenericPointerEvent(event)) {
5710 return true;
5711 }
5712 } else if (dispatchGenericFocusedEvent(event)) {
5713 return true;
5714 }
5715
Jeff Brown10b62902011-06-20 16:40:37 -07005716 if (dispatchGenericMotionEventInternal(event)) {
5717 return true;
5718 }
5719
5720 if (mInputEventConsistencyVerifier != null) {
5721 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5722 }
5723 return false;
5724 }
5725
5726 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005727 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005728 ListenerInfo li = mListenerInfo;
5729 if (li != null && li.mOnGenericMotionListener != null
5730 && (mViewFlags & ENABLED_MASK) == ENABLED
5731 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005732 return true;
5733 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005734
5735 if (onGenericMotionEvent(event)) {
5736 return true;
5737 }
5738
5739 if (mInputEventConsistencyVerifier != null) {
5740 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5741 }
5742 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005743 }
5744
5745 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005746 * Dispatch a hover event.
5747 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005748 * Do not call this method directly.
5749 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005750 * </p>
5751 *
5752 * @param event The motion event to be dispatched.
5753 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005754 */
5755 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005756 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005757 ListenerInfo li = mListenerInfo;
5758 if (li != null && li.mOnHoverListener != null
5759 && (mViewFlags & ENABLED_MASK) == ENABLED
5760 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07005761 return true;
5762 }
5763
Jeff Browna032cc02011-03-07 16:56:21 -08005764 return onHoverEvent(event);
5765 }
5766
5767 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005768 * Returns true if the view has a child to which it has recently sent
5769 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5770 * it does not have a hovered child, then it must be the innermost hovered view.
5771 * @hide
5772 */
5773 protected boolean hasHoveredChild() {
5774 return false;
5775 }
5776
5777 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005778 * Dispatch a generic motion event to the view under the first pointer.
5779 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005780 * Do not call this method directly.
5781 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005782 * </p>
5783 *
5784 * @param event The motion event to be dispatched.
5785 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005786 */
5787 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5788 return false;
5789 }
5790
5791 /**
5792 * Dispatch a generic motion event to the currently focused view.
5793 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005794 * Do not call this method directly.
5795 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005796 * </p>
5797 *
5798 * @param event The motion event to be dispatched.
5799 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005800 */
5801 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5802 return false;
5803 }
5804
5805 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005806 * Dispatch a pointer event.
5807 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005808 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5809 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5810 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005811 * and should not be expected to handle other pointing device features.
5812 * </p>
5813 *
5814 * @param event The motion event to be dispatched.
5815 * @return True if the event was handled by the view, false otherwise.
5816 * @hide
5817 */
5818 public final boolean dispatchPointerEvent(MotionEvent event) {
5819 if (event.isTouchEvent()) {
5820 return dispatchTouchEvent(event);
5821 } else {
5822 return dispatchGenericMotionEvent(event);
5823 }
5824 }
5825
5826 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005827 * Called when the window containing this view gains or loses window focus.
5828 * ViewGroups should override to route to their children.
5829 *
5830 * @param hasFocus True if the window containing this view now has focus,
5831 * false otherwise.
5832 */
5833 public void dispatchWindowFocusChanged(boolean hasFocus) {
5834 onWindowFocusChanged(hasFocus);
5835 }
5836
5837 /**
5838 * Called when the window containing this view gains or loses focus. Note
5839 * that this is separate from view focus: to receive key events, both
5840 * your view and its window must have focus. If a window is displayed
5841 * on top of yours that takes input focus, then your own window will lose
5842 * focus but the view focus will remain unchanged.
5843 *
5844 * @param hasWindowFocus True if the window containing this view now has
5845 * focus, false otherwise.
5846 */
5847 public void onWindowFocusChanged(boolean hasWindowFocus) {
5848 InputMethodManager imm = InputMethodManager.peekInstance();
5849 if (!hasWindowFocus) {
5850 if (isPressed()) {
5851 setPressed(false);
5852 }
5853 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5854 imm.focusOut(this);
5855 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005856 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005857 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005858 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005859 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5860 imm.focusIn(this);
5861 }
5862 refreshDrawableState();
5863 }
5864
5865 /**
5866 * Returns true if this view is in a window that currently has window focus.
5867 * Note that this is not the same as the view itself having focus.
5868 *
5869 * @return True if this view is in a window that currently has window focus.
5870 */
5871 public boolean hasWindowFocus() {
5872 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5873 }
5874
5875 /**
Adam Powell326d8082009-12-09 15:10:07 -08005876 * Dispatch a view visibility change down the view hierarchy.
5877 * ViewGroups should override to route to their children.
5878 * @param changedView The view whose visibility changed. Could be 'this' or
5879 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005880 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5881 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005882 */
5883 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5884 onVisibilityChanged(changedView, visibility);
5885 }
5886
5887 /**
5888 * Called when the visibility of the view or an ancestor of the view is changed.
5889 * @param changedView The view whose visibility changed. Could be 'this' or
5890 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005891 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5892 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005893 */
5894 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005895 if (visibility == VISIBLE) {
5896 if (mAttachInfo != null) {
5897 initialAwakenScrollBars();
5898 } else {
5899 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5900 }
5901 }
Adam Powell326d8082009-12-09 15:10:07 -08005902 }
5903
5904 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005905 * Dispatch a hint about whether this view is displayed. For instance, when
5906 * a View moves out of the screen, it might receives a display hint indicating
5907 * the view is not displayed. Applications should not <em>rely</em> on this hint
5908 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005909 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005910 * @param hint A hint about whether or not this view is displayed:
5911 * {@link #VISIBLE} or {@link #INVISIBLE}.
5912 */
5913 public void dispatchDisplayHint(int hint) {
5914 onDisplayHint(hint);
5915 }
5916
5917 /**
5918 * Gives this view a hint about whether is displayed or not. For instance, when
5919 * a View moves out of the screen, it might receives a display hint indicating
5920 * the view is not displayed. Applications should not <em>rely</em> on this hint
5921 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005922 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005923 * @param hint A hint about whether or not this view is displayed:
5924 * {@link #VISIBLE} or {@link #INVISIBLE}.
5925 */
5926 protected void onDisplayHint(int hint) {
5927 }
5928
5929 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005930 * Dispatch a window visibility change down the view hierarchy.
5931 * ViewGroups should override to route to their children.
5932 *
5933 * @param visibility The new visibility of the window.
5934 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005935 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005936 */
5937 public void dispatchWindowVisibilityChanged(int visibility) {
5938 onWindowVisibilityChanged(visibility);
5939 }
5940
5941 /**
5942 * Called when the window containing has change its visibility
5943 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5944 * that this tells you whether or not your window is being made visible
5945 * to the window manager; this does <em>not</em> tell you whether or not
5946 * your window is obscured by other windows on the screen, even if it
5947 * is itself visible.
5948 *
5949 * @param visibility The new visibility of the window.
5950 */
5951 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005952 if (visibility == VISIBLE) {
5953 initialAwakenScrollBars();
5954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005955 }
5956
5957 /**
5958 * Returns the current visibility of the window this view is attached to
5959 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5960 *
5961 * @return Returns the current visibility of the view's window.
5962 */
5963 public int getWindowVisibility() {
5964 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5965 }
5966
5967 /**
5968 * Retrieve the overall visible display size in which the window this view is
5969 * attached to has been positioned in. This takes into account screen
5970 * decorations above the window, for both cases where the window itself
5971 * is being position inside of them or the window is being placed under
5972 * then and covered insets are used for the window to position its content
5973 * inside. In effect, this tells you the available area where content can
5974 * be placed and remain visible to users.
5975 *
5976 * <p>This function requires an IPC back to the window manager to retrieve
5977 * the requested information, so should not be used in performance critical
5978 * code like drawing.
5979 *
5980 * @param outRect Filled in with the visible display frame. If the view
5981 * is not attached to a window, this is simply the raw display size.
5982 */
5983 public void getWindowVisibleDisplayFrame(Rect outRect) {
5984 if (mAttachInfo != null) {
5985 try {
5986 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5987 } catch (RemoteException e) {
5988 return;
5989 }
5990 // XXX This is really broken, and probably all needs to be done
5991 // in the window manager, and we need to know more about whether
5992 // we want the area behind or in front of the IME.
5993 final Rect insets = mAttachInfo.mVisibleInsets;
5994 outRect.left += insets.left;
5995 outRect.top += insets.top;
5996 outRect.right -= insets.right;
5997 outRect.bottom -= insets.bottom;
5998 return;
5999 }
6000 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006001 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006002 }
6003
6004 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006005 * Dispatch a notification about a resource configuration change down
6006 * the view hierarchy.
6007 * ViewGroups should override to route to their children.
6008 *
6009 * @param newConfig The new resource configuration.
6010 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006011 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006012 */
6013 public void dispatchConfigurationChanged(Configuration newConfig) {
6014 onConfigurationChanged(newConfig);
6015 }
6016
6017 /**
6018 * Called when the current configuration of the resources being used
6019 * by the application have changed. You can use this to decide when
6020 * to reload resources that can changed based on orientation and other
6021 * configuration characterstics. You only need to use this if you are
6022 * not relying on the normal {@link android.app.Activity} mechanism of
6023 * recreating the activity instance upon a configuration change.
6024 *
6025 * @param newConfig The new resource configuration.
6026 */
6027 protected void onConfigurationChanged(Configuration newConfig) {
6028 }
6029
6030 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006031 * Private function to aggregate all per-view attributes in to the view
6032 * root.
6033 */
6034 void dispatchCollectViewAttributes(int visibility) {
6035 performCollectViewAttributes(visibility);
6036 }
6037
6038 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08006039 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006040 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
6041 mAttachInfo.mKeepScreenOn = true;
6042 }
6043 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006044 ListenerInfo li = mListenerInfo;
6045 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006046 mAttachInfo.mHasSystemUiListeners = true;
6047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006048 }
6049 }
6050
6051 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006052 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006053 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006054 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6055 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006056 ai.mRecomputeGlobalAttributes = true;
6057 }
6058 }
6059 }
6060
6061 /**
6062 * Returns whether the device is currently in touch mode. Touch mode is entered
6063 * once the user begins interacting with the device by touch, and affects various
6064 * things like whether focus is always visible to the user.
6065 *
6066 * @return Whether the device is in touch mode.
6067 */
6068 @ViewDebug.ExportedProperty
6069 public boolean isInTouchMode() {
6070 if (mAttachInfo != null) {
6071 return mAttachInfo.mInTouchMode;
6072 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006073 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006074 }
6075 }
6076
6077 /**
6078 * Returns the context the view is running in, through which it can
6079 * access the current theme, resources, etc.
6080 *
6081 * @return The view's Context.
6082 */
6083 @ViewDebug.CapturedViewProperty
6084 public final Context getContext() {
6085 return mContext;
6086 }
6087
6088 /**
6089 * Handle a key event before it is processed by any input method
6090 * associated with the view hierarchy. This can be used to intercept
6091 * key events in special situations before the IME consumes them; a
6092 * typical example would be handling the BACK key to update the application's
6093 * UI instead of allowing the IME to see it and close itself.
6094 *
6095 * @param keyCode The value in event.getKeyCode().
6096 * @param event Description of the key event.
6097 * @return If you handled the event, return true. If you want to allow the
6098 * event to be handled by the next receiver, return false.
6099 */
6100 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
6101 return false;
6102 }
6103
6104 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006105 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
6106 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006107 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
6108 * is released, if the view is enabled and clickable.
6109 *
6110 * @param keyCode A key code that represents the button pressed, from
6111 * {@link android.view.KeyEvent}.
6112 * @param event The KeyEvent object that defines the button action.
6113 */
6114 public boolean onKeyDown(int keyCode, KeyEvent event) {
6115 boolean result = false;
6116
6117 switch (keyCode) {
6118 case KeyEvent.KEYCODE_DPAD_CENTER:
6119 case KeyEvent.KEYCODE_ENTER: {
6120 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6121 return true;
6122 }
6123 // Long clickable items don't necessarily have to be clickable
6124 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
6125 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
6126 (event.getRepeatCount() == 0)) {
6127 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006128 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006129 return true;
6130 }
6131 break;
6132 }
6133 }
6134 return result;
6135 }
6136
6137 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006138 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
6139 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
6140 * the event).
6141 */
6142 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
6143 return false;
6144 }
6145
6146 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006147 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
6148 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006149 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
6150 * {@link KeyEvent#KEYCODE_ENTER} is released.
6151 *
6152 * @param keyCode A key code that represents the button pressed, from
6153 * {@link android.view.KeyEvent}.
6154 * @param event The KeyEvent object that defines the button action.
6155 */
6156 public boolean onKeyUp(int keyCode, KeyEvent event) {
6157 boolean result = false;
6158
6159 switch (keyCode) {
6160 case KeyEvent.KEYCODE_DPAD_CENTER:
6161 case KeyEvent.KEYCODE_ENTER: {
6162 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6163 return true;
6164 }
6165 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
6166 setPressed(false);
6167
6168 if (!mHasPerformedLongPress) {
6169 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006170 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006171
6172 result = performClick();
6173 }
6174 }
6175 break;
6176 }
6177 }
6178 return result;
6179 }
6180
6181 /**
6182 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
6183 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
6184 * the event).
6185 *
6186 * @param keyCode A key code that represents the button pressed, from
6187 * {@link android.view.KeyEvent}.
6188 * @param repeatCount The number of times the action was made.
6189 * @param event The KeyEvent object that defines the button action.
6190 */
6191 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6192 return false;
6193 }
6194
6195 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006196 * Called on the focused view when a key shortcut event is not handled.
6197 * Override this method to implement local key shortcuts for the View.
6198 * Key shortcuts can also be implemented by setting the
6199 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006200 *
6201 * @param keyCode The value in event.getKeyCode().
6202 * @param event Description of the key event.
6203 * @return If you handled the event, return true. If you want to allow the
6204 * event to be handled by the next receiver, return false.
6205 */
6206 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6207 return false;
6208 }
6209
6210 /**
6211 * Check whether the called view is a text editor, in which case it
6212 * would make sense to automatically display a soft input window for
6213 * it. Subclasses should override this if they implement
6214 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006215 * a call on that method would return a non-null InputConnection, and
6216 * they are really a first-class editor that the user would normally
6217 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006218 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006219 * <p>The default implementation always returns false. This does
6220 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6221 * will not be called or the user can not otherwise perform edits on your
6222 * view; it is just a hint to the system that this is not the primary
6223 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006224 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006225 * @return Returns true if this view is a text editor, else false.
6226 */
6227 public boolean onCheckIsTextEditor() {
6228 return false;
6229 }
Romain Guy8506ab42009-06-11 17:35:47 -07006230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006231 /**
6232 * Create a new InputConnection for an InputMethod to interact
6233 * with the view. The default implementation returns null, since it doesn't
6234 * support input methods. You can override this to implement such support.
6235 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006236 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006237 * <p>When implementing this, you probably also want to implement
6238 * {@link #onCheckIsTextEditor()} to indicate you will return a
6239 * non-null InputConnection.
6240 *
6241 * @param outAttrs Fill in with attribute information about the connection.
6242 */
6243 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6244 return null;
6245 }
6246
6247 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006248 * Called by the {@link android.view.inputmethod.InputMethodManager}
6249 * when a view who is not the current
6250 * input connection target is trying to make a call on the manager. The
6251 * default implementation returns false; you can override this to return
6252 * true for certain views if you are performing InputConnection proxying
6253 * to them.
6254 * @param view The View that is making the InputMethodManager call.
6255 * @return Return true to allow the call, false to reject.
6256 */
6257 public boolean checkInputConnectionProxy(View view) {
6258 return false;
6259 }
Romain Guy8506ab42009-06-11 17:35:47 -07006260
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006261 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006262 * Show the context menu for this view. It is not safe to hold on to the
6263 * menu after returning from this method.
6264 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006265 * You should normally not overload this method. Overload
6266 * {@link #onCreateContextMenu(ContextMenu)} or define an
6267 * {@link OnCreateContextMenuListener} to add items to the context menu.
6268 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006269 * @param menu The context menu to populate
6270 */
6271 public void createContextMenu(ContextMenu menu) {
6272 ContextMenuInfo menuInfo = getContextMenuInfo();
6273
6274 // Sets the current menu info so all items added to menu will have
6275 // my extra info set.
6276 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6277
6278 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006279 ListenerInfo li = mListenerInfo;
6280 if (li != null && li.mOnCreateContextMenuListener != null) {
6281 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006282 }
6283
6284 // Clear the extra information so subsequent items that aren't mine don't
6285 // have my extra info.
6286 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6287
6288 if (mParent != null) {
6289 mParent.createContextMenu(menu);
6290 }
6291 }
6292
6293 /**
6294 * Views should implement this if they have extra information to associate
6295 * with the context menu. The return result is supplied as a parameter to
6296 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6297 * callback.
6298 *
6299 * @return Extra information about the item for which the context menu
6300 * should be shown. This information will vary across different
6301 * subclasses of View.
6302 */
6303 protected ContextMenuInfo getContextMenuInfo() {
6304 return null;
6305 }
6306
6307 /**
6308 * Views should implement this if the view itself is going to add items to
6309 * the context menu.
6310 *
6311 * @param menu the context menu to populate
6312 */
6313 protected void onCreateContextMenu(ContextMenu menu) {
6314 }
6315
6316 /**
6317 * Implement this method to handle trackball motion events. The
6318 * <em>relative</em> movement of the trackball since the last event
6319 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6320 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6321 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6322 * they will often be fractional values, representing the more fine-grained
6323 * movement information available from a trackball).
6324 *
6325 * @param event The motion event.
6326 * @return True if the event was handled, false otherwise.
6327 */
6328 public boolean onTrackballEvent(MotionEvent event) {
6329 return false;
6330 }
6331
6332 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006333 * Implement this method to handle generic motion events.
6334 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006335 * Generic motion events describe joystick movements, mouse hovers, track pad
6336 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006337 * {@link MotionEvent#getSource() source} of the motion event specifies
6338 * the class of input that was received. Implementations of this method
6339 * must examine the bits in the source before processing the event.
6340 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006341 * </p><p>
6342 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6343 * are delivered to the view under the pointer. All other generic motion events are
6344 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006345 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07006346 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006347 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006348 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6349 * // process the joystick movement...
6350 * return true;
6351 * }
6352 * }
6353 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6354 * switch (event.getAction()) {
6355 * case MotionEvent.ACTION_HOVER_MOVE:
6356 * // process the mouse hover movement...
6357 * return true;
6358 * case MotionEvent.ACTION_SCROLL:
6359 * // process the scroll wheel movement...
6360 * return true;
6361 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006362 * }
6363 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07006364 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08006365 *
6366 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006367 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006368 */
6369 public boolean onGenericMotionEvent(MotionEvent event) {
6370 return false;
6371 }
6372
6373 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006374 * Implement this method to handle hover events.
6375 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006376 * This method is called whenever a pointer is hovering into, over, or out of the
6377 * bounds of a view and the view is not currently being touched.
6378 * Hover events are represented as pointer events with action
6379 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6380 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6381 * </p>
6382 * <ul>
6383 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6384 * when the pointer enters the bounds of the view.</li>
6385 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6386 * when the pointer has already entered the bounds of the view and has moved.</li>
6387 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6388 * when the pointer has exited the bounds of the view or when the pointer is
6389 * about to go down due to a button click, tap, or similar user action that
6390 * causes the view to be touched.</li>
6391 * </ul>
6392 * <p>
6393 * The view should implement this method to return true to indicate that it is
6394 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006395 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006396 * The default implementation calls {@link #setHovered} to update the hovered state
6397 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006398 * is enabled and is clickable. The default implementation also sends hover
6399 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006400 * </p>
6401 *
6402 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006403 * @return True if the view handled the hover event.
6404 *
6405 * @see #isHovered
6406 * @see #setHovered
6407 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006408 */
6409 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006410 // The root view may receive hover (or touch) events that are outside the bounds of
6411 // the window. This code ensures that we only send accessibility events for
6412 // hovers that are actually within the bounds of the root view.
6413 final int action = event.getAction();
6414 if (!mSendingHoverAccessibilityEvents) {
6415 if ((action == MotionEvent.ACTION_HOVER_ENTER
6416 || action == MotionEvent.ACTION_HOVER_MOVE)
6417 && !hasHoveredChild()
6418 && pointInView(event.getX(), event.getY())) {
6419 mSendingHoverAccessibilityEvents = true;
6420 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6421 }
6422 } else {
6423 if (action == MotionEvent.ACTION_HOVER_EXIT
6424 || (action == MotionEvent.ACTION_HOVER_MOVE
6425 && !pointInView(event.getX(), event.getY()))) {
6426 mSendingHoverAccessibilityEvents = false;
6427 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6428 }
Jeff Browna1b24182011-07-28 13:38:24 -07006429 }
6430
Jeff Brown87b7f802011-06-21 18:35:45 -07006431 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006432 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006433 case MotionEvent.ACTION_HOVER_ENTER:
6434 setHovered(true);
6435 break;
6436 case MotionEvent.ACTION_HOVER_EXIT:
6437 setHovered(false);
6438 break;
6439 }
Jeff Browna1b24182011-07-28 13:38:24 -07006440
6441 // Dispatch the event to onGenericMotionEvent before returning true.
6442 // This is to provide compatibility with existing applications that
6443 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6444 // break because of the new default handling for hoverable views
6445 // in onHoverEvent.
6446 // Note that onGenericMotionEvent will be called by default when
6447 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6448 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006449 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006450 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006451 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006452 }
6453
6454 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006455 * Returns true if the view should handle {@link #onHoverEvent}
6456 * by calling {@link #setHovered} to change its hovered state.
6457 *
6458 * @return True if the view is hoverable.
6459 */
6460 private boolean isHoverable() {
6461 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006462 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006463 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6464 return false;
6465 }
6466
6467 return (viewFlags & CLICKABLE) == CLICKABLE
6468 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6469 }
6470
6471 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006472 * Returns true if the view is currently hovered.
6473 *
6474 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006475 *
6476 * @see #setHovered
6477 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006478 */
Jeff Brown10b62902011-06-20 16:40:37 -07006479 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006480 public boolean isHovered() {
6481 return (mPrivateFlags & HOVERED) != 0;
6482 }
6483
6484 /**
6485 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006486 * <p>
6487 * Calling this method also changes the drawable state of the view. This
6488 * enables the view to react to hover by using different drawable resources
6489 * to change its appearance.
6490 * </p><p>
6491 * The {@link #onHoverChanged} method is called when the hovered state changes.
6492 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006493 *
6494 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006495 *
6496 * @see #isHovered
6497 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006498 */
6499 public void setHovered(boolean hovered) {
6500 if (hovered) {
6501 if ((mPrivateFlags & HOVERED) == 0) {
6502 mPrivateFlags |= HOVERED;
6503 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006504 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006505 }
6506 } else {
6507 if ((mPrivateFlags & HOVERED) != 0) {
6508 mPrivateFlags &= ~HOVERED;
6509 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006510 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006511 }
6512 }
6513 }
6514
6515 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006516 * Implement this method to handle hover state changes.
6517 * <p>
6518 * This method is called whenever the hover state changes as a result of a
6519 * call to {@link #setHovered}.
6520 * </p>
6521 *
6522 * @param hovered The current hover state, as returned by {@link #isHovered}.
6523 *
6524 * @see #isHovered
6525 * @see #setHovered
6526 */
6527 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006528 }
6529
6530 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006531 * Implement this method to handle touch screen motion events.
6532 *
6533 * @param event The motion event.
6534 * @return True if the event was handled, false otherwise.
6535 */
6536 public boolean onTouchEvent(MotionEvent event) {
6537 final int viewFlags = mViewFlags;
6538
6539 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006540 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08006541 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006543 // A disabled view that is clickable still consumes the touch
6544 // events, it just doesn't respond to them.
6545 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6546 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6547 }
6548
6549 if (mTouchDelegate != null) {
6550 if (mTouchDelegate.onTouchEvent(event)) {
6551 return true;
6552 }
6553 }
6554
6555 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6556 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6557 switch (event.getAction()) {
6558 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006559 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6560 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006561 // take focus if we don't have it already and we should in
6562 // touch mode.
6563 boolean focusTaken = false;
6564 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6565 focusTaken = requestFocus();
6566 }
6567
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006568 if (prepressed) {
6569 // The button is being released before we actually
6570 // showed it as pressed. Make it show the pressed
6571 // state now (before scheduling the click) to ensure
6572 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08006573 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006574 }
Joe Malin32736f02011-01-19 16:14:20 -08006575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006576 if (!mHasPerformedLongPress) {
6577 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006578 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006579
6580 // Only perform take click actions if we were in the pressed state
6581 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006582 // Use a Runnable and post this rather than calling
6583 // performClick directly. This lets other visual state
6584 // of the view update before click actions start.
6585 if (mPerformClick == null) {
6586 mPerformClick = new PerformClick();
6587 }
6588 if (!post(mPerformClick)) {
6589 performClick();
6590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006591 }
6592 }
6593
6594 if (mUnsetPressedState == null) {
6595 mUnsetPressedState = new UnsetPressedState();
6596 }
6597
Adam Powelle14579b2009-12-16 18:39:52 -08006598 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006599 postDelayed(mUnsetPressedState,
6600 ViewConfiguration.getPressedStateDuration());
6601 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006602 // If the post failed, unpress right now
6603 mUnsetPressedState.run();
6604 }
Adam Powelle14579b2009-12-16 18:39:52 -08006605 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006606 }
6607 break;
6608
6609 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006610 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006611
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006612 if (performButtonActionOnTouchDown(event)) {
6613 break;
6614 }
6615
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006616 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006617 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006618
6619 // For views inside a scrolling container, delay the pressed feedback for
6620 // a short period in case this is a scroll.
6621 if (isInScrollingContainer) {
6622 mPrivateFlags |= PREPRESSED;
6623 if (mPendingCheckForTap == null) {
6624 mPendingCheckForTap = new CheckForTap();
6625 }
6626 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6627 } else {
6628 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08006629 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006630 checkForLongClick(0);
6631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006632 break;
6633
6634 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08006635 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08006636 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006637 break;
6638
6639 case MotionEvent.ACTION_MOVE:
6640 final int x = (int) event.getX();
6641 final int y = (int) event.getY();
6642
6643 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006644 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006645 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006646 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006647 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006648 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006649 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006650
Adam Powell4d6f0662012-02-21 15:11:11 -08006651 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006653 }
6654 break;
6655 }
6656 return true;
6657 }
6658
6659 return false;
6660 }
6661
6662 /**
Adam Powell10298662011-08-14 18:26:30 -07006663 * @hide
6664 */
6665 public boolean isInScrollingContainer() {
6666 ViewParent p = getParent();
6667 while (p != null && p instanceof ViewGroup) {
6668 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6669 return true;
6670 }
6671 p = p.getParent();
6672 }
6673 return false;
6674 }
6675
6676 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006677 * Remove the longpress detection timer.
6678 */
6679 private void removeLongPressCallback() {
6680 if (mPendingCheckForLongPress != null) {
6681 removeCallbacks(mPendingCheckForLongPress);
6682 }
6683 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006684
6685 /**
6686 * Remove the pending click action
6687 */
6688 private void removePerformClickCallback() {
6689 if (mPerformClick != null) {
6690 removeCallbacks(mPerformClick);
6691 }
6692 }
6693
Adam Powelle14579b2009-12-16 18:39:52 -08006694 /**
Romain Guya440b002010-02-24 15:57:54 -08006695 * Remove the prepress detection timer.
6696 */
6697 private void removeUnsetPressCallback() {
6698 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6699 setPressed(false);
6700 removeCallbacks(mUnsetPressedState);
6701 }
6702 }
6703
6704 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006705 * Remove the tap detection timer.
6706 */
6707 private void removeTapCallback() {
6708 if (mPendingCheckForTap != null) {
6709 mPrivateFlags &= ~PREPRESSED;
6710 removeCallbacks(mPendingCheckForTap);
6711 }
6712 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006713
6714 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006715 * Cancels a pending long press. Your subclass can use this if you
6716 * want the context menu to come up if the user presses and holds
6717 * at the same place, but you don't want it to come up if they press
6718 * and then move around enough to cause scrolling.
6719 */
6720 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006721 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006722
6723 /*
6724 * The prepressed state handled by the tap callback is a display
6725 * construct, but the tap callback will post a long press callback
6726 * less its own timeout. Remove it here.
6727 */
6728 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006729 }
6730
6731 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006732 * Remove the pending callback for sending a
6733 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6734 */
6735 private void removeSendViewScrolledAccessibilityEventCallback() {
6736 if (mSendViewScrolledAccessibilityEvent != null) {
6737 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6738 }
6739 }
6740
6741 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006742 * Sets the TouchDelegate for this View.
6743 */
6744 public void setTouchDelegate(TouchDelegate delegate) {
6745 mTouchDelegate = delegate;
6746 }
6747
6748 /**
6749 * Gets the TouchDelegate for this View.
6750 */
6751 public TouchDelegate getTouchDelegate() {
6752 return mTouchDelegate;
6753 }
6754
6755 /**
6756 * Set flags controlling behavior of this view.
6757 *
6758 * @param flags Constant indicating the value which should be set
6759 * @param mask Constant indicating the bit range that should be changed
6760 */
6761 void setFlags(int flags, int mask) {
6762 int old = mViewFlags;
6763 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6764
6765 int changed = mViewFlags ^ old;
6766 if (changed == 0) {
6767 return;
6768 }
6769 int privateFlags = mPrivateFlags;
6770
6771 /* Check if the FOCUSABLE bit has changed */
6772 if (((changed & FOCUSABLE_MASK) != 0) &&
6773 ((privateFlags & HAS_BOUNDS) !=0)) {
6774 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6775 && ((privateFlags & FOCUSED) != 0)) {
6776 /* Give up focus if we are no longer focusable */
6777 clearFocus();
6778 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6779 && ((privateFlags & FOCUSED) == 0)) {
6780 /*
6781 * Tell the view system that we are now available to take focus
6782 * if no one else already has it.
6783 */
6784 if (mParent != null) mParent.focusableViewAvailable(this);
6785 }
6786 }
6787
6788 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6789 if ((changed & VISIBILITY_MASK) != 0) {
6790 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006791 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006792 * it was not visible. Marking it drawn ensures that the invalidation will
6793 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006794 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006795 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006796 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006797
6798 needGlobalAttributesUpdate(true);
6799
6800 // a view becoming visible is worth notifying the parent
6801 // about in case nothing has focus. even if this specific view
6802 // isn't focusable, it may contain something that is, so let
6803 // the root view try to give this focus if nothing else does.
6804 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6805 mParent.focusableViewAvailable(this);
6806 }
6807 }
6808 }
6809
6810 /* Check if the GONE bit has changed */
6811 if ((changed & GONE) != 0) {
6812 needGlobalAttributesUpdate(false);
6813 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006814
Romain Guyecd80ee2009-12-03 17:13:02 -08006815 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6816 if (hasFocus()) clearFocus();
6817 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006818 if (mParent instanceof View) {
6819 // GONE views noop invalidation, so invalidate the parent
6820 ((View) mParent).invalidate(true);
6821 }
6822 // Mark the view drawn to ensure that it gets invalidated properly the next
6823 // time it is visible and gets invalidated
6824 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006825 }
6826 if (mAttachInfo != null) {
6827 mAttachInfo.mViewVisibilityChanged = true;
6828 }
6829 }
6830
6831 /* Check if the VISIBLE bit has changed */
6832 if ((changed & INVISIBLE) != 0) {
6833 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006834 /*
6835 * If this view is becoming invisible, set the DRAWN flag so that
6836 * the next invalidate() will not be skipped.
6837 */
6838 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006839
6840 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6841 // root view becoming invisible shouldn't clear focus
6842 if (getRootView() != this) {
6843 clearFocus();
6844 }
6845 }
6846 if (mAttachInfo != null) {
6847 mAttachInfo.mViewVisibilityChanged = true;
6848 }
6849 }
6850
Adam Powell326d8082009-12-09 15:10:07 -08006851 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006852 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08006853 ((ViewGroup) mParent).onChildVisibilityChanged(this,
6854 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08006855 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006856 } else if (mParent != null) {
6857 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006858 }
Adam Powell326d8082009-12-09 15:10:07 -08006859 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6860 }
6861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006862 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6863 destroyDrawingCache();
6864 }
6865
6866 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6867 destroyDrawingCache();
6868 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006869 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006870 }
6871
6872 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6873 destroyDrawingCache();
6874 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6875 }
6876
6877 if ((changed & DRAW_MASK) != 0) {
6878 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6879 if (mBGDrawable != null) {
6880 mPrivateFlags &= ~SKIP_DRAW;
6881 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6882 } else {
6883 mPrivateFlags |= SKIP_DRAW;
6884 }
6885 } else {
6886 mPrivateFlags &= ~SKIP_DRAW;
6887 }
6888 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006889 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006890 }
6891
6892 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006893 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006894 mParent.recomputeViewAttributes(this);
6895 }
6896 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006897
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006898 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006899 requestLayout();
6900 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006901 }
6902
6903 /**
6904 * Change the view's z order in the tree, so it's on top of other sibling
6905 * views
6906 */
6907 public void bringToFront() {
6908 if (mParent != null) {
6909 mParent.bringChildToFront(this);
6910 }
6911 }
6912
6913 /**
6914 * This is called in response to an internal scroll in this view (i.e., the
6915 * view scrolled its own contents). This is typically as a result of
6916 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6917 * called.
6918 *
6919 * @param l Current horizontal scroll origin.
6920 * @param t Current vertical scroll origin.
6921 * @param oldl Previous horizontal scroll origin.
6922 * @param oldt Previous vertical scroll origin.
6923 */
6924 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006925 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6926 postSendViewScrolledAccessibilityEventCallback();
6927 }
6928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006929 mBackgroundSizeChanged = true;
6930
6931 final AttachInfo ai = mAttachInfo;
6932 if (ai != null) {
6933 ai.mViewScrollChanged = true;
6934 }
6935 }
6936
6937 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006938 * Interface definition for a callback to be invoked when the layout bounds of a view
6939 * changes due to layout processing.
6940 */
6941 public interface OnLayoutChangeListener {
6942 /**
6943 * Called when the focus state of a view has changed.
6944 *
6945 * @param v The view whose state has changed.
6946 * @param left The new value of the view's left property.
6947 * @param top The new value of the view's top property.
6948 * @param right The new value of the view's right property.
6949 * @param bottom The new value of the view's bottom property.
6950 * @param oldLeft The previous value of the view's left property.
6951 * @param oldTop The previous value of the view's top property.
6952 * @param oldRight The previous value of the view's right property.
6953 * @param oldBottom The previous value of the view's bottom property.
6954 */
6955 void onLayoutChange(View v, int left, int top, int right, int bottom,
6956 int oldLeft, int oldTop, int oldRight, int oldBottom);
6957 }
6958
6959 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006960 * This is called during layout when the size of this view has changed. If
6961 * you were just added to the view hierarchy, you're called with the old
6962 * values of 0.
6963 *
6964 * @param w Current width of this view.
6965 * @param h Current height of this view.
6966 * @param oldw Old width of this view.
6967 * @param oldh Old height of this view.
6968 */
6969 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6970 }
6971
6972 /**
6973 * Called by draw to draw the child views. This may be overridden
6974 * by derived classes to gain control just before its children are drawn
6975 * (but after its own view has been drawn).
6976 * @param canvas the canvas on which to draw the view
6977 */
6978 protected void dispatchDraw(Canvas canvas) {
6979 }
6980
6981 /**
6982 * Gets the parent of this view. Note that the parent is a
6983 * ViewParent and not necessarily a View.
6984 *
6985 * @return Parent of this view.
6986 */
6987 public final ViewParent getParent() {
6988 return mParent;
6989 }
6990
6991 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006992 * Set the horizontal scrolled position of your view. This will cause a call to
6993 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6994 * invalidated.
6995 * @param value the x position to scroll to
6996 */
6997 public void setScrollX(int value) {
6998 scrollTo(value, mScrollY);
6999 }
7000
7001 /**
7002 * Set the vertical scrolled position of your view. This will cause a call to
7003 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7004 * invalidated.
7005 * @param value the y position to scroll to
7006 */
7007 public void setScrollY(int value) {
7008 scrollTo(mScrollX, value);
7009 }
7010
7011 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007012 * Return the scrolled left position of this view. This is the left edge of
7013 * the displayed part of your view. You do not need to draw any pixels
7014 * farther left, since those are outside of the frame of your view on
7015 * screen.
7016 *
7017 * @return The left edge of the displayed part of your view, in pixels.
7018 */
7019 public final int getScrollX() {
7020 return mScrollX;
7021 }
7022
7023 /**
7024 * Return the scrolled top position of this view. This is the top edge of
7025 * the displayed part of your view. You do not need to draw any pixels above
7026 * it, since those are outside of the frame of your view on screen.
7027 *
7028 * @return The top edge of the displayed part of your view, in pixels.
7029 */
7030 public final int getScrollY() {
7031 return mScrollY;
7032 }
7033
7034 /**
7035 * Return the width of the your view.
7036 *
7037 * @return The width of your view, in pixels.
7038 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007039 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007040 public final int getWidth() {
7041 return mRight - mLeft;
7042 }
7043
7044 /**
7045 * Return the height of your view.
7046 *
7047 * @return The height of your view, in pixels.
7048 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007049 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007050 public final int getHeight() {
7051 return mBottom - mTop;
7052 }
7053
7054 /**
7055 * Return the visible drawing bounds of your view. Fills in the output
7056 * rectangle with the values from getScrollX(), getScrollY(),
7057 * getWidth(), and getHeight().
7058 *
7059 * @param outRect The (scrolled) drawing bounds of the view.
7060 */
7061 public void getDrawingRect(Rect outRect) {
7062 outRect.left = mScrollX;
7063 outRect.top = mScrollY;
7064 outRect.right = mScrollX + (mRight - mLeft);
7065 outRect.bottom = mScrollY + (mBottom - mTop);
7066 }
7067
7068 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007069 * Like {@link #getMeasuredWidthAndState()}, but only returns the
7070 * raw width component (that is the result is masked by
7071 * {@link #MEASURED_SIZE_MASK}).
7072 *
7073 * @return The raw measured width of this view.
7074 */
7075 public final int getMeasuredWidth() {
7076 return mMeasuredWidth & MEASURED_SIZE_MASK;
7077 }
7078
7079 /**
7080 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007081 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007082 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007083 * This should be used during measurement and layout calculations only. Use
7084 * {@link #getWidth()} to see how wide a view is after layout.
7085 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007086 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007087 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08007088 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007089 return mMeasuredWidth;
7090 }
7091
7092 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007093 * Like {@link #getMeasuredHeightAndState()}, but only returns the
7094 * raw width component (that is the result is masked by
7095 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007096 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007097 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007098 */
7099 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08007100 return mMeasuredHeight & MEASURED_SIZE_MASK;
7101 }
7102
7103 /**
7104 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007105 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007106 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
7107 * This should be used during measurement and layout calculations only. Use
7108 * {@link #getHeight()} to see how wide a view is after layout.
7109 *
7110 * @return The measured width of this view as a bit mask.
7111 */
7112 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007113 return mMeasuredHeight;
7114 }
7115
7116 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007117 * Return only the state bits of {@link #getMeasuredWidthAndState()}
7118 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
7119 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
7120 * and the height component is at the shifted bits
7121 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
7122 */
7123 public final int getMeasuredState() {
7124 return (mMeasuredWidth&MEASURED_STATE_MASK)
7125 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
7126 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
7127 }
7128
7129 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007130 * The transform matrix of this view, which is calculated based on the current
7131 * roation, scale, and pivot properties.
7132 *
7133 * @see #getRotation()
7134 * @see #getScaleX()
7135 * @see #getScaleY()
7136 * @see #getPivotX()
7137 * @see #getPivotY()
7138 * @return The current transform matrix for the view
7139 */
7140 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007141 if (mTransformationInfo != null) {
7142 updateMatrix();
7143 return mTransformationInfo.mMatrix;
7144 }
7145 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07007146 }
7147
7148 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007149 * Utility function to determine if the value is far enough away from zero to be
7150 * considered non-zero.
7151 * @param value A floating point value to check for zero-ness
7152 * @return whether the passed-in value is far enough away from zero to be considered non-zero
7153 */
7154 private static boolean nonzero(float value) {
7155 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
7156 }
7157
7158 /**
Jeff Brown86671742010-09-30 20:00:15 -07007159 * Returns true if the transform matrix is the identity matrix.
7160 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08007161 *
Romain Guy33e72ae2010-07-17 12:40:29 -07007162 * @return True if the transform matrix is the identity matrix, false otherwise.
7163 */
Jeff Brown86671742010-09-30 20:00:15 -07007164 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007165 if (mTransformationInfo != null) {
7166 updateMatrix();
7167 return mTransformationInfo.mMatrixIsIdentity;
7168 }
7169 return true;
7170 }
7171
7172 void ensureTransformationInfo() {
7173 if (mTransformationInfo == null) {
7174 mTransformationInfo = new TransformationInfo();
7175 }
Jeff Brown86671742010-09-30 20:00:15 -07007176 }
7177
7178 /**
7179 * Recomputes the transform matrix if necessary.
7180 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007181 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007182 final TransformationInfo info = mTransformationInfo;
7183 if (info == null) {
7184 return;
7185 }
7186 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007187 // transform-related properties have changed since the last time someone
7188 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007189
7190 // Figure out if we need to update the pivot point
7191 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007192 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7193 info.mPrevWidth = mRight - mLeft;
7194 info.mPrevHeight = mBottom - mTop;
7195 info.mPivotX = info.mPrevWidth / 2f;
7196 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007197 }
7198 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007199 info.mMatrix.reset();
7200 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7201 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7202 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7203 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007204 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007205 if (info.mCamera == null) {
7206 info.mCamera = new Camera();
7207 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007208 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007209 info.mCamera.save();
7210 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7211 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7212 info.mCamera.getMatrix(info.matrix3D);
7213 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7214 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7215 info.mPivotY + info.mTranslationY);
7216 info.mMatrix.postConcat(info.matrix3D);
7217 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007218 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007219 info.mMatrixDirty = false;
7220 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7221 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007222 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007223 }
7224
7225 /**
7226 * Utility method to retrieve the inverse of the current mMatrix property.
7227 * We cache the matrix to avoid recalculating it when transform properties
7228 * have not changed.
7229 *
7230 * @return The inverse of the current matrix of this view.
7231 */
Jeff Brown86671742010-09-30 20:00:15 -07007232 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007233 final TransformationInfo info = mTransformationInfo;
7234 if (info != null) {
7235 updateMatrix();
7236 if (info.mInverseMatrixDirty) {
7237 if (info.mInverseMatrix == null) {
7238 info.mInverseMatrix = new Matrix();
7239 }
7240 info.mMatrix.invert(info.mInverseMatrix);
7241 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007242 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007243 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007244 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007245 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007246 }
7247
7248 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007249 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7250 * views are drawn) from the camera to this view. The camera's distance
7251 * affects 3D transformations, for instance rotations around the X and Y
7252 * axis. If the rotationX or rotationY properties are changed and this view is
7253 * large (more than half the size of the screen), it is recommended to always
7254 * use a camera distance that's greater than the height (X axis rotation) or
7255 * the width (Y axis rotation) of this view.</p>
7256 *
7257 * <p>The distance of the camera from the view plane can have an affect on the
7258 * perspective distortion of the view when it is rotated around the x or y axis.
7259 * For example, a large distance will result in a large viewing angle, and there
7260 * will not be much perspective distortion of the view as it rotates. A short
7261 * distance may cause much more perspective distortion upon rotation, and can
7262 * also result in some drawing artifacts if the rotated view ends up partially
7263 * behind the camera (which is why the recommendation is to use a distance at
7264 * least as far as the size of the view, if the view is to be rotated.)</p>
7265 *
7266 * <p>The distance is expressed in "depth pixels." The default distance depends
7267 * on the screen density. For instance, on a medium density display, the
7268 * default distance is 1280. On a high density display, the default distance
7269 * is 1920.</p>
7270 *
7271 * <p>If you want to specify a distance that leads to visually consistent
7272 * results across various densities, use the following formula:</p>
7273 * <pre>
7274 * float scale = context.getResources().getDisplayMetrics().density;
7275 * view.setCameraDistance(distance * scale);
7276 * </pre>
7277 *
7278 * <p>The density scale factor of a high density display is 1.5,
7279 * and 1920 = 1280 * 1.5.</p>
7280 *
7281 * @param distance The distance in "depth pixels", if negative the opposite
7282 * value is used
7283 *
7284 * @see #setRotationX(float)
7285 * @see #setRotationY(float)
7286 */
7287 public void setCameraDistance(float distance) {
7288 invalidateParentCaches();
7289 invalidate(false);
7290
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007291 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007292 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007293 final TransformationInfo info = mTransformationInfo;
7294 if (info.mCamera == null) {
7295 info.mCamera = new Camera();
7296 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007297 }
7298
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007299 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7300 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007301
7302 invalidate(false);
7303 }
7304
7305 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007306 * The degrees that the view is rotated around the pivot point.
7307 *
Romain Guya5364ee2011-02-24 14:46:04 -08007308 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007309 * @see #getPivotX()
7310 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007311 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007312 * @return The degrees of rotation.
7313 */
Chet Haasea5531132012-02-02 13:41:44 -08007314 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007315 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007316 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007317 }
7318
7319 /**
Chet Haase897247b2010-09-09 14:54:47 -07007320 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7321 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007322 *
7323 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007324 *
7325 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007326 * @see #getPivotX()
7327 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007328 * @see #setRotationX(float)
7329 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007330 *
7331 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007332 */
7333 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007334 ensureTransformationInfo();
7335 final TransformationInfo info = mTransformationInfo;
7336 if (info.mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007337 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007338 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007339 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007340 info.mRotation = rotation;
7341 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007342 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007343 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007344 }
7345 }
7346
7347 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007348 * The degrees that the view is rotated around the vertical axis through the pivot point.
7349 *
7350 * @see #getPivotX()
7351 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007352 * @see #setRotationY(float)
7353 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007354 * @return The degrees of Y rotation.
7355 */
Chet Haasea5531132012-02-02 13:41:44 -08007356 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007357 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007358 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007359 }
7360
7361 /**
Chet Haase897247b2010-09-09 14:54:47 -07007362 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7363 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7364 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007365 *
7366 * When rotating large views, it is recommended to adjust the camera distance
7367 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007368 *
7369 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007370 *
7371 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007372 * @see #getPivotX()
7373 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007374 * @see #setRotation(float)
7375 * @see #setRotationX(float)
7376 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007377 *
7378 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007379 */
7380 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007381 ensureTransformationInfo();
7382 final TransformationInfo info = mTransformationInfo;
7383 if (info.mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007384 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007385 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007386 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007387 info.mRotationY = rotationY;
7388 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007389 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007390 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007391 }
7392 }
7393
7394 /**
7395 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7396 *
7397 * @see #getPivotX()
7398 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007399 * @see #setRotationX(float)
7400 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007401 * @return The degrees of X rotation.
7402 */
Chet Haasea5531132012-02-02 13:41:44 -08007403 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007404 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007405 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007406 }
7407
7408 /**
Chet Haase897247b2010-09-09 14:54:47 -07007409 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7410 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7411 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007412 *
7413 * When rotating large views, it is recommended to adjust the camera distance
7414 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007415 *
7416 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007417 *
7418 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007419 * @see #getPivotX()
7420 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007421 * @see #setRotation(float)
7422 * @see #setRotationY(float)
7423 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007424 *
7425 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007426 */
7427 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007428 ensureTransformationInfo();
7429 final TransformationInfo info = mTransformationInfo;
7430 if (info.mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007431 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007432 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007433 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007434 info.mRotationX = rotationX;
7435 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007436 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007437 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007438 }
7439 }
7440
7441 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007442 * The amount that the view is scaled in x around the pivot point, as a proportion of
7443 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7444 *
Joe Onorato93162322010-09-16 15:42:01 -04007445 * <p>By default, this is 1.0f.
7446 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007447 * @see #getPivotX()
7448 * @see #getPivotY()
7449 * @return The scaling factor.
7450 */
Chet Haasea5531132012-02-02 13:41:44 -08007451 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007452 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007453 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007454 }
7455
7456 /**
7457 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7458 * the view's unscaled width. A value of 1 means that no scaling is applied.
7459 *
7460 * @param scaleX The scaling factor.
7461 * @see #getPivotX()
7462 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007463 *
7464 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007465 */
7466 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007467 ensureTransformationInfo();
7468 final TransformationInfo info = mTransformationInfo;
7469 if (info.mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007470 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007471 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007472 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007473 info.mScaleX = scaleX;
7474 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007475 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007476 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007477 }
7478 }
7479
7480 /**
7481 * The amount that the view is scaled in y around the pivot point, as a proportion of
7482 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7483 *
Joe Onorato93162322010-09-16 15:42:01 -04007484 * <p>By default, this is 1.0f.
7485 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007486 * @see #getPivotX()
7487 * @see #getPivotY()
7488 * @return The scaling factor.
7489 */
Chet Haasea5531132012-02-02 13:41:44 -08007490 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007491 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007492 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007493 }
7494
7495 /**
7496 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7497 * the view's unscaled width. A value of 1 means that no scaling is applied.
7498 *
7499 * @param scaleY The scaling factor.
7500 * @see #getPivotX()
7501 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007502 *
7503 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007504 */
7505 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007506 ensureTransformationInfo();
7507 final TransformationInfo info = mTransformationInfo;
7508 if (info.mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007509 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007510 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007511 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007512 info.mScaleY = scaleY;
7513 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007514 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007515 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007516 }
7517 }
7518
7519 /**
7520 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7521 * and {@link #setScaleX(float) scaled}.
7522 *
7523 * @see #getRotation()
7524 * @see #getScaleX()
7525 * @see #getScaleY()
7526 * @see #getPivotY()
7527 * @return The x location of the pivot point.
7528 */
Chet Haasea5531132012-02-02 13:41:44 -08007529 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007530 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007531 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007532 }
7533
7534 /**
7535 * Sets the x location of the point around which the view is
7536 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007537 * By default, the pivot point is centered on the object.
7538 * Setting this property disables this behavior and causes the view to use only the
7539 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007540 *
7541 * @param pivotX The x location of the pivot point.
7542 * @see #getRotation()
7543 * @see #getScaleX()
7544 * @see #getScaleY()
7545 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007546 *
7547 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007548 */
7549 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007550 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007551 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007552 final TransformationInfo info = mTransformationInfo;
7553 if (info.mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007554 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007555 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007556 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007557 info.mPivotX = pivotX;
7558 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007559 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007560 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007561 }
7562 }
7563
7564 /**
7565 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7566 * and {@link #setScaleY(float) scaled}.
7567 *
7568 * @see #getRotation()
7569 * @see #getScaleX()
7570 * @see #getScaleY()
7571 * @see #getPivotY()
7572 * @return The y location of the pivot point.
7573 */
Chet Haasea5531132012-02-02 13:41:44 -08007574 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007575 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007576 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007577 }
7578
7579 /**
7580 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007581 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7582 * Setting this property disables this behavior and causes the view to use only the
7583 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007584 *
7585 * @param pivotY The y location of the pivot point.
7586 * @see #getRotation()
7587 * @see #getScaleX()
7588 * @see #getScaleY()
7589 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007590 *
7591 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007592 */
7593 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007594 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007595 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007596 final TransformationInfo info = mTransformationInfo;
7597 if (info.mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007598 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007599 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007600 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007601 info.mPivotY = pivotY;
7602 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007603 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007604 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007605 }
7606 }
7607
7608 /**
7609 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7610 * completely transparent and 1 means the view is completely opaque.
7611 *
Joe Onorato93162322010-09-16 15:42:01 -04007612 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007613 * @return The opacity of the view.
7614 */
Chet Haasea5531132012-02-02 13:41:44 -08007615 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007616 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007617 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007618 }
7619
7620 /**
Romain Guy171c5922011-01-06 10:04:23 -08007621 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7622 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007623 *
Romain Guy171c5922011-01-06 10:04:23 -08007624 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7625 * responsible for applying the opacity itself. Otherwise, calling this method is
7626 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007627 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007628 *
Chet Haasea5531132012-02-02 13:41:44 -08007629 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
7630 * performance implications. It is generally best to use the alpha property sparingly and
7631 * transiently, as in the case of fading animations.</p>
7632 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007633 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007634 *
Joe Malin32736f02011-01-19 16:14:20 -08007635 * @see #setLayerType(int, android.graphics.Paint)
7636 *
Chet Haase73066682010-11-29 15:55:32 -08007637 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007638 */
7639 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007640 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007641 if (mTransformationInfo.mAlpha != alpha) {
7642 mTransformationInfo.mAlpha = alpha;
7643 invalidateParentCaches();
7644 if (onSetAlpha((int) (alpha * 255))) {
7645 mPrivateFlags |= ALPHA_SET;
7646 // subclass is handling alpha - don't optimize rendering cache invalidation
7647 invalidate(true);
7648 } else {
7649 mPrivateFlags &= ~ALPHA_SET;
7650 invalidate(false);
7651 }
Chet Haaseed032702010-10-01 14:05:54 -07007652 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007653 }
7654
7655 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007656 * Faster version of setAlpha() which performs the same steps except there are
7657 * no calls to invalidate(). The caller of this function should perform proper invalidation
7658 * on the parent and this object. The return value indicates whether the subclass handles
7659 * alpha (the return value for onSetAlpha()).
7660 *
7661 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007662 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
7663 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08007664 */
7665 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007666 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007667 if (mTransformationInfo.mAlpha != alpha) {
7668 mTransformationInfo.mAlpha = alpha;
7669 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7670 if (subclassHandlesAlpha) {
7671 mPrivateFlags |= ALPHA_SET;
7672 return true;
7673 } else {
7674 mPrivateFlags &= ~ALPHA_SET;
7675 }
Chet Haasea00f3862011-02-22 06:34:40 -08007676 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007677 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08007678 }
7679
7680 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007681 * Top position of this view relative to its parent.
7682 *
7683 * @return The top of this view, in pixels.
7684 */
7685 @ViewDebug.CapturedViewProperty
7686 public final int getTop() {
7687 return mTop;
7688 }
7689
7690 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007691 * Sets the top position of this view relative to its parent. This method is meant to be called
7692 * by the layout system and should not generally be called otherwise, because the property
7693 * may be changed at any time by the layout.
7694 *
7695 * @param top The top of this view, in pixels.
7696 */
7697 public final void setTop(int top) {
7698 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007699 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007700 final boolean matrixIsIdentity = mTransformationInfo == null
7701 || mTransformationInfo.mMatrixIsIdentity;
7702 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007703 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007704 int minTop;
7705 int yLoc;
7706 if (top < mTop) {
7707 minTop = top;
7708 yLoc = top - mTop;
7709 } else {
7710 minTop = mTop;
7711 yLoc = 0;
7712 }
Chet Haasee9140a72011-02-16 16:23:29 -08007713 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007714 }
7715 } else {
7716 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007717 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007718 }
7719
Chet Haaseed032702010-10-01 14:05:54 -07007720 int width = mRight - mLeft;
7721 int oldHeight = mBottom - mTop;
7722
Chet Haase21cd1382010-09-01 17:42:29 -07007723 mTop = top;
7724
Chet Haaseed032702010-10-01 14:05:54 -07007725 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7726
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007727 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007728 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7729 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007730 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007731 }
Chet Haase21cd1382010-09-01 17:42:29 -07007732 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007733 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007734 }
Chet Haase55dbb652010-12-21 20:15:08 -08007735 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007736 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007737 }
7738 }
7739
7740 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007741 * Bottom position of this view relative to its parent.
7742 *
7743 * @return The bottom of this view, in pixels.
7744 */
7745 @ViewDebug.CapturedViewProperty
7746 public final int getBottom() {
7747 return mBottom;
7748 }
7749
7750 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007751 * True if this view has changed since the last time being drawn.
7752 *
7753 * @return The dirty state of this view.
7754 */
7755 public boolean isDirty() {
7756 return (mPrivateFlags & DIRTY_MASK) != 0;
7757 }
7758
7759 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007760 * Sets the bottom position of this view relative to its parent. This method is meant to be
7761 * called by the layout system and should not generally be called otherwise, because the
7762 * property may be changed at any time by the layout.
7763 *
7764 * @param bottom The bottom of this view, in pixels.
7765 */
7766 public final void setBottom(int bottom) {
7767 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007768 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007769 final boolean matrixIsIdentity = mTransformationInfo == null
7770 || mTransformationInfo.mMatrixIsIdentity;
7771 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007772 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007773 int maxBottom;
7774 if (bottom < mBottom) {
7775 maxBottom = mBottom;
7776 } else {
7777 maxBottom = bottom;
7778 }
Chet Haasee9140a72011-02-16 16:23:29 -08007779 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007780 }
7781 } else {
7782 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007783 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007784 }
7785
Chet Haaseed032702010-10-01 14:05:54 -07007786 int width = mRight - mLeft;
7787 int oldHeight = mBottom - mTop;
7788
Chet Haase21cd1382010-09-01 17:42:29 -07007789 mBottom = bottom;
7790
Chet Haaseed032702010-10-01 14:05:54 -07007791 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7792
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007793 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007794 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7795 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007796 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007797 }
Chet Haase21cd1382010-09-01 17:42:29 -07007798 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007799 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007800 }
Chet Haase55dbb652010-12-21 20:15:08 -08007801 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007802 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007803 }
7804 }
7805
7806 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007807 * Left position of this view relative to its parent.
7808 *
7809 * @return The left edge of this view, in pixels.
7810 */
7811 @ViewDebug.CapturedViewProperty
7812 public final int getLeft() {
7813 return mLeft;
7814 }
7815
7816 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007817 * Sets the left position of this view relative to its parent. This method is meant to be called
7818 * by the layout system and should not generally be called otherwise, because the property
7819 * may be changed at any time by the layout.
7820 *
7821 * @param left The bottom of this view, in pixels.
7822 */
7823 public final void setLeft(int left) {
7824 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007825 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007826 final boolean matrixIsIdentity = mTransformationInfo == null
7827 || mTransformationInfo.mMatrixIsIdentity;
7828 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007829 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007830 int minLeft;
7831 int xLoc;
7832 if (left < mLeft) {
7833 minLeft = left;
7834 xLoc = left - mLeft;
7835 } else {
7836 minLeft = mLeft;
7837 xLoc = 0;
7838 }
Chet Haasee9140a72011-02-16 16:23:29 -08007839 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007840 }
7841 } else {
7842 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007843 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007844 }
7845
Chet Haaseed032702010-10-01 14:05:54 -07007846 int oldWidth = mRight - mLeft;
7847 int height = mBottom - mTop;
7848
Chet Haase21cd1382010-09-01 17:42:29 -07007849 mLeft = left;
7850
Chet Haaseed032702010-10-01 14:05:54 -07007851 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7852
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007853 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007854 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7855 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007856 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007857 }
Chet Haase21cd1382010-09-01 17:42:29 -07007858 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007859 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007860 }
Chet Haase55dbb652010-12-21 20:15:08 -08007861 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007862 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007863 }
7864 }
7865
7866 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007867 * Right position of this view relative to its parent.
7868 *
7869 * @return The right edge of this view, in pixels.
7870 */
7871 @ViewDebug.CapturedViewProperty
7872 public final int getRight() {
7873 return mRight;
7874 }
7875
7876 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007877 * Sets the right position of this view relative to its parent. This method is meant to be called
7878 * by the layout system and should not generally be called otherwise, because the property
7879 * may be changed at any time by the layout.
7880 *
7881 * @param right The bottom of this view, in pixels.
7882 */
7883 public final void setRight(int right) {
7884 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007885 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007886 final boolean matrixIsIdentity = mTransformationInfo == null
7887 || mTransformationInfo.mMatrixIsIdentity;
7888 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007889 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007890 int maxRight;
7891 if (right < mRight) {
7892 maxRight = mRight;
7893 } else {
7894 maxRight = right;
7895 }
Chet Haasee9140a72011-02-16 16:23:29 -08007896 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007897 }
7898 } else {
7899 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007900 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007901 }
7902
Chet Haaseed032702010-10-01 14:05:54 -07007903 int oldWidth = mRight - mLeft;
7904 int height = mBottom - mTop;
7905
Chet Haase21cd1382010-09-01 17:42:29 -07007906 mRight = right;
7907
Chet Haaseed032702010-10-01 14:05:54 -07007908 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7909
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007910 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007911 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7912 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007913 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007914 }
Chet Haase21cd1382010-09-01 17:42:29 -07007915 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007916 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007917 }
Chet Haase55dbb652010-12-21 20:15:08 -08007918 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007919 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007920 }
7921 }
7922
7923 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007924 * The visual x position of this view, in pixels. This is equivalent to the
7925 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007926 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007927 *
Chet Haasedf030d22010-07-30 17:22:38 -07007928 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007929 */
Chet Haasea5531132012-02-02 13:41:44 -08007930 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007931 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007932 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007933 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007934
Chet Haasedf030d22010-07-30 17:22:38 -07007935 /**
7936 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7937 * {@link #setTranslationX(float) translationX} property to be the difference between
7938 * the x value passed in and the current {@link #getLeft() left} property.
7939 *
7940 * @param x The visual x position of this view, in pixels.
7941 */
7942 public void setX(float x) {
7943 setTranslationX(x - mLeft);
7944 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007945
Chet Haasedf030d22010-07-30 17:22:38 -07007946 /**
7947 * The visual y position of this view, in pixels. This is equivalent to the
7948 * {@link #setTranslationY(float) translationY} property plus the current
7949 * {@link #getTop() top} property.
7950 *
7951 * @return The visual y position of this view, in pixels.
7952 */
Chet Haasea5531132012-02-02 13:41:44 -08007953 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007954 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007955 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007956 }
7957
7958 /**
7959 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7960 * {@link #setTranslationY(float) translationY} property to be the difference between
7961 * the y value passed in and the current {@link #getTop() top} property.
7962 *
7963 * @param y The visual y position of this view, in pixels.
7964 */
7965 public void setY(float y) {
7966 setTranslationY(y - mTop);
7967 }
7968
7969
7970 /**
7971 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7972 * This position is post-layout, in addition to wherever the object's
7973 * layout placed it.
7974 *
7975 * @return The horizontal position of this view relative to its left position, in pixels.
7976 */
Chet Haasea5531132012-02-02 13:41:44 -08007977 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007978 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007979 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07007980 }
7981
7982 /**
7983 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7984 * This effectively positions the object post-layout, in addition to wherever the object's
7985 * layout placed it.
7986 *
7987 * @param translationX The horizontal position of this view relative to its left position,
7988 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007989 *
7990 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007991 */
7992 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007993 ensureTransformationInfo();
7994 final TransformationInfo info = mTransformationInfo;
7995 if (info.mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007996 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007997 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007998 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007999 info.mTranslationX = translationX;
8000 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07008001 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008002 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008003 }
8004 }
8005
8006 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008007 * The horizontal location of this view relative to its {@link #getTop() top} position.
8008 * This position is post-layout, in addition to wherever the object's
8009 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008010 *
Chet Haasedf030d22010-07-30 17:22:38 -07008011 * @return The vertical position of this view relative to its top position,
8012 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008013 */
Chet Haasea5531132012-02-02 13:41:44 -08008014 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008015 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008016 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008017 }
8018
8019 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008020 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
8021 * This effectively positions the object post-layout, in addition to wherever the object's
8022 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008023 *
Chet Haasedf030d22010-07-30 17:22:38 -07008024 * @param translationY The vertical position of this view relative to its top position,
8025 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008026 *
8027 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07008028 */
Chet Haasedf030d22010-07-30 17:22:38 -07008029 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008030 ensureTransformationInfo();
8031 final TransformationInfo info = mTransformationInfo;
8032 if (info.mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008033 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07008034 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07008035 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008036 info.mTranslationY = translationY;
8037 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07008038 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008039 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07008040 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008041 }
8042
8043 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008044 * Hit rectangle in parent's coordinates
8045 *
8046 * @param outRect The hit rectangle of the view.
8047 */
8048 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07008049 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008050 final TransformationInfo info = mTransformationInfo;
8051 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008052 outRect.set(mLeft, mTop, mRight, mBottom);
8053 } else {
8054 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008055 tmpRect.set(-info.mPivotX, -info.mPivotY,
8056 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
8057 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07008058 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
8059 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07008060 }
8061 }
8062
8063 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07008064 * Determines whether the given point, in local coordinates is inside the view.
8065 */
8066 /*package*/ final boolean pointInView(float localX, float localY) {
8067 return localX >= 0 && localX < (mRight - mLeft)
8068 && localY >= 0 && localY < (mBottom - mTop);
8069 }
8070
8071 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008072 * Utility method to determine whether the given point, in local coordinates,
8073 * is inside the view, where the area of the view is expanded by the slop factor.
8074 * This method is called while processing touch-move events to determine if the event
8075 * is still within the view.
8076 */
8077 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07008078 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07008079 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008080 }
8081
8082 /**
8083 * When a view has focus and the user navigates away from it, the next view is searched for
8084 * starting from the rectangle filled in by this method.
8085 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008086 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
8087 * of the view. However, if your view maintains some idea of internal selection,
8088 * such as a cursor, or a selected row or column, you should override this method and
8089 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008090 *
8091 * @param r The rectangle to fill in, in this view's coordinates.
8092 */
8093 public void getFocusedRect(Rect r) {
8094 getDrawingRect(r);
8095 }
8096
8097 /**
8098 * If some part of this view is not clipped by any of its parents, then
8099 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01008100 * coordinates (without taking possible View rotations into account), offset
8101 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
8102 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008103 *
8104 * @param r If true is returned, r holds the global coordinates of the
8105 * visible portion of this view.
8106 * @param globalOffset If true is returned, globalOffset holds the dx,dy
8107 * between this view and its root. globalOffet may be null.
8108 * @return true if r is non-empty (i.e. part of the view is visible at the
8109 * root level.
8110 */
8111 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
8112 int width = mRight - mLeft;
8113 int height = mBottom - mTop;
8114 if (width > 0 && height > 0) {
8115 r.set(0, 0, width, height);
8116 if (globalOffset != null) {
8117 globalOffset.set(-mScrollX, -mScrollY);
8118 }
8119 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
8120 }
8121 return false;
8122 }
8123
8124 public final boolean getGlobalVisibleRect(Rect r) {
8125 return getGlobalVisibleRect(r, null);
8126 }
8127
8128 public final boolean getLocalVisibleRect(Rect r) {
8129 Point offset = new Point();
8130 if (getGlobalVisibleRect(r, offset)) {
8131 r.offset(-offset.x, -offset.y); // make r local
8132 return true;
8133 }
8134 return false;
8135 }
8136
8137 /**
8138 * Offset this view's vertical location by the specified number of pixels.
8139 *
8140 * @param offset the number of pixels to offset the view by
8141 */
8142 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008143 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008144 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008145 final boolean matrixIsIdentity = mTransformationInfo == null
8146 || mTransformationInfo.mMatrixIsIdentity;
8147 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008148 final ViewParent p = mParent;
8149 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008150 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008151 int minTop;
8152 int maxBottom;
8153 int yLoc;
8154 if (offset < 0) {
8155 minTop = mTop + offset;
8156 maxBottom = mBottom;
8157 yLoc = offset;
8158 } else {
8159 minTop = mTop;
8160 maxBottom = mBottom + offset;
8161 yLoc = 0;
8162 }
8163 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8164 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008165 }
8166 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008167 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008168 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008169
Chet Haasec3aa3612010-06-17 08:50:37 -07008170 mTop += offset;
8171 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008172
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008173 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008174 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008175 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008176 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008177 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008179 }
8180
8181 /**
8182 * Offset this view's horizontal location by the specified amount of pixels.
8183 *
8184 * @param offset the numer of pixels to offset the view by
8185 */
8186 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008187 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008188 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008189 final boolean matrixIsIdentity = mTransformationInfo == null
8190 || mTransformationInfo.mMatrixIsIdentity;
8191 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008192 final ViewParent p = mParent;
8193 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008194 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008195 int minLeft;
8196 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008197 if (offset < 0) {
8198 minLeft = mLeft + offset;
8199 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008200 } else {
8201 minLeft = mLeft;
8202 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008203 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008204 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008205 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008206 }
8207 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008208 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008209 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008210
Chet Haasec3aa3612010-06-17 08:50:37 -07008211 mLeft += offset;
8212 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008213
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008214 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008215 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008216 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008217 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008218 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008220 }
8221
8222 /**
8223 * Get the LayoutParams associated with this view. All views should have
8224 * layout parameters. These supply parameters to the <i>parent</i> of this
8225 * view specifying how it should be arranged. There are many subclasses of
8226 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8227 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008228 *
8229 * This method may return null if this View is not attached to a parent
8230 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8231 * was not invoked successfully. When a View is attached to a parent
8232 * ViewGroup, this method must not return null.
8233 *
8234 * @return The LayoutParams associated with this view, or null if no
8235 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008236 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008237 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008238 public ViewGroup.LayoutParams getLayoutParams() {
8239 return mLayoutParams;
8240 }
8241
8242 /**
8243 * Set the layout parameters associated with this view. These supply
8244 * parameters to the <i>parent</i> of this view specifying how it should be
8245 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8246 * correspond to the different subclasses of ViewGroup that are responsible
8247 * for arranging their children.
8248 *
Romain Guy01c174b2011-02-22 11:51:06 -08008249 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008250 */
8251 public void setLayoutParams(ViewGroup.LayoutParams params) {
8252 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008253 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008254 }
8255 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08008256 if (mParent instanceof ViewGroup) {
8257 ((ViewGroup) mParent).onSetLayoutParams(this, params);
8258 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008259 requestLayout();
8260 }
8261
8262 /**
8263 * Set the scrolled position of your view. This will cause a call to
8264 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8265 * invalidated.
8266 * @param x the x position to scroll to
8267 * @param y the y position to scroll to
8268 */
8269 public void scrollTo(int x, int y) {
8270 if (mScrollX != x || mScrollY != y) {
8271 int oldX = mScrollX;
8272 int oldY = mScrollY;
8273 mScrollX = x;
8274 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008275 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008276 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008277 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008278 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008279 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008280 }
8281 }
8282
8283 /**
8284 * Move the scrolled position of your view. This will cause a call to
8285 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8286 * invalidated.
8287 * @param x the amount of pixels to scroll by horizontally
8288 * @param y the amount of pixels to scroll by vertically
8289 */
8290 public void scrollBy(int x, int y) {
8291 scrollTo(mScrollX + x, mScrollY + y);
8292 }
8293
8294 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008295 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8296 * animation to fade the scrollbars out after a default delay. If a subclass
8297 * provides animated scrolling, the start delay should equal the duration
8298 * of the scrolling animation.</p>
8299 *
8300 * <p>The animation starts only if at least one of the scrollbars is
8301 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8302 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8303 * this method returns true, and false otherwise. If the animation is
8304 * started, this method calls {@link #invalidate()}; in that case the
8305 * caller should not call {@link #invalidate()}.</p>
8306 *
8307 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008308 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008309 *
8310 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8311 * and {@link #scrollTo(int, int)}.</p>
8312 *
8313 * @return true if the animation is played, false otherwise
8314 *
8315 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008316 * @see #scrollBy(int, int)
8317 * @see #scrollTo(int, int)
8318 * @see #isHorizontalScrollBarEnabled()
8319 * @see #isVerticalScrollBarEnabled()
8320 * @see #setHorizontalScrollBarEnabled(boolean)
8321 * @see #setVerticalScrollBarEnabled(boolean)
8322 */
8323 protected boolean awakenScrollBars() {
8324 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008325 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008326 }
8327
8328 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008329 * Trigger the scrollbars to draw.
8330 * This method differs from awakenScrollBars() only in its default duration.
8331 * initialAwakenScrollBars() will show the scroll bars for longer than
8332 * usual to give the user more of a chance to notice them.
8333 *
8334 * @return true if the animation is played, false otherwise.
8335 */
8336 private boolean initialAwakenScrollBars() {
8337 return mScrollCache != null &&
8338 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8339 }
8340
8341 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008342 * <p>
8343 * Trigger the scrollbars to draw. When invoked this method starts an
8344 * animation to fade the scrollbars out after a fixed delay. If a subclass
8345 * provides animated scrolling, the start delay should equal the duration of
8346 * the scrolling animation.
8347 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008348 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008349 * <p>
8350 * The animation starts only if at least one of the scrollbars is enabled,
8351 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8352 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8353 * this method returns true, and false otherwise. If the animation is
8354 * started, this method calls {@link #invalidate()}; in that case the caller
8355 * should not call {@link #invalidate()}.
8356 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008357 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008358 * <p>
8359 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008360 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008361 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008362 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008363 * @param startDelay the delay, in milliseconds, after which the animation
8364 * should start; when the delay is 0, the animation starts
8365 * immediately
8366 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008367 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008368 * @see #scrollBy(int, int)
8369 * @see #scrollTo(int, int)
8370 * @see #isHorizontalScrollBarEnabled()
8371 * @see #isVerticalScrollBarEnabled()
8372 * @see #setHorizontalScrollBarEnabled(boolean)
8373 * @see #setVerticalScrollBarEnabled(boolean)
8374 */
8375 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008376 return awakenScrollBars(startDelay, true);
8377 }
Joe Malin32736f02011-01-19 16:14:20 -08008378
Mike Cleron290947b2009-09-29 18:34:32 -07008379 /**
8380 * <p>
8381 * Trigger the scrollbars to draw. When invoked this method starts an
8382 * animation to fade the scrollbars out after a fixed delay. If a subclass
8383 * provides animated scrolling, the start delay should equal the duration of
8384 * the scrolling animation.
8385 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008386 *
Mike Cleron290947b2009-09-29 18:34:32 -07008387 * <p>
8388 * The animation starts only if at least one of the scrollbars is enabled,
8389 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8390 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8391 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008392 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008393 * is set to true; in that case the caller
8394 * should not call {@link #invalidate()}.
8395 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008396 *
Mike Cleron290947b2009-09-29 18:34:32 -07008397 * <p>
8398 * This method should be invoked everytime a subclass directly updates the
8399 * scroll parameters.
8400 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008401 *
Mike Cleron290947b2009-09-29 18:34:32 -07008402 * @param startDelay the delay, in milliseconds, after which the animation
8403 * should start; when the delay is 0, the animation starts
8404 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008405 *
Mike Cleron290947b2009-09-29 18:34:32 -07008406 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008407 *
Mike Cleron290947b2009-09-29 18:34:32 -07008408 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008409 *
Mike Cleron290947b2009-09-29 18:34:32 -07008410 * @see #scrollBy(int, int)
8411 * @see #scrollTo(int, int)
8412 * @see #isHorizontalScrollBarEnabled()
8413 * @see #isVerticalScrollBarEnabled()
8414 * @see #setHorizontalScrollBarEnabled(boolean)
8415 * @see #setVerticalScrollBarEnabled(boolean)
8416 */
8417 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008418 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008419
Mike Cleronf116bf82009-09-27 19:14:12 -07008420 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8421 return false;
8422 }
8423
8424 if (scrollCache.scrollBar == null) {
8425 scrollCache.scrollBar = new ScrollBarDrawable();
8426 }
8427
8428 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8429
Mike Cleron290947b2009-09-29 18:34:32 -07008430 if (invalidate) {
8431 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008432 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008433 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008434
8435 if (scrollCache.state == ScrollabilityCache.OFF) {
8436 // FIXME: this is copied from WindowManagerService.
8437 // We should get this value from the system when it
8438 // is possible to do so.
8439 final int KEY_REPEAT_FIRST_DELAY = 750;
8440 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8441 }
8442
8443 // Tell mScrollCache when we should start fading. This may
8444 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008445 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008446 scrollCache.fadeStartTime = fadeStartTime;
8447 scrollCache.state = ScrollabilityCache.ON;
8448
8449 // Schedule our fader to run, unscheduling any old ones first
8450 if (mAttachInfo != null) {
8451 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8452 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8453 }
8454
8455 return true;
8456 }
8457
8458 return false;
8459 }
8460
8461 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008462 * Do not invalidate views which are not visible and which are not running an animation. They
8463 * will not get drawn and they should not set dirty flags as if they will be drawn
8464 */
8465 private boolean skipInvalidate() {
8466 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8467 (!(mParent instanceof ViewGroup) ||
8468 !((ViewGroup) mParent).isViewTransitioning(this));
8469 }
8470 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008471 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008472 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8473 * in the future. This must be called from a UI thread. To call from a non-UI
8474 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008475 *
8476 * WARNING: This method is destructive to dirty.
8477 * @param dirty the rectangle representing the bounds of the dirty region
8478 */
8479 public void invalidate(Rect dirty) {
8480 if (ViewDebug.TRACE_HIERARCHY) {
8481 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8482 }
8483
Chet Haaseaceafe62011-08-26 15:44:33 -07008484 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008485 return;
8486 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008487 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008488 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8489 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008490 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008491 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008492 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008493 final ViewParent p = mParent;
8494 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008495 //noinspection PointlessBooleanExpression,ConstantConditions
8496 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8497 if (p != null && ai != null && ai.mHardwareAccelerated) {
8498 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008499 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008500 p.invalidateChild(this, null);
8501 return;
8502 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008504 if (p != null && ai != null) {
8505 final int scrollX = mScrollX;
8506 final int scrollY = mScrollY;
8507 final Rect r = ai.mTmpInvalRect;
8508 r.set(dirty.left - scrollX, dirty.top - scrollY,
8509 dirty.right - scrollX, dirty.bottom - scrollY);
8510 mParent.invalidateChild(this, r);
8511 }
8512 }
8513 }
8514
8515 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008516 * 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 -08008517 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008518 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8519 * will be called at some point in the future. This must be called from
8520 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008521 * @param l the left position of the dirty region
8522 * @param t the top position of the dirty region
8523 * @param r the right position of the dirty region
8524 * @param b the bottom position of the dirty region
8525 */
8526 public void invalidate(int l, int t, int r, int b) {
8527 if (ViewDebug.TRACE_HIERARCHY) {
8528 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8529 }
8530
Chet Haaseaceafe62011-08-26 15:44:33 -07008531 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008532 return;
8533 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008534 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008535 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8536 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008537 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008538 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008539 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008540 final ViewParent p = mParent;
8541 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008542 //noinspection PointlessBooleanExpression,ConstantConditions
8543 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8544 if (p != null && ai != null && ai.mHardwareAccelerated) {
8545 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008546 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008547 p.invalidateChild(this, null);
8548 return;
8549 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008551 if (p != null && ai != null && l < r && t < b) {
8552 final int scrollX = mScrollX;
8553 final int scrollY = mScrollY;
8554 final Rect tmpr = ai.mTmpInvalRect;
8555 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8556 p.invalidateChild(this, tmpr);
8557 }
8558 }
8559 }
8560
8561 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008562 * Invalidate the whole view. If the view is visible,
8563 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8564 * the future. This must be called from a UI thread. To call from a non-UI thread,
8565 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008566 */
8567 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008568 invalidate(true);
8569 }
Joe Malin32736f02011-01-19 16:14:20 -08008570
Chet Haaseed032702010-10-01 14:05:54 -07008571 /**
8572 * This is where the invalidate() work actually happens. A full invalidate()
8573 * causes the drawing cache to be invalidated, but this function can be called with
8574 * invalidateCache set to false to skip that invalidation step for cases that do not
8575 * need it (for example, a component that remains at the same dimensions with the same
8576 * content).
8577 *
8578 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8579 * well. This is usually true for a full invalidate, but may be set to false if the
8580 * View's contents or dimensions have not changed.
8581 */
Romain Guy849d0a32011-02-01 17:20:48 -08008582 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008583 if (ViewDebug.TRACE_HIERARCHY) {
8584 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8585 }
8586
Chet Haaseaceafe62011-08-26 15:44:33 -07008587 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008588 return;
8589 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008590 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008591 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008592 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8593 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008594 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008595 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008596 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008597 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008598 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8599 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008600 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008601 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008602 //noinspection PointlessBooleanExpression,ConstantConditions
8603 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8604 if (p != null && ai != null && ai.mHardwareAccelerated) {
8605 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008606 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008607 p.invalidateChild(this, null);
8608 return;
8609 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008610 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008612 if (p != null && ai != null) {
8613 final Rect r = ai.mTmpInvalRect;
8614 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8615 // Don't call invalidate -- we don't want to internally scroll
8616 // our own bounds
8617 p.invalidateChild(this, r);
8618 }
8619 }
8620 }
8621
8622 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008623 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008624 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8625 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008626 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8627 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008628 *
8629 * @hide
8630 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008631 protected void invalidateParentCaches() {
8632 if (mParent instanceof View) {
8633 ((View) mParent).mPrivateFlags |= INVALIDATED;
8634 }
8635 }
Joe Malin32736f02011-01-19 16:14:20 -08008636
Romain Guy0fd89bf2011-01-26 15:41:30 -08008637 /**
8638 * Used to indicate that the parent of this view should be invalidated. This functionality
8639 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8640 * which is necessary when various parent-managed properties of the view change, such as
8641 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8642 * an invalidation event to the parent.
8643 *
8644 * @hide
8645 */
8646 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008647 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008648 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008649 }
8650 }
8651
8652 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008653 * Indicates whether this View is opaque. An opaque View guarantees that it will
8654 * draw all the pixels overlapping its bounds using a fully opaque color.
8655 *
8656 * Subclasses of View should override this method whenever possible to indicate
8657 * whether an instance is opaque. Opaque Views are treated in a special way by
8658 * the View hierarchy, possibly allowing it to perform optimizations during
8659 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008660 *
Romain Guy24443ea2009-05-11 11:56:30 -07008661 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008662 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008663 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008664 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008665 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008666 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8667 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008668 }
8669
Adam Powell20232d02010-12-08 21:08:53 -08008670 /**
8671 * @hide
8672 */
8673 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008674 // Opaque if:
8675 // - Has a background
8676 // - Background is opaque
8677 // - Doesn't have scrollbars or scrollbars are inside overlay
8678
8679 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8680 mPrivateFlags |= OPAQUE_BACKGROUND;
8681 } else {
8682 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8683 }
8684
8685 final int flags = mViewFlags;
8686 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8687 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8688 mPrivateFlags |= OPAQUE_SCROLLBARS;
8689 } else {
8690 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8691 }
8692 }
8693
8694 /**
8695 * @hide
8696 */
8697 protected boolean hasOpaqueScrollbars() {
8698 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008699 }
8700
8701 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008702 * @return A handler associated with the thread running the View. This
8703 * handler can be used to pump events in the UI events queue.
8704 */
8705 public Handler getHandler() {
8706 if (mAttachInfo != null) {
8707 return mAttachInfo.mHandler;
8708 }
8709 return null;
8710 }
8711
8712 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08008713 * Gets the view root associated with the View.
8714 * @return The view root, or null if none.
8715 * @hide
8716 */
8717 public ViewRootImpl getViewRootImpl() {
8718 if (mAttachInfo != null) {
8719 return mAttachInfo.mViewRootImpl;
8720 }
8721 return null;
8722 }
8723
8724 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008725 * <p>Causes the Runnable to be added to the message queue.
8726 * The runnable will be run on the user interface thread.</p>
8727 *
8728 * <p>This method can be invoked from outside of the UI thread
8729 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008730 *
8731 * @param action The Runnable that will be executed.
8732 *
8733 * @return Returns true if the Runnable was successfully placed in to the
8734 * message queue. Returns false on failure, usually because the
8735 * looper processing the message queue is exiting.
8736 */
8737 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008738 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008739 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008740 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008741 }
Jeff Browna175a5b2012-02-15 19:18:31 -08008742 // Assume that post will succeed later
8743 ViewRootImpl.getRunQueue().post(action);
8744 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008745 }
8746
8747 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008748 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008749 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008750 * The runnable will be run on the user interface thread.</p>
8751 *
8752 * <p>This method can be invoked from outside of the UI thread
8753 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008754 *
8755 * @param action The Runnable that will be executed.
8756 * @param delayMillis The delay (in milliseconds) until the Runnable
8757 * will be executed.
8758 *
8759 * @return true if the Runnable was successfully placed in to the
8760 * message queue. Returns false on failure, usually because the
8761 * looper processing the message queue is exiting. Note that a
8762 * result of true does not mean the Runnable will be processed --
8763 * if the looper is quit before the delivery time of the message
8764 * occurs then the message will be dropped.
8765 */
8766 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008767 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008768 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008769 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008770 }
Jeff Browna175a5b2012-02-15 19:18:31 -08008771 // Assume that post will succeed later
8772 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
8773 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008774 }
8775
8776 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008777 * <p>Removes the specified Runnable from the message queue.</p>
8778 *
8779 * <p>This method can be invoked from outside of the UI thread
8780 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008781 *
8782 * @param action The Runnable to remove from the message handling queue
8783 *
8784 * @return true if this view could ask the Handler to remove the Runnable,
8785 * false otherwise. When the returned value is true, the Runnable
8786 * may or may not have been actually removed from the message queue
8787 * (for instance, if the Runnable was not in the queue already.)
8788 */
8789 public boolean removeCallbacks(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008790 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008791 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008792 attachInfo.mHandler.removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008793 } else {
8794 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008795 ViewRootImpl.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008797 return true;
8798 }
8799
8800 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008801 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
8802 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008803 *
Romain Guye63a4f32011-08-11 11:33:31 -07008804 * <p>This method can be invoked from outside of the UI thread
8805 * only when this View is attached to a window.</p>
8806 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008807 * @see #invalidate()
8808 */
8809 public void postInvalidate() {
8810 postInvalidateDelayed(0);
8811 }
8812
8813 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008814 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8815 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
8816 *
8817 * <p>This method can be invoked from outside of the UI thread
8818 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008819 *
8820 * @param left The left coordinate of the rectangle to invalidate.
8821 * @param top The top coordinate of the rectangle to invalidate.
8822 * @param right The right coordinate of the rectangle to invalidate.
8823 * @param bottom The bottom coordinate of the rectangle to invalidate.
8824 *
8825 * @see #invalidate(int, int, int, int)
8826 * @see #invalidate(Rect)
8827 */
8828 public void postInvalidate(int left, int top, int right, int bottom) {
8829 postInvalidateDelayed(0, left, top, right, bottom);
8830 }
8831
8832 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008833 * <p>Cause an invalidate to happen on a subsequent cycle through the event
8834 * loop. Waits for the specified amount of time.</p>
8835 *
8836 * <p>This method can be invoked from outside of the UI thread
8837 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008838 *
8839 * @param delayMilliseconds the duration in milliseconds to delay the
8840 * invalidation by
8841 */
8842 public void postInvalidateDelayed(long delayMilliseconds) {
8843 // We try only with the AttachInfo because there's no point in invalidating
8844 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08008845 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008846 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008847 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008848 }
8849 }
8850
8851 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008852 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8853 * through the event loop. Waits for the specified amount of time.</p>
8854 *
8855 * <p>This method can be invoked from outside of the UI thread
8856 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008857 *
8858 * @param delayMilliseconds the duration in milliseconds to delay the
8859 * invalidation by
8860 * @param left The left coordinate of the rectangle to invalidate.
8861 * @param top The top coordinate of the rectangle to invalidate.
8862 * @param right The right coordinate of the rectangle to invalidate.
8863 * @param bottom The bottom coordinate of the rectangle to invalidate.
8864 */
8865 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8866 int right, int bottom) {
8867
8868 // We try only with the AttachInfo because there's no point in invalidating
8869 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08008870 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008871 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008872 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8873 info.target = this;
8874 info.left = left;
8875 info.top = top;
8876 info.right = right;
8877 info.bottom = bottom;
8878
Jeff Browna175a5b2012-02-15 19:18:31 -08008879 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008880 }
8881 }
8882
8883 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -08008884 * <p>Cause an invalidate to happen on the next animation time step, typically the
8885 * next display frame.</p>
8886 *
8887 * <p>This method can be invoked from outside of the UI thread
8888 * only when this View is attached to a window.</p>
8889 *
8890 * @hide
8891 */
8892 public void postInvalidateOnAnimation() {
8893 // We try only with the AttachInfo because there's no point in invalidating
8894 // if we are not attached to our window
8895 final AttachInfo attachInfo = mAttachInfo;
8896 if (attachInfo != null) {
8897 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
8898 }
8899 }
8900
8901 /**
8902 * <p>Cause an invalidate of the specified area to happen on the next animation
8903 * time step, typically the next display frame.</p>
8904 *
8905 * <p>This method can be invoked from outside of the UI thread
8906 * only when this View is attached to a window.</p>
8907 *
8908 * @param left The left coordinate of the rectangle to invalidate.
8909 * @param top The top coordinate of the rectangle to invalidate.
8910 * @param right The right coordinate of the rectangle to invalidate.
8911 * @param bottom The bottom coordinate of the rectangle to invalidate.
8912 *
8913 * @hide
8914 */
8915 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
8916 // We try only with the AttachInfo because there's no point in invalidating
8917 // if we are not attached to our window
8918 final AttachInfo attachInfo = mAttachInfo;
8919 if (attachInfo != null) {
8920 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8921 info.target = this;
8922 info.left = left;
8923 info.top = top;
8924 info.right = right;
8925 info.bottom = bottom;
8926
8927 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
8928 }
8929 }
8930
8931 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008932 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8933 * This event is sent at most once every
8934 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8935 */
8936 private void postSendViewScrolledAccessibilityEventCallback() {
8937 if (mSendViewScrolledAccessibilityEvent == null) {
8938 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8939 }
8940 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8941 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8942 postDelayed(mSendViewScrolledAccessibilityEvent,
8943 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8944 }
8945 }
8946
8947 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008948 * Called by a parent to request that a child update its values for mScrollX
8949 * and mScrollY if necessary. This will typically be done if the child is
8950 * animating a scroll using a {@link android.widget.Scroller Scroller}
8951 * object.
8952 */
8953 public void computeScroll() {
8954 }
8955
8956 /**
8957 * <p>Indicate whether the horizontal edges are faded when the view is
8958 * scrolled horizontally.</p>
8959 *
8960 * @return true if the horizontal edges should are faded on scroll, false
8961 * otherwise
8962 *
8963 * @see #setHorizontalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008964 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008965 */
8966 public boolean isHorizontalFadingEdgeEnabled() {
8967 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8968 }
8969
8970 /**
8971 * <p>Define whether the horizontal edges should be faded when this view
8972 * is scrolled horizontally.</p>
8973 *
8974 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8975 * be faded when the view is scrolled
8976 * horizontally
8977 *
8978 * @see #isHorizontalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008979 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008980 */
8981 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8982 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8983 if (horizontalFadingEdgeEnabled) {
8984 initScrollCache();
8985 }
8986
8987 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8988 }
8989 }
8990
8991 /**
8992 * <p>Indicate whether the vertical edges are faded when the view is
8993 * scrolled horizontally.</p>
8994 *
8995 * @return true if the vertical edges should are faded on scroll, false
8996 * otherwise
8997 *
8998 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008999 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009000 */
9001 public boolean isVerticalFadingEdgeEnabled() {
9002 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
9003 }
9004
9005 /**
9006 * <p>Define whether the vertical edges should be faded when this view
9007 * is scrolled vertically.</p>
9008 *
9009 * @param verticalFadingEdgeEnabled true if the vertical edges should
9010 * be faded when the view is scrolled
9011 * vertically
9012 *
9013 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009014 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009015 */
9016 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
9017 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
9018 if (verticalFadingEdgeEnabled) {
9019 initScrollCache();
9020 }
9021
9022 mViewFlags ^= FADING_EDGE_VERTICAL;
9023 }
9024 }
9025
9026 /**
9027 * Returns the strength, or intensity, of the top faded edge. The strength is
9028 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9029 * returns 0.0 or 1.0 but no value in between.
9030 *
9031 * Subclasses should override this method to provide a smoother fade transition
9032 * when scrolling occurs.
9033 *
9034 * @return the intensity of the top fade as a float between 0.0f and 1.0f
9035 */
9036 protected float getTopFadingEdgeStrength() {
9037 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
9038 }
9039
9040 /**
9041 * Returns the strength, or intensity, of the bottom faded edge. The strength is
9042 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9043 * returns 0.0 or 1.0 but no value in between.
9044 *
9045 * Subclasses should override this method to provide a smoother fade transition
9046 * when scrolling occurs.
9047 *
9048 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
9049 */
9050 protected float getBottomFadingEdgeStrength() {
9051 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
9052 computeVerticalScrollRange() ? 1.0f : 0.0f;
9053 }
9054
9055 /**
9056 * Returns the strength, or intensity, of the left faded edge. The strength is
9057 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9058 * returns 0.0 or 1.0 but no value in between.
9059 *
9060 * Subclasses should override this method to provide a smoother fade transition
9061 * when scrolling occurs.
9062 *
9063 * @return the intensity of the left fade as a float between 0.0f and 1.0f
9064 */
9065 protected float getLeftFadingEdgeStrength() {
9066 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
9067 }
9068
9069 /**
9070 * Returns the strength, or intensity, of the right faded edge. The strength is
9071 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9072 * returns 0.0 or 1.0 but no value in between.
9073 *
9074 * Subclasses should override this method to provide a smoother fade transition
9075 * when scrolling occurs.
9076 *
9077 * @return the intensity of the right fade as a float between 0.0f and 1.0f
9078 */
9079 protected float getRightFadingEdgeStrength() {
9080 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
9081 computeHorizontalScrollRange() ? 1.0f : 0.0f;
9082 }
9083
9084 /**
9085 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
9086 * scrollbar is not drawn by default.</p>
9087 *
9088 * @return true if the horizontal scrollbar should be painted, false
9089 * otherwise
9090 *
9091 * @see #setHorizontalScrollBarEnabled(boolean)
9092 */
9093 public boolean isHorizontalScrollBarEnabled() {
9094 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9095 }
9096
9097 /**
9098 * <p>Define whether the horizontal scrollbar should be drawn or not. The
9099 * scrollbar is not drawn by default.</p>
9100 *
9101 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
9102 * be painted
9103 *
9104 * @see #isHorizontalScrollBarEnabled()
9105 */
9106 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
9107 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
9108 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009109 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009110 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009111 }
9112 }
9113
9114 /**
9115 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
9116 * scrollbar is not drawn by default.</p>
9117 *
9118 * @return true if the vertical scrollbar should be painted, false
9119 * otherwise
9120 *
9121 * @see #setVerticalScrollBarEnabled(boolean)
9122 */
9123 public boolean isVerticalScrollBarEnabled() {
9124 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
9125 }
9126
9127 /**
9128 * <p>Define whether the vertical scrollbar should be drawn or not. The
9129 * scrollbar is not drawn by default.</p>
9130 *
9131 * @param verticalScrollBarEnabled true if the vertical scrollbar should
9132 * be painted
9133 *
9134 * @see #isVerticalScrollBarEnabled()
9135 */
9136 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9137 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9138 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009139 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009140 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009141 }
9142 }
9143
Adam Powell20232d02010-12-08 21:08:53 -08009144 /**
9145 * @hide
9146 */
9147 protected void recomputePadding() {
9148 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009149 }
Joe Malin32736f02011-01-19 16:14:20 -08009150
Mike Cleronfe81d382009-09-28 14:22:16 -07009151 /**
9152 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009153 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009154 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009155 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009156 */
9157 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9158 initScrollCache();
9159 final ScrollabilityCache scrollabilityCache = mScrollCache;
9160 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009161 if (fadeScrollbars) {
9162 scrollabilityCache.state = ScrollabilityCache.OFF;
9163 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009164 scrollabilityCache.state = ScrollabilityCache.ON;
9165 }
9166 }
Joe Malin32736f02011-01-19 16:14:20 -08009167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009168 /**
Joe Malin32736f02011-01-19 16:14:20 -08009169 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009170 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009171 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009172 * @return true if scrollbar fading is enabled
9173 */
9174 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009175 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009176 }
Joe Malin32736f02011-01-19 16:14:20 -08009177
Mike Cleron52f0a642009-09-28 18:21:37 -07009178 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009179 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9180 * inset. When inset, they add to the padding of the view. And the scrollbars
9181 * can be drawn inside the padding area or on the edge of the view. For example,
9182 * if a view has a background drawable and you want to draw the scrollbars
9183 * inside the padding specified by the drawable, you can use
9184 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9185 * appear at the edge of the view, ignoring the padding, then you can use
9186 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9187 * @param style the style of the scrollbars. Should be one of
9188 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9189 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9190 * @see #SCROLLBARS_INSIDE_OVERLAY
9191 * @see #SCROLLBARS_INSIDE_INSET
9192 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9193 * @see #SCROLLBARS_OUTSIDE_INSET
9194 */
9195 public void setScrollBarStyle(int style) {
9196 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9197 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009198 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009199 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009200 }
9201 }
9202
9203 /**
9204 * <p>Returns the current scrollbar style.</p>
9205 * @return the current scrollbar style
9206 * @see #SCROLLBARS_INSIDE_OVERLAY
9207 * @see #SCROLLBARS_INSIDE_INSET
9208 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9209 * @see #SCROLLBARS_OUTSIDE_INSET
9210 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009211 @ViewDebug.ExportedProperty(mapping = {
9212 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9213 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9214 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9215 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9216 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009217 public int getScrollBarStyle() {
9218 return mViewFlags & SCROLLBARS_STYLE_MASK;
9219 }
9220
9221 /**
9222 * <p>Compute the horizontal range that the horizontal scrollbar
9223 * represents.</p>
9224 *
9225 * <p>The range is expressed in arbitrary units that must be the same as the
9226 * units used by {@link #computeHorizontalScrollExtent()} and
9227 * {@link #computeHorizontalScrollOffset()}.</p>
9228 *
9229 * <p>The default range is the drawing width of this view.</p>
9230 *
9231 * @return the total horizontal range represented by the horizontal
9232 * scrollbar
9233 *
9234 * @see #computeHorizontalScrollExtent()
9235 * @see #computeHorizontalScrollOffset()
9236 * @see android.widget.ScrollBarDrawable
9237 */
9238 protected int computeHorizontalScrollRange() {
9239 return getWidth();
9240 }
9241
9242 /**
9243 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9244 * within the horizontal range. This value is used to compute the position
9245 * of the thumb within the scrollbar's track.</p>
9246 *
9247 * <p>The range is expressed in arbitrary units that must be the same as the
9248 * units used by {@link #computeHorizontalScrollRange()} and
9249 * {@link #computeHorizontalScrollExtent()}.</p>
9250 *
9251 * <p>The default offset is the scroll offset of this view.</p>
9252 *
9253 * @return the horizontal offset of the scrollbar's thumb
9254 *
9255 * @see #computeHorizontalScrollRange()
9256 * @see #computeHorizontalScrollExtent()
9257 * @see android.widget.ScrollBarDrawable
9258 */
9259 protected int computeHorizontalScrollOffset() {
9260 return mScrollX;
9261 }
9262
9263 /**
9264 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9265 * within the horizontal range. This value is used to compute the length
9266 * of the thumb within the scrollbar's track.</p>
9267 *
9268 * <p>The range is expressed in arbitrary units that must be the same as the
9269 * units used by {@link #computeHorizontalScrollRange()} and
9270 * {@link #computeHorizontalScrollOffset()}.</p>
9271 *
9272 * <p>The default extent is the drawing width of this view.</p>
9273 *
9274 * @return the horizontal extent of the scrollbar's thumb
9275 *
9276 * @see #computeHorizontalScrollRange()
9277 * @see #computeHorizontalScrollOffset()
9278 * @see android.widget.ScrollBarDrawable
9279 */
9280 protected int computeHorizontalScrollExtent() {
9281 return getWidth();
9282 }
9283
9284 /**
9285 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9286 *
9287 * <p>The range is expressed in arbitrary units that must be the same as the
9288 * units used by {@link #computeVerticalScrollExtent()} and
9289 * {@link #computeVerticalScrollOffset()}.</p>
9290 *
9291 * @return the total vertical range represented by the vertical scrollbar
9292 *
9293 * <p>The default range is the drawing height of this view.</p>
9294 *
9295 * @see #computeVerticalScrollExtent()
9296 * @see #computeVerticalScrollOffset()
9297 * @see android.widget.ScrollBarDrawable
9298 */
9299 protected int computeVerticalScrollRange() {
9300 return getHeight();
9301 }
9302
9303 /**
9304 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9305 * within the horizontal range. This value is used to compute the position
9306 * of the thumb within the scrollbar's track.</p>
9307 *
9308 * <p>The range is expressed in arbitrary units that must be the same as the
9309 * units used by {@link #computeVerticalScrollRange()} and
9310 * {@link #computeVerticalScrollExtent()}.</p>
9311 *
9312 * <p>The default offset is the scroll offset of this view.</p>
9313 *
9314 * @return the vertical offset of the scrollbar's thumb
9315 *
9316 * @see #computeVerticalScrollRange()
9317 * @see #computeVerticalScrollExtent()
9318 * @see android.widget.ScrollBarDrawable
9319 */
9320 protected int computeVerticalScrollOffset() {
9321 return mScrollY;
9322 }
9323
9324 /**
9325 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9326 * within the vertical range. This value is used to compute the length
9327 * of the thumb within the scrollbar's track.</p>
9328 *
9329 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009330 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009331 * {@link #computeVerticalScrollOffset()}.</p>
9332 *
9333 * <p>The default extent is the drawing height of this view.</p>
9334 *
9335 * @return the vertical extent of the scrollbar's thumb
9336 *
9337 * @see #computeVerticalScrollRange()
9338 * @see #computeVerticalScrollOffset()
9339 * @see android.widget.ScrollBarDrawable
9340 */
9341 protected int computeVerticalScrollExtent() {
9342 return getHeight();
9343 }
9344
9345 /**
Adam Powell69159442011-06-13 17:53:06 -07009346 * Check if this view can be scrolled horizontally in a certain direction.
9347 *
9348 * @param direction Negative to check scrolling left, positive to check scrolling right.
9349 * @return true if this view can be scrolled in the specified direction, false otherwise.
9350 */
9351 public boolean canScrollHorizontally(int direction) {
9352 final int offset = computeHorizontalScrollOffset();
9353 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9354 if (range == 0) return false;
9355 if (direction < 0) {
9356 return offset > 0;
9357 } else {
9358 return offset < range - 1;
9359 }
9360 }
9361
9362 /**
9363 * Check if this view can be scrolled vertically in a certain direction.
9364 *
9365 * @param direction Negative to check scrolling up, positive to check scrolling down.
9366 * @return true if this view can be scrolled in the specified direction, false otherwise.
9367 */
9368 public boolean canScrollVertically(int direction) {
9369 final int offset = computeVerticalScrollOffset();
9370 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9371 if (range == 0) return false;
9372 if (direction < 0) {
9373 return offset > 0;
9374 } else {
9375 return offset < range - 1;
9376 }
9377 }
9378
9379 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009380 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9381 * scrollbars are painted only if they have been awakened first.</p>
9382 *
9383 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009384 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009385 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009386 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009387 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009388 // scrollbars are drawn only when the animation is running
9389 final ScrollabilityCache cache = mScrollCache;
9390 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009391
Mike Cleronf116bf82009-09-27 19:14:12 -07009392 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009393
Mike Cleronf116bf82009-09-27 19:14:12 -07009394 if (state == ScrollabilityCache.OFF) {
9395 return;
9396 }
Joe Malin32736f02011-01-19 16:14:20 -08009397
Mike Cleronf116bf82009-09-27 19:14:12 -07009398 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009399
Mike Cleronf116bf82009-09-27 19:14:12 -07009400 if (state == ScrollabilityCache.FADING) {
9401 // We're fading -- get our fade interpolation
9402 if (cache.interpolatorValues == null) {
9403 cache.interpolatorValues = new float[1];
9404 }
Joe Malin32736f02011-01-19 16:14:20 -08009405
Mike Cleronf116bf82009-09-27 19:14:12 -07009406 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009407
Mike Cleronf116bf82009-09-27 19:14:12 -07009408 // Stops the animation if we're done
9409 if (cache.scrollBarInterpolator.timeToValues(values) ==
9410 Interpolator.Result.FREEZE_END) {
9411 cache.state = ScrollabilityCache.OFF;
9412 } else {
9413 cache.scrollBar.setAlpha(Math.round(values[0]));
9414 }
Joe Malin32736f02011-01-19 16:14:20 -08009415
9416 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009417 // drawing. We only want this when we're fading so that
9418 // we prevent excessive redraws
9419 invalidate = true;
9420 } else {
9421 // We're just on -- but we may have been fading before so
9422 // reset alpha
9423 cache.scrollBar.setAlpha(255);
9424 }
9425
Joe Malin32736f02011-01-19 16:14:20 -08009426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009427 final int viewFlags = mViewFlags;
9428
9429 final boolean drawHorizontalScrollBar =
9430 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9431 final boolean drawVerticalScrollBar =
9432 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9433 && !isVerticalScrollBarHidden();
9434
9435 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9436 final int width = mRight - mLeft;
9437 final int height = mBottom - mTop;
9438
9439 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009440
Mike Reede8853fc2009-09-04 14:01:48 -04009441 final int scrollX = mScrollX;
9442 final int scrollY = mScrollY;
9443 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9444
Mike Cleronf116bf82009-09-27 19:14:12 -07009445 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009447 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009448 int size = scrollBar.getSize(false);
9449 if (size <= 0) {
9450 size = cache.scrollBarSize;
9451 }
9452
Mike Cleronf116bf82009-09-27 19:14:12 -07009453 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009454 computeHorizontalScrollOffset(),
9455 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009456 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009457 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009458 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009459 left = scrollX + (mPaddingLeft & inside);
9460 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9461 bottom = top + size;
9462 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9463 if (invalidate) {
9464 invalidate(left, top, right, bottom);
9465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009466 }
9467
9468 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009469 int size = scrollBar.getSize(true);
9470 if (size <= 0) {
9471 size = cache.scrollBarSize;
9472 }
9473
Mike Reede8853fc2009-09-04 14:01:48 -04009474 scrollBar.setParameters(computeVerticalScrollRange(),
9475 computeVerticalScrollOffset(),
9476 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009477 switch (mVerticalScrollbarPosition) {
9478 default:
9479 case SCROLLBAR_POSITION_DEFAULT:
9480 case SCROLLBAR_POSITION_RIGHT:
9481 left = scrollX + width - size - (mUserPaddingRight & inside);
9482 break;
9483 case SCROLLBAR_POSITION_LEFT:
9484 left = scrollX + (mUserPaddingLeft & inside);
9485 break;
9486 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009487 top = scrollY + (mPaddingTop & inside);
9488 right = left + size;
9489 bottom = scrollY + height - (mUserPaddingBottom & inside);
9490 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9491 if (invalidate) {
9492 invalidate(left, top, right, bottom);
9493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009494 }
9495 }
9496 }
9497 }
Romain Guy8506ab42009-06-11 17:35:47 -07009498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009499 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009500 * 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 -08009501 * FastScroller is visible.
9502 * @return whether to temporarily hide the vertical scrollbar
9503 * @hide
9504 */
9505 protected boolean isVerticalScrollBarHidden() {
9506 return false;
9507 }
9508
9509 /**
9510 * <p>Draw the horizontal scrollbar if
9511 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9512 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009513 * @param canvas the canvas on which to draw the scrollbar
9514 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009515 *
9516 * @see #isHorizontalScrollBarEnabled()
9517 * @see #computeHorizontalScrollRange()
9518 * @see #computeHorizontalScrollExtent()
9519 * @see #computeHorizontalScrollOffset()
9520 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009521 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009522 */
Romain Guy8fb95422010-08-17 18:38:51 -07009523 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9524 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009525 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009526 scrollBar.draw(canvas);
9527 }
Mike Reede8853fc2009-09-04 14:01:48 -04009528
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009529 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009530 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9531 * returns true.</p>
9532 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009533 * @param canvas the canvas on which to draw the scrollbar
9534 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009535 *
9536 * @see #isVerticalScrollBarEnabled()
9537 * @see #computeVerticalScrollRange()
9538 * @see #computeVerticalScrollExtent()
9539 * @see #computeVerticalScrollOffset()
9540 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009541 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009542 */
Romain Guy8fb95422010-08-17 18:38:51 -07009543 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9544 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009545 scrollBar.setBounds(l, t, r, b);
9546 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009547 }
9548
9549 /**
9550 * Implement this to do your drawing.
9551 *
9552 * @param canvas the canvas on which the background will be drawn
9553 */
9554 protected void onDraw(Canvas canvas) {
9555 }
9556
9557 /*
9558 * Caller is responsible for calling requestLayout if necessary.
9559 * (This allows addViewInLayout to not request a new layout.)
9560 */
9561 void assignParent(ViewParent parent) {
9562 if (mParent == null) {
9563 mParent = parent;
9564 } else if (parent == null) {
9565 mParent = null;
9566 } else {
9567 throw new RuntimeException("view " + this + " being added, but"
9568 + " it already has a parent");
9569 }
9570 }
9571
9572 /**
9573 * This is called when the view is attached to a window. At this point it
9574 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009575 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9576 * however it may be called any time before the first onDraw -- including
9577 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009578 *
9579 * @see #onDetachedFromWindow()
9580 */
9581 protected void onAttachedToWindow() {
9582 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9583 mParent.requestTransparentRegion(this);
9584 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009585 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9586 initialAwakenScrollBars();
9587 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9588 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009589 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009590 // Order is important here: LayoutDirection MUST be resolved before Padding
9591 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009592 resolveLayoutDirectionIfNeeded();
9593 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009594 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009595 if (isFocused()) {
9596 InputMethodManager imm = InputMethodManager.peekInstance();
9597 imm.focusIn(this);
9598 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009599 }
Cibu Johny86666632010-02-22 13:01:02 -08009600
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009601 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009602 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9603 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009604 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009605 private void resolveLayoutDirectionIfNeeded() {
9606 // Do not resolve if it is not needed
9607 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9608
9609 // Clear any previous layout direction resolution
9610 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9611
9612 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009613 switch (getLayoutDirection()) {
9614 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009615 // We cannot do the resolution if there is no parent
9616 if (mParent == null) return;
9617
Cibu Johny86666632010-02-22 13:01:02 -08009618 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009619 if (mParent instanceof ViewGroup) {
9620 ViewGroup viewGroup = ((ViewGroup) mParent);
9621
9622 // Check if the parent view group can resolve
9623 if (! viewGroup.canResolveLayoutDirection()) {
9624 return;
9625 }
9626 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9627 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9628 }
Cibu Johny86666632010-02-22 13:01:02 -08009629 }
9630 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009631 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009632 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009633 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009634 case LAYOUT_DIRECTION_LOCALE:
9635 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009636 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009637 }
9638 break;
9639 default:
9640 // Nothing to do, LTR by default
9641 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009642
9643 // Set to resolved
9644 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009645 onResolvedLayoutDirectionChanged();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009646 }
9647
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009648 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009649 * Called when layout direction has been resolved.
9650 *
9651 * The default implementation does nothing.
9652 */
9653 public void onResolvedLayoutDirectionChanged() {
9654 }
9655
9656 /**
9657 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009658 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009659 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009660 // If the user specified the absolute padding (either with android:padding or
9661 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9662 // use the default padding or the padding from the background drawable
9663 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009664 int resolvedLayoutDirection = getResolvedLayoutDirection();
9665 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009666 case LAYOUT_DIRECTION_RTL:
9667 // Start user padding override Right user padding. Otherwise, if Right user
9668 // padding is not defined, use the default Right padding. If Right user padding
9669 // is defined, just use it.
9670 if (mUserPaddingStart >= 0) {
9671 mUserPaddingRight = mUserPaddingStart;
9672 } else if (mUserPaddingRight < 0) {
9673 mUserPaddingRight = mPaddingRight;
9674 }
9675 if (mUserPaddingEnd >= 0) {
9676 mUserPaddingLeft = mUserPaddingEnd;
9677 } else if (mUserPaddingLeft < 0) {
9678 mUserPaddingLeft = mPaddingLeft;
9679 }
9680 break;
9681 case LAYOUT_DIRECTION_LTR:
9682 default:
9683 // Start user padding override Left user padding. Otherwise, if Left user
9684 // padding is not defined, use the default left padding. If Left user padding
9685 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009686 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009687 mUserPaddingLeft = mUserPaddingStart;
9688 } else if (mUserPaddingLeft < 0) {
9689 mUserPaddingLeft = mPaddingLeft;
9690 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009691 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009692 mUserPaddingRight = mUserPaddingEnd;
9693 } else if (mUserPaddingRight < 0) {
9694 mUserPaddingRight = mPaddingRight;
9695 }
9696 }
9697
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009698 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9699
9700 recomputePadding();
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009701 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009702 }
9703
9704 /**
9705 * Resolve padding depending on the layout direction. Subclasses that care about
9706 * padding resolution should override this method. The default implementation does
9707 * nothing.
9708 *
9709 * @param layoutDirection the direction of the layout
9710 *
Fabrice Di Meglio085585f2012-03-05 13:33:57 -08009711 * {@link #LAYOUT_DIRECTION_LTR}
9712 * {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009713 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009714 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009715 }
9716
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009717 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009718 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009719 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009720 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009721 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009722 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009723 switch (getLayoutDirection()) {
9724 case LAYOUT_DIRECTION_INHERIT:
9725 return (mParent != null);
9726 default:
9727 return true;
9728 }
9729 }
9730
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009731 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009732 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
9733 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009734 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009735 public void resetResolvedLayoutDirection() {
9736 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009737 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009738 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -08009739 // Reset also the text direction
9740 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009741 }
9742
9743 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009744 * Called during reset of resolved layout direction.
9745 *
9746 * Subclasses need to override this method to clear cached information that depends on the
9747 * resolved layout direction, or to inform child views that inherit their layout direction.
9748 *
9749 * The default implementation does nothing.
9750 */
9751 public void onResolvedLayoutDirectionReset() {
9752 }
9753
9754 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009755 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009756 *
9757 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009758 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009759 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009760 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -08009761 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009762 }
9763
9764 /**
9765 * This is called when the view is detached from a window. At this point it
9766 * no longer has a surface for drawing.
9767 *
9768 * @see #onAttachedToWindow()
9769 */
9770 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009771 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009772
Romain Guya440b002010-02-24 15:57:54 -08009773 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009774 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009775 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009776 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009778 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009779
Romain Guy6d7475d2011-07-27 16:28:21 -07009780 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009781
Chet Haasedaf98e92011-01-10 14:10:36 -08009782 if (mDisplayList != null) {
9783 mDisplayList.invalidate();
9784 }
9785
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009786 if (mAttachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009787 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009788 }
9789
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009790 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009791
9792 resetResolvedLayoutDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009793 }
9794
9795 /**
9796 * @return The number of times this view has been attached to a window
9797 */
9798 protected int getWindowAttachCount() {
9799 return mWindowAttachCount;
9800 }
9801
9802 /**
9803 * Retrieve a unique token identifying the window this view is attached to.
9804 * @return Return the window's token for use in
9805 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9806 */
9807 public IBinder getWindowToken() {
9808 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9809 }
9810
9811 /**
9812 * Retrieve a unique token identifying the top-level "real" window of
9813 * the window that this view is attached to. That is, this is like
9814 * {@link #getWindowToken}, except if the window this view in is a panel
9815 * window (attached to another containing window), then the token of
9816 * the containing window is returned instead.
9817 *
9818 * @return Returns the associated window token, either
9819 * {@link #getWindowToken()} or the containing window's token.
9820 */
9821 public IBinder getApplicationWindowToken() {
9822 AttachInfo ai = mAttachInfo;
9823 if (ai != null) {
9824 IBinder appWindowToken = ai.mPanelParentWindowToken;
9825 if (appWindowToken == null) {
9826 appWindowToken = ai.mWindowToken;
9827 }
9828 return appWindowToken;
9829 }
9830 return null;
9831 }
9832
9833 /**
9834 * Retrieve private session object this view hierarchy is using to
9835 * communicate with the window manager.
9836 * @return the session object to communicate with the window manager
9837 */
9838 /*package*/ IWindowSession getWindowSession() {
9839 return mAttachInfo != null ? mAttachInfo.mSession : null;
9840 }
9841
9842 /**
9843 * @param info the {@link android.view.View.AttachInfo} to associated with
9844 * this view
9845 */
9846 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9847 //System.out.println("Attached! " + this);
9848 mAttachInfo = info;
9849 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009850 // We will need to evaluate the drawable state at least once.
9851 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009852 if (mFloatingTreeObserver != null) {
9853 info.mTreeObserver.merge(mFloatingTreeObserver);
9854 mFloatingTreeObserver = null;
9855 }
9856 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9857 mAttachInfo.mScrollContainers.add(this);
9858 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9859 }
9860 performCollectViewAttributes(visibility);
9861 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009862
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009863 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009864 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009865 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009866 if (listeners != null && listeners.size() > 0) {
9867 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9868 // perform the dispatching. The iterator is a safe guard against listeners that
9869 // could mutate the list by calling the various add/remove methods. This prevents
9870 // the array from being modified while we iterate it.
9871 for (OnAttachStateChangeListener listener : listeners) {
9872 listener.onViewAttachedToWindow(this);
9873 }
9874 }
9875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009876 int vis = info.mWindowVisibility;
9877 if (vis != GONE) {
9878 onWindowVisibilityChanged(vis);
9879 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009880 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9881 // If nobody has evaluated the drawable state yet, then do it now.
9882 refreshDrawableState();
9883 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009884 }
9885
9886 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009887 AttachInfo info = mAttachInfo;
9888 if (info != null) {
9889 int vis = info.mWindowVisibility;
9890 if (vis != GONE) {
9891 onWindowVisibilityChanged(GONE);
9892 }
9893 }
9894
9895 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009896
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009897 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009898 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009899 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009900 if (listeners != null && listeners.size() > 0) {
9901 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9902 // perform the dispatching. The iterator is a safe guard against listeners that
9903 // could mutate the list by calling the various add/remove methods. This prevents
9904 // the array from being modified while we iterate it.
9905 for (OnAttachStateChangeListener listener : listeners) {
9906 listener.onViewDetachedFromWindow(this);
9907 }
9908 }
9909
Romain Guy01d5edc2011-01-28 11:28:53 -08009910 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009911 mAttachInfo.mScrollContainers.remove(this);
9912 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9913 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009915 mAttachInfo = null;
9916 }
9917
9918 /**
9919 * Store this view hierarchy's frozen state into the given container.
9920 *
9921 * @param container The SparseArray in which to save the view's state.
9922 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009923 * @see #restoreHierarchyState(android.util.SparseArray)
9924 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9925 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009926 */
9927 public void saveHierarchyState(SparseArray<Parcelable> container) {
9928 dispatchSaveInstanceState(container);
9929 }
9930
9931 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009932 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9933 * this view and its children. May be overridden to modify how freezing happens to a
9934 * 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 -08009935 *
9936 * @param container The SparseArray in which to save the view's state.
9937 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009938 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9939 * @see #saveHierarchyState(android.util.SparseArray)
9940 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009941 */
9942 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9943 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9944 mPrivateFlags &= ~SAVE_STATE_CALLED;
9945 Parcelable state = onSaveInstanceState();
9946 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9947 throw new IllegalStateException(
9948 "Derived class did not call super.onSaveInstanceState()");
9949 }
9950 if (state != null) {
9951 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9952 // + ": " + state);
9953 container.put(mID, state);
9954 }
9955 }
9956 }
9957
9958 /**
9959 * Hook allowing a view to generate a representation of its internal state
9960 * that can later be used to create a new instance with that same state.
9961 * This state should only contain information that is not persistent or can
9962 * not be reconstructed later. For example, you will never store your
9963 * current position on screen because that will be computed again when a
9964 * new instance of the view is placed in its view hierarchy.
9965 * <p>
9966 * Some examples of things you may store here: the current cursor position
9967 * in a text view (but usually not the text itself since that is stored in a
9968 * content provider or other persistent storage), the currently selected
9969 * item in a list view.
9970 *
9971 * @return Returns a Parcelable object containing the view's current dynamic
9972 * state, or null if there is nothing interesting to save. The
9973 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009974 * @see #onRestoreInstanceState(android.os.Parcelable)
9975 * @see #saveHierarchyState(android.util.SparseArray)
9976 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009977 * @see #setSaveEnabled(boolean)
9978 */
9979 protected Parcelable onSaveInstanceState() {
9980 mPrivateFlags |= SAVE_STATE_CALLED;
9981 return BaseSavedState.EMPTY_STATE;
9982 }
9983
9984 /**
9985 * Restore this view hierarchy's frozen state from the given container.
9986 *
9987 * @param container The SparseArray which holds previously frozen states.
9988 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009989 * @see #saveHierarchyState(android.util.SparseArray)
9990 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9991 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009992 */
9993 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9994 dispatchRestoreInstanceState(container);
9995 }
9996
9997 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009998 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9999 * state for this view and its children. May be overridden to modify how restoring
10000 * happens to a view's children; for example, some views may want to not store state
10001 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010002 *
10003 * @param container The SparseArray which holds previously saved state.
10004 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010005 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10006 * @see #restoreHierarchyState(android.util.SparseArray)
10007 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010008 */
10009 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
10010 if (mID != NO_ID) {
10011 Parcelable state = container.get(mID);
10012 if (state != null) {
10013 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
10014 // + ": " + state);
10015 mPrivateFlags &= ~SAVE_STATE_CALLED;
10016 onRestoreInstanceState(state);
10017 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10018 throw new IllegalStateException(
10019 "Derived class did not call super.onRestoreInstanceState()");
10020 }
10021 }
10022 }
10023 }
10024
10025 /**
10026 * Hook allowing a view to re-apply a representation of its internal state that had previously
10027 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
10028 * null state.
10029 *
10030 * @param state The frozen state that had previously been returned by
10031 * {@link #onSaveInstanceState}.
10032 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010033 * @see #onSaveInstanceState()
10034 * @see #restoreHierarchyState(android.util.SparseArray)
10035 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010036 */
10037 protected void onRestoreInstanceState(Parcelable state) {
10038 mPrivateFlags |= SAVE_STATE_CALLED;
10039 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080010040 throw new IllegalArgumentException("Wrong state class, expecting View State but "
10041 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080010042 + "when two views of different type have the same id in the same hierarchy. "
10043 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080010044 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010045 }
10046 }
10047
10048 /**
10049 * <p>Return the time at which the drawing of the view hierarchy started.</p>
10050 *
10051 * @return the drawing start time in milliseconds
10052 */
10053 public long getDrawingTime() {
10054 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
10055 }
10056
10057 /**
10058 * <p>Enables or disables the duplication of the parent's state into this view. When
10059 * duplication is enabled, this view gets its drawable state from its parent rather
10060 * than from its own internal properties.</p>
10061 *
10062 * <p>Note: in the current implementation, setting this property to true after the
10063 * view was added to a ViewGroup might have no effect at all. This property should
10064 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
10065 *
10066 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
10067 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010068 *
Gilles Debunnefb817032011-01-13 13:52:49 -080010069 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
10070 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010071 *
10072 * @param enabled True to enable duplication of the parent's drawable state, false
10073 * to disable it.
10074 *
10075 * @see #getDrawableState()
10076 * @see #isDuplicateParentStateEnabled()
10077 */
10078 public void setDuplicateParentStateEnabled(boolean enabled) {
10079 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
10080 }
10081
10082 /**
10083 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
10084 *
10085 * @return True if this view's drawable state is duplicated from the parent,
10086 * false otherwise
10087 *
10088 * @see #getDrawableState()
10089 * @see #setDuplicateParentStateEnabled(boolean)
10090 */
10091 public boolean isDuplicateParentStateEnabled() {
10092 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
10093 }
10094
10095 /**
Romain Guy171c5922011-01-06 10:04:23 -080010096 * <p>Specifies the type of layer backing this view. The layer can be
10097 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
10098 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010099 *
Romain Guy171c5922011-01-06 10:04:23 -080010100 * <p>A layer is associated with an optional {@link android.graphics.Paint}
10101 * instance that controls how the layer is composed on screen. The following
10102 * properties of the paint are taken into account when composing the layer:</p>
10103 * <ul>
10104 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
10105 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
10106 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
10107 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080010108 *
Romain Guy171c5922011-01-06 10:04:23 -080010109 * <p>If this view has an alpha value set to < 1.0 by calling
10110 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
10111 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
10112 * equivalent to setting a hardware layer on this view and providing a paint with
10113 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080010114 *
Romain Guy171c5922011-01-06 10:04:23 -080010115 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
10116 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
10117 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010118 *
Romain Guy171c5922011-01-06 10:04:23 -080010119 * @param layerType The ype of layer to use with this view, must be one of
10120 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10121 * {@link #LAYER_TYPE_HARDWARE}
10122 * @param paint The paint used to compose the layer. This argument is optional
10123 * and can be null. It is ignored when the layer type is
10124 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080010125 *
10126 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080010127 * @see #LAYER_TYPE_NONE
10128 * @see #LAYER_TYPE_SOFTWARE
10129 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080010130 * @see #setAlpha(float)
10131 *
Romain Guy171c5922011-01-06 10:04:23 -080010132 * @attr ref android.R.styleable#View_layerType
10133 */
10134 public void setLayerType(int layerType, Paint paint) {
10135 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080010136 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080010137 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
10138 }
Chet Haasedaf98e92011-01-10 14:10:36 -080010139
Romain Guyd6cd5722011-01-17 14:42:41 -080010140 if (layerType == mLayerType) {
10141 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
10142 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010143 invalidateParentCaches();
10144 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080010145 }
10146 return;
10147 }
Romain Guy171c5922011-01-06 10:04:23 -080010148
10149 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080010150 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070010151 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010152 destroyLayer();
Romain Guy31f2c2e2011-11-21 10:55:41 -080010153 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080010154 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010155 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010156 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080010157 default:
10158 break;
Romain Guy171c5922011-01-06 10:04:23 -080010159 }
10160
10161 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010162 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10163 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10164 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010165
Romain Guy0fd89bf2011-01-26 15:41:30 -080010166 invalidateParentCaches();
10167 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010168 }
10169
10170 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010171 * Indicates whether this view has a static layer. A view with layer type
10172 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10173 * dynamic.
10174 */
10175 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080010176 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070010177 }
10178
10179 /**
Romain Guy171c5922011-01-06 10:04:23 -080010180 * Indicates what type of layer is currently associated with this view. By default
10181 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10182 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10183 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010184 *
Romain Guy171c5922011-01-06 10:04:23 -080010185 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10186 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010187 *
10188 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010189 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010190 * @see #LAYER_TYPE_NONE
10191 * @see #LAYER_TYPE_SOFTWARE
10192 * @see #LAYER_TYPE_HARDWARE
10193 */
10194 public int getLayerType() {
10195 return mLayerType;
10196 }
Joe Malin32736f02011-01-19 16:14:20 -080010197
Romain Guy6c319ca2011-01-11 14:29:25 -080010198 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010199 * Forces this view's layer to be created and this view to be rendered
10200 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10201 * invoking this method will have no effect.
10202 *
10203 * This method can for instance be used to render a view into its layer before
10204 * starting an animation. If this view is complex, rendering into the layer
10205 * before starting the animation will avoid skipping frames.
10206 *
10207 * @throws IllegalStateException If this view is not attached to a window
10208 *
10209 * @see #setLayerType(int, android.graphics.Paint)
10210 */
10211 public void buildLayer() {
10212 if (mLayerType == LAYER_TYPE_NONE) return;
10213
10214 if (mAttachInfo == null) {
10215 throw new IllegalStateException("This view must be attached to a window first");
10216 }
10217
10218 switch (mLayerType) {
10219 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080010220 if (mAttachInfo.mHardwareRenderer != null &&
10221 mAttachInfo.mHardwareRenderer.isEnabled() &&
10222 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080010223 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080010224 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010225 break;
10226 case LAYER_TYPE_SOFTWARE:
10227 buildDrawingCache(true);
10228 break;
10229 }
10230 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010231
10232 // Make sure the HardwareRenderer.validate() was invoked before calling this method
10233 void flushLayer() {
10234 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
10235 mHardwareLayer.flush();
10236 }
10237 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010238
10239 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010240 * <p>Returns a hardware layer that can be used to draw this view again
10241 * without executing its draw method.</p>
10242 *
10243 * @return A HardwareLayer ready to render, or null if an error occurred.
10244 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080010245 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010246 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10247 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010248 return null;
10249 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010250
10251 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080010252
10253 final int width = mRight - mLeft;
10254 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010255
Romain Guy6c319ca2011-01-11 14:29:25 -080010256 if (width == 0 || height == 0) {
10257 return null;
10258 }
10259
10260 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10261 if (mHardwareLayer == null) {
10262 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10263 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -080010264 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010265 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10266 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -080010267 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010268 }
10269
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010270 // The layer is not valid if the underlying GPU resources cannot be allocated
10271 if (!mHardwareLayer.isValid()) {
10272 return null;
10273 }
10274
Michael Jurka7e52caf2012-03-06 15:57:06 -080010275 mHardwareLayer.redraw(getDisplayList(), mLocalDirtyRect);
10276 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010277 }
10278
10279 return mHardwareLayer;
10280 }
Romain Guy171c5922011-01-06 10:04:23 -080010281
Romain Guy589b0bb2011-10-10 13:57:47 -070010282 /**
10283 * Destroys this View's hardware layer if possible.
10284 *
10285 * @return True if the layer was destroyed, false otherwise.
10286 *
10287 * @see #setLayerType(int, android.graphics.Paint)
10288 * @see #LAYER_TYPE_HARDWARE
10289 */
Romain Guy65b345f2011-07-27 18:51:50 -070010290 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -070010291 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010292 AttachInfo info = mAttachInfo;
10293 if (info != null && info.mHardwareRenderer != null &&
10294 info.mHardwareRenderer.isEnabled() && info.mHardwareRenderer.validate()) {
10295 mHardwareLayer.destroy();
10296 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080010297
Romain Guy9c4b79a2011-11-10 19:23:58 -080010298 invalidate(true);
10299 invalidateParentCaches();
10300 }
Romain Guy65b345f2011-07-27 18:51:50 -070010301 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010302 }
Romain Guy65b345f2011-07-27 18:51:50 -070010303 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010304 }
10305
Romain Guy171c5922011-01-06 10:04:23 -080010306 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080010307 * Destroys all hardware rendering resources. This method is invoked
10308 * when the system needs to reclaim resources. Upon execution of this
10309 * method, you should free any OpenGL resources created by the view.
10310 *
10311 * Note: you <strong>must</strong> call
10312 * <code>super.destroyHardwareResources()</code> when overriding
10313 * this method.
10314 *
10315 * @hide
10316 */
10317 protected void destroyHardwareResources() {
10318 destroyLayer();
10319 }
10320
10321 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010322 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10323 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10324 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10325 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10326 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10327 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010328 *
Romain Guy171c5922011-01-06 10:04:23 -080010329 * <p>Enabling the drawing cache is similar to
10330 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010331 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10332 * drawing cache has no effect on rendering because the system uses a different mechanism
10333 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10334 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10335 * for information on how to enable software and hardware layers.</p>
10336 *
10337 * <p>This API can be used to manually generate
10338 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10339 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010340 *
10341 * @param enabled true to enable the drawing cache, false otherwise
10342 *
10343 * @see #isDrawingCacheEnabled()
10344 * @see #getDrawingCache()
10345 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010346 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010347 */
10348 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010349 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010350 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10351 }
10352
10353 /**
10354 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10355 *
10356 * @return true if the drawing cache is enabled
10357 *
10358 * @see #setDrawingCacheEnabled(boolean)
10359 * @see #getDrawingCache()
10360 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010361 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010362 public boolean isDrawingCacheEnabled() {
10363 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10364 }
10365
10366 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010367 * Debugging utility which recursively outputs the dirty state of a view and its
10368 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010369 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010370 * @hide
10371 */
Romain Guy676b1732011-02-14 14:45:33 -080010372 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010373 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10374 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10375 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10376 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10377 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10378 if (clear) {
10379 mPrivateFlags &= clearMask;
10380 }
10381 if (this instanceof ViewGroup) {
10382 ViewGroup parent = (ViewGroup) this;
10383 final int count = parent.getChildCount();
10384 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010385 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010386 child.outputDirtyFlags(indent + " ", clear, clearMask);
10387 }
10388 }
10389 }
10390
10391 /**
10392 * This method is used by ViewGroup to cause its children to restore or recreate their
10393 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10394 * to recreate its own display list, which would happen if it went through the normal
10395 * draw/dispatchDraw mechanisms.
10396 *
10397 * @hide
10398 */
10399 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010400
10401 /**
10402 * A view that is not attached or hardware accelerated cannot create a display list.
10403 * This method checks these conditions and returns the appropriate result.
10404 *
10405 * @return true if view has the ability to create a display list, false otherwise.
10406 *
10407 * @hide
10408 */
10409 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010410 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010411 }
Joe Malin32736f02011-01-19 16:14:20 -080010412
Chet Haasedaf98e92011-01-10 14:10:36 -080010413 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080010414 * @return The HardwareRenderer associated with that view or null if hardware rendering
10415 * is not supported or this this has not been attached to a window.
10416 *
10417 * @hide
10418 */
10419 public HardwareRenderer getHardwareRenderer() {
10420 if (mAttachInfo != null) {
10421 return mAttachInfo.mHardwareRenderer;
10422 }
10423 return null;
10424 }
10425
10426 /**
Romain Guyb051e892010-09-28 19:09:36 -070010427 * <p>Returns a display list that can be used to draw this view again
10428 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010429 *
Romain Guyb051e892010-09-28 19:09:36 -070010430 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010431 *
10432 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010433 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010434 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -080010435 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -070010436 return null;
10437 }
10438
Chet Haasedaf98e92011-01-10 14:10:36 -080010439 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10440 mDisplayList == null || !mDisplayList.isValid() ||
10441 mRecreateDisplayList)) {
10442 // Don't need to recreate the display list, just need to tell our
10443 // children to restore/recreate theirs
10444 if (mDisplayList != null && mDisplayList.isValid() &&
10445 !mRecreateDisplayList) {
10446 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10447 mPrivateFlags &= ~DIRTY_MASK;
10448 dispatchGetDisplayList();
10449
10450 return mDisplayList;
10451 }
10452
10453 // If we got here, we're recreating it. Mark it as such to ensure that
10454 // we copy in child display lists into ours in drawChild()
10455 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -080010456 if (mDisplayList == null) {
Romain Guy13631f32012-01-30 17:41:55 -080010457 final String name = getClass().getSimpleName();
10458 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
Chet Haasedaf98e92011-01-10 14:10:36 -080010459 // If we're creating a new display list, make sure our parent gets invalidated
10460 // since they will need to recreate their display list to account for this
10461 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -080010462 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -080010463 }
Romain Guyb051e892010-09-28 19:09:36 -070010464
10465 final HardwareCanvas canvas = mDisplayList.start();
Romain Guye080af32011-09-08 15:03:39 -070010466 int restoreCount = 0;
Romain Guyb051e892010-09-28 19:09:36 -070010467 try {
10468 int width = mRight - mLeft;
10469 int height = mBottom - mTop;
10470
10471 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -080010472 // The dirty rect should always be null for a display list
10473 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -070010474
Chet Haasedaf98e92011-01-10 14:10:36 -080010475 computeScroll();
Romain Guye080af32011-09-08 15:03:39 -070010476
10477 restoreCount = canvas.save();
Chet Haasedaf98e92011-01-10 14:10:36 -080010478 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010479 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -070010480 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -080010481
Romain Guyb051e892010-09-28 19:09:36 -070010482 // Fast path for layouts with no backgrounds
10483 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -070010484 dispatchDraw(canvas);
10485 } else {
10486 draw(canvas);
10487 }
Romain Guyb051e892010-09-28 19:09:36 -070010488 } finally {
Romain Guye080af32011-09-08 15:03:39 -070010489 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -070010490 canvas.onPostDraw();
10491
10492 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -070010493 }
Chet Haase77785f92011-01-25 23:22:09 -080010494 } else {
10495 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10496 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -070010497 }
10498
10499 return mDisplayList;
10500 }
10501
10502 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010503 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010504 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010505 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010506 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010507 * @see #getDrawingCache(boolean)
10508 */
10509 public Bitmap getDrawingCache() {
10510 return getDrawingCache(false);
10511 }
10512
10513 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010514 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10515 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10516 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10517 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10518 * request the drawing cache by calling this method and draw it on screen if the
10519 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010520 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010521 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10522 * this method will create a bitmap of the same size as this view. Because this bitmap
10523 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10524 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10525 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10526 * size than the view. This implies that your application must be able to handle this
10527 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010528 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010529 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10530 * the current density of the screen when the application is in compatibility
10531 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010532 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010533 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010534 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010535 * @see #setDrawingCacheEnabled(boolean)
10536 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010537 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010538 * @see #destroyDrawingCache()
10539 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010540 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010541 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10542 return null;
10543 }
10544 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010545 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010546 }
Romain Guy02890fd2010-08-06 17:58:44 -070010547 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010548 }
10549
10550 /**
10551 * <p>Frees the resources used by the drawing cache. If you call
10552 * {@link #buildDrawingCache()} manually without calling
10553 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10554 * should cleanup the cache with this method afterwards.</p>
10555 *
10556 * @see #setDrawingCacheEnabled(boolean)
10557 * @see #buildDrawingCache()
10558 * @see #getDrawingCache()
10559 */
10560 public void destroyDrawingCache() {
10561 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010562 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010563 mDrawingCache = null;
10564 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010565 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010566 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010567 mUnscaledDrawingCache = null;
10568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010569 }
10570
10571 /**
10572 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070010573 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010574 * view will always be drawn on top of a solid color.
10575 *
10576 * @param color The background color to use for the drawing cache's bitmap
10577 *
10578 * @see #setDrawingCacheEnabled(boolean)
10579 * @see #buildDrawingCache()
10580 * @see #getDrawingCache()
10581 */
10582 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010583 if (color != mDrawingCacheBackgroundColor) {
10584 mDrawingCacheBackgroundColor = color;
10585 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010587 }
10588
10589 /**
10590 * @see #setDrawingCacheBackgroundColor(int)
10591 *
10592 * @return The background color to used for the drawing cache's bitmap
10593 */
10594 public int getDrawingCacheBackgroundColor() {
10595 return mDrawingCacheBackgroundColor;
10596 }
10597
10598 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010599 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010600 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010601 * @see #buildDrawingCache(boolean)
10602 */
10603 public void buildDrawingCache() {
10604 buildDrawingCache(false);
10605 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010606
Romain Guyfbd8f692009-06-26 14:51:58 -070010607 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010608 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10609 *
10610 * <p>If you call {@link #buildDrawingCache()} manually without calling
10611 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10612 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010613 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010614 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10615 * this method will create a bitmap of the same size as this view. Because this bitmap
10616 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10617 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10618 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10619 * size than the view. This implies that your application must be able to handle this
10620 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010621 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010622 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10623 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010624 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010625 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010626 *
10627 * @see #getDrawingCache()
10628 * @see #destroyDrawingCache()
10629 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010630 public void buildDrawingCache(boolean autoScale) {
10631 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010632 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010633 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010634
10635 if (ViewDebug.TRACE_HIERARCHY) {
10636 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10637 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010638
Romain Guy8506ab42009-06-11 17:35:47 -070010639 int width = mRight - mLeft;
10640 int height = mBottom - mTop;
10641
10642 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010643 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010644
Romain Guye1123222009-06-29 14:24:56 -070010645 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010646 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10647 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010648 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010649
10650 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010651 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010652 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010653
10654 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010655 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010656 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010657 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10658 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010659 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010660 return;
10661 }
10662
10663 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010664 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010665
10666 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010667 Bitmap.Config quality;
10668 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010669 // Never pick ARGB_4444 because it looks awful
10670 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010671 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10672 case DRAWING_CACHE_QUALITY_AUTO:
10673 quality = Bitmap.Config.ARGB_8888;
10674 break;
10675 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010676 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010677 break;
10678 case DRAWING_CACHE_QUALITY_HIGH:
10679 quality = Bitmap.Config.ARGB_8888;
10680 break;
10681 default:
10682 quality = Bitmap.Config.ARGB_8888;
10683 break;
10684 }
10685 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010686 // Optimization for translucent windows
10687 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010688 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010689 }
10690
10691 // Try to cleanup memory
10692 if (bitmap != null) bitmap.recycle();
10693
10694 try {
10695 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010696 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010697 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010698 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010699 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010700 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010701 }
Adam Powell26153a32010-11-08 15:22:27 -080010702 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010703 } catch (OutOfMemoryError e) {
10704 // If there is not enough memory to create the bitmap cache, just
10705 // ignore the issue as bitmap caches are not required to draw the
10706 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010707 if (autoScale) {
10708 mDrawingCache = null;
10709 } else {
10710 mUnscaledDrawingCache = null;
10711 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010712 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010713 return;
10714 }
10715
10716 clear = drawingCacheBackgroundColor != 0;
10717 }
10718
10719 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010720 if (attachInfo != null) {
10721 canvas = attachInfo.mCanvas;
10722 if (canvas == null) {
10723 canvas = new Canvas();
10724 }
10725 canvas.setBitmap(bitmap);
10726 // Temporarily clobber the cached Canvas in case one of our children
10727 // is also using a drawing cache. Without this, the children would
10728 // steal the canvas by attaching their own bitmap to it and bad, bad
10729 // thing would happen (invisible views, corrupted drawings, etc.)
10730 attachInfo.mCanvas = null;
10731 } else {
10732 // This case should hopefully never or seldom happen
10733 canvas = new Canvas(bitmap);
10734 }
10735
10736 if (clear) {
10737 bitmap.eraseColor(drawingCacheBackgroundColor);
10738 }
10739
10740 computeScroll();
10741 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010742
Romain Guye1123222009-06-29 14:24:56 -070010743 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010744 final float scale = attachInfo.mApplicationScale;
10745 canvas.scale(scale, scale);
10746 }
Joe Malin32736f02011-01-19 16:14:20 -080010747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010748 canvas.translate(-mScrollX, -mScrollY);
10749
Romain Guy5bcdff42009-05-14 21:27:18 -070010750 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010751 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10752 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010753 mPrivateFlags |= DRAWING_CACHE_VALID;
10754 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010755
10756 // Fast path for layouts with no backgrounds
10757 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10758 if (ViewDebug.TRACE_HIERARCHY) {
10759 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10760 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010761 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010762 dispatchDraw(canvas);
10763 } else {
10764 draw(canvas);
10765 }
10766
10767 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010768 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010769
10770 if (attachInfo != null) {
10771 // Restore the cached Canvas for our siblings
10772 attachInfo.mCanvas = canvas;
10773 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010774 }
10775 }
10776
10777 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010778 * Create a snapshot of the view into a bitmap. We should probably make
10779 * some form of this public, but should think about the API.
10780 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010781 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010782 int width = mRight - mLeft;
10783 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010784
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010785 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010786 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010787 width = (int) ((width * scale) + 0.5f);
10788 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010789
Romain Guy8c11e312009-09-14 15:15:30 -070010790 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010791 if (bitmap == null) {
10792 throw new OutOfMemoryError();
10793 }
10794
Romain Guyc529d8d2011-09-06 15:01:39 -070010795 Resources resources = getResources();
10796 if (resources != null) {
10797 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
10798 }
Joe Malin32736f02011-01-19 16:14:20 -080010799
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010800 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010801 if (attachInfo != null) {
10802 canvas = attachInfo.mCanvas;
10803 if (canvas == null) {
10804 canvas = new Canvas();
10805 }
10806 canvas.setBitmap(bitmap);
10807 // Temporarily clobber the cached Canvas in case one of our children
10808 // is also using a drawing cache. Without this, the children would
10809 // steal the canvas by attaching their own bitmap to it and bad, bad
10810 // things would happen (invisible views, corrupted drawings, etc.)
10811 attachInfo.mCanvas = null;
10812 } else {
10813 // This case should hopefully never or seldom happen
10814 canvas = new Canvas(bitmap);
10815 }
10816
Romain Guy5bcdff42009-05-14 21:27:18 -070010817 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010818 bitmap.eraseColor(backgroundColor);
10819 }
10820
10821 computeScroll();
10822 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010823 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010824 canvas.translate(-mScrollX, -mScrollY);
10825
Romain Guy5bcdff42009-05-14 21:27:18 -070010826 // Temporarily remove the dirty mask
10827 int flags = mPrivateFlags;
10828 mPrivateFlags &= ~DIRTY_MASK;
10829
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010830 // Fast path for layouts with no backgrounds
10831 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10832 dispatchDraw(canvas);
10833 } else {
10834 draw(canvas);
10835 }
10836
Romain Guy5bcdff42009-05-14 21:27:18 -070010837 mPrivateFlags = flags;
10838
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010839 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010840 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010841
10842 if (attachInfo != null) {
10843 // Restore the cached Canvas for our siblings
10844 attachInfo.mCanvas = canvas;
10845 }
Romain Guy8506ab42009-06-11 17:35:47 -070010846
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010847 return bitmap;
10848 }
10849
10850 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010851 * Indicates whether this View is currently in edit mode. A View is usually
10852 * in edit mode when displayed within a developer tool. For instance, if
10853 * this View is being drawn by a visual user interface builder, this method
10854 * should return true.
10855 *
10856 * Subclasses should check the return value of this method to provide
10857 * different behaviors if their normal behavior might interfere with the
10858 * host environment. For instance: the class spawns a thread in its
10859 * constructor, the drawing code relies on device-specific features, etc.
10860 *
10861 * This method is usually checked in the drawing code of custom widgets.
10862 *
10863 * @return True if this View is in edit mode, false otherwise.
10864 */
10865 public boolean isInEditMode() {
10866 return false;
10867 }
10868
10869 /**
10870 * If the View draws content inside its padding and enables fading edges,
10871 * it needs to support padding offsets. Padding offsets are added to the
10872 * fading edges to extend the length of the fade so that it covers pixels
10873 * drawn inside the padding.
10874 *
10875 * Subclasses of this class should override this method if they need
10876 * to draw content inside the padding.
10877 *
10878 * @return True if padding offset must be applied, false otherwise.
10879 *
10880 * @see #getLeftPaddingOffset()
10881 * @see #getRightPaddingOffset()
10882 * @see #getTopPaddingOffset()
10883 * @see #getBottomPaddingOffset()
10884 *
10885 * @since CURRENT
10886 */
10887 protected boolean isPaddingOffsetRequired() {
10888 return false;
10889 }
10890
10891 /**
10892 * Amount by which to extend the left fading region. Called only when
10893 * {@link #isPaddingOffsetRequired()} returns true.
10894 *
10895 * @return The left padding offset in pixels.
10896 *
10897 * @see #isPaddingOffsetRequired()
10898 *
10899 * @since CURRENT
10900 */
10901 protected int getLeftPaddingOffset() {
10902 return 0;
10903 }
10904
10905 /**
10906 * Amount by which to extend the right fading region. Called only when
10907 * {@link #isPaddingOffsetRequired()} returns true.
10908 *
10909 * @return The right padding offset in pixels.
10910 *
10911 * @see #isPaddingOffsetRequired()
10912 *
10913 * @since CURRENT
10914 */
10915 protected int getRightPaddingOffset() {
10916 return 0;
10917 }
10918
10919 /**
10920 * Amount by which to extend the top fading region. Called only when
10921 * {@link #isPaddingOffsetRequired()} returns true.
10922 *
10923 * @return The top padding offset in pixels.
10924 *
10925 * @see #isPaddingOffsetRequired()
10926 *
10927 * @since CURRENT
10928 */
10929 protected int getTopPaddingOffset() {
10930 return 0;
10931 }
10932
10933 /**
10934 * Amount by which to extend the bottom fading region. Called only when
10935 * {@link #isPaddingOffsetRequired()} returns true.
10936 *
10937 * @return The bottom padding offset in pixels.
10938 *
10939 * @see #isPaddingOffsetRequired()
10940 *
10941 * @since CURRENT
10942 */
10943 protected int getBottomPaddingOffset() {
10944 return 0;
10945 }
10946
10947 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070010948 * @hide
10949 * @param offsetRequired
10950 */
10951 protected int getFadeTop(boolean offsetRequired) {
10952 int top = mPaddingTop;
10953 if (offsetRequired) top += getTopPaddingOffset();
10954 return top;
10955 }
10956
10957 /**
10958 * @hide
10959 * @param offsetRequired
10960 */
10961 protected int getFadeHeight(boolean offsetRequired) {
10962 int padding = mPaddingTop;
10963 if (offsetRequired) padding += getTopPaddingOffset();
10964 return mBottom - mTop - mPaddingBottom - padding;
10965 }
10966
10967 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010968 * <p>Indicates whether this view is attached to an hardware accelerated
10969 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010970 *
Romain Guy2bffd262010-09-12 17:40:02 -070010971 * <p>Even if this method returns true, it does not mean that every call
10972 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10973 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10974 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10975 * window is hardware accelerated,
10976 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10977 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010978 *
Romain Guy2bffd262010-09-12 17:40:02 -070010979 * @return True if the view is attached to a window and the window is
10980 * hardware accelerated; false in any other case.
10981 */
10982 public boolean isHardwareAccelerated() {
10983 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10984 }
Joe Malin32736f02011-01-19 16:14:20 -080010985
Romain Guy2bffd262010-09-12 17:40:02 -070010986 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080010987 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
10988 * case of an active Animation being run on the view.
10989 */
10990 private boolean drawAnimation(ViewGroup parent, long drawingTime,
10991 Animation a, boolean scalingRequired) {
10992 Transformation invalidationTransform;
10993 final int flags = parent.mGroupFlags;
10994 final boolean initialized = a.isInitialized();
10995 if (!initialized) {
10996 a.initialize(mRight - mLeft, mBottom - mTop, getWidth(), getHeight());
10997 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
10998 onAnimationStart();
10999 }
11000
11001 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
11002 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
11003 if (parent.mInvalidationTransformation == null) {
11004 parent.mInvalidationTransformation = new Transformation();
11005 }
11006 invalidationTransform = parent.mInvalidationTransformation;
11007 a.getTransformation(drawingTime, invalidationTransform, 1f);
11008 } else {
11009 invalidationTransform = parent.mChildTransformation;
11010 }
11011 if (more) {
11012 if (!a.willChangeBounds()) {
11013 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
11014 parent.FLAG_OPTIMIZE_INVALIDATE) {
11015 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
11016 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
11017 // The child need to draw an animation, potentially offscreen, so
11018 // make sure we do not cancel invalidate requests
11019 parent.mPrivateFlags |= DRAW_ANIMATION;
11020 parent.invalidate(mLeft, mTop, mRight, mBottom);
11021 }
11022 } else {
11023 if (parent.mInvalidateRegion == null) {
11024 parent.mInvalidateRegion = new RectF();
11025 }
11026 final RectF region = parent.mInvalidateRegion;
11027 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
11028 invalidationTransform);
11029
11030 // The child need to draw an animation, potentially offscreen, so
11031 // make sure we do not cancel invalidate requests
11032 parent.mPrivateFlags |= DRAW_ANIMATION;
11033
11034 final int left = mLeft + (int) region.left;
11035 final int top = mTop + (int) region.top;
11036 parent.invalidate(left, top, left + (int) (region.width() + .5f),
11037 top + (int) (region.height() + .5f));
11038 }
11039 }
11040 return more;
11041 }
11042
11043 /**
Chet Haase64a48c12012-02-13 16:33:29 -080011044 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
11045 * This draw() method is an implementation detail and is not intended to be overridden or
11046 * to be called from anywhere else other than ViewGroup.drawChild().
11047 */
11048 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
11049 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080011050 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080011051 final int flags = parent.mGroupFlags;
11052
11053 if ((flags & parent.FLAG_CLEAR_TRANSFORMATION) == parent.FLAG_CLEAR_TRANSFORMATION) {
11054 parent.mChildTransformation.clear();
11055 parent.mGroupFlags &= ~parent.FLAG_CLEAR_TRANSFORMATION;
11056 }
11057
11058 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080011059 boolean concatMatrix = false;
11060
11061 boolean scalingRequired = false;
11062 boolean caching;
11063 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
11064
11065 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
11066 if ((flags & parent.FLAG_CHILDREN_DRAWN_WITH_CACHE) == parent.FLAG_CHILDREN_DRAWN_WITH_CACHE ||
11067 (flags & parent.FLAG_ALWAYS_DRAWN_WITH_CACHE) == parent.FLAG_ALWAYS_DRAWN_WITH_CACHE) {
11068 caching = true;
11069 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
11070 } else {
11071 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
11072 }
11073
Chet Haasebcca79a2012-02-14 08:45:14 -080011074 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080011075 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011076 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080011077 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080011078 transformToApply = parent.mChildTransformation;
Chet Haase64a48c12012-02-13 16:33:29 -080011079 } else if ((flags & parent.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) ==
11080 parent.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011081 final boolean hasTransform =
11082 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080011083 if (hasTransform) {
11084 final int transformType = parent.mChildTransformation.getTransformationType();
11085 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
11086 parent.mChildTransformation : null;
11087 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
11088 }
11089 }
11090
11091 concatMatrix |= !childHasIdentityMatrix;
11092
11093 // Sets the flag as early as possible to allow draw() implementations
11094 // to call invalidate() successfully when doing animations
11095 mPrivateFlags |= DRAWN;
11096
Chet Haasebcca79a2012-02-14 08:45:14 -080011097 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080011098 (mPrivateFlags & DRAW_ANIMATION) == 0) {
11099 return more;
11100 }
11101
11102 if (hardwareAccelerated) {
11103 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
11104 // retain the flag's value temporarily in the mRecreateDisplayList flag
11105 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
11106 mPrivateFlags &= ~INVALIDATED;
11107 }
11108
11109 computeScroll();
11110
11111 final int sx = mScrollX;
11112 final int sy = mScrollY;
11113
11114 DisplayList displayList = null;
11115 Bitmap cache = null;
11116 boolean hasDisplayList = false;
11117 if (caching) {
11118 if (!hardwareAccelerated) {
11119 if (layerType != LAYER_TYPE_NONE) {
11120 layerType = LAYER_TYPE_SOFTWARE;
11121 buildDrawingCache(true);
11122 }
11123 cache = getDrawingCache(true);
11124 } else {
11125 switch (layerType) {
11126 case LAYER_TYPE_SOFTWARE:
11127 buildDrawingCache(true);
11128 cache = getDrawingCache(true);
11129 break;
11130 case LAYER_TYPE_NONE:
11131 // Delay getting the display list until animation-driven alpha values are
11132 // set up and possibly passed on to the view
11133 hasDisplayList = canHaveDisplayList();
11134 break;
11135 }
11136 }
11137 }
11138
11139 final boolean hasNoCache = cache == null || hasDisplayList;
11140 final boolean offsetForScroll = cache == null && !hasDisplayList &&
11141 layerType != LAYER_TYPE_HARDWARE;
11142
11143 final int restoreTo = canvas.save();
11144 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011145 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080011146 } else {
Chet Haasebcca79a2012-02-14 08:45:14 -080011147 canvas.translate(mLeft, mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080011148 if (scalingRequired) {
11149 // mAttachInfo cannot be null, otherwise scalingRequired == false
11150 final float scale = 1.0f / mAttachInfo.mApplicationScale;
11151 canvas.scale(scale, scale);
11152 }
11153 }
11154
11155 float alpha = getAlpha();
11156 if (transformToApply != null || alpha < 1.0f || !hasIdentityMatrix()) {
11157 if (transformToApply != null || !childHasIdentityMatrix) {
11158 int transX = 0;
11159 int transY = 0;
11160
11161 if (offsetForScroll) {
11162 transX = -sx;
11163 transY = -sy;
11164 }
11165
11166 if (transformToApply != null) {
11167 if (concatMatrix) {
11168 // Undo the scroll translation, apply the transformation matrix,
11169 // then redo the scroll translate to get the correct result.
11170 canvas.translate(-transX, -transY);
11171 canvas.concat(transformToApply.getMatrix());
11172 canvas.translate(transX, transY);
11173 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11174 }
11175
11176 float transformAlpha = transformToApply.getAlpha();
11177 if (transformAlpha < 1.0f) {
11178 alpha *= transformToApply.getAlpha();
11179 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11180 }
11181 }
11182
11183 if (!childHasIdentityMatrix) {
11184 canvas.translate(-transX, -transY);
11185 canvas.concat(getMatrix());
11186 canvas.translate(transX, transY);
11187 }
11188 }
11189
11190 if (alpha < 1.0f) {
11191 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11192 if (hasNoCache) {
11193 final int multipliedAlpha = (int) (255 * alpha);
11194 if (!onSetAlpha(multipliedAlpha)) {
11195 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11196 if ((flags & parent.FLAG_CLIP_CHILDREN) == parent.FLAG_CLIP_CHILDREN ||
11197 layerType != LAYER_TYPE_NONE) {
11198 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
11199 }
11200 if (layerType == LAYER_TYPE_NONE) {
11201 final int scrollX = hasDisplayList ? 0 : sx;
11202 final int scrollY = hasDisplayList ? 0 : sy;
Chet Haasebcca79a2012-02-14 08:45:14 -080011203 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
11204 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080011205 }
11206 } else {
11207 // Alpha is handled by the child directly, clobber the layer's alpha
11208 mPrivateFlags |= ALPHA_SET;
11209 }
11210 }
11211 }
11212 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11213 onSetAlpha(255);
11214 mPrivateFlags &= ~ALPHA_SET;
11215 }
11216
11217 if ((flags & parent.FLAG_CLIP_CHILDREN) == parent.FLAG_CLIP_CHILDREN) {
11218 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011219 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080011220 } else {
11221 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011222 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080011223 } else {
11224 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
11225 }
11226 }
11227 }
11228
11229 if (hasDisplayList) {
11230 displayList = getDisplayList();
11231 if (!displayList.isValid()) {
11232 // Uncommon, but possible. If a view is removed from the hierarchy during the call
11233 // to getDisplayList(), the display list will be marked invalid and we should not
11234 // try to use it again.
11235 displayList = null;
11236 hasDisplayList = false;
11237 }
11238 }
11239
11240 if (hasNoCache) {
11241 boolean layerRendered = false;
11242 if (layerType == LAYER_TYPE_HARDWARE) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011243 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080011244 if (layer != null && layer.isValid()) {
11245 mLayerPaint.setAlpha((int) (alpha * 255));
11246 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
11247 layerRendered = true;
11248 } else {
11249 final int scrollX = hasDisplayList ? 0 : sx;
11250 final int scrollY = hasDisplayList ? 0 : sy;
11251 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080011252 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080011253 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11254 }
11255 }
11256
11257 if (!layerRendered) {
11258 if (!hasDisplayList) {
11259 // Fast path for layouts with no backgrounds
11260 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11261 if (ViewDebug.TRACE_HIERARCHY) {
11262 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
11263 }
11264 mPrivateFlags &= ~DIRTY_MASK;
11265 dispatchDraw(canvas);
11266 } else {
11267 draw(canvas);
11268 }
11269 } else {
11270 mPrivateFlags &= ~DIRTY_MASK;
Chet Haasebcca79a2012-02-14 08:45:14 -080011271 ((HardwareCanvas) canvas).drawDisplayList(displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -080011272 mRight - mLeft, mBottom - mTop, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080011273 }
11274 }
11275 } else if (cache != null) {
11276 mPrivateFlags &= ~DIRTY_MASK;
11277 Paint cachePaint;
11278
11279 if (layerType == LAYER_TYPE_NONE) {
11280 cachePaint = parent.mCachePaint;
11281 if (cachePaint == null) {
11282 cachePaint = new Paint();
11283 cachePaint.setDither(false);
11284 parent.mCachePaint = cachePaint;
11285 }
11286 if (alpha < 1.0f) {
11287 cachePaint.setAlpha((int) (alpha * 255));
11288 parent.mGroupFlags |= parent.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haasebcca79a2012-02-14 08:45:14 -080011289 } else if ((flags & parent.FLAG_ALPHA_LOWER_THAN_ONE) ==
11290 parent.FLAG_ALPHA_LOWER_THAN_ONE) {
Chet Haase64a48c12012-02-13 16:33:29 -080011291 cachePaint.setAlpha(255);
11292 parent.mGroupFlags &= ~parent.FLAG_ALPHA_LOWER_THAN_ONE;
11293 }
11294 } else {
11295 cachePaint = mLayerPaint;
11296 cachePaint.setAlpha((int) (alpha * 255));
11297 }
11298 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
11299 }
11300
11301 canvas.restoreToCount(restoreTo);
11302
11303 if (a != null && !more) {
11304 if (!hardwareAccelerated && !a.getFillAfter()) {
11305 onSetAlpha(255);
11306 }
11307 parent.finishAnimatingView(this, a);
11308 }
11309
11310 if (more && hardwareAccelerated) {
11311 // invalidation is the trigger to recreate display lists, so if we're using
11312 // display lists to render, force an invalidate to allow the animation to
11313 // continue drawing another frame
11314 parent.invalidate(true);
11315 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11316 // alpha animations should cause the child to recreate its display list
11317 invalidate(true);
11318 }
11319 }
11320
11321 mRecreateDisplayList = false;
11322
11323 return more;
11324 }
11325
11326 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011327 * Manually render this view (and all of its children) to the given Canvas.
11328 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011329 * called. When implementing a view, implement
11330 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
11331 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011332 *
11333 * @param canvas The Canvas to which the View is rendered.
11334 */
11335 public void draw(Canvas canvas) {
11336 if (ViewDebug.TRACE_HIERARCHY) {
11337 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
11338 }
11339
Romain Guy5bcdff42009-05-14 21:27:18 -070011340 final int privateFlags = mPrivateFlags;
11341 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
11342 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
11343 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070011344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011345 /*
11346 * Draw traversal performs several drawing steps which must be executed
11347 * in the appropriate order:
11348 *
11349 * 1. Draw the background
11350 * 2. If necessary, save the canvas' layers to prepare for fading
11351 * 3. Draw view's content
11352 * 4. Draw children
11353 * 5. If necessary, draw the fading edges and restore layers
11354 * 6. Draw decorations (scrollbars for instance)
11355 */
11356
11357 // Step 1, draw the background, if needed
11358 int saveCount;
11359
Romain Guy24443ea2009-05-11 11:56:30 -070011360 if (!dirtyOpaque) {
11361 final Drawable background = mBGDrawable;
11362 if (background != null) {
11363 final int scrollX = mScrollX;
11364 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011365
Romain Guy24443ea2009-05-11 11:56:30 -070011366 if (mBackgroundSizeChanged) {
11367 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
11368 mBackgroundSizeChanged = false;
11369 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011370
Romain Guy24443ea2009-05-11 11:56:30 -070011371 if ((scrollX | scrollY) == 0) {
11372 background.draw(canvas);
11373 } else {
11374 canvas.translate(scrollX, scrollY);
11375 background.draw(canvas);
11376 canvas.translate(-scrollX, -scrollY);
11377 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011378 }
11379 }
11380
11381 // skip step 2 & 5 if possible (common case)
11382 final int viewFlags = mViewFlags;
11383 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
11384 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
11385 if (!verticalEdges && !horizontalEdges) {
11386 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011387 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011388
11389 // Step 4, draw the children
11390 dispatchDraw(canvas);
11391
11392 // Step 6, draw decorations (scrollbars)
11393 onDrawScrollBars(canvas);
11394
11395 // we're done...
11396 return;
11397 }
11398
11399 /*
11400 * Here we do the full fledged routine...
11401 * (this is an uncommon case where speed matters less,
11402 * this is why we repeat some of the tests that have been
11403 * done above)
11404 */
11405
11406 boolean drawTop = false;
11407 boolean drawBottom = false;
11408 boolean drawLeft = false;
11409 boolean drawRight = false;
11410
11411 float topFadeStrength = 0.0f;
11412 float bottomFadeStrength = 0.0f;
11413 float leftFadeStrength = 0.0f;
11414 float rightFadeStrength = 0.0f;
11415
11416 // Step 2, save the canvas' layers
11417 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011418
11419 final boolean offsetRequired = isPaddingOffsetRequired();
11420 if (offsetRequired) {
11421 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011422 }
11423
11424 int left = mScrollX + paddingLeft;
11425 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070011426 int top = mScrollY + getFadeTop(offsetRequired);
11427 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011428
11429 if (offsetRequired) {
11430 right += getRightPaddingOffset();
11431 bottom += getBottomPaddingOffset();
11432 }
11433
11434 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011435 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
11436 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011437
11438 // clip the fade length if top and bottom fades overlap
11439 // overlapping fades produce odd-looking artifacts
11440 if (verticalEdges && (top + length > bottom - length)) {
11441 length = (bottom - top) / 2;
11442 }
11443
11444 // also clip horizontal fades if necessary
11445 if (horizontalEdges && (left + length > right - length)) {
11446 length = (right - left) / 2;
11447 }
11448
11449 if (verticalEdges) {
11450 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011451 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011452 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011453 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011454 }
11455
11456 if (horizontalEdges) {
11457 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011458 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011459 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011460 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011461 }
11462
11463 saveCount = canvas.getSaveCount();
11464
11465 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070011466 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011467 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11468
11469 if (drawTop) {
11470 canvas.saveLayer(left, top, right, top + length, null, flags);
11471 }
11472
11473 if (drawBottom) {
11474 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
11475 }
11476
11477 if (drawLeft) {
11478 canvas.saveLayer(left, top, left + length, bottom, null, flags);
11479 }
11480
11481 if (drawRight) {
11482 canvas.saveLayer(right - length, top, right, bottom, null, flags);
11483 }
11484 } else {
11485 scrollabilityCache.setFadeColor(solidColor);
11486 }
11487
11488 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011489 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011490
11491 // Step 4, draw the children
11492 dispatchDraw(canvas);
11493
11494 // Step 5, draw the fade effect and restore layers
11495 final Paint p = scrollabilityCache.paint;
11496 final Matrix matrix = scrollabilityCache.matrix;
11497 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011498
11499 if (drawTop) {
11500 matrix.setScale(1, fadeHeight * topFadeStrength);
11501 matrix.postTranslate(left, top);
11502 fade.setLocalMatrix(matrix);
11503 canvas.drawRect(left, top, right, top + length, p);
11504 }
11505
11506 if (drawBottom) {
11507 matrix.setScale(1, fadeHeight * bottomFadeStrength);
11508 matrix.postRotate(180);
11509 matrix.postTranslate(left, bottom);
11510 fade.setLocalMatrix(matrix);
11511 canvas.drawRect(left, bottom - length, right, bottom, p);
11512 }
11513
11514 if (drawLeft) {
11515 matrix.setScale(1, fadeHeight * leftFadeStrength);
11516 matrix.postRotate(-90);
11517 matrix.postTranslate(left, top);
11518 fade.setLocalMatrix(matrix);
11519 canvas.drawRect(left, top, left + length, bottom, p);
11520 }
11521
11522 if (drawRight) {
11523 matrix.setScale(1, fadeHeight * rightFadeStrength);
11524 matrix.postRotate(90);
11525 matrix.postTranslate(right, top);
11526 fade.setLocalMatrix(matrix);
11527 canvas.drawRect(right - length, top, right, bottom, p);
11528 }
11529
11530 canvas.restoreToCount(saveCount);
11531
11532 // Step 6, draw decorations (scrollbars)
11533 onDrawScrollBars(canvas);
11534 }
11535
11536 /**
11537 * Override this if your view is known to always be drawn on top of a solid color background,
11538 * and needs to draw fading edges. Returning a non-zero color enables the view system to
11539 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
11540 * should be set to 0xFF.
11541 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011542 * @see #setVerticalFadingEdgeEnabled(boolean)
11543 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011544 *
11545 * @return The known solid color background for this view, or 0 if the color may vary
11546 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011547 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011548 public int getSolidColor() {
11549 return 0;
11550 }
11551
11552 /**
11553 * Build a human readable string representation of the specified view flags.
11554 *
11555 * @param flags the view flags to convert to a string
11556 * @return a String representing the supplied flags
11557 */
11558 private static String printFlags(int flags) {
11559 String output = "";
11560 int numFlags = 0;
11561 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
11562 output += "TAKES_FOCUS";
11563 numFlags++;
11564 }
11565
11566 switch (flags & VISIBILITY_MASK) {
11567 case INVISIBLE:
11568 if (numFlags > 0) {
11569 output += " ";
11570 }
11571 output += "INVISIBLE";
11572 // USELESS HERE numFlags++;
11573 break;
11574 case GONE:
11575 if (numFlags > 0) {
11576 output += " ";
11577 }
11578 output += "GONE";
11579 // USELESS HERE numFlags++;
11580 break;
11581 default:
11582 break;
11583 }
11584 return output;
11585 }
11586
11587 /**
11588 * Build a human readable string representation of the specified private
11589 * view flags.
11590 *
11591 * @param privateFlags the private view flags to convert to a string
11592 * @return a String representing the supplied flags
11593 */
11594 private static String printPrivateFlags(int privateFlags) {
11595 String output = "";
11596 int numFlags = 0;
11597
11598 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
11599 output += "WANTS_FOCUS";
11600 numFlags++;
11601 }
11602
11603 if ((privateFlags & FOCUSED) == FOCUSED) {
11604 if (numFlags > 0) {
11605 output += " ";
11606 }
11607 output += "FOCUSED";
11608 numFlags++;
11609 }
11610
11611 if ((privateFlags & SELECTED) == SELECTED) {
11612 if (numFlags > 0) {
11613 output += " ";
11614 }
11615 output += "SELECTED";
11616 numFlags++;
11617 }
11618
11619 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
11620 if (numFlags > 0) {
11621 output += " ";
11622 }
11623 output += "IS_ROOT_NAMESPACE";
11624 numFlags++;
11625 }
11626
11627 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
11628 if (numFlags > 0) {
11629 output += " ";
11630 }
11631 output += "HAS_BOUNDS";
11632 numFlags++;
11633 }
11634
11635 if ((privateFlags & DRAWN) == DRAWN) {
11636 if (numFlags > 0) {
11637 output += " ";
11638 }
11639 output += "DRAWN";
11640 // USELESS HERE numFlags++;
11641 }
11642 return output;
11643 }
11644
11645 /**
11646 * <p>Indicates whether or not this view's layout will be requested during
11647 * the next hierarchy layout pass.</p>
11648 *
11649 * @return true if the layout will be forced during next layout pass
11650 */
11651 public boolean isLayoutRequested() {
11652 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
11653 }
11654
11655 /**
11656 * Assign a size and position to a view and all of its
11657 * descendants
11658 *
11659 * <p>This is the second phase of the layout mechanism.
11660 * (The first is measuring). In this phase, each parent calls
11661 * layout on all of its children to position them.
11662 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080011663 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011664 *
Chet Haase9c087442011-01-12 16:20:16 -080011665 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011666 * Derived classes with children should override
11667 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080011668 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011669 *
11670 * @param l Left position, relative to parent
11671 * @param t Top position, relative to parent
11672 * @param r Right position, relative to parent
11673 * @param b Bottom position, relative to parent
11674 */
Romain Guy5429e1d2010-09-07 12:38:00 -070011675 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080011676 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070011677 int oldL = mLeft;
11678 int oldT = mTop;
11679 int oldB = mBottom;
11680 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011681 boolean changed = setFrame(l, t, r, b);
11682 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
11683 if (ViewDebug.TRACE_HIERARCHY) {
11684 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
11685 }
11686
11687 onLayout(changed, l, t, r, b);
11688 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070011689
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011690 ListenerInfo li = mListenerInfo;
11691 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070011692 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011693 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070011694 int numListeners = listenersCopy.size();
11695 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070011696 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070011697 }
11698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011699 }
11700 mPrivateFlags &= ~FORCE_LAYOUT;
11701 }
11702
11703 /**
11704 * Called from layout when this view should
11705 * assign a size and position to each of its children.
11706 *
11707 * Derived classes with children should override
11708 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070011709 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011710 * @param changed This is a new size or position for this view
11711 * @param left Left position, relative to parent
11712 * @param top Top position, relative to parent
11713 * @param right Right position, relative to parent
11714 * @param bottom Bottom position, relative to parent
11715 */
11716 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
11717 }
11718
11719 /**
11720 * Assign a size and position to this view.
11721 *
11722 * This is called from layout.
11723 *
11724 * @param left Left position, relative to parent
11725 * @param top Top position, relative to parent
11726 * @param right Right position, relative to parent
11727 * @param bottom Bottom position, relative to parent
11728 * @return true if the new size and position are different than the
11729 * previous ones
11730 * {@hide}
11731 */
11732 protected boolean setFrame(int left, int top, int right, int bottom) {
11733 boolean changed = false;
11734
11735 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070011736 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011737 + right + "," + bottom + ")");
11738 }
11739
11740 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
11741 changed = true;
11742
11743 // Remember our drawn bit
11744 int drawn = mPrivateFlags & DRAWN;
11745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011746 int oldWidth = mRight - mLeft;
11747 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070011748 int newWidth = right - left;
11749 int newHeight = bottom - top;
11750 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
11751
11752 // Invalidate our old position
11753 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011754
11755 mLeft = left;
11756 mTop = top;
11757 mRight = right;
11758 mBottom = bottom;
11759
11760 mPrivateFlags |= HAS_BOUNDS;
11761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011762
Chet Haase75755e22011-07-18 17:48:25 -070011763 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011764 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
11765 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070011766 if (mTransformationInfo != null) {
11767 mTransformationInfo.mMatrixDirty = true;
11768 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011769 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011770 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
11771 }
11772
11773 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
11774 // If we are visible, force the DRAWN bit to on so that
11775 // this invalidate will go through (at least to our parent).
11776 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011777 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011778 // the DRAWN bit.
11779 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070011780 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080011781 // parent display list may need to be recreated based on a change in the bounds
11782 // of any child
11783 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011784 }
11785
11786 // Reset drawn bit to original value (invalidate turns it off)
11787 mPrivateFlags |= drawn;
11788
11789 mBackgroundSizeChanged = true;
11790 }
11791 return changed;
11792 }
11793
11794 /**
11795 * Finalize inflating a view from XML. This is called as the last phase
11796 * of inflation, after all child views have been added.
11797 *
11798 * <p>Even if the subclass overrides onFinishInflate, they should always be
11799 * sure to call the super method, so that we get called.
11800 */
11801 protected void onFinishInflate() {
11802 }
11803
11804 /**
11805 * Returns the resources associated with this view.
11806 *
11807 * @return Resources object.
11808 */
11809 public Resources getResources() {
11810 return mResources;
11811 }
11812
11813 /**
11814 * Invalidates the specified Drawable.
11815 *
11816 * @param drawable the drawable to invalidate
11817 */
11818 public void invalidateDrawable(Drawable drawable) {
11819 if (verifyDrawable(drawable)) {
11820 final Rect dirty = drawable.getBounds();
11821 final int scrollX = mScrollX;
11822 final int scrollY = mScrollY;
11823
11824 invalidate(dirty.left + scrollX, dirty.top + scrollY,
11825 dirty.right + scrollX, dirty.bottom + scrollY);
11826 }
11827 }
11828
11829 /**
11830 * Schedules an action on a drawable to occur at a specified time.
11831 *
11832 * @param who the recipient of the action
11833 * @param what the action to run on the drawable
11834 * @param when the time at which the action must occur. Uses the
11835 * {@link SystemClock#uptimeMillis} timebase.
11836 */
11837 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080011838 if (verifyDrawable(who) && what != null) {
11839 if (mAttachInfo != null) {
11840 mAttachInfo.mHandler.postAtTime(what, who, when);
11841 } else {
11842 ViewRootImpl.getRunQueue().postDelayed(what, when - SystemClock.uptimeMillis());
11843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011844 }
11845 }
11846
11847 /**
11848 * Cancels a scheduled action on a drawable.
11849 *
11850 * @param who the recipient of the action
11851 * @param what the action to cancel
11852 */
11853 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080011854 if (verifyDrawable(who) && what != null) {
11855 if (mAttachInfo != null) {
11856 mAttachInfo.mHandler.removeCallbacks(what, who);
11857 } else {
11858 ViewRootImpl.getRunQueue().removeCallbacks(what);
11859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011860 }
11861 }
11862
11863 /**
11864 * Unschedule any events associated with the given Drawable. This can be
11865 * used when selecting a new Drawable into a view, so that the previous
11866 * one is completely unscheduled.
11867 *
11868 * @param who The Drawable to unschedule.
11869 *
11870 * @see #drawableStateChanged
11871 */
11872 public void unscheduleDrawable(Drawable who) {
11873 if (mAttachInfo != null) {
11874 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
11875 }
11876 }
11877
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011878 /**
11879 * Return the layout direction of a given Drawable.
11880 *
11881 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011882 */
11883 public int getResolvedLayoutDirection(Drawable who) {
11884 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070011885 }
11886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011887 /**
11888 * If your view subclass is displaying its own Drawable objects, it should
11889 * override this function and return true for any Drawable it is
11890 * displaying. This allows animations for those drawables to be
11891 * scheduled.
11892 *
11893 * <p>Be sure to call through to the super class when overriding this
11894 * function.
11895 *
11896 * @param who The Drawable to verify. Return true if it is one you are
11897 * displaying, else return the result of calling through to the
11898 * super class.
11899 *
11900 * @return boolean If true than the Drawable is being displayed in the
11901 * view; else false and it is not allowed to animate.
11902 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011903 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
11904 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011905 */
11906 protected boolean verifyDrawable(Drawable who) {
11907 return who == mBGDrawable;
11908 }
11909
11910 /**
11911 * This function is called whenever the state of the view changes in such
11912 * a way that it impacts the state of drawables being shown.
11913 *
11914 * <p>Be sure to call through to the superclass when overriding this
11915 * function.
11916 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011917 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011918 */
11919 protected void drawableStateChanged() {
11920 Drawable d = mBGDrawable;
11921 if (d != null && d.isStateful()) {
11922 d.setState(getDrawableState());
11923 }
11924 }
11925
11926 /**
11927 * Call this to force a view to update its drawable state. This will cause
11928 * drawableStateChanged to be called on this view. Views that are interested
11929 * in the new state should call getDrawableState.
11930 *
11931 * @see #drawableStateChanged
11932 * @see #getDrawableState
11933 */
11934 public void refreshDrawableState() {
11935 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
11936 drawableStateChanged();
11937
11938 ViewParent parent = mParent;
11939 if (parent != null) {
11940 parent.childDrawableStateChanged(this);
11941 }
11942 }
11943
11944 /**
11945 * Return an array of resource IDs of the drawable states representing the
11946 * current state of the view.
11947 *
11948 * @return The current drawable state
11949 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011950 * @see Drawable#setState(int[])
11951 * @see #drawableStateChanged()
11952 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011953 */
11954 public final int[] getDrawableState() {
11955 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
11956 return mDrawableState;
11957 } else {
11958 mDrawableState = onCreateDrawableState(0);
11959 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
11960 return mDrawableState;
11961 }
11962 }
11963
11964 /**
11965 * Generate the new {@link android.graphics.drawable.Drawable} state for
11966 * this view. This is called by the view
11967 * system when the cached Drawable state is determined to be invalid. To
11968 * retrieve the current state, you should use {@link #getDrawableState}.
11969 *
11970 * @param extraSpace if non-zero, this is the number of extra entries you
11971 * would like in the returned array in which you can place your own
11972 * states.
11973 *
11974 * @return Returns an array holding the current {@link Drawable} state of
11975 * the view.
11976 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011977 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011978 */
11979 protected int[] onCreateDrawableState(int extraSpace) {
11980 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
11981 mParent instanceof View) {
11982 return ((View) mParent).onCreateDrawableState(extraSpace);
11983 }
11984
11985 int[] drawableState;
11986
11987 int privateFlags = mPrivateFlags;
11988
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011989 int viewStateIndex = 0;
11990 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
11991 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
11992 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070011993 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011994 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
11995 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070011996 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
11997 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011998 // This is set if HW acceleration is requested, even if the current
11999 // process doesn't allow it. This is just to allow app preview
12000 // windows to better match their app.
12001 viewStateIndex |= VIEW_STATE_ACCELERATED;
12002 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070012003 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012004
Christopher Tate3d4bf172011-03-28 16:16:46 -070012005 final int privateFlags2 = mPrivateFlags2;
12006 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
12007 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
12008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012009 drawableState = VIEW_STATE_SETS[viewStateIndex];
12010
12011 //noinspection ConstantIfStatement
12012 if (false) {
12013 Log.i("View", "drawableStateIndex=" + viewStateIndex);
12014 Log.i("View", toString()
12015 + " pressed=" + ((privateFlags & PRESSED) != 0)
12016 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
12017 + " fo=" + hasFocus()
12018 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012019 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012020 + ": " + Arrays.toString(drawableState));
12021 }
12022
12023 if (extraSpace == 0) {
12024 return drawableState;
12025 }
12026
12027 final int[] fullState;
12028 if (drawableState != null) {
12029 fullState = new int[drawableState.length + extraSpace];
12030 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
12031 } else {
12032 fullState = new int[extraSpace];
12033 }
12034
12035 return fullState;
12036 }
12037
12038 /**
12039 * Merge your own state values in <var>additionalState</var> into the base
12040 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012041 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012042 *
12043 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012044 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012045 * own additional state values.
12046 *
12047 * @param additionalState The additional state values you would like
12048 * added to <var>baseState</var>; this array is not modified.
12049 *
12050 * @return As a convenience, the <var>baseState</var> array you originally
12051 * passed into the function is returned.
12052 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012053 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012054 */
12055 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
12056 final int N = baseState.length;
12057 int i = N - 1;
12058 while (i >= 0 && baseState[i] == 0) {
12059 i--;
12060 }
12061 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
12062 return baseState;
12063 }
12064
12065 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070012066 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
12067 * on all Drawable objects associated with this view.
12068 */
12069 public void jumpDrawablesToCurrentState() {
12070 if (mBGDrawable != null) {
12071 mBGDrawable.jumpToCurrentState();
12072 }
12073 }
12074
12075 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012076 * Sets the background color for this view.
12077 * @param color the color of the background
12078 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012079 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012080 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070012081 if (mBGDrawable instanceof ColorDrawable) {
12082 ((ColorDrawable) mBGDrawable).setColor(color);
12083 } else {
12084 setBackgroundDrawable(new ColorDrawable(color));
12085 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012086 }
12087
12088 /**
12089 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070012090 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012091 * @param resid The identifier of the resource.
12092 * @attr ref android.R.styleable#View_background
12093 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012094 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012095 public void setBackgroundResource(int resid) {
12096 if (resid != 0 && resid == mBackgroundResource) {
12097 return;
12098 }
12099
12100 Drawable d= null;
12101 if (resid != 0) {
12102 d = mResources.getDrawable(resid);
12103 }
12104 setBackgroundDrawable(d);
12105
12106 mBackgroundResource = resid;
12107 }
12108
12109 /**
12110 * Set the background to a given Drawable, or remove the background. If the
12111 * background has padding, this View's padding is set to the background's
12112 * padding. However, when a background is removed, this View's padding isn't
12113 * touched. If setting the padding is desired, please use
12114 * {@link #setPadding(int, int, int, int)}.
12115 *
12116 * @param d The Drawable to use as the background, or null to remove the
12117 * background
12118 */
12119 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070012120 if (d == mBGDrawable) {
12121 return;
12122 }
12123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012124 boolean requestLayout = false;
12125
12126 mBackgroundResource = 0;
12127
12128 /*
12129 * Regardless of whether we're setting a new background or not, we want
12130 * to clear the previous drawable.
12131 */
12132 if (mBGDrawable != null) {
12133 mBGDrawable.setCallback(null);
12134 unscheduleDrawable(mBGDrawable);
12135 }
12136
12137 if (d != null) {
12138 Rect padding = sThreadLocal.get();
12139 if (padding == null) {
12140 padding = new Rect();
12141 sThreadLocal.set(padding);
12142 }
12143 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012144 switch (d.getResolvedLayoutDirectionSelf()) {
12145 case LAYOUT_DIRECTION_RTL:
12146 setPadding(padding.right, padding.top, padding.left, padding.bottom);
12147 break;
12148 case LAYOUT_DIRECTION_LTR:
12149 default:
12150 setPadding(padding.left, padding.top, padding.right, padding.bottom);
12151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012152 }
12153
12154 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
12155 // if it has a different minimum size, we should layout again
12156 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
12157 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
12158 requestLayout = true;
12159 }
12160
12161 d.setCallback(this);
12162 if (d.isStateful()) {
12163 d.setState(getDrawableState());
12164 }
12165 d.setVisible(getVisibility() == VISIBLE, false);
12166 mBGDrawable = d;
12167
12168 if ((mPrivateFlags & SKIP_DRAW) != 0) {
12169 mPrivateFlags &= ~SKIP_DRAW;
12170 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
12171 requestLayout = true;
12172 }
12173 } else {
12174 /* Remove the background */
12175 mBGDrawable = null;
12176
12177 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
12178 /*
12179 * This view ONLY drew the background before and we're removing
12180 * the background, so now it won't draw anything
12181 * (hence we SKIP_DRAW)
12182 */
12183 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
12184 mPrivateFlags |= SKIP_DRAW;
12185 }
12186
12187 /*
12188 * When the background is set, we try to apply its padding to this
12189 * View. When the background is removed, we don't touch this View's
12190 * padding. This is noted in the Javadocs. Hence, we don't need to
12191 * requestLayout(), the invalidate() below is sufficient.
12192 */
12193
12194 // The old background's minimum size could have affected this
12195 // View's layout, so let's requestLayout
12196 requestLayout = true;
12197 }
12198
Romain Guy8f1344f52009-05-15 16:03:59 -070012199 computeOpaqueFlags();
12200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012201 if (requestLayout) {
12202 requestLayout();
12203 }
12204
12205 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012206 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012207 }
12208
12209 /**
12210 * Gets the background drawable
12211 * @return The drawable used as the background for this view, if any.
12212 */
12213 public Drawable getBackground() {
12214 return mBGDrawable;
12215 }
12216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012217 /**
12218 * Sets the padding. The view may add on the space required to display
12219 * the scrollbars, depending on the style and visibility of the scrollbars.
12220 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
12221 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
12222 * from the values set in this call.
12223 *
12224 * @attr ref android.R.styleable#View_padding
12225 * @attr ref android.R.styleable#View_paddingBottom
12226 * @attr ref android.R.styleable#View_paddingLeft
12227 * @attr ref android.R.styleable#View_paddingRight
12228 * @attr ref android.R.styleable#View_paddingTop
12229 * @param left the left padding in pixels
12230 * @param top the top padding in pixels
12231 * @param right the right padding in pixels
12232 * @param bottom the bottom padding in pixels
12233 */
12234 public void setPadding(int left, int top, int right, int bottom) {
12235 boolean changed = false;
12236
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012237 mUserPaddingRelative = false;
12238
Adam Powell20232d02010-12-08 21:08:53 -080012239 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012240 mUserPaddingRight = right;
12241 mUserPaddingBottom = bottom;
12242
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012243 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070012244
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012245 // Common case is there are no scroll bars.
12246 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012247 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080012248 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012249 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080012250 switch (mVerticalScrollbarPosition) {
12251 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012252 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
12253 left += offset;
12254 } else {
12255 right += offset;
12256 }
12257 break;
Adam Powell20232d02010-12-08 21:08:53 -080012258 case SCROLLBAR_POSITION_RIGHT:
12259 right += offset;
12260 break;
12261 case SCROLLBAR_POSITION_LEFT:
12262 left += offset;
12263 break;
12264 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012265 }
Adam Powell20232d02010-12-08 21:08:53 -080012266 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012267 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
12268 ? 0 : getHorizontalScrollbarHeight();
12269 }
12270 }
Romain Guy8506ab42009-06-11 17:35:47 -070012271
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012272 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012273 changed = true;
12274 mPaddingLeft = left;
12275 }
12276 if (mPaddingTop != top) {
12277 changed = true;
12278 mPaddingTop = top;
12279 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012280 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012281 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012282 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012283 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012284 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012285 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012286 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012287 }
12288
12289 if (changed) {
12290 requestLayout();
12291 }
12292 }
12293
12294 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012295 * Sets the relative padding. The view may add on the space required to display
12296 * the scrollbars, depending on the style and visibility of the scrollbars.
12297 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
12298 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
12299 * from the values set in this call.
12300 *
12301 * @attr ref android.R.styleable#View_padding
12302 * @attr ref android.R.styleable#View_paddingBottom
12303 * @attr ref android.R.styleable#View_paddingStart
12304 * @attr ref android.R.styleable#View_paddingEnd
12305 * @attr ref android.R.styleable#View_paddingTop
12306 * @param start the start padding in pixels
12307 * @param top the top padding in pixels
12308 * @param end the end padding in pixels
12309 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012310 */
12311 public void setPaddingRelative(int start, int top, int end, int bottom) {
12312 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070012313
12314 mUserPaddingStart = start;
12315 mUserPaddingEnd = end;
12316
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012317 switch(getResolvedLayoutDirection()) {
12318 case LAYOUT_DIRECTION_RTL:
12319 setPadding(end, top, start, bottom);
12320 break;
12321 case LAYOUT_DIRECTION_LTR:
12322 default:
12323 setPadding(start, top, end, bottom);
12324 }
12325 }
12326
12327 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012328 * Returns the top padding of this view.
12329 *
12330 * @return the top padding in pixels
12331 */
12332 public int getPaddingTop() {
12333 return mPaddingTop;
12334 }
12335
12336 /**
12337 * Returns the bottom padding of this view. If there are inset and enabled
12338 * scrollbars, this value may include the space required to display the
12339 * scrollbars as well.
12340 *
12341 * @return the bottom padding in pixels
12342 */
12343 public int getPaddingBottom() {
12344 return mPaddingBottom;
12345 }
12346
12347 /**
12348 * Returns the left padding of this view. If there are inset and enabled
12349 * scrollbars, this value may include the space required to display the
12350 * scrollbars as well.
12351 *
12352 * @return the left padding in pixels
12353 */
12354 public int getPaddingLeft() {
12355 return mPaddingLeft;
12356 }
12357
12358 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012359 * Returns the start padding of this view. If there are inset and enabled
12360 * scrollbars, this value may include the space required to display the
12361 * scrollbars as well.
12362 *
12363 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012364 */
12365 public int getPaddingStart() {
12366 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12367 mPaddingRight : mPaddingLeft;
12368 }
12369
12370 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012371 * Returns the right padding of this view. If there are inset and enabled
12372 * scrollbars, this value may include the space required to display the
12373 * scrollbars as well.
12374 *
12375 * @return the right padding in pixels
12376 */
12377 public int getPaddingRight() {
12378 return mPaddingRight;
12379 }
12380
12381 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012382 * Returns the end padding of this view. If there are inset and enabled
12383 * scrollbars, this value may include the space required to display the
12384 * scrollbars as well.
12385 *
12386 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012387 */
12388 public int getPaddingEnd() {
12389 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12390 mPaddingLeft : mPaddingRight;
12391 }
12392
12393 /**
12394 * Return if the padding as been set thru relative values
12395 * {@link #setPaddingRelative(int, int, int, int)} or thru
12396 * @attr ref android.R.styleable#View_paddingStart or
12397 * @attr ref android.R.styleable#View_paddingEnd
12398 *
12399 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012400 */
12401 public boolean isPaddingRelative() {
12402 return mUserPaddingRelative;
12403 }
12404
12405 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012406 * Changes the selection state of this view. A view can be selected or not.
12407 * Note that selection is not the same as focus. Views are typically
12408 * selected in the context of an AdapterView like ListView or GridView;
12409 * the selected view is the view that is highlighted.
12410 *
12411 * @param selected true if the view must be selected, false otherwise
12412 */
12413 public void setSelected(boolean selected) {
12414 if (((mPrivateFlags & SELECTED) != 0) != selected) {
12415 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070012416 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080012417 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012418 refreshDrawableState();
12419 dispatchSetSelected(selected);
12420 }
12421 }
12422
12423 /**
12424 * Dispatch setSelected to all of this View's children.
12425 *
12426 * @see #setSelected(boolean)
12427 *
12428 * @param selected The new selected state
12429 */
12430 protected void dispatchSetSelected(boolean selected) {
12431 }
12432
12433 /**
12434 * Indicates the selection state of this view.
12435 *
12436 * @return true if the view is selected, false otherwise
12437 */
12438 @ViewDebug.ExportedProperty
12439 public boolean isSelected() {
12440 return (mPrivateFlags & SELECTED) != 0;
12441 }
12442
12443 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012444 * Changes the activated state of this view. A view can be activated or not.
12445 * Note that activation is not the same as selection. Selection is
12446 * a transient property, representing the view (hierarchy) the user is
12447 * currently interacting with. Activation is a longer-term state that the
12448 * user can move views in and out of. For example, in a list view with
12449 * single or multiple selection enabled, the views in the current selection
12450 * set are activated. (Um, yeah, we are deeply sorry about the terminology
12451 * here.) The activated state is propagated down to children of the view it
12452 * is set on.
12453 *
12454 * @param activated true if the view must be activated, false otherwise
12455 */
12456 public void setActivated(boolean activated) {
12457 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
12458 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012459 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012460 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070012461 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012462 }
12463 }
12464
12465 /**
12466 * Dispatch setActivated to all of this View's children.
12467 *
12468 * @see #setActivated(boolean)
12469 *
12470 * @param activated The new activated state
12471 */
12472 protected void dispatchSetActivated(boolean activated) {
12473 }
12474
12475 /**
12476 * Indicates the activation state of this view.
12477 *
12478 * @return true if the view is activated, false otherwise
12479 */
12480 @ViewDebug.ExportedProperty
12481 public boolean isActivated() {
12482 return (mPrivateFlags & ACTIVATED) != 0;
12483 }
12484
12485 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012486 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
12487 * observer can be used to get notifications when global events, like
12488 * layout, happen.
12489 *
12490 * The returned ViewTreeObserver observer is not guaranteed to remain
12491 * valid for the lifetime of this View. If the caller of this method keeps
12492 * a long-lived reference to ViewTreeObserver, it should always check for
12493 * the return value of {@link ViewTreeObserver#isAlive()}.
12494 *
12495 * @return The ViewTreeObserver for this view's hierarchy.
12496 */
12497 public ViewTreeObserver getViewTreeObserver() {
12498 if (mAttachInfo != null) {
12499 return mAttachInfo.mTreeObserver;
12500 }
12501 if (mFloatingTreeObserver == null) {
12502 mFloatingTreeObserver = new ViewTreeObserver();
12503 }
12504 return mFloatingTreeObserver;
12505 }
12506
12507 /**
12508 * <p>Finds the topmost view in the current view hierarchy.</p>
12509 *
12510 * @return the topmost view containing this view
12511 */
12512 public View getRootView() {
12513 if (mAttachInfo != null) {
12514 final View v = mAttachInfo.mRootView;
12515 if (v != null) {
12516 return v;
12517 }
12518 }
Romain Guy8506ab42009-06-11 17:35:47 -070012519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012520 View parent = this;
12521
12522 while (parent.mParent != null && parent.mParent instanceof View) {
12523 parent = (View) parent.mParent;
12524 }
12525
12526 return parent;
12527 }
12528
12529 /**
12530 * <p>Computes the coordinates of this view on the screen. The argument
12531 * must be an array of two integers. After the method returns, the array
12532 * contains the x and y location in that order.</p>
12533 *
12534 * @param location an array of two integers in which to hold the coordinates
12535 */
12536 public void getLocationOnScreen(int[] location) {
12537 getLocationInWindow(location);
12538
12539 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070012540 if (info != null) {
12541 location[0] += info.mWindowLeft;
12542 location[1] += info.mWindowTop;
12543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012544 }
12545
12546 /**
12547 * <p>Computes the coordinates of this view in its window. The argument
12548 * must be an array of two integers. After the method returns, the array
12549 * contains the x and y location in that order.</p>
12550 *
12551 * @param location an array of two integers in which to hold the coordinates
12552 */
12553 public void getLocationInWindow(int[] location) {
12554 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012555 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012556 }
12557
Gilles Debunne6583ce52011-12-06 18:09:02 -080012558 if (mAttachInfo == null) {
12559 // When the view is not attached to a window, this method does not make sense
12560 location[0] = location[1] = 0;
12561 return;
12562 }
12563
Gilles Debunnecea45132011-11-24 02:19:27 +010012564 float[] position = mAttachInfo.mTmpTransformLocation;
12565 position[0] = position[1] = 0.0f;
12566
12567 if (!hasIdentityMatrix()) {
12568 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012570
Gilles Debunnecea45132011-11-24 02:19:27 +010012571 position[0] += mLeft;
12572 position[1] += mTop;
12573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012574 ViewParent viewParent = mParent;
12575 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012576 final View view = (View) viewParent;
12577
12578 position[0] -= view.mScrollX;
12579 position[1] -= view.mScrollY;
12580
12581 if (!view.hasIdentityMatrix()) {
12582 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012583 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012584
12585 position[0] += view.mLeft;
12586 position[1] += view.mTop;
12587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012588 viewParent = view.mParent;
12589 }
Romain Guy8506ab42009-06-11 17:35:47 -070012590
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012591 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012592 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010012593 final ViewRootImpl vr = (ViewRootImpl) viewParent;
12594 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012595 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012596
12597 location[0] = (int) (position[0] + 0.5f);
12598 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012599 }
12600
12601 /**
12602 * {@hide}
12603 * @param id the id of the view to be found
12604 * @return the view of the specified id, null if cannot be found
12605 */
12606 protected View findViewTraversal(int id) {
12607 if (id == mID) {
12608 return this;
12609 }
12610 return null;
12611 }
12612
12613 /**
12614 * {@hide}
12615 * @param tag the tag of the view to be found
12616 * @return the view of specified tag, null if cannot be found
12617 */
12618 protected View findViewWithTagTraversal(Object tag) {
12619 if (tag != null && tag.equals(mTag)) {
12620 return this;
12621 }
12622 return null;
12623 }
12624
12625 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012626 * {@hide}
12627 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070012628 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080012629 * @return The first view that matches the predicate or null.
12630 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070012631 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080012632 if (predicate.apply(this)) {
12633 return this;
12634 }
12635 return null;
12636 }
12637
12638 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012639 * Look for a child view with the given id. If this view has the given
12640 * id, return this view.
12641 *
12642 * @param id The id to search for.
12643 * @return The view that has the given id in the hierarchy or null
12644 */
12645 public final View findViewById(int id) {
12646 if (id < 0) {
12647 return null;
12648 }
12649 return findViewTraversal(id);
12650 }
12651
12652 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070012653 * Finds a view by its unuque and stable accessibility id.
12654 *
12655 * @param accessibilityId The searched accessibility id.
12656 * @return The found view.
12657 */
12658 final View findViewByAccessibilityId(int accessibilityId) {
12659 if (accessibilityId < 0) {
12660 return null;
12661 }
12662 return findViewByAccessibilityIdTraversal(accessibilityId);
12663 }
12664
12665 /**
12666 * Performs the traversal to find a view by its unuque and stable accessibility id.
12667 *
12668 * <strong>Note:</strong>This method does not stop at the root namespace
12669 * boundary since the user can touch the screen at an arbitrary location
12670 * potentially crossing the root namespace bounday which will send an
12671 * accessibility event to accessibility services and they should be able
12672 * to obtain the event source. Also accessibility ids are guaranteed to be
12673 * unique in the window.
12674 *
12675 * @param accessibilityId The accessibility id.
12676 * @return The found view.
12677 */
12678 View findViewByAccessibilityIdTraversal(int accessibilityId) {
12679 if (getAccessibilityViewId() == accessibilityId) {
12680 return this;
12681 }
12682 return null;
12683 }
12684
12685 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012686 * Look for a child view with the given tag. If this view has the given
12687 * tag, return this view.
12688 *
12689 * @param tag The tag to search for, using "tag.equals(getTag())".
12690 * @return The View that has the given tag in the hierarchy or null
12691 */
12692 public final View findViewWithTag(Object tag) {
12693 if (tag == null) {
12694 return null;
12695 }
12696 return findViewWithTagTraversal(tag);
12697 }
12698
12699 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012700 * {@hide}
12701 * Look for a child view that matches the specified predicate.
12702 * If this view matches the predicate, return this view.
12703 *
12704 * @param predicate The predicate to evaluate.
12705 * @return The first view that matches the predicate or null.
12706 */
12707 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070012708 return findViewByPredicateTraversal(predicate, null);
12709 }
12710
12711 /**
12712 * {@hide}
12713 * Look for a child view that matches the specified predicate,
12714 * starting with the specified view and its descendents and then
12715 * recusively searching the ancestors and siblings of that view
12716 * until this view is reached.
12717 *
12718 * This method is useful in cases where the predicate does not match
12719 * a single unique view (perhaps multiple views use the same id)
12720 * and we are trying to find the view that is "closest" in scope to the
12721 * starting view.
12722 *
12723 * @param start The view to start from.
12724 * @param predicate The predicate to evaluate.
12725 * @return The first view that matches the predicate or null.
12726 */
12727 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
12728 View childToSkip = null;
12729 for (;;) {
12730 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
12731 if (view != null || start == this) {
12732 return view;
12733 }
12734
12735 ViewParent parent = start.getParent();
12736 if (parent == null || !(parent instanceof View)) {
12737 return null;
12738 }
12739
12740 childToSkip = start;
12741 start = (View) parent;
12742 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080012743 }
12744
12745 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012746 * Sets the identifier for this view. The identifier does not have to be
12747 * unique in this view's hierarchy. The identifier should be a positive
12748 * number.
12749 *
12750 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070012751 * @see #getId()
12752 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012753 *
12754 * @param id a number used to identify the view
12755 *
12756 * @attr ref android.R.styleable#View_id
12757 */
12758 public void setId(int id) {
12759 mID = id;
12760 }
12761
12762 /**
12763 * {@hide}
12764 *
12765 * @param isRoot true if the view belongs to the root namespace, false
12766 * otherwise
12767 */
12768 public void setIsRootNamespace(boolean isRoot) {
12769 if (isRoot) {
12770 mPrivateFlags |= IS_ROOT_NAMESPACE;
12771 } else {
12772 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
12773 }
12774 }
12775
12776 /**
12777 * {@hide}
12778 *
12779 * @return true if the view belongs to the root namespace, false otherwise
12780 */
12781 public boolean isRootNamespace() {
12782 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
12783 }
12784
12785 /**
12786 * Returns this view's identifier.
12787 *
12788 * @return a positive integer used to identify the view or {@link #NO_ID}
12789 * if the view has no ID
12790 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012791 * @see #setId(int)
12792 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012793 * @attr ref android.R.styleable#View_id
12794 */
12795 @ViewDebug.CapturedViewProperty
12796 public int getId() {
12797 return mID;
12798 }
12799
12800 /**
12801 * Returns this view's tag.
12802 *
12803 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070012804 *
12805 * @see #setTag(Object)
12806 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012807 */
12808 @ViewDebug.ExportedProperty
12809 public Object getTag() {
12810 return mTag;
12811 }
12812
12813 /**
12814 * Sets the tag associated with this view. A tag can be used to mark
12815 * a view in its hierarchy and does not have to be unique within the
12816 * hierarchy. Tags can also be used to store data within a view without
12817 * resorting to another data structure.
12818 *
12819 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070012820 *
12821 * @see #getTag()
12822 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012823 */
12824 public void setTag(final Object tag) {
12825 mTag = tag;
12826 }
12827
12828 /**
Romain Guyd90a3312009-05-06 14:54:28 -070012829 * Returns the tag associated with this view and the specified key.
12830 *
12831 * @param key The key identifying the tag
12832 *
12833 * @return the Object stored in this view as a tag
12834 *
12835 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070012836 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070012837 */
12838 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012839 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070012840 return null;
12841 }
12842
12843 /**
12844 * Sets a tag associated with this view and a key. A tag can be used
12845 * to mark a view in its hierarchy and does not have to be unique within
12846 * the hierarchy. Tags can also be used to store data within a view
12847 * without resorting to another data structure.
12848 *
12849 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070012850 * application to ensure it is unique (see the <a
12851 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
12852 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070012853 * the Android framework or not associated with any package will cause
12854 * an {@link IllegalArgumentException} to be thrown.
12855 *
12856 * @param key The key identifying the tag
12857 * @param tag An Object to tag the view with
12858 *
12859 * @throws IllegalArgumentException If they specified key is not valid
12860 *
12861 * @see #setTag(Object)
12862 * @see #getTag(int)
12863 */
12864 public void setTag(int key, final Object tag) {
12865 // If the package id is 0x00 or 0x01, it's either an undefined package
12866 // or a framework id
12867 if ((key >>> 24) < 2) {
12868 throw new IllegalArgumentException("The key must be an application-specific "
12869 + "resource id.");
12870 }
12871
Adam Powell2b2f6d62011-09-23 11:15:39 -070012872 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012873 }
12874
12875 /**
12876 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
12877 * framework id.
12878 *
12879 * @hide
12880 */
12881 public void setTagInternal(int key, Object tag) {
12882 if ((key >>> 24) != 0x1) {
12883 throw new IllegalArgumentException("The key must be a framework-specific "
12884 + "resource id.");
12885 }
12886
Adam Powell2b2f6d62011-09-23 11:15:39 -070012887 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012888 }
12889
Adam Powell2b2f6d62011-09-23 11:15:39 -070012890 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012891 if (mKeyedTags == null) {
12892 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070012893 }
12894
Adam Powell7db82ac2011-09-22 19:44:04 -070012895 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012896 }
12897
12898 /**
Romain Guy13922e02009-05-12 17:56:14 -070012899 * @param consistency The type of consistency. See ViewDebug for more information.
12900 *
12901 * @hide
12902 */
12903 protected boolean dispatchConsistencyCheck(int consistency) {
12904 return onConsistencyCheck(consistency);
12905 }
12906
12907 /**
12908 * Method that subclasses should implement to check their consistency. The type of
12909 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070012910 *
Romain Guy13922e02009-05-12 17:56:14 -070012911 * @param consistency The type of consistency. See ViewDebug for more information.
12912 *
12913 * @throws IllegalStateException if the view is in an inconsistent state.
12914 *
12915 * @hide
12916 */
12917 protected boolean onConsistencyCheck(int consistency) {
12918 boolean result = true;
12919
12920 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
12921 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
12922
12923 if (checkLayout) {
12924 if (getParent() == null) {
12925 result = false;
12926 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12927 "View " + this + " does not have a parent.");
12928 }
12929
12930 if (mAttachInfo == null) {
12931 result = false;
12932 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12933 "View " + this + " is not attached to a window.");
12934 }
12935 }
12936
12937 if (checkDrawing) {
12938 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
12939 // from their draw() method
12940
12941 if ((mPrivateFlags & DRAWN) != DRAWN &&
12942 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
12943 result = false;
12944 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12945 "View " + this + " was invalidated but its drawing cache is valid.");
12946 }
12947 }
12948
12949 return result;
12950 }
12951
12952 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012953 * Prints information about this view in the log output, with the tag
12954 * {@link #VIEW_LOG_TAG}.
12955 *
12956 * @hide
12957 */
12958 public void debug() {
12959 debug(0);
12960 }
12961
12962 /**
12963 * Prints information about this view in the log output, with the tag
12964 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
12965 * indentation defined by the <code>depth</code>.
12966 *
12967 * @param depth the indentation level
12968 *
12969 * @hide
12970 */
12971 protected void debug(int depth) {
12972 String output = debugIndent(depth - 1);
12973
12974 output += "+ " + this;
12975 int id = getId();
12976 if (id != -1) {
12977 output += " (id=" + id + ")";
12978 }
12979 Object tag = getTag();
12980 if (tag != null) {
12981 output += " (tag=" + tag + ")";
12982 }
12983 Log.d(VIEW_LOG_TAG, output);
12984
12985 if ((mPrivateFlags & FOCUSED) != 0) {
12986 output = debugIndent(depth) + " FOCUSED";
12987 Log.d(VIEW_LOG_TAG, output);
12988 }
12989
12990 output = debugIndent(depth);
12991 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
12992 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
12993 + "} ";
12994 Log.d(VIEW_LOG_TAG, output);
12995
12996 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
12997 || mPaddingBottom != 0) {
12998 output = debugIndent(depth);
12999 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
13000 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
13001 Log.d(VIEW_LOG_TAG, output);
13002 }
13003
13004 output = debugIndent(depth);
13005 output += "mMeasureWidth=" + mMeasuredWidth +
13006 " mMeasureHeight=" + mMeasuredHeight;
13007 Log.d(VIEW_LOG_TAG, output);
13008
13009 output = debugIndent(depth);
13010 if (mLayoutParams == null) {
13011 output += "BAD! no layout params";
13012 } else {
13013 output = mLayoutParams.debug(output);
13014 }
13015 Log.d(VIEW_LOG_TAG, output);
13016
13017 output = debugIndent(depth);
13018 output += "flags={";
13019 output += View.printFlags(mViewFlags);
13020 output += "}";
13021 Log.d(VIEW_LOG_TAG, output);
13022
13023 output = debugIndent(depth);
13024 output += "privateFlags={";
13025 output += View.printPrivateFlags(mPrivateFlags);
13026 output += "}";
13027 Log.d(VIEW_LOG_TAG, output);
13028 }
13029
13030 /**
13031 * Creates an string of whitespaces used for indentation.
13032 *
13033 * @param depth the indentation level
13034 * @return a String containing (depth * 2 + 3) * 2 white spaces
13035 *
13036 * @hide
13037 */
13038 protected static String debugIndent(int depth) {
13039 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
13040 for (int i = 0; i < (depth * 2) + 3; i++) {
13041 spaces.append(' ').append(' ');
13042 }
13043 return spaces.toString();
13044 }
13045
13046 /**
13047 * <p>Return the offset of the widget's text baseline from the widget's top
13048 * boundary. If this widget does not support baseline alignment, this
13049 * method returns -1. </p>
13050 *
13051 * @return the offset of the baseline within the widget's bounds or -1
13052 * if baseline alignment is not supported
13053 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070013054 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013055 public int getBaseline() {
13056 return -1;
13057 }
13058
13059 /**
13060 * Call this when something has changed which has invalidated the
13061 * layout of this view. This will schedule a layout pass of the view
13062 * tree.
13063 */
13064 public void requestLayout() {
13065 if (ViewDebug.TRACE_HIERARCHY) {
13066 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
13067 }
13068
13069 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013070 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013071
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070013072 if (mParent != null) {
13073 if (mLayoutParams != null) {
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080013074 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070013075 }
13076 if (!mParent.isLayoutRequested()) {
13077 mParent.requestLayout();
13078 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013079 }
13080 }
13081
13082 /**
13083 * Forces this view to be laid out during the next layout pass.
13084 * This method does not call requestLayout() or forceLayout()
13085 * on the parent.
13086 */
13087 public void forceLayout() {
13088 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013089 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013090 }
13091
13092 /**
13093 * <p>
13094 * This is called to find out how big a view should be. The parent
13095 * supplies constraint information in the width and height parameters.
13096 * </p>
13097 *
13098 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080013099 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013100 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080013101 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013102 * </p>
13103 *
13104 *
13105 * @param widthMeasureSpec Horizontal space requirements as imposed by the
13106 * parent
13107 * @param heightMeasureSpec Vertical space requirements as imposed by the
13108 * parent
13109 *
13110 * @see #onMeasure(int, int)
13111 */
13112 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
13113 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
13114 widthMeasureSpec != mOldWidthMeasureSpec ||
13115 heightMeasureSpec != mOldHeightMeasureSpec) {
13116
13117 // first clears the measured dimension flag
13118 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
13119
13120 if (ViewDebug.TRACE_HIERARCHY) {
13121 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
13122 }
13123
13124 // measure ourselves, this should set the measured dimension flag back
13125 onMeasure(widthMeasureSpec, heightMeasureSpec);
13126
13127 // flag not set, setMeasuredDimension() was not invoked, we raise
13128 // an exception to warn the developer
13129 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
13130 throw new IllegalStateException("onMeasure() did not set the"
13131 + " measured dimension by calling"
13132 + " setMeasuredDimension()");
13133 }
13134
13135 mPrivateFlags |= LAYOUT_REQUIRED;
13136 }
13137
13138 mOldWidthMeasureSpec = widthMeasureSpec;
13139 mOldHeightMeasureSpec = heightMeasureSpec;
13140 }
13141
13142 /**
13143 * <p>
13144 * Measure the view and its content to determine the measured width and the
13145 * measured height. This method is invoked by {@link #measure(int, int)} and
13146 * should be overriden by subclasses to provide accurate and efficient
13147 * measurement of their contents.
13148 * </p>
13149 *
13150 * <p>
13151 * <strong>CONTRACT:</strong> When overriding this method, you
13152 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
13153 * measured width and height of this view. Failure to do so will trigger an
13154 * <code>IllegalStateException</code>, thrown by
13155 * {@link #measure(int, int)}. Calling the superclass'
13156 * {@link #onMeasure(int, int)} is a valid use.
13157 * </p>
13158 *
13159 * <p>
13160 * The base class implementation of measure defaults to the background size,
13161 * unless a larger size is allowed by the MeasureSpec. Subclasses should
13162 * override {@link #onMeasure(int, int)} to provide better measurements of
13163 * their content.
13164 * </p>
13165 *
13166 * <p>
13167 * If this method is overridden, it is the subclass's responsibility to make
13168 * sure the measured height and width are at least the view's minimum height
13169 * and width ({@link #getSuggestedMinimumHeight()} and
13170 * {@link #getSuggestedMinimumWidth()}).
13171 * </p>
13172 *
13173 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
13174 * The requirements are encoded with
13175 * {@link android.view.View.MeasureSpec}.
13176 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
13177 * The requirements are encoded with
13178 * {@link android.view.View.MeasureSpec}.
13179 *
13180 * @see #getMeasuredWidth()
13181 * @see #getMeasuredHeight()
13182 * @see #setMeasuredDimension(int, int)
13183 * @see #getSuggestedMinimumHeight()
13184 * @see #getSuggestedMinimumWidth()
13185 * @see android.view.View.MeasureSpec#getMode(int)
13186 * @see android.view.View.MeasureSpec#getSize(int)
13187 */
13188 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
13189 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
13190 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
13191 }
13192
13193 /**
13194 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
13195 * measured width and measured height. Failing to do so will trigger an
13196 * exception at measurement time.</p>
13197 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080013198 * @param measuredWidth The measured width of this view. May be a complex
13199 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13200 * {@link #MEASURED_STATE_TOO_SMALL}.
13201 * @param measuredHeight The measured height of this view. May be a complex
13202 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13203 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013204 */
13205 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
13206 mMeasuredWidth = measuredWidth;
13207 mMeasuredHeight = measuredHeight;
13208
13209 mPrivateFlags |= MEASURED_DIMENSION_SET;
13210 }
13211
13212 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080013213 * Merge two states as returned by {@link #getMeasuredState()}.
13214 * @param curState The current state as returned from a view or the result
13215 * of combining multiple views.
13216 * @param newState The new view state to combine.
13217 * @return Returns a new integer reflecting the combination of the two
13218 * states.
13219 */
13220 public static int combineMeasuredStates(int curState, int newState) {
13221 return curState | newState;
13222 }
13223
13224 /**
13225 * Version of {@link #resolveSizeAndState(int, int, int)}
13226 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
13227 */
13228 public static int resolveSize(int size, int measureSpec) {
13229 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
13230 }
13231
13232 /**
13233 * Utility to reconcile a desired size and state, with constraints imposed
13234 * by a MeasureSpec. Will take the desired size, unless a different size
13235 * is imposed by the constraints. The returned value is a compound integer,
13236 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
13237 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
13238 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013239 *
13240 * @param size How big the view wants to be
13241 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080013242 * @return Size information bit mask as defined by
13243 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013244 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080013245 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013246 int result = size;
13247 int specMode = MeasureSpec.getMode(measureSpec);
13248 int specSize = MeasureSpec.getSize(measureSpec);
13249 switch (specMode) {
13250 case MeasureSpec.UNSPECIFIED:
13251 result = size;
13252 break;
13253 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080013254 if (specSize < size) {
13255 result = specSize | MEASURED_STATE_TOO_SMALL;
13256 } else {
13257 result = size;
13258 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013259 break;
13260 case MeasureSpec.EXACTLY:
13261 result = specSize;
13262 break;
13263 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080013264 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013265 }
13266
13267 /**
13268 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070013269 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013270 * by the MeasureSpec.
13271 *
13272 * @param size Default size for this view
13273 * @param measureSpec Constraints imposed by the parent
13274 * @return The size this view should be.
13275 */
13276 public static int getDefaultSize(int size, int measureSpec) {
13277 int result = size;
13278 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070013279 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013280
13281 switch (specMode) {
13282 case MeasureSpec.UNSPECIFIED:
13283 result = size;
13284 break;
13285 case MeasureSpec.AT_MOST:
13286 case MeasureSpec.EXACTLY:
13287 result = specSize;
13288 break;
13289 }
13290 return result;
13291 }
13292
13293 /**
13294 * Returns the suggested minimum height that the view should use. This
13295 * returns the maximum of the view's minimum height
13296 * and the background's minimum height
13297 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
13298 * <p>
13299 * When being used in {@link #onMeasure(int, int)}, the caller should still
13300 * ensure the returned height is within the requirements of the parent.
13301 *
13302 * @return The suggested minimum height of the view.
13303 */
13304 protected int getSuggestedMinimumHeight() {
13305 int suggestedMinHeight = mMinHeight;
13306
13307 if (mBGDrawable != null) {
13308 final int bgMinHeight = mBGDrawable.getMinimumHeight();
13309 if (suggestedMinHeight < bgMinHeight) {
13310 suggestedMinHeight = bgMinHeight;
13311 }
13312 }
13313
13314 return suggestedMinHeight;
13315 }
13316
13317 /**
13318 * Returns the suggested minimum width that the view should use. This
13319 * returns the maximum of the view's minimum width)
13320 * and the background's minimum width
13321 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
13322 * <p>
13323 * When being used in {@link #onMeasure(int, int)}, the caller should still
13324 * ensure the returned width is within the requirements of the parent.
13325 *
13326 * @return The suggested minimum width of the view.
13327 */
13328 protected int getSuggestedMinimumWidth() {
13329 int suggestedMinWidth = mMinWidth;
13330
13331 if (mBGDrawable != null) {
13332 final int bgMinWidth = mBGDrawable.getMinimumWidth();
13333 if (suggestedMinWidth < bgMinWidth) {
13334 suggestedMinWidth = bgMinWidth;
13335 }
13336 }
13337
13338 return suggestedMinWidth;
13339 }
13340
13341 /**
13342 * Sets the minimum height of the view. It is not guaranteed the view will
13343 * be able to achieve this minimum height (for example, if its parent layout
13344 * constrains it with less available height).
13345 *
13346 * @param minHeight The minimum height the view will try to be.
13347 */
13348 public void setMinimumHeight(int minHeight) {
13349 mMinHeight = minHeight;
13350 }
13351
13352 /**
13353 * Sets the minimum width of the view. It is not guaranteed the view will
13354 * be able to achieve this minimum width (for example, if its parent layout
13355 * constrains it with less available width).
13356 *
13357 * @param minWidth The minimum width the view will try to be.
13358 */
13359 public void setMinimumWidth(int minWidth) {
13360 mMinWidth = minWidth;
13361 }
13362
13363 /**
13364 * Get the animation currently associated with this view.
13365 *
13366 * @return The animation that is currently playing or
13367 * scheduled to play for this view.
13368 */
13369 public Animation getAnimation() {
13370 return mCurrentAnimation;
13371 }
13372
13373 /**
13374 * Start the specified animation now.
13375 *
13376 * @param animation the animation to start now
13377 */
13378 public void startAnimation(Animation animation) {
13379 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
13380 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013381 invalidateParentCaches();
13382 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013383 }
13384
13385 /**
13386 * Cancels any animations for this view.
13387 */
13388 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080013389 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080013390 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080013391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013392 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013393 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013394 }
13395
13396 /**
13397 * Sets the next animation to play for this view.
13398 * If you want the animation to play immediately, use
13399 * startAnimation. This method provides allows fine-grained
13400 * control over the start time and invalidation, but you
13401 * must make sure that 1) the animation has a start time set, and
13402 * 2) the view will be invalidated when the animation is supposed to
13403 * start.
13404 *
13405 * @param animation The next animation, or null.
13406 */
13407 public void setAnimation(Animation animation) {
13408 mCurrentAnimation = animation;
13409 if (animation != null) {
13410 animation.reset();
13411 }
13412 }
13413
13414 /**
13415 * Invoked by a parent ViewGroup to notify the start of the animation
13416 * currently associated with this view. If you override this method,
13417 * always call super.onAnimationStart();
13418 *
13419 * @see #setAnimation(android.view.animation.Animation)
13420 * @see #getAnimation()
13421 */
13422 protected void onAnimationStart() {
13423 mPrivateFlags |= ANIMATION_STARTED;
13424 }
13425
13426 /**
13427 * Invoked by a parent ViewGroup to notify the end of the animation
13428 * currently associated with this view. If you override this method,
13429 * always call super.onAnimationEnd();
13430 *
13431 * @see #setAnimation(android.view.animation.Animation)
13432 * @see #getAnimation()
13433 */
13434 protected void onAnimationEnd() {
13435 mPrivateFlags &= ~ANIMATION_STARTED;
13436 }
13437
13438 /**
13439 * Invoked if there is a Transform that involves alpha. Subclass that can
13440 * draw themselves with the specified alpha should return true, and then
13441 * respect that alpha when their onDraw() is called. If this returns false
13442 * then the view may be redirected to draw into an offscreen buffer to
13443 * fulfill the request, which will look fine, but may be slower than if the
13444 * subclass handles it internally. The default implementation returns false.
13445 *
13446 * @param alpha The alpha (0..255) to apply to the view's drawing
13447 * @return true if the view can draw with the specified alpha.
13448 */
13449 protected boolean onSetAlpha(int alpha) {
13450 return false;
13451 }
13452
13453 /**
13454 * This is used by the RootView to perform an optimization when
13455 * the view hierarchy contains one or several SurfaceView.
13456 * SurfaceView is always considered transparent, but its children are not,
13457 * therefore all View objects remove themselves from the global transparent
13458 * region (passed as a parameter to this function).
13459 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013460 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013461 *
13462 * @return Returns true if the effective visibility of the view at this
13463 * point is opaque, regardless of the transparent region; returns false
13464 * if it is possible for underlying windows to be seen behind the view.
13465 *
13466 * {@hide}
13467 */
13468 public boolean gatherTransparentRegion(Region region) {
13469 final AttachInfo attachInfo = mAttachInfo;
13470 if (region != null && attachInfo != null) {
13471 final int pflags = mPrivateFlags;
13472 if ((pflags & SKIP_DRAW) == 0) {
13473 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
13474 // remove it from the transparent region.
13475 final int[] location = attachInfo.mTransparentLocation;
13476 getLocationInWindow(location);
13477 region.op(location[0], location[1], location[0] + mRight - mLeft,
13478 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
13479 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
13480 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
13481 // exists, so we remove the background drawable's non-transparent
13482 // parts from this transparent region.
13483 applyDrawableToTransparentRegion(mBGDrawable, region);
13484 }
13485 }
13486 return true;
13487 }
13488
13489 /**
13490 * Play a sound effect for this view.
13491 *
13492 * <p>The framework will play sound effects for some built in actions, such as
13493 * clicking, but you may wish to play these effects in your widget,
13494 * for instance, for internal navigation.
13495 *
13496 * <p>The sound effect will only be played if sound effects are enabled by the user, and
13497 * {@link #isSoundEffectsEnabled()} is true.
13498 *
13499 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
13500 */
13501 public void playSoundEffect(int soundConstant) {
13502 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
13503 return;
13504 }
13505 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
13506 }
13507
13508 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013509 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013510 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013511 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013512 *
13513 * <p>The framework will provide haptic feedback for some built in actions,
13514 * such as long presses, but you may wish to provide feedback for your
13515 * own widget.
13516 *
13517 * <p>The feedback will only be performed if
13518 * {@link #isHapticFeedbackEnabled()} is true.
13519 *
13520 * @param feedbackConstant One of the constants defined in
13521 * {@link HapticFeedbackConstants}
13522 */
13523 public boolean performHapticFeedback(int feedbackConstant) {
13524 return performHapticFeedback(feedbackConstant, 0);
13525 }
13526
13527 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013528 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013529 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013530 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013531 *
13532 * @param feedbackConstant One of the constants defined in
13533 * {@link HapticFeedbackConstants}
13534 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
13535 */
13536 public boolean performHapticFeedback(int feedbackConstant, int flags) {
13537 if (mAttachInfo == null) {
13538 return false;
13539 }
Romain Guyf607bdc2010-09-10 19:20:06 -070013540 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070013541 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013542 && !isHapticFeedbackEnabled()) {
13543 return false;
13544 }
Romain Guy812ccbe2010-06-01 14:07:24 -070013545 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
13546 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013547 }
13548
13549 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013550 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013551 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13552 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013553 */
13554 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040013555 if (visibility != mSystemUiVisibility) {
13556 mSystemUiVisibility = visibility;
13557 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13558 mParent.recomputeViewAttributes(this);
13559 }
Joe Onorato664644d2011-01-23 17:53:23 -080013560 }
13561 }
13562
13563 /**
13564 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013565 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13566 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013567 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080013568 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080013569 return mSystemUiVisibility;
13570 }
13571
Scott Mainec6331b2011-05-24 16:55:56 -070013572 /**
13573 * Set a listener to receive callbacks when the visibility of the system bar changes.
13574 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
13575 */
Joe Onorato664644d2011-01-23 17:53:23 -080013576 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013577 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080013578 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13579 mParent.recomputeViewAttributes(this);
13580 }
13581 }
13582
13583 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013584 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
13585 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080013586 */
13587 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013588 ListenerInfo li = mListenerInfo;
13589 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
13590 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080013591 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080013592 }
13593 }
13594
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013595 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
13596 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
13597 if (val != mSystemUiVisibility) {
13598 setSystemUiVisibility(val);
13599 }
13600 }
13601
Joe Onorato664644d2011-01-23 17:53:23 -080013602 /**
Joe Malin32736f02011-01-19 16:14:20 -080013603 * Creates an image that the system displays during the drag and drop
13604 * operation. This is called a &quot;drag shadow&quot;. The default implementation
13605 * for a DragShadowBuilder based on a View returns an image that has exactly the same
13606 * appearance as the given View. The default also positions the center of the drag shadow
13607 * directly under the touch point. If no View is provided (the constructor with no parameters
13608 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
13609 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
13610 * default is an invisible drag shadow.
13611 * <p>
13612 * You are not required to use the View you provide to the constructor as the basis of the
13613 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
13614 * anything you want as the drag shadow.
13615 * </p>
13616 * <p>
13617 * You pass a DragShadowBuilder object to the system when you start the drag. The system
13618 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
13619 * size and position of the drag shadow. It uses this data to construct a
13620 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
13621 * so that your application can draw the shadow image in the Canvas.
13622 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070013623 *
13624 * <div class="special reference">
13625 * <h3>Developer Guides</h3>
13626 * <p>For a guide to implementing drag and drop features, read the
13627 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
13628 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070013629 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013630 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070013631 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070013632
13633 /**
Joe Malin32736f02011-01-19 16:14:20 -080013634 * Constructs a shadow image builder based on a View. By default, the resulting drag
13635 * shadow will have the same appearance and dimensions as the View, with the touch point
13636 * over the center of the View.
13637 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070013638 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013639 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070013640 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070013641 }
13642
Christopher Tate17ed60c2011-01-18 12:50:26 -080013643 /**
13644 * Construct a shadow builder object with no associated View. This
13645 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
13646 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
13647 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080013648 * reference to any View object. If they are not overridden, then the result is an
13649 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080013650 */
13651 public DragShadowBuilder() {
13652 mView = new WeakReference<View>(null);
13653 }
13654
13655 /**
13656 * Returns the View object that had been passed to the
13657 * {@link #View.DragShadowBuilder(View)}
13658 * constructor. If that View parameter was {@code null} or if the
13659 * {@link #View.DragShadowBuilder()}
13660 * constructor was used to instantiate the builder object, this method will return
13661 * null.
13662 *
13663 * @return The View object associate with this builder object.
13664 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070013665 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070013666 final public View getView() {
13667 return mView.get();
13668 }
13669
Christopher Tate2c095f32010-10-04 14:13:40 -070013670 /**
Joe Malin32736f02011-01-19 16:14:20 -080013671 * Provides the metrics for the shadow image. These include the dimensions of
13672 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070013673 * be centered under the touch location while dragging.
13674 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013675 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080013676 * same as the dimensions of the View itself and centers the shadow under
13677 * the touch point.
13678 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013679 *
Joe Malin32736f02011-01-19 16:14:20 -080013680 * @param shadowSize A {@link android.graphics.Point} containing the width and height
13681 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
13682 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
13683 * image.
13684 *
13685 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
13686 * shadow image that should be underneath the touch point during the drag and drop
13687 * operation. Your application must set {@link android.graphics.Point#x} to the
13688 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070013689 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013690 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070013691 final View view = mView.get();
13692 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013693 shadowSize.set(view.getWidth(), view.getHeight());
13694 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070013695 } else {
13696 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
13697 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013698 }
13699
13700 /**
Joe Malin32736f02011-01-19 16:14:20 -080013701 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
13702 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013703 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070013704 *
Joe Malin32736f02011-01-19 16:14:20 -080013705 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070013706 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013707 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070013708 final View view = mView.get();
13709 if (view != null) {
13710 view.draw(canvas);
13711 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013712 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070013713 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013714 }
13715 }
13716
13717 /**
Joe Malin32736f02011-01-19 16:14:20 -080013718 * Starts a drag and drop operation. When your application calls this method, it passes a
13719 * {@link android.view.View.DragShadowBuilder} object to the system. The
13720 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
13721 * to get metrics for the drag shadow, and then calls the object's
13722 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
13723 * <p>
13724 * Once the system has the drag shadow, it begins the drag and drop operation by sending
13725 * drag events to all the View objects in your application that are currently visible. It does
13726 * this either by calling the View object's drag listener (an implementation of
13727 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
13728 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
13729 * Both are passed a {@link android.view.DragEvent} object that has a
13730 * {@link android.view.DragEvent#getAction()} value of
13731 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
13732 * </p>
13733 * <p>
13734 * Your application can invoke startDrag() on any attached View object. The View object does not
13735 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
13736 * be related to the View the user selected for dragging.
13737 * </p>
13738 * @param data A {@link android.content.ClipData} object pointing to the data to be
13739 * transferred by the drag and drop operation.
13740 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
13741 * drag shadow.
13742 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
13743 * drop operation. This Object is put into every DragEvent object sent by the system during the
13744 * current drag.
13745 * <p>
13746 * myLocalState is a lightweight mechanism for the sending information from the dragged View
13747 * to the target Views. For example, it can contain flags that differentiate between a
13748 * a copy operation and a move operation.
13749 * </p>
13750 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
13751 * so the parameter should be set to 0.
13752 * @return {@code true} if the method completes successfully, or
13753 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
13754 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070013755 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013756 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013757 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070013758 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013759 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070013760 }
13761 boolean okay = false;
13762
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013763 Point shadowSize = new Point();
13764 Point shadowTouchPoint = new Point();
13765 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070013766
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013767 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
13768 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
13769 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070013770 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013771
Chris Tatea32dcf72010-10-14 12:13:50 -070013772 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013773 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
13774 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070013775 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013776 Surface surface = new Surface();
13777 try {
13778 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013779 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070013780 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070013781 + " surface=" + surface);
13782 if (token != null) {
13783 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070013784 try {
Chris Tate6b391282010-10-14 15:48:59 -070013785 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013786 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070013787 } finally {
13788 surface.unlockCanvasAndPost(canvas);
13789 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013790
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013791 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080013792
13793 // Cache the local state object for delivery with DragEvents
13794 root.setLocalDragState(myLocalState);
13795
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013796 // repurpose 'shadowSize' for the last touch point
13797 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070013798
Christopher Tatea53146c2010-09-07 11:57:52 -070013799 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013800 shadowSize.x, shadowSize.y,
13801 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070013802 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070013803
13804 // Off and running! Release our local surface instance; the drag
13805 // shadow surface is now managed by the system process.
13806 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070013807 }
13808 } catch (Exception e) {
13809 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
13810 surface.destroy();
13811 }
13812
13813 return okay;
13814 }
13815
Christopher Tatea53146c2010-09-07 11:57:52 -070013816 /**
Joe Malin32736f02011-01-19 16:14:20 -080013817 * Handles drag events sent by the system following a call to
13818 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
13819 *<p>
13820 * When the system calls this method, it passes a
13821 * {@link android.view.DragEvent} object. A call to
13822 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
13823 * in DragEvent. The method uses these to determine what is happening in the drag and drop
13824 * operation.
13825 * @param event The {@link android.view.DragEvent} sent by the system.
13826 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
13827 * in DragEvent, indicating the type of drag event represented by this object.
13828 * @return {@code true} if the method was successful, otherwise {@code false}.
13829 * <p>
13830 * The method should return {@code true} in response to an action type of
13831 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
13832 * operation.
13833 * </p>
13834 * <p>
13835 * The method should also return {@code true} in response to an action type of
13836 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
13837 * {@code false} if it didn't.
13838 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013839 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070013840 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070013841 return false;
13842 }
13843
13844 /**
Joe Malin32736f02011-01-19 16:14:20 -080013845 * Detects if this View is enabled and has a drag event listener.
13846 * If both are true, then it calls the drag event listener with the
13847 * {@link android.view.DragEvent} it received. If the drag event listener returns
13848 * {@code true}, then dispatchDragEvent() returns {@code true}.
13849 * <p>
13850 * For all other cases, the method calls the
13851 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
13852 * method and returns its result.
13853 * </p>
13854 * <p>
13855 * This ensures that a drag event is always consumed, even if the View does not have a drag
13856 * event listener. However, if the View has a listener and the listener returns true, then
13857 * onDragEvent() is not called.
13858 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013859 */
13860 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080013861 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013862 ListenerInfo li = mListenerInfo;
13863 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
13864 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070013865 return true;
13866 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013867 return onDragEvent(event);
13868 }
13869
Christopher Tate3d4bf172011-03-28 16:16:46 -070013870 boolean canAcceptDrag() {
13871 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
13872 }
13873
Christopher Tatea53146c2010-09-07 11:57:52 -070013874 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070013875 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
13876 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070013877 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070013878 */
13879 public void onCloseSystemDialogs(String reason) {
13880 }
Joe Malin32736f02011-01-19 16:14:20 -080013881
Dianne Hackbornffa42482009-09-23 22:20:11 -070013882 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013883 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070013884 * update a Region being computed for
13885 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013886 * that any non-transparent parts of the Drawable are removed from the
13887 * given transparent region.
13888 *
13889 * @param dr The Drawable whose transparency is to be applied to the region.
13890 * @param region A Region holding the current transparency information,
13891 * where any parts of the region that are set are considered to be
13892 * transparent. On return, this region will be modified to have the
13893 * transparency information reduced by the corresponding parts of the
13894 * Drawable that are not transparent.
13895 * {@hide}
13896 */
13897 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
13898 if (DBG) {
13899 Log.i("View", "Getting transparent region for: " + this);
13900 }
13901 final Region r = dr.getTransparentRegion();
13902 final Rect db = dr.getBounds();
13903 final AttachInfo attachInfo = mAttachInfo;
13904 if (r != null && attachInfo != null) {
13905 final int w = getRight()-getLeft();
13906 final int h = getBottom()-getTop();
13907 if (db.left > 0) {
13908 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
13909 r.op(0, 0, db.left, h, Region.Op.UNION);
13910 }
13911 if (db.right < w) {
13912 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
13913 r.op(db.right, 0, w, h, Region.Op.UNION);
13914 }
13915 if (db.top > 0) {
13916 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
13917 r.op(0, 0, w, db.top, Region.Op.UNION);
13918 }
13919 if (db.bottom < h) {
13920 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
13921 r.op(0, db.bottom, w, h, Region.Op.UNION);
13922 }
13923 final int[] location = attachInfo.mTransparentLocation;
13924 getLocationInWindow(location);
13925 r.translate(location[0], location[1]);
13926 region.op(r, Region.Op.INTERSECT);
13927 } else {
13928 region.op(db, Region.Op.DIFFERENCE);
13929 }
13930 }
13931
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013932 private void checkForLongClick(int delayOffset) {
13933 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
13934 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013935
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013936 if (mPendingCheckForLongPress == null) {
13937 mPendingCheckForLongPress = new CheckForLongPress();
13938 }
13939 mPendingCheckForLongPress.rememberWindowAttachCount();
13940 postDelayed(mPendingCheckForLongPress,
13941 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013942 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013943 }
13944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013945 /**
13946 * Inflate a view from an XML resource. This convenience method wraps the {@link
13947 * LayoutInflater} class, which provides a full range of options for view inflation.
13948 *
13949 * @param context The Context object for your activity or application.
13950 * @param resource The resource ID to inflate
13951 * @param root A view group that will be the parent. Used to properly inflate the
13952 * layout_* parameters.
13953 * @see LayoutInflater
13954 */
13955 public static View inflate(Context context, int resource, ViewGroup root) {
13956 LayoutInflater factory = LayoutInflater.from(context);
13957 return factory.inflate(resource, root);
13958 }
Romain Guy33e72ae2010-07-17 12:40:29 -070013959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013960 /**
Adam Powell637d3372010-08-25 14:37:03 -070013961 * Scroll the view with standard behavior for scrolling beyond the normal
13962 * content boundaries. Views that call this method should override
13963 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
13964 * results of an over-scroll operation.
13965 *
13966 * Views can use this method to handle any touch or fling-based scrolling.
13967 *
13968 * @param deltaX Change in X in pixels
13969 * @param deltaY Change in Y in pixels
13970 * @param scrollX Current X scroll value in pixels before applying deltaX
13971 * @param scrollY Current Y scroll value in pixels before applying deltaY
13972 * @param scrollRangeX Maximum content scroll range along the X axis
13973 * @param scrollRangeY Maximum content scroll range along the Y axis
13974 * @param maxOverScrollX Number of pixels to overscroll by in either direction
13975 * along the X axis.
13976 * @param maxOverScrollY Number of pixels to overscroll by in either direction
13977 * along the Y axis.
13978 * @param isTouchEvent true if this scroll operation is the result of a touch event.
13979 * @return true if scrolling was clamped to an over-scroll boundary along either
13980 * axis, false otherwise.
13981 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013982 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070013983 protected boolean overScrollBy(int deltaX, int deltaY,
13984 int scrollX, int scrollY,
13985 int scrollRangeX, int scrollRangeY,
13986 int maxOverScrollX, int maxOverScrollY,
13987 boolean isTouchEvent) {
13988 final int overScrollMode = mOverScrollMode;
13989 final boolean canScrollHorizontal =
13990 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
13991 final boolean canScrollVertical =
13992 computeVerticalScrollRange() > computeVerticalScrollExtent();
13993 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
13994 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
13995 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
13996 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
13997
13998 int newScrollX = scrollX + deltaX;
13999 if (!overScrollHorizontal) {
14000 maxOverScrollX = 0;
14001 }
14002
14003 int newScrollY = scrollY + deltaY;
14004 if (!overScrollVertical) {
14005 maxOverScrollY = 0;
14006 }
14007
14008 // Clamp values if at the limits and record
14009 final int left = -maxOverScrollX;
14010 final int right = maxOverScrollX + scrollRangeX;
14011 final int top = -maxOverScrollY;
14012 final int bottom = maxOverScrollY + scrollRangeY;
14013
14014 boolean clampedX = false;
14015 if (newScrollX > right) {
14016 newScrollX = right;
14017 clampedX = true;
14018 } else if (newScrollX < left) {
14019 newScrollX = left;
14020 clampedX = true;
14021 }
14022
14023 boolean clampedY = false;
14024 if (newScrollY > bottom) {
14025 newScrollY = bottom;
14026 clampedY = true;
14027 } else if (newScrollY < top) {
14028 newScrollY = top;
14029 clampedY = true;
14030 }
14031
14032 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
14033
14034 return clampedX || clampedY;
14035 }
14036
14037 /**
14038 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
14039 * respond to the results of an over-scroll operation.
14040 *
14041 * @param scrollX New X scroll value in pixels
14042 * @param scrollY New Y scroll value in pixels
14043 * @param clampedX True if scrollX was clamped to an over-scroll boundary
14044 * @param clampedY True if scrollY was clamped to an over-scroll boundary
14045 */
14046 protected void onOverScrolled(int scrollX, int scrollY,
14047 boolean clampedX, boolean clampedY) {
14048 // Intentionally empty.
14049 }
14050
14051 /**
14052 * Returns the over-scroll mode for this view. The result will be
14053 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14054 * (allow over-scrolling only if the view content is larger than the container),
14055 * or {@link #OVER_SCROLL_NEVER}.
14056 *
14057 * @return This view's over-scroll mode.
14058 */
14059 public int getOverScrollMode() {
14060 return mOverScrollMode;
14061 }
14062
14063 /**
14064 * Set the over-scroll mode for this view. Valid over-scroll modes are
14065 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14066 * (allow over-scrolling only if the view content is larger than the container),
14067 * or {@link #OVER_SCROLL_NEVER}.
14068 *
14069 * Setting the over-scroll mode of a view will have an effect only if the
14070 * view is capable of scrolling.
14071 *
14072 * @param overScrollMode The new over-scroll mode for this view.
14073 */
14074 public void setOverScrollMode(int overScrollMode) {
14075 if (overScrollMode != OVER_SCROLL_ALWAYS &&
14076 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
14077 overScrollMode != OVER_SCROLL_NEVER) {
14078 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
14079 }
14080 mOverScrollMode = overScrollMode;
14081 }
14082
14083 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014084 * Gets a scale factor that determines the distance the view should scroll
14085 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
14086 * @return The vertical scroll scale factor.
14087 * @hide
14088 */
14089 protected float getVerticalScrollFactor() {
14090 if (mVerticalScrollFactor == 0) {
14091 TypedValue outValue = new TypedValue();
14092 if (!mContext.getTheme().resolveAttribute(
14093 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
14094 throw new IllegalStateException(
14095 "Expected theme to define listPreferredItemHeight.");
14096 }
14097 mVerticalScrollFactor = outValue.getDimension(
14098 mContext.getResources().getDisplayMetrics());
14099 }
14100 return mVerticalScrollFactor;
14101 }
14102
14103 /**
14104 * Gets a scale factor that determines the distance the view should scroll
14105 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
14106 * @return The horizontal scroll scale factor.
14107 * @hide
14108 */
14109 protected float getHorizontalScrollFactor() {
14110 // TODO: Should use something else.
14111 return getVerticalScrollFactor();
14112 }
14113
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014114 /**
14115 * Return the value specifying the text direction or policy that was set with
14116 * {@link #setTextDirection(int)}.
14117 *
14118 * @return the defined text direction. It can be one of:
14119 *
14120 * {@link #TEXT_DIRECTION_INHERIT},
14121 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14122 * {@link #TEXT_DIRECTION_ANY_RTL},
14123 * {@link #TEXT_DIRECTION_LTR},
14124 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014125 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014126 */
14127 public int getTextDirection() {
14128 return mTextDirection;
14129 }
14130
14131 /**
14132 * Set the text direction.
14133 *
14134 * @param textDirection the direction to set. Should be one of:
14135 *
14136 * {@link #TEXT_DIRECTION_INHERIT},
14137 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14138 * {@link #TEXT_DIRECTION_ANY_RTL},
14139 * {@link #TEXT_DIRECTION_LTR},
14140 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014141 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014142 */
14143 public void setTextDirection(int textDirection) {
14144 if (textDirection != mTextDirection) {
14145 mTextDirection = textDirection;
14146 resetResolvedTextDirection();
14147 requestLayout();
14148 }
14149 }
14150
14151 /**
14152 * Return the resolved text direction.
14153 *
14154 * @return the resolved text direction. Return one of:
14155 *
Doug Feltcb3791202011-07-07 11:57:48 -070014156 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14157 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014158 * {@link #TEXT_DIRECTION_LTR},
14159 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014160 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014161 */
14162 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070014163 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014164 resolveTextDirection();
14165 }
14166 return mResolvedTextDirection;
14167 }
14168
14169 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014170 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
14171 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014172 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014173 public void resolveTextDirection() {
14174 if (mResolvedTextDirection != TEXT_DIRECTION_INHERIT) {
14175 // Resolution has already been done.
14176 return;
14177 }
Doug Feltcb3791202011-07-07 11:57:48 -070014178 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
14179 mResolvedTextDirection = mTextDirection;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014180 } else if (mParent != null && mParent instanceof ViewGroup) {
Doug Feltcb3791202011-07-07 11:57:48 -070014181 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014182 } else {
14183 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Doug Feltcb3791202011-07-07 11:57:48 -070014184 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014185 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014186 }
14187
14188 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014189 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014190 * resolution should override this method.
14191 *
14192 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014193 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014194 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014195 }
14196
14197 /**
14198 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014199 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014200 * reset is done.
14201 */
14202 public void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070014203 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014204 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014205 }
14206
14207 /**
14208 * Called when text direction is reset. Subclasses that care about text direction reset should
14209 * override this method and do a reset of the text direction of their children. The default
14210 * implementation does nothing.
14211 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014212 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014213 }
14214
Chet Haaseb39f0512011-05-24 14:36:40 -070014215 //
14216 // Properties
14217 //
14218 /**
14219 * A Property wrapper around the <code>alpha</code> functionality handled by the
14220 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
14221 */
Chet Haased47f1532011-12-16 11:18:52 -080014222 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014223 @Override
14224 public void setValue(View object, float value) {
14225 object.setAlpha(value);
14226 }
14227
14228 @Override
14229 public Float get(View object) {
14230 return object.getAlpha();
14231 }
14232 };
14233
14234 /**
14235 * A Property wrapper around the <code>translationX</code> functionality handled by the
14236 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
14237 */
Chet Haased47f1532011-12-16 11:18:52 -080014238 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014239 @Override
14240 public void setValue(View object, float value) {
14241 object.setTranslationX(value);
14242 }
14243
14244 @Override
14245 public Float get(View object) {
14246 return object.getTranslationX();
14247 }
14248 };
14249
14250 /**
14251 * A Property wrapper around the <code>translationY</code> functionality handled by the
14252 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
14253 */
Chet Haased47f1532011-12-16 11:18:52 -080014254 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014255 @Override
14256 public void setValue(View object, float value) {
14257 object.setTranslationY(value);
14258 }
14259
14260 @Override
14261 public Float get(View object) {
14262 return object.getTranslationY();
14263 }
14264 };
14265
14266 /**
14267 * A Property wrapper around the <code>x</code> functionality handled by the
14268 * {@link View#setX(float)} and {@link View#getX()} methods.
14269 */
Chet Haased47f1532011-12-16 11:18:52 -080014270 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014271 @Override
14272 public void setValue(View object, float value) {
14273 object.setX(value);
14274 }
14275
14276 @Override
14277 public Float get(View object) {
14278 return object.getX();
14279 }
14280 };
14281
14282 /**
14283 * A Property wrapper around the <code>y</code> functionality handled by the
14284 * {@link View#setY(float)} and {@link View#getY()} methods.
14285 */
Chet Haased47f1532011-12-16 11:18:52 -080014286 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014287 @Override
14288 public void setValue(View object, float value) {
14289 object.setY(value);
14290 }
14291
14292 @Override
14293 public Float get(View object) {
14294 return object.getY();
14295 }
14296 };
14297
14298 /**
14299 * A Property wrapper around the <code>rotation</code> functionality handled by the
14300 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
14301 */
Chet Haased47f1532011-12-16 11:18:52 -080014302 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014303 @Override
14304 public void setValue(View object, float value) {
14305 object.setRotation(value);
14306 }
14307
14308 @Override
14309 public Float get(View object) {
14310 return object.getRotation();
14311 }
14312 };
14313
14314 /**
14315 * A Property wrapper around the <code>rotationX</code> functionality handled by the
14316 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
14317 */
Chet Haased47f1532011-12-16 11:18:52 -080014318 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014319 @Override
14320 public void setValue(View object, float value) {
14321 object.setRotationX(value);
14322 }
14323
14324 @Override
14325 public Float get(View object) {
14326 return object.getRotationX();
14327 }
14328 };
14329
14330 /**
14331 * A Property wrapper around the <code>rotationY</code> functionality handled by the
14332 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
14333 */
Chet Haased47f1532011-12-16 11:18:52 -080014334 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014335 @Override
14336 public void setValue(View object, float value) {
14337 object.setRotationY(value);
14338 }
14339
14340 @Override
14341 public Float get(View object) {
14342 return object.getRotationY();
14343 }
14344 };
14345
14346 /**
14347 * A Property wrapper around the <code>scaleX</code> functionality handled by the
14348 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
14349 */
Chet Haased47f1532011-12-16 11:18:52 -080014350 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014351 @Override
14352 public void setValue(View object, float value) {
14353 object.setScaleX(value);
14354 }
14355
14356 @Override
14357 public Float get(View object) {
14358 return object.getScaleX();
14359 }
14360 };
14361
14362 /**
14363 * A Property wrapper around the <code>scaleY</code> functionality handled by the
14364 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
14365 */
Chet Haased47f1532011-12-16 11:18:52 -080014366 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014367 @Override
14368 public void setValue(View object, float value) {
14369 object.setScaleY(value);
14370 }
14371
14372 @Override
14373 public Float get(View object) {
14374 return object.getScaleY();
14375 }
14376 };
14377
Jeff Brown33bbfd22011-02-24 20:55:35 -080014378 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014379 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
14380 * Each MeasureSpec represents a requirement for either the width or the height.
14381 * A MeasureSpec is comprised of a size and a mode. There are three possible
14382 * modes:
14383 * <dl>
14384 * <dt>UNSPECIFIED</dt>
14385 * <dd>
14386 * The parent has not imposed any constraint on the child. It can be whatever size
14387 * it wants.
14388 * </dd>
14389 *
14390 * <dt>EXACTLY</dt>
14391 * <dd>
14392 * The parent has determined an exact size for the child. The child is going to be
14393 * given those bounds regardless of how big it wants to be.
14394 * </dd>
14395 *
14396 * <dt>AT_MOST</dt>
14397 * <dd>
14398 * The child can be as large as it wants up to the specified size.
14399 * </dd>
14400 * </dl>
14401 *
14402 * MeasureSpecs are implemented as ints to reduce object allocation. This class
14403 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
14404 */
14405 public static class MeasureSpec {
14406 private static final int MODE_SHIFT = 30;
14407 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
14408
14409 /**
14410 * Measure specification mode: The parent has not imposed any constraint
14411 * on the child. It can be whatever size it wants.
14412 */
14413 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
14414
14415 /**
14416 * Measure specification mode: The parent has determined an exact size
14417 * for the child. The child is going to be given those bounds regardless
14418 * of how big it wants to be.
14419 */
14420 public static final int EXACTLY = 1 << MODE_SHIFT;
14421
14422 /**
14423 * Measure specification mode: The child can be as large as it wants up
14424 * to the specified size.
14425 */
14426 public static final int AT_MOST = 2 << MODE_SHIFT;
14427
14428 /**
14429 * Creates a measure specification based on the supplied size and mode.
14430 *
14431 * The mode must always be one of the following:
14432 * <ul>
14433 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
14434 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
14435 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
14436 * </ul>
14437 *
14438 * @param size the size of the measure specification
14439 * @param mode the mode of the measure specification
14440 * @return the measure specification based on size and mode
14441 */
14442 public static int makeMeasureSpec(int size, int mode) {
14443 return size + mode;
14444 }
14445
14446 /**
14447 * Extracts the mode from the supplied measure specification.
14448 *
14449 * @param measureSpec the measure specification to extract the mode from
14450 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
14451 * {@link android.view.View.MeasureSpec#AT_MOST} or
14452 * {@link android.view.View.MeasureSpec#EXACTLY}
14453 */
14454 public static int getMode(int measureSpec) {
14455 return (measureSpec & MODE_MASK);
14456 }
14457
14458 /**
14459 * Extracts the size from the supplied measure specification.
14460 *
14461 * @param measureSpec the measure specification to extract the size from
14462 * @return the size in pixels defined in the supplied measure specification
14463 */
14464 public static int getSize(int measureSpec) {
14465 return (measureSpec & ~MODE_MASK);
14466 }
14467
14468 /**
14469 * Returns a String representation of the specified measure
14470 * specification.
14471 *
14472 * @param measureSpec the measure specification to convert to a String
14473 * @return a String with the following format: "MeasureSpec: MODE SIZE"
14474 */
14475 public static String toString(int measureSpec) {
14476 int mode = getMode(measureSpec);
14477 int size = getSize(measureSpec);
14478
14479 StringBuilder sb = new StringBuilder("MeasureSpec: ");
14480
14481 if (mode == UNSPECIFIED)
14482 sb.append("UNSPECIFIED ");
14483 else if (mode == EXACTLY)
14484 sb.append("EXACTLY ");
14485 else if (mode == AT_MOST)
14486 sb.append("AT_MOST ");
14487 else
14488 sb.append(mode).append(" ");
14489
14490 sb.append(size);
14491 return sb.toString();
14492 }
14493 }
14494
14495 class CheckForLongPress implements Runnable {
14496
14497 private int mOriginalWindowAttachCount;
14498
14499 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070014500 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014501 && mOriginalWindowAttachCount == mWindowAttachCount) {
14502 if (performLongClick()) {
14503 mHasPerformedLongPress = true;
14504 }
14505 }
14506 }
14507
14508 public void rememberWindowAttachCount() {
14509 mOriginalWindowAttachCount = mWindowAttachCount;
14510 }
14511 }
Joe Malin32736f02011-01-19 16:14:20 -080014512
Adam Powelle14579b2009-12-16 18:39:52 -080014513 private final class CheckForTap implements Runnable {
14514 public void run() {
14515 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080014516 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014517 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080014518 }
14519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014520
Adam Powella35d7682010-03-12 14:48:13 -080014521 private final class PerformClick implements Runnable {
14522 public void run() {
14523 performClick();
14524 }
14525 }
14526
Dianne Hackborn63042d62011-01-26 18:56:29 -080014527 /** @hide */
14528 public void hackTurnOffWindowResizeAnim(boolean off) {
14529 mAttachInfo.mTurnOffWindowResizeAnim = off;
14530 }
Joe Malin32736f02011-01-19 16:14:20 -080014531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014532 /**
Chet Haasea00f3862011-02-22 06:34:40 -080014533 * This method returns a ViewPropertyAnimator object, which can be used to animate
14534 * specific properties on this View.
14535 *
14536 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
14537 */
14538 public ViewPropertyAnimator animate() {
14539 if (mAnimator == null) {
14540 mAnimator = new ViewPropertyAnimator(this);
14541 }
14542 return mAnimator;
14543 }
14544
14545 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014546 * Interface definition for a callback to be invoked when a key event is
14547 * dispatched to this view. The callback will be invoked before the key
14548 * event is given to the view.
14549 */
14550 public interface OnKeyListener {
14551 /**
14552 * Called when a key is dispatched to a view. This allows listeners to
14553 * get a chance to respond before the target view.
14554 *
14555 * @param v The view the key has been dispatched to.
14556 * @param keyCode The code for the physical key that was pressed
14557 * @param event The KeyEvent object containing full information about
14558 * the event.
14559 * @return True if the listener has consumed the event, false otherwise.
14560 */
14561 boolean onKey(View v, int keyCode, KeyEvent event);
14562 }
14563
14564 /**
14565 * Interface definition for a callback to be invoked when a touch event is
14566 * dispatched to this view. The callback will be invoked before the touch
14567 * event is given to the view.
14568 */
14569 public interface OnTouchListener {
14570 /**
14571 * Called when a touch event is dispatched to a view. This allows listeners to
14572 * get a chance to respond before the target view.
14573 *
14574 * @param v The view the touch event has been dispatched to.
14575 * @param event The MotionEvent object containing full information about
14576 * the event.
14577 * @return True if the listener has consumed the event, false otherwise.
14578 */
14579 boolean onTouch(View v, MotionEvent event);
14580 }
14581
14582 /**
Jeff Brown10b62902011-06-20 16:40:37 -070014583 * Interface definition for a callback to be invoked when a hover event is
14584 * dispatched to this view. The callback will be invoked before the hover
14585 * event is given to the view.
14586 */
14587 public interface OnHoverListener {
14588 /**
14589 * Called when a hover event is dispatched to a view. This allows listeners to
14590 * get a chance to respond before the target view.
14591 *
14592 * @param v The view the hover event has been dispatched to.
14593 * @param event The MotionEvent object containing full information about
14594 * the event.
14595 * @return True if the listener has consumed the event, false otherwise.
14596 */
14597 boolean onHover(View v, MotionEvent event);
14598 }
14599
14600 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014601 * Interface definition for a callback to be invoked when a generic motion event is
14602 * dispatched to this view. The callback will be invoked before the generic motion
14603 * event is given to the view.
14604 */
14605 public interface OnGenericMotionListener {
14606 /**
14607 * Called when a generic motion event is dispatched to a view. This allows listeners to
14608 * get a chance to respond before the target view.
14609 *
14610 * @param v The view the generic motion event has been dispatched to.
14611 * @param event The MotionEvent object containing full information about
14612 * the event.
14613 * @return True if the listener has consumed the event, false otherwise.
14614 */
14615 boolean onGenericMotion(View v, MotionEvent event);
14616 }
14617
14618 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014619 * Interface definition for a callback to be invoked when a view has been clicked and held.
14620 */
14621 public interface OnLongClickListener {
14622 /**
14623 * Called when a view has been clicked and held.
14624 *
14625 * @param v The view that was clicked and held.
14626 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080014627 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014628 */
14629 boolean onLongClick(View v);
14630 }
14631
14632 /**
Chris Tate32affef2010-10-18 15:29:21 -070014633 * Interface definition for a callback to be invoked when a drag is being dispatched
14634 * to this view. The callback will be invoked before the hosting view's own
14635 * onDrag(event) method. If the listener wants to fall back to the hosting view's
14636 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070014637 *
14638 * <div class="special reference">
14639 * <h3>Developer Guides</h3>
14640 * <p>For a guide to implementing drag and drop features, read the
14641 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
14642 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070014643 */
14644 public interface OnDragListener {
14645 /**
14646 * Called when a drag event is dispatched to a view. This allows listeners
14647 * to get a chance to override base View behavior.
14648 *
Joe Malin32736f02011-01-19 16:14:20 -080014649 * @param v The View that received the drag event.
14650 * @param event The {@link android.view.DragEvent} object for the drag event.
14651 * @return {@code true} if the drag event was handled successfully, or {@code false}
14652 * if the drag event was not handled. Note that {@code false} will trigger the View
14653 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070014654 */
14655 boolean onDrag(View v, DragEvent event);
14656 }
14657
14658 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014659 * Interface definition for a callback to be invoked when the focus state of
14660 * a view changed.
14661 */
14662 public interface OnFocusChangeListener {
14663 /**
14664 * Called when the focus state of a view has changed.
14665 *
14666 * @param v The view whose state has changed.
14667 * @param hasFocus The new focus state of v.
14668 */
14669 void onFocusChange(View v, boolean hasFocus);
14670 }
14671
14672 /**
14673 * Interface definition for a callback to be invoked when a view is clicked.
14674 */
14675 public interface OnClickListener {
14676 /**
14677 * Called when a view has been clicked.
14678 *
14679 * @param v The view that was clicked.
14680 */
14681 void onClick(View v);
14682 }
14683
14684 /**
14685 * Interface definition for a callback to be invoked when the context menu
14686 * for this view is being built.
14687 */
14688 public interface OnCreateContextMenuListener {
14689 /**
14690 * Called when the context menu for this view is being built. It is not
14691 * safe to hold onto the menu after this method returns.
14692 *
14693 * @param menu The context menu that is being built
14694 * @param v The view for which the context menu is being built
14695 * @param menuInfo Extra information about the item for which the
14696 * context menu should be shown. This information will vary
14697 * depending on the class of v.
14698 */
14699 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
14700 }
14701
Joe Onorato664644d2011-01-23 17:53:23 -080014702 /**
14703 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014704 * visibility. This reports <strong>global</strong> changes to the system UI
14705 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080014706 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070014707 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080014708 */
14709 public interface OnSystemUiVisibilityChangeListener {
14710 /**
14711 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070014712 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080014713 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040014714 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014715 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
14716 * <strong>global</strong> state of the UI visibility flags, not what your
14717 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080014718 */
14719 public void onSystemUiVisibilityChange(int visibility);
14720 }
14721
Adam Powell4afd62b2011-02-18 15:02:18 -080014722 /**
14723 * Interface definition for a callback to be invoked when this view is attached
14724 * or detached from its window.
14725 */
14726 public interface OnAttachStateChangeListener {
14727 /**
14728 * Called when the view is attached to a window.
14729 * @param v The view that was attached
14730 */
14731 public void onViewAttachedToWindow(View v);
14732 /**
14733 * Called when the view is detached from a window.
14734 * @param v The view that was detached
14735 */
14736 public void onViewDetachedFromWindow(View v);
14737 }
14738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014739 private final class UnsetPressedState implements Runnable {
14740 public void run() {
14741 setPressed(false);
14742 }
14743 }
14744
14745 /**
14746 * Base class for derived classes that want to save and restore their own
14747 * state in {@link android.view.View#onSaveInstanceState()}.
14748 */
14749 public static class BaseSavedState extends AbsSavedState {
14750 /**
14751 * Constructor used when reading from a parcel. Reads the state of the superclass.
14752 *
14753 * @param source
14754 */
14755 public BaseSavedState(Parcel source) {
14756 super(source);
14757 }
14758
14759 /**
14760 * Constructor called by derived classes when creating their SavedState objects
14761 *
14762 * @param superState The state of the superclass of this view
14763 */
14764 public BaseSavedState(Parcelable superState) {
14765 super(superState);
14766 }
14767
14768 public static final Parcelable.Creator<BaseSavedState> CREATOR =
14769 new Parcelable.Creator<BaseSavedState>() {
14770 public BaseSavedState createFromParcel(Parcel in) {
14771 return new BaseSavedState(in);
14772 }
14773
14774 public BaseSavedState[] newArray(int size) {
14775 return new BaseSavedState[size];
14776 }
14777 };
14778 }
14779
14780 /**
14781 * A set of information given to a view when it is attached to its parent
14782 * window.
14783 */
14784 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014785 interface Callbacks {
14786 void playSoundEffect(int effectId);
14787 boolean performHapticFeedback(int effectId, boolean always);
14788 }
14789
14790 /**
14791 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
14792 * to a Handler. This class contains the target (View) to invalidate and
14793 * the coordinates of the dirty rectangle.
14794 *
14795 * For performance purposes, this class also implements a pool of up to
14796 * POOL_LIMIT objects that get reused. This reduces memory allocations
14797 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014798 */
Romain Guyd928d682009-03-31 17:52:16 -070014799 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014800 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070014801 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
14802 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070014803 public InvalidateInfo newInstance() {
14804 return new InvalidateInfo();
14805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014806
Romain Guyd928d682009-03-31 17:52:16 -070014807 public void onAcquired(InvalidateInfo element) {
14808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014809
Romain Guyd928d682009-03-31 17:52:16 -070014810 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070014811 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070014812 }
14813 }, POOL_LIMIT)
14814 );
14815
14816 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014817 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014818
14819 View target;
14820
14821 int left;
14822 int top;
14823 int right;
14824 int bottom;
14825
Romain Guyd928d682009-03-31 17:52:16 -070014826 public void setNextPoolable(InvalidateInfo element) {
14827 mNext = element;
14828 }
14829
14830 public InvalidateInfo getNextPoolable() {
14831 return mNext;
14832 }
14833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014834 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070014835 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014836 }
14837
14838 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070014839 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014840 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014841
14842 public boolean isPooled() {
14843 return mIsPooled;
14844 }
14845
14846 public void setPooled(boolean isPooled) {
14847 mIsPooled = isPooled;
14848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014849 }
14850
14851 final IWindowSession mSession;
14852
14853 final IWindow mWindow;
14854
14855 final IBinder mWindowToken;
14856
14857 final Callbacks mRootCallbacks;
14858
Romain Guy59a12ca2011-06-09 17:48:21 -070014859 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080014860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014861 /**
14862 * The top view of the hierarchy.
14863 */
14864 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070014865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014866 IBinder mPanelParentWindowToken;
14867 Surface mSurface;
14868
Romain Guyb051e892010-09-28 19:09:36 -070014869 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014870 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070014871 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080014872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014873 /**
Romain Guy8506ab42009-06-11 17:35:47 -070014874 * Scale factor used by the compatibility mode
14875 */
14876 float mApplicationScale;
14877
14878 /**
14879 * Indicates whether the application is in compatibility mode
14880 */
14881 boolean mScalingRequired;
14882
14883 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014884 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080014885 */
14886 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080014887
Dianne Hackborn63042d62011-01-26 18:56:29 -080014888 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014889 * Left position of this view's window
14890 */
14891 int mWindowLeft;
14892
14893 /**
14894 * Top position of this view's window
14895 */
14896 int mWindowTop;
14897
14898 /**
Adam Powell26153a32010-11-08 15:22:27 -080014899 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070014900 */
Adam Powell26153a32010-11-08 15:22:27 -080014901 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070014902
14903 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014904 * For windows that are full-screen but using insets to layout inside
14905 * of the screen decorations, these are the current insets for the
14906 * content of the window.
14907 */
14908 final Rect mContentInsets = new Rect();
14909
14910 /**
14911 * For windows that are full-screen but using insets to layout inside
14912 * of the screen decorations, these are the current insets for the
14913 * actual visible parts of the window.
14914 */
14915 final Rect mVisibleInsets = new Rect();
14916
14917 /**
14918 * The internal insets given by this window. This value is
14919 * supplied by the client (through
14920 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
14921 * be given to the window manager when changed to be used in laying
14922 * out windows behind it.
14923 */
14924 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
14925 = new ViewTreeObserver.InternalInsetsInfo();
14926
14927 /**
14928 * All views in the window's hierarchy that serve as scroll containers,
14929 * used to determine if the window can be resized or must be panned
14930 * to adjust for a soft input area.
14931 */
14932 final ArrayList<View> mScrollContainers = new ArrayList<View>();
14933
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070014934 final KeyEvent.DispatcherState mKeyDispatchState
14935 = new KeyEvent.DispatcherState();
14936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014937 /**
14938 * Indicates whether the view's window currently has the focus.
14939 */
14940 boolean mHasWindowFocus;
14941
14942 /**
14943 * The current visibility of the window.
14944 */
14945 int mWindowVisibility;
14946
14947 /**
14948 * Indicates the time at which drawing started to occur.
14949 */
14950 long mDrawingTime;
14951
14952 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070014953 * Indicates whether or not ignoring the DIRTY_MASK flags.
14954 */
14955 boolean mIgnoreDirtyState;
14956
14957 /**
Romain Guy02ccac62011-06-24 13:20:23 -070014958 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
14959 * to avoid clearing that flag prematurely.
14960 */
14961 boolean mSetIgnoreDirtyState = false;
14962
14963 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014964 * Indicates whether the view's window is currently in touch mode.
14965 */
14966 boolean mInTouchMode;
14967
14968 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014969 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014970 * the next time it performs a traversal
14971 */
14972 boolean mRecomputeGlobalAttributes;
14973
14974 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014975 * Always report new attributes at next traversal.
14976 */
14977 boolean mForceReportNewAttributes;
14978
14979 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014980 * Set during a traveral if any views want to keep the screen on.
14981 */
14982 boolean mKeepScreenOn;
14983
14984 /**
Joe Onorato664644d2011-01-23 17:53:23 -080014985 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
14986 */
14987 int mSystemUiVisibility;
14988
14989 /**
14990 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
14991 * attached.
14992 */
14993 boolean mHasSystemUiListeners;
14994
14995 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014996 * Set if the visibility of any views has changed.
14997 */
14998 boolean mViewVisibilityChanged;
14999
15000 /**
15001 * Set to true if a view has been scrolled.
15002 */
15003 boolean mViewScrollChanged;
15004
15005 /**
15006 * Global to the view hierarchy used as a temporary for dealing with
15007 * x/y points in the transparent region computations.
15008 */
15009 final int[] mTransparentLocation = new int[2];
15010
15011 /**
15012 * Global to the view hierarchy used as a temporary for dealing with
15013 * x/y points in the ViewGroup.invalidateChild implementation.
15014 */
15015 final int[] mInvalidateChildLocation = new int[2];
15016
Chet Haasec3aa3612010-06-17 08:50:37 -070015017
15018 /**
15019 * Global to the view hierarchy used as a temporary for dealing with
15020 * x/y location when view is transformed.
15021 */
15022 final float[] mTmpTransformLocation = new float[2];
15023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015024 /**
15025 * The view tree observer used to dispatch global events like
15026 * layout, pre-draw, touch mode change, etc.
15027 */
15028 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
15029
15030 /**
15031 * A Canvas used by the view hierarchy to perform bitmap caching.
15032 */
15033 Canvas mCanvas;
15034
15035 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080015036 * The view root impl.
15037 */
15038 final ViewRootImpl mViewRootImpl;
15039
15040 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015041 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015042 * handler can be used to pump events in the UI events queue.
15043 */
15044 final Handler mHandler;
15045
15046 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015047 * Temporary for use in computing invalidate rectangles while
15048 * calling up the hierarchy.
15049 */
15050 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070015051
15052 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070015053 * Temporary for use in computing hit areas with transformed views
15054 */
15055 final RectF mTmpTransformRect = new RectF();
15056
15057 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070015058 * Temporary list for use in collecting focusable descendents of a view.
15059 */
15060 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
15061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015062 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070015063 * The id of the window for accessibility purposes.
15064 */
15065 int mAccessibilityWindowId = View.NO_ID;
15066
15067 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015068 * Creates a new set of attachment information with the specified
15069 * events handler and thread.
15070 *
15071 * @param handler the events handler the view must use
15072 */
15073 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080015074 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015075 mSession = session;
15076 mWindow = window;
15077 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080015078 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015079 mHandler = handler;
15080 mRootCallbacks = effectPlayer;
15081 }
15082 }
15083
15084 /**
15085 * <p>ScrollabilityCache holds various fields used by a View when scrolling
15086 * is supported. This avoids keeping too many unused fields in most
15087 * instances of View.</p>
15088 */
Mike Cleronf116bf82009-09-27 19:14:12 -070015089 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080015090
Mike Cleronf116bf82009-09-27 19:14:12 -070015091 /**
15092 * Scrollbars are not visible
15093 */
15094 public static final int OFF = 0;
15095
15096 /**
15097 * Scrollbars are visible
15098 */
15099 public static final int ON = 1;
15100
15101 /**
15102 * Scrollbars are fading away
15103 */
15104 public static final int FADING = 2;
15105
15106 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080015107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015108 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070015109 public int scrollBarDefaultDelayBeforeFade;
15110 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015111
15112 public int scrollBarSize;
15113 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070015114 public float[] interpolatorValues;
15115 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015116
15117 public final Paint paint;
15118 public final Matrix matrix;
15119 public Shader shader;
15120
Mike Cleronf116bf82009-09-27 19:14:12 -070015121 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
15122
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015123 private static final float[] OPAQUE = { 255 };
15124 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080015125
Mike Cleronf116bf82009-09-27 19:14:12 -070015126 /**
15127 * When fading should start. This time moves into the future every time
15128 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
15129 */
15130 public long fadeStartTime;
15131
15132
15133 /**
15134 * The current state of the scrollbars: ON, OFF, or FADING
15135 */
15136 public int state = OFF;
15137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015138 private int mLastColor;
15139
Mike Cleronf116bf82009-09-27 19:14:12 -070015140 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015141 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
15142 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070015143 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
15144 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015145
15146 paint = new Paint();
15147 matrix = new Matrix();
15148 // use use a height of 1, and then wack the matrix each time we
15149 // actually use it.
15150 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070015151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015152 paint.setShader(shader);
15153 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070015154 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015155 }
Romain Guy8506ab42009-06-11 17:35:47 -070015156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015157 public void setFadeColor(int color) {
15158 if (color != 0 && color != mLastColor) {
15159 mLastColor = color;
15160 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070015161
Romain Guye55e1a72009-08-27 10:42:26 -070015162 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
15163 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070015164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015165 paint.setShader(shader);
15166 // Restore the default transfer mode (src_over)
15167 paint.setXfermode(null);
15168 }
15169 }
Joe Malin32736f02011-01-19 16:14:20 -080015170
Mike Cleronf116bf82009-09-27 19:14:12 -070015171 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070015172 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070015173 if (now >= fadeStartTime) {
15174
15175 // the animation fades the scrollbars out by changing
15176 // the opacity (alpha) from fully opaque to fully
15177 // transparent
15178 int nextFrame = (int) now;
15179 int framesCount = 0;
15180
15181 Interpolator interpolator = scrollBarInterpolator;
15182
15183 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015184 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070015185
15186 // End transparent
15187 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015188 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070015189
15190 state = FADING;
15191
15192 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080015193 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070015194 }
15195 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070015196 }
Mike Cleronf116bf82009-09-27 19:14:12 -070015197
Svetoslav Ganova0156172011-06-26 17:55:44 -070015198 /**
15199 * Resuable callback for sending
15200 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
15201 */
15202 private class SendViewScrolledAccessibilityEvent implements Runnable {
15203 public volatile boolean mIsPending;
15204
15205 public void run() {
15206 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
15207 mIsPending = false;
15208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015209 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015210
15211 /**
15212 * <p>
15213 * This class represents a delegate that can be registered in a {@link View}
15214 * to enhance accessibility support via composition rather via inheritance.
15215 * It is specifically targeted to widget developers that extend basic View
15216 * classes i.e. classes in package android.view, that would like their
15217 * applications to be backwards compatible.
15218 * </p>
15219 * <p>
15220 * A scenario in which a developer would like to use an accessibility delegate
15221 * is overriding a method introduced in a later API version then the minimal API
15222 * version supported by the application. For example, the method
15223 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
15224 * in API version 4 when the accessibility APIs were first introduced. If a
15225 * developer would like his application to run on API version 4 devices (assuming
15226 * all other APIs used by the application are version 4 or lower) and take advantage
15227 * of this method, instead of overriding the method which would break the application's
15228 * backwards compatibility, he can override the corresponding method in this
15229 * delegate and register the delegate in the target View if the API version of
15230 * the system is high enough i.e. the API version is same or higher to the API
15231 * version that introduced
15232 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
15233 * </p>
15234 * <p>
15235 * Here is an example implementation:
15236 * </p>
15237 * <code><pre><p>
15238 * if (Build.VERSION.SDK_INT >= 14) {
15239 * // If the API version is equal of higher than the version in
15240 * // which onInitializeAccessibilityNodeInfo was introduced we
15241 * // register a delegate with a customized implementation.
15242 * View view = findViewById(R.id.view_id);
15243 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
15244 * public void onInitializeAccessibilityNodeInfo(View host,
15245 * AccessibilityNodeInfo info) {
15246 * // Let the default implementation populate the info.
15247 * super.onInitializeAccessibilityNodeInfo(host, info);
15248 * // Set some other information.
15249 * info.setEnabled(host.isEnabled());
15250 * }
15251 * });
15252 * }
15253 * </code></pre></p>
15254 * <p>
15255 * This delegate contains methods that correspond to the accessibility methods
15256 * in View. If a delegate has been specified the implementation in View hands
15257 * off handling to the corresponding method in this delegate. The default
15258 * implementation the delegate methods behaves exactly as the corresponding
15259 * method in View for the case of no accessibility delegate been set. Hence,
15260 * to customize the behavior of a View method, clients can override only the
15261 * corresponding delegate method without altering the behavior of the rest
15262 * accessibility related methods of the host view.
15263 * </p>
15264 */
15265 public static class AccessibilityDelegate {
15266
15267 /**
15268 * Sends an accessibility event of the given type. If accessibility is not
15269 * enabled this method has no effect.
15270 * <p>
15271 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
15272 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
15273 * been set.
15274 * </p>
15275 *
15276 * @param host The View hosting the delegate.
15277 * @param eventType The type of the event to send.
15278 *
15279 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
15280 */
15281 public void sendAccessibilityEvent(View host, int eventType) {
15282 host.sendAccessibilityEventInternal(eventType);
15283 }
15284
15285 /**
15286 * Sends an accessibility event. This method behaves exactly as
15287 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
15288 * empty {@link AccessibilityEvent} and does not perform a check whether
15289 * accessibility is enabled.
15290 * <p>
15291 * The default implementation behaves as
15292 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15293 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
15294 * the case of no accessibility delegate been set.
15295 * </p>
15296 *
15297 * @param host The View hosting the delegate.
15298 * @param event The event to send.
15299 *
15300 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15301 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15302 */
15303 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
15304 host.sendAccessibilityEventUncheckedInternal(event);
15305 }
15306
15307 /**
15308 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
15309 * to its children for adding their text content to the event.
15310 * <p>
15311 * The default implementation behaves as
15312 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15313 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
15314 * the case of no accessibility delegate been set.
15315 * </p>
15316 *
15317 * @param host The View hosting the delegate.
15318 * @param event The event.
15319 * @return True if the event population was completed.
15320 *
15321 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15322 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15323 */
15324 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15325 return host.dispatchPopulateAccessibilityEventInternal(event);
15326 }
15327
15328 /**
15329 * Gives a chance to the host View to populate the accessibility event with its
15330 * text content.
15331 * <p>
15332 * The default implementation behaves as
15333 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
15334 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
15335 * the case of no accessibility delegate been set.
15336 * </p>
15337 *
15338 * @param host The View hosting the delegate.
15339 * @param event The accessibility event which to populate.
15340 *
15341 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
15342 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
15343 */
15344 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15345 host.onPopulateAccessibilityEventInternal(event);
15346 }
15347
15348 /**
15349 * Initializes an {@link AccessibilityEvent} with information about the
15350 * the host View which is the event source.
15351 * <p>
15352 * The default implementation behaves as
15353 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
15354 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
15355 * the case of no accessibility delegate been set.
15356 * </p>
15357 *
15358 * @param host The View hosting the delegate.
15359 * @param event The event to initialize.
15360 *
15361 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
15362 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
15363 */
15364 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
15365 host.onInitializeAccessibilityEventInternal(event);
15366 }
15367
15368 /**
15369 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
15370 * <p>
15371 * The default implementation behaves as
15372 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15373 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
15374 * the case of no accessibility delegate been set.
15375 * </p>
15376 *
15377 * @param host The View hosting the delegate.
15378 * @param info The instance to initialize.
15379 *
15380 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15381 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15382 */
15383 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
15384 host.onInitializeAccessibilityNodeInfoInternal(info);
15385 }
15386
15387 /**
15388 * Called when a child of the host View has requested sending an
15389 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
15390 * to augment the event.
15391 * <p>
15392 * The default implementation behaves as
15393 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15394 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
15395 * the case of no accessibility delegate been set.
15396 * </p>
15397 *
15398 * @param host The View hosting the delegate.
15399 * @param child The child which requests sending the event.
15400 * @param event The event to be sent.
15401 * @return True if the event should be sent
15402 *
15403 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15404 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15405 */
15406 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
15407 AccessibilityEvent event) {
15408 return host.onRequestSendAccessibilityEventInternal(child, event);
15409 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070015410
15411 /**
15412 * Gets the provider for managing a virtual view hierarchy rooted at this View
15413 * and reported to {@link android.accessibilityservice.AccessibilityService}s
15414 * that explore the window content.
15415 * <p>
15416 * The default implementation behaves as
15417 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
15418 * the case of no accessibility delegate been set.
15419 * </p>
15420 *
15421 * @return The provider.
15422 *
15423 * @see AccessibilityNodeProvider
15424 */
15425 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
15426 return null;
15427 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015429}