blob: 384e39a4d2e274c486f2376ce60af0e15c3b75a0 [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;
43import android.os.Message;
44import android.os.Parcel;
45import android.os.Parcelable;
46import android.os.RemoteException;
47import android.os.SystemClock;
Svetoslav Ganovea515ae2011-09-14 18:15:32 -070048import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070050import android.util.FloatProperty;
51import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070053import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070054import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070055import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070057import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080059import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070061import android.view.accessibility.AccessibilityEvent;
62import android.view.accessibility.AccessibilityEventSource;
63import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070064import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070065import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070067import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080068import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070069import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.view.inputmethod.InputConnection;
71import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.widget.ScrollBarDrawable;
73
Romain Guy1ef3fdb2011-09-09 15:30:30 -070074import static android.os.Build.VERSION_CODES.*;
75
Doug Feltcb3791202011-07-07 11:57:48 -070076import com.android.internal.R;
77import com.android.internal.util.Predicate;
78import com.android.internal.view.menu.MenuBuilder;
79
Christopher Tatea0374192010-10-05 13:06:41 -070080import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070081import java.lang.reflect.InvocationTargetException;
82import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import java.util.ArrayList;
84import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070085import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080086import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
88/**
89 * <p>
90 * This class represents the basic building block for user interface components. A View
91 * occupies a rectangular area on the screen and is responsible for drawing and
92 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070093 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
95 * are invisible containers that hold other Views (or other ViewGroups) and define
96 * their layout properties.
97 * </p>
98 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -070099 * <div class="special reference">
100 * <h3>Developer Guides</h3>
101 * <p>For information about using this class to develop your application's user interface,
102 * read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700104 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 * <a name="Using"></a>
106 * <h3>Using Views</h3>
107 * <p>
108 * All of the views in a window are arranged in a single tree. You can add views
109 * either from code or by specifying a tree of views in one or more XML layout
110 * files. There are many specialized subclasses of views that act as controls or
111 * are capable of displaying text, images, or other content.
112 * </p>
113 * <p>
114 * Once you have created a tree of views, there are typically a few types of
115 * common operations you may wish to perform:
116 * <ul>
117 * <li><strong>Set properties:</strong> for example setting the text of a
118 * {@link android.widget.TextView}. The available properties and the methods
119 * that set them will vary among the different subclasses of views. Note that
120 * properties that are known at build time can be set in the XML layout
121 * files.</li>
122 * <li><strong>Set focus:</strong> The framework will handled moving focus in
123 * response to user input. To force focus to a specific view, call
124 * {@link #requestFocus}.</li>
125 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
126 * that will be notified when something interesting happens to the view. For
127 * example, all views will let you set a listener to be notified when the view
128 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700129 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
130 * Other view subclasses offer more specialized listeners. For example, a Button
131 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700133 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * </ul>
135 * </p>
136 * <p><em>
137 * Note: The Android framework is responsible for measuring, laying out and
138 * drawing views. You should not call methods that perform these actions on
139 * views yourself unless you are actually implementing a
140 * {@link android.view.ViewGroup}.
141 * </em></p>
142 *
143 * <a name="Lifecycle"></a>
144 * <h3>Implementing a Custom View</h3>
145 *
146 * <p>
147 * To implement a custom view, you will usually begin by providing overrides for
148 * some of the standard methods that the framework calls on all views. You do
149 * not need to override all of these methods. In fact, you can start by just
150 * overriding {@link #onDraw(android.graphics.Canvas)}.
151 * <table border="2" width="85%" align="center" cellpadding="5">
152 * <thead>
153 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
154 * </thead>
155 *
156 * <tbody>
157 * <tr>
158 * <td rowspan="2">Creation</td>
159 * <td>Constructors</td>
160 * <td>There is a form of the constructor that are called when the view
161 * is created from code and a form that is called when the view is
162 * inflated from a layout file. The second form should parse and apply
163 * any attributes defined in the layout file.
164 * </td>
165 * </tr>
166 * <tr>
167 * <td><code>{@link #onFinishInflate()}</code></td>
168 * <td>Called after a view and all of its children has been inflated
169 * from XML.</td>
170 * </tr>
171 *
172 * <tr>
173 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700174 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 * <td>Called to determine the size requirements for this view and all
176 * of its children.
177 * </td>
178 * </tr>
179 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700180 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <td>Called when this view should assign a size and position to all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700186 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * <td>Called when the size of this view has changed.
188 * </td>
189 * </tr>
190 *
191 * <tr>
192 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700193 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 * <td>Called when the view should render its content.
195 * </td>
196 * </tr>
197 *
198 * <tr>
199 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700200 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 * <td>Called when a new key event occurs.
202 * </td>
203 * </tr>
204 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700205 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 * <td>Called when a key up event occurs.
207 * </td>
208 * </tr>
209 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700210 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 * <td>Called when a trackball motion event occurs.
212 * </td>
213 * </tr>
214 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700215 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 * <td>Called when a touch screen motion event occurs.
217 * </td>
218 * </tr>
219 *
220 * <tr>
221 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700222 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 * <td>Called when the view gains or loses focus.
224 * </td>
225 * </tr>
226 *
227 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700228 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <td>Called when the window containing the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
234 * <td rowspan="3">Attaching</td>
235 * <td><code>{@link #onAttachedToWindow()}</code></td>
236 * <td>Called when the view is attached to a window.
237 * </td>
238 * </tr>
239 *
240 * <tr>
241 * <td><code>{@link #onDetachedFromWindow}</code></td>
242 * <td>Called when the view is detached from its window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700247 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 * <td>Called when the visibility of the window containing the view
249 * has changed.
250 * </td>
251 * </tr>
252 * </tbody>
253 *
254 * </table>
255 * </p>
256 *
257 * <a name="IDs"></a>
258 * <h3>IDs</h3>
259 * Views may have an integer id associated with them. These ids are typically
260 * assigned in the layout XML files, and are used to find specific views within
261 * the view tree. A common pattern is to:
262 * <ul>
263 * <li>Define a Button in the layout file and assign it a unique ID.
264 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700265 * &lt;Button
266 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 * android:layout_width="wrap_content"
268 * android:layout_height="wrap_content"
269 * android:text="@string/my_button_text"/&gt;
270 * </pre></li>
271 * <li>From the onCreate method of an Activity, find the Button
272 * <pre class="prettyprint">
273 * Button myButton = (Button) findViewById(R.id.my_button);
274 * </pre></li>
275 * </ul>
276 * <p>
277 * View IDs need not be unique throughout the tree, but it is good practice to
278 * ensure that they are at least unique within the part of the tree you are
279 * searching.
280 * </p>
281 *
282 * <a name="Position"></a>
283 * <h3>Position</h3>
284 * <p>
285 * The geometry of a view is that of a rectangle. A view has a location,
286 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
287 * two dimensions, expressed as a width and a height. The unit for location
288 * and dimensions is the pixel.
289 * </p>
290 *
291 * <p>
292 * It is possible to retrieve the location of a view by invoking the methods
293 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
294 * coordinate of the rectangle representing the view. The latter returns the
295 * top, or Y, coordinate of the rectangle representing the view. These methods
296 * both return the location of the view relative to its parent. For instance,
297 * when getLeft() returns 20, that means the view is located 20 pixels to the
298 * right of the left edge of its direct parent.
299 * </p>
300 *
301 * <p>
302 * In addition, several convenience methods are offered to avoid unnecessary
303 * computations, namely {@link #getRight()} and {@link #getBottom()}.
304 * These methods return the coordinates of the right and bottom edges of the
305 * rectangle representing the view. For instance, calling {@link #getRight()}
306 * is similar to the following computation: <code>getLeft() + getWidth()</code>
307 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
308 * </p>
309 *
310 * <a name="SizePaddingMargins"></a>
311 * <h3>Size, padding and margins</h3>
312 * <p>
313 * The size of a view is expressed with a width and a height. A view actually
314 * possess two pairs of width and height values.
315 * </p>
316 *
317 * <p>
318 * The first pair is known as <em>measured width</em> and
319 * <em>measured height</em>. These dimensions define how big a view wants to be
320 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
321 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
322 * and {@link #getMeasuredHeight()}.
323 * </p>
324 *
325 * <p>
326 * The second pair is simply known as <em>width</em> and <em>height</em>, or
327 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
328 * dimensions define the actual size of the view on screen, at drawing time and
329 * after layout. These values may, but do not have to, be different from the
330 * measured width and height. The width and height can be obtained by calling
331 * {@link #getWidth()} and {@link #getHeight()}.
332 * </p>
333 *
334 * <p>
335 * To measure its dimensions, a view takes into account its padding. The padding
336 * is expressed in pixels for the left, top, right and bottom parts of the view.
337 * Padding can be used to offset the content of the view by a specific amount of
338 * pixels. For instance, a left padding of 2 will push the view's content by
339 * 2 pixels to the right of the left edge. Padding can be set using the
340 * {@link #setPadding(int, int, int, int)} method and queried by calling
341 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
Fabrice Di Megliod8703a92011-06-16 18:54:08 -0700342 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 * </p>
344 *
345 * <p>
346 * Even though a view can define a padding, it does not provide any support for
347 * margins. However, view groups provide such a support. Refer to
348 * {@link android.view.ViewGroup} and
349 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
350 * </p>
351 *
352 * <a name="Layout"></a>
353 * <h3>Layout</h3>
354 * <p>
355 * Layout is a two pass process: a measure pass and a layout pass. The measuring
356 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
357 * of the view tree. Each view pushes dimension specifications down the tree
358 * during the recursion. At the end of the measure pass, every view has stored
359 * its measurements. The second pass happens in
360 * {@link #layout(int,int,int,int)} and is also top-down. During
361 * this pass each parent is responsible for positioning all of its children
362 * using the sizes computed in the measure pass.
363 * </p>
364 *
365 * <p>
366 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
367 * {@link #getMeasuredHeight()} values must be set, along with those for all of
368 * that view's descendants. A view's measured width and measured height values
369 * must respect the constraints imposed by the view's parents. This guarantees
370 * that at the end of the measure pass, all parents accept all of their
371 * children's measurements. A parent view may call measure() more than once on
372 * its children. For example, the parent may measure each child once with
373 * unspecified dimensions to find out how big they want to be, then call
374 * measure() on them again with actual numbers if the sum of all the children's
375 * unconstrained sizes is too big or too small.
376 * </p>
377 *
378 * <p>
379 * The measure pass uses two classes to communicate dimensions. The
380 * {@link MeasureSpec} class is used by views to tell their parents how they
381 * want to be measured and positioned. The base LayoutParams class just
382 * describes how big the view wants to be for both width and height. For each
383 * dimension, it can specify one of:
384 * <ul>
385 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800386 * <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 -0800387 * (minus padding)
388 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
389 * enclose its content (plus padding).
390 * </ul>
391 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
392 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
393 * an X and Y value.
394 * </p>
395 *
396 * <p>
397 * MeasureSpecs are used to push requirements down the tree from parent to
398 * child. A MeasureSpec can be in one of three modes:
399 * <ul>
400 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
401 * of a child view. For example, a LinearLayout may call measure() on its child
402 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
403 * tall the child view wants to be given a width of 240 pixels.
404 * <li>EXACTLY: This is used by the parent to impose an exact size on the
405 * child. The child must use this size, and guarantee that all of its
406 * descendants will fit within this size.
407 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
408 * child. The child must gurantee that it and all of its descendants will fit
409 * within this size.
410 * </ul>
411 * </p>
412 *
413 * <p>
414 * To intiate a layout, call {@link #requestLayout}. This method is typically
415 * called by a view on itself when it believes that is can no longer fit within
416 * its current bounds.
417 * </p>
418 *
419 * <a name="Drawing"></a>
420 * <h3>Drawing</h3>
421 * <p>
422 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700423 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 * this means that parents will draw before (i.e., behind) their children, with
425 * siblings drawn in the order they appear in the tree.
426 * If you set a background drawable for a View, then the View will draw it for you
427 * before calling back to its <code>onDraw()</code> method.
428 * </p>
429 *
430 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700431 * 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 -0800432 * </p>
433 *
434 * <p>
435 * To force a view to draw, call {@link #invalidate()}.
436 * </p>
437 *
438 * <a name="EventHandlingThreading"></a>
439 * <h3>Event Handling and Threading</h3>
440 * <p>
441 * The basic cycle of a view is as follows:
442 * <ol>
443 * <li>An event comes in and is dispatched to the appropriate view. The view
444 * handles the event and notifies any listeners.</li>
445 * <li>If in the course of processing the event, the view's bounds may need
446 * to be changed, the view will call {@link #requestLayout()}.</li>
447 * <li>Similarly, if in the course of processing the event the view's appearance
448 * may need to be changed, the view will call {@link #invalidate()}.</li>
449 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
450 * the framework will take care of measuring, laying out, and drawing the tree
451 * as appropriate.</li>
452 * </ol>
453 * </p>
454 *
455 * <p><em>Note: The entire view tree is single threaded. You must always be on
456 * the UI thread when calling any method on any view.</em>
457 * If you are doing work on other threads and want to update the state of a view
458 * from that thread, you should use a {@link Handler}.
459 * </p>
460 *
461 * <a name="FocusHandling"></a>
462 * <h3>Focus Handling</h3>
463 * <p>
464 * The framework will handle routine focus movement in response to user input.
465 * This includes changing the focus as views are removed or hidden, or as new
466 * views become available. Views indicate their willingness to take focus
467 * through the {@link #isFocusable} method. To change whether a view can take
468 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
469 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
470 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
471 * </p>
472 * <p>
473 * Focus movement is based on an algorithm which finds the nearest neighbor in a
474 * given direction. In rare cases, the default algorithm may not match the
475 * intended behavior of the developer. In these situations, you can provide
476 * explicit overrides by using these XML attributes in the layout file:
477 * <pre>
478 * nextFocusDown
479 * nextFocusLeft
480 * nextFocusRight
481 * nextFocusUp
482 * </pre>
483 * </p>
484 *
485 *
486 * <p>
487 * To get a particular view to take focus, call {@link #requestFocus()}.
488 * </p>
489 *
490 * <a name="TouchMode"></a>
491 * <h3>Touch Mode</h3>
492 * <p>
493 * When a user is navigating a user interface via directional keys such as a D-pad, it is
494 * necessary to give focus to actionable items such as buttons so the user can see
495 * what will take input. If the device has touch capabilities, however, and the user
496 * begins interacting with the interface by touching it, it is no longer necessary to
497 * always highlight, or give focus to, a particular view. This motivates a mode
498 * for interaction named 'touch mode'.
499 * </p>
500 * <p>
501 * For a touch capable device, once the user touches the screen, the device
502 * will enter touch mode. From this point onward, only views for which
503 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
504 * Other views that are touchable, like buttons, will not take focus when touched; they will
505 * only fire the on click listeners.
506 * </p>
507 * <p>
508 * Any time a user hits a directional key, such as a D-pad direction, the view device will
509 * exit touch mode, and find a view to take focus, so that the user may resume interacting
510 * with the user interface without touching the screen again.
511 * </p>
512 * <p>
513 * The touch mode state is maintained across {@link android.app.Activity}s. Call
514 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
515 * </p>
516 *
517 * <a name="Scrolling"></a>
518 * <h3>Scrolling</h3>
519 * <p>
520 * The framework provides basic support for views that wish to internally
521 * scroll their content. This includes keeping track of the X and Y scroll
522 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800523 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700524 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 * </p>
526 *
527 * <a name="Tags"></a>
528 * <h3>Tags</h3>
529 * <p>
530 * Unlike IDs, tags are not used to identify views. Tags are essentially an
531 * extra piece of information that can be associated with a view. They are most
532 * often used as a convenience to store data related to views in the views
533 * themselves rather than by putting them in a separate structure.
534 * </p>
535 *
536 * <a name="Animation"></a>
537 * <h3>Animation</h3>
538 * <p>
539 * You can attach an {@link Animation} object to a view using
540 * {@link #setAnimation(Animation)} or
541 * {@link #startAnimation(Animation)}. The animation can alter the scale,
542 * rotation, translation and alpha of a view over time. If the animation is
543 * attached to a view that has children, the animation will affect the entire
544 * subtree rooted by that node. When an animation is started, the framework will
545 * take care of redrawing the appropriate views until the animation completes.
546 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800547 * <p>
548 * Starting with Android 3.0, the preferred way of animating views is to use the
549 * {@link android.animation} package APIs.
550 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 *
Jeff Brown85a31762010-09-01 17:01:00 -0700552 * <a name="Security"></a>
553 * <h3>Security</h3>
554 * <p>
555 * Sometimes it is essential that an application be able to verify that an action
556 * is being performed with the full knowledge and consent of the user, such as
557 * granting a permission request, making a purchase or clicking on an advertisement.
558 * Unfortunately, a malicious application could try to spoof the user into
559 * performing these actions, unaware, by concealing the intended purpose of the view.
560 * As a remedy, the framework offers a touch filtering mechanism that can be used to
561 * improve the security of views that provide access to sensitive functionality.
562 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700563 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800564 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700565 * will discard touches that are received whenever the view's window is obscured by
566 * another visible window. As a result, the view will not receive touches whenever a
567 * toast, dialog or other window appears above the view's window.
568 * </p><p>
569 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700570 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
571 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700572 * </p>
573 *
Romain Guy171c5922011-01-06 10:04:23 -0800574 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700575 * @attr ref android.R.styleable#View_background
576 * @attr ref android.R.styleable#View_clickable
577 * @attr ref android.R.styleable#View_contentDescription
578 * @attr ref android.R.styleable#View_drawingCacheQuality
579 * @attr ref android.R.styleable#View_duplicateParentState
580 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700581 * @attr ref android.R.styleable#View_requiresFadingEdge
Romain Guyd6a463a2009-05-21 23:10:10 -0700582 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700583 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700585 * @attr ref android.R.styleable#View_isScrollContainer
586 * @attr ref android.R.styleable#View_focusable
587 * @attr ref android.R.styleable#View_focusableInTouchMode
588 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
589 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800590 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700591 * @attr ref android.R.styleable#View_longClickable
592 * @attr ref android.R.styleable#View_minHeight
593 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 * @attr ref android.R.styleable#View_nextFocusDown
595 * @attr ref android.R.styleable#View_nextFocusLeft
596 * @attr ref android.R.styleable#View_nextFocusRight
597 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700598 * @attr ref android.R.styleable#View_onClick
599 * @attr ref android.R.styleable#View_padding
600 * @attr ref android.R.styleable#View_paddingBottom
601 * @attr ref android.R.styleable#View_paddingLeft
602 * @attr ref android.R.styleable#View_paddingRight
603 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800604 * @attr ref android.R.styleable#View_paddingStart
605 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700606 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800607 * @attr ref android.R.styleable#View_rotation
608 * @attr ref android.R.styleable#View_rotationX
609 * @attr ref android.R.styleable#View_rotationY
610 * @attr ref android.R.styleable#View_scaleX
611 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 * @attr ref android.R.styleable#View_scrollX
613 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700614 * @attr ref android.R.styleable#View_scrollbarSize
615 * @attr ref android.R.styleable#View_scrollbarStyle
616 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700617 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
618 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
620 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * @attr ref android.R.styleable#View_scrollbarThumbVertical
622 * @attr ref android.R.styleable#View_scrollbarTrackVertical
623 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
624 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700625 * @attr ref android.R.styleable#View_soundEffectsEnabled
626 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800627 * @attr ref android.R.styleable#View_transformPivotX
628 * @attr ref android.R.styleable#View_transformPivotY
629 * @attr ref android.R.styleable#View_translationX
630 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700631 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 *
633 * @see android.view.ViewGroup
634 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700635public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
636 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 private static final boolean DBG = false;
638
639 /**
640 * The logging tag used by this class with android.util.Log.
641 */
642 protected static final String VIEW_LOG_TAG = "View";
643
644 /**
645 * Used to mark a View that has no ID.
646 */
647 public static final int NO_ID = -1;
648
649 /**
650 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
651 * calling setFlags.
652 */
653 private static final int NOT_FOCUSABLE = 0x00000000;
654
655 /**
656 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
657 * setFlags.
658 */
659 private static final int FOCUSABLE = 0x00000001;
660
661 /**
662 * Mask for use with setFlags indicating bits used for focus.
663 */
664 private static final int FOCUSABLE_MASK = 0x00000001;
665
666 /**
667 * This view will adjust its padding to fit sytem windows (e.g. status bar)
668 */
669 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
670
671 /**
Scott Main812634c22011-07-27 13:22:35 -0700672 * This view is visible.
673 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
674 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 */
676 public static final int VISIBLE = 0x00000000;
677
678 /**
679 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700680 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
681 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 */
683 public static final int INVISIBLE = 0x00000004;
684
685 /**
686 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700687 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
688 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 */
690 public static final int GONE = 0x00000008;
691
692 /**
693 * Mask for use with setFlags indicating bits used for visibility.
694 * {@hide}
695 */
696 static final int VISIBILITY_MASK = 0x0000000C;
697
698 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
699
700 /**
701 * This view is enabled. Intrepretation varies by subclass.
702 * Use with ENABLED_MASK when calling setFlags.
703 * {@hide}
704 */
705 static final int ENABLED = 0x00000000;
706
707 /**
708 * This view is disabled. Intrepretation varies by subclass.
709 * Use with ENABLED_MASK when calling setFlags.
710 * {@hide}
711 */
712 static final int DISABLED = 0x00000020;
713
714 /**
715 * Mask for use with setFlags indicating bits used for indicating whether
716 * this view is enabled
717 * {@hide}
718 */
719 static final int ENABLED_MASK = 0x00000020;
720
721 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700722 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
723 * called and further optimizations will be performed. It is okay to have
724 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 * {@hide}
726 */
727 static final int WILL_NOT_DRAW = 0x00000080;
728
729 /**
730 * Mask for use with setFlags indicating bits used for indicating whether
731 * this view is will draw
732 * {@hide}
733 */
734 static final int DRAW_MASK = 0x00000080;
735
736 /**
737 * <p>This view doesn't show scrollbars.</p>
738 * {@hide}
739 */
740 static final int SCROLLBARS_NONE = 0x00000000;
741
742 /**
743 * <p>This view shows horizontal scrollbars.</p>
744 * {@hide}
745 */
746 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
747
748 /**
749 * <p>This view shows vertical scrollbars.</p>
750 * {@hide}
751 */
752 static final int SCROLLBARS_VERTICAL = 0x00000200;
753
754 /**
755 * <p>Mask for use with setFlags indicating bits used for indicating which
756 * scrollbars are enabled.</p>
757 * {@hide}
758 */
759 static final int SCROLLBARS_MASK = 0x00000300;
760
Jeff Brown85a31762010-09-01 17:01:00 -0700761 /**
762 * Indicates that the view should filter touches when its window is obscured.
763 * Refer to the class comments for more information about this security feature.
764 * {@hide}
765 */
766 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
767
768 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769
770 /**
771 * <p>This view doesn't show fading edges.</p>
772 * {@hide}
773 */
774 static final int FADING_EDGE_NONE = 0x00000000;
775
776 /**
777 * <p>This view shows horizontal fading edges.</p>
778 * {@hide}
779 */
780 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
781
782 /**
783 * <p>This view shows vertical fading edges.</p>
784 * {@hide}
785 */
786 static final int FADING_EDGE_VERTICAL = 0x00002000;
787
788 /**
789 * <p>Mask for use with setFlags indicating bits used for indicating which
790 * fading edges are enabled.</p>
791 * {@hide}
792 */
793 static final int FADING_EDGE_MASK = 0x00003000;
794
795 /**
796 * <p>Indicates this view can be clicked. When clickable, a View reacts
797 * to clicks by notifying the OnClickListener.<p>
798 * {@hide}
799 */
800 static final int CLICKABLE = 0x00004000;
801
802 /**
803 * <p>Indicates this view is caching its drawing into a bitmap.</p>
804 * {@hide}
805 */
806 static final int DRAWING_CACHE_ENABLED = 0x00008000;
807
808 /**
809 * <p>Indicates that no icicle should be saved for this view.<p>
810 * {@hide}
811 */
812 static final int SAVE_DISABLED = 0x000010000;
813
814 /**
815 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
816 * property.</p>
817 * {@hide}
818 */
819 static final int SAVE_DISABLED_MASK = 0x000010000;
820
821 /**
822 * <p>Indicates that no drawing cache should ever be created for this view.<p>
823 * {@hide}
824 */
825 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
826
827 /**
828 * <p>Indicates this view can take / keep focus when int touch mode.</p>
829 * {@hide}
830 */
831 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
832
833 /**
834 * <p>Enables low quality mode for the drawing cache.</p>
835 */
836 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
837
838 /**
839 * <p>Enables high quality mode for the drawing cache.</p>
840 */
841 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
842
843 /**
844 * <p>Enables automatic quality mode for the drawing cache.</p>
845 */
846 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
847
848 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
849 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
850 };
851
852 /**
853 * <p>Mask for use with setFlags indicating bits used for the cache
854 * quality property.</p>
855 * {@hide}
856 */
857 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
858
859 /**
860 * <p>
861 * Indicates this view can be long clicked. When long clickable, a View
862 * reacts to long clicks by notifying the OnLongClickListener or showing a
863 * context menu.
864 * </p>
865 * {@hide}
866 */
867 static final int LONG_CLICKABLE = 0x00200000;
868
869 /**
870 * <p>Indicates that this view gets its drawable states from its direct parent
871 * and ignores its original internal states.</p>
872 *
873 * @hide
874 */
875 static final int DUPLICATE_PARENT_STATE = 0x00400000;
876
877 /**
878 * The scrollbar style to display the scrollbars inside the content area,
879 * without increasing the padding. The scrollbars will be overlaid with
880 * translucency on the view's content.
881 */
882 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
883
884 /**
885 * The scrollbar style to display the scrollbars inside the padded area,
886 * increasing the padding of the view. The scrollbars will not overlap the
887 * content area of the view.
888 */
889 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
890
891 /**
892 * The scrollbar style to display the scrollbars at the edge of the view,
893 * without increasing the padding. The scrollbars will be overlaid with
894 * translucency.
895 */
896 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
897
898 /**
899 * The scrollbar style to display the scrollbars at the edge of the view,
900 * increasing the padding of the view. The scrollbars will only overlap the
901 * background, if any.
902 */
903 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
904
905 /**
906 * Mask to check if the scrollbar style is overlay or inset.
907 * {@hide}
908 */
909 static final int SCROLLBARS_INSET_MASK = 0x01000000;
910
911 /**
912 * Mask to check if the scrollbar style is inside or outside.
913 * {@hide}
914 */
915 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
916
917 /**
918 * Mask for scrollbar style.
919 * {@hide}
920 */
921 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
922
923 /**
924 * View flag indicating that the screen should remain on while the
925 * window containing this view is visible to the user. This effectively
926 * takes care of automatically setting the WindowManager's
927 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
928 */
929 public static final int KEEP_SCREEN_ON = 0x04000000;
930
931 /**
932 * View flag indicating whether this view should have sound effects enabled
933 * for events such as clicking and touching.
934 */
935 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
936
937 /**
938 * View flag indicating whether this view should have haptic feedback
939 * enabled for events such as long presses.
940 */
941 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
942
943 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700944 * <p>Indicates that the view hierarchy should stop saving state when
945 * it reaches this view. If state saving is initiated immediately at
946 * the view, it will be allowed.
947 * {@hide}
948 */
949 static final int PARENT_SAVE_DISABLED = 0x20000000;
950
951 /**
952 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
953 * {@hide}
954 */
955 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
956
957 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800958 * Horizontal direction of this view is from Left to Right.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700959 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800960 * {@hide}
961 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700962 public static final int LAYOUT_DIRECTION_LTR = 0x00000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800963
964 /**
965 * Horizontal direction of this view is from Right to Left.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700966 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800967 * {@hide}
968 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700969 public static final int LAYOUT_DIRECTION_RTL = 0x40000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800970
971 /**
972 * Horizontal direction of this view is inherited from its parent.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700973 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800974 * {@hide}
975 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700976 public static final int LAYOUT_DIRECTION_INHERIT = 0x80000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800977
978 /**
979 * Horizontal direction of this view is from deduced from the default language
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700980 * script for the locale. Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800981 * {@hide}
982 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700983 public static final int LAYOUT_DIRECTION_LOCALE = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800984
985 /**
986 * Mask for use with setFlags indicating bits used for horizontalDirection.
987 * {@hide}
988 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700989 static final int LAYOUT_DIRECTION_MASK = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800990
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700991 /*
992 * Array of horizontal direction flags for mapping attribute "horizontalDirection" to correct
993 * flag value.
994 * {@hide}
995 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700996 private static final int[] LAYOUT_DIRECTION_FLAGS = {LAYOUT_DIRECTION_LTR,
997 LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE};
Cibu Johny7632cb92010-02-22 13:01:02 -0800998
999 /**
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001000 * Default horizontalDirection.
1001 * {@hide}
1002 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07001003 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001004
1005 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001006 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1007 * should add all focusable Views regardless if they are focusable in touch mode.
1008 */
1009 public static final int FOCUSABLES_ALL = 0x00000000;
1010
1011 /**
1012 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1013 * should add only Views focusable in touch mode.
1014 */
1015 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1016
1017 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001018 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 * item.
1020 */
1021 public static final int FOCUS_BACKWARD = 0x00000001;
1022
1023 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001024 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 * item.
1026 */
1027 public static final int FOCUS_FORWARD = 0x00000002;
1028
1029 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001030 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 */
1032 public static final int FOCUS_LEFT = 0x00000011;
1033
1034 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001035 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 */
1037 public static final int FOCUS_UP = 0x00000021;
1038
1039 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001040 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 */
1042 public static final int FOCUS_RIGHT = 0x00000042;
1043
1044 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001045 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 */
1047 public static final int FOCUS_DOWN = 0x00000082;
1048
1049 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001050 * Bits of {@link #getMeasuredWidthAndState()} and
1051 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1052 */
1053 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1054
1055 /**
1056 * Bits of {@link #getMeasuredWidthAndState()} and
1057 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1058 */
1059 public static final int MEASURED_STATE_MASK = 0xff000000;
1060
1061 /**
1062 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1063 * for functions that combine both width and height into a single int,
1064 * such as {@link #getMeasuredState()} and the childState argument of
1065 * {@link #resolveSizeAndState(int, int, int)}.
1066 */
1067 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1068
1069 /**
1070 * Bit of {@link #getMeasuredWidthAndState()} and
1071 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1072 * is smaller that the space the view would like to have.
1073 */
1074 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1075
1076 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 * Base View state sets
1078 */
1079 // Singles
1080 /**
1081 * Indicates the view has no states set. States are used with
1082 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1083 * view depending on its state.
1084 *
1085 * @see android.graphics.drawable.Drawable
1086 * @see #getDrawableState()
1087 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001088 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 /**
1090 * Indicates the view is enabled. States are used with
1091 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1092 * view depending on its state.
1093 *
1094 * @see android.graphics.drawable.Drawable
1095 * @see #getDrawableState()
1096 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001097 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 /**
1099 * Indicates the view is focused. States are used with
1100 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1101 * view depending on its state.
1102 *
1103 * @see android.graphics.drawable.Drawable
1104 * @see #getDrawableState()
1105 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001106 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 /**
1108 * Indicates the view is selected. States are used with
1109 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1110 * view depending on its state.
1111 *
1112 * @see android.graphics.drawable.Drawable
1113 * @see #getDrawableState()
1114 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001115 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 /**
1117 * Indicates the view is pressed. States are used with
1118 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1119 * view depending on its state.
1120 *
1121 * @see android.graphics.drawable.Drawable
1122 * @see #getDrawableState()
1123 * @hide
1124 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001125 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 /**
1127 * Indicates the view's window has focus. States are used with
1128 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1129 * view depending on its state.
1130 *
1131 * @see android.graphics.drawable.Drawable
1132 * @see #getDrawableState()
1133 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001134 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 // Doubles
1136 /**
1137 * Indicates the view is enabled and has the focus.
1138 *
1139 * @see #ENABLED_STATE_SET
1140 * @see #FOCUSED_STATE_SET
1141 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001142 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 /**
1144 * Indicates the view is enabled and selected.
1145 *
1146 * @see #ENABLED_STATE_SET
1147 * @see #SELECTED_STATE_SET
1148 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001149 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 /**
1151 * Indicates the view is enabled and that its window has focus.
1152 *
1153 * @see #ENABLED_STATE_SET
1154 * @see #WINDOW_FOCUSED_STATE_SET
1155 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001156 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 /**
1158 * Indicates the view is focused and selected.
1159 *
1160 * @see #FOCUSED_STATE_SET
1161 * @see #SELECTED_STATE_SET
1162 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001163 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 /**
1165 * Indicates the view has the focus and that its window has the focus.
1166 *
1167 * @see #FOCUSED_STATE_SET
1168 * @see #WINDOW_FOCUSED_STATE_SET
1169 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001170 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 /**
1172 * Indicates the view is selected and that its window has the focus.
1173 *
1174 * @see #SELECTED_STATE_SET
1175 * @see #WINDOW_FOCUSED_STATE_SET
1176 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001177 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 // Triples
1179 /**
1180 * Indicates the view is enabled, focused and selected.
1181 *
1182 * @see #ENABLED_STATE_SET
1183 * @see #FOCUSED_STATE_SET
1184 * @see #SELECTED_STATE_SET
1185 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001186 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 /**
1188 * Indicates the view is enabled, focused and its window has the focus.
1189 *
1190 * @see #ENABLED_STATE_SET
1191 * @see #FOCUSED_STATE_SET
1192 * @see #WINDOW_FOCUSED_STATE_SET
1193 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001194 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 /**
1196 * Indicates the view is enabled, selected and its window has the focus.
1197 *
1198 * @see #ENABLED_STATE_SET
1199 * @see #SELECTED_STATE_SET
1200 * @see #WINDOW_FOCUSED_STATE_SET
1201 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001202 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 /**
1204 * Indicates the view is focused, selected and its window has the focus.
1205 *
1206 * @see #FOCUSED_STATE_SET
1207 * @see #SELECTED_STATE_SET
1208 * @see #WINDOW_FOCUSED_STATE_SET
1209 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001210 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 /**
1212 * Indicates the view is enabled, focused, selected and its window
1213 * has the focus.
1214 *
1215 * @see #ENABLED_STATE_SET
1216 * @see #FOCUSED_STATE_SET
1217 * @see #SELECTED_STATE_SET
1218 * @see #WINDOW_FOCUSED_STATE_SET
1219 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001220 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 /**
1222 * Indicates the view is pressed and its window has the focus.
1223 *
1224 * @see #PRESSED_STATE_SET
1225 * @see #WINDOW_FOCUSED_STATE_SET
1226 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001227 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 /**
1229 * Indicates the view is pressed and selected.
1230 *
1231 * @see #PRESSED_STATE_SET
1232 * @see #SELECTED_STATE_SET
1233 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001234 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 /**
1236 * Indicates the view is pressed, selected and its window has the focus.
1237 *
1238 * @see #PRESSED_STATE_SET
1239 * @see #SELECTED_STATE_SET
1240 * @see #WINDOW_FOCUSED_STATE_SET
1241 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001242 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 /**
1244 * Indicates the view is pressed and focused.
1245 *
1246 * @see #PRESSED_STATE_SET
1247 * @see #FOCUSED_STATE_SET
1248 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001249 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 /**
1251 * Indicates the view is pressed, focused and its window has the focus.
1252 *
1253 * @see #PRESSED_STATE_SET
1254 * @see #FOCUSED_STATE_SET
1255 * @see #WINDOW_FOCUSED_STATE_SET
1256 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001257 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 /**
1259 * Indicates the view is pressed, focused and selected.
1260 *
1261 * @see #PRESSED_STATE_SET
1262 * @see #SELECTED_STATE_SET
1263 * @see #FOCUSED_STATE_SET
1264 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001265 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 /**
1267 * Indicates the view is pressed, focused, selected and its window has the focus.
1268 *
1269 * @see #PRESSED_STATE_SET
1270 * @see #FOCUSED_STATE_SET
1271 * @see #SELECTED_STATE_SET
1272 * @see #WINDOW_FOCUSED_STATE_SET
1273 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001274 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 /**
1276 * Indicates the view is pressed and enabled.
1277 *
1278 * @see #PRESSED_STATE_SET
1279 * @see #ENABLED_STATE_SET
1280 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001281 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 /**
1283 * Indicates the view is pressed, enabled and its window has the focus.
1284 *
1285 * @see #PRESSED_STATE_SET
1286 * @see #ENABLED_STATE_SET
1287 * @see #WINDOW_FOCUSED_STATE_SET
1288 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001289 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 /**
1291 * Indicates the view is pressed, enabled and selected.
1292 *
1293 * @see #PRESSED_STATE_SET
1294 * @see #ENABLED_STATE_SET
1295 * @see #SELECTED_STATE_SET
1296 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001297 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 /**
1299 * Indicates the view is pressed, enabled, selected and its window has the
1300 * focus.
1301 *
1302 * @see #PRESSED_STATE_SET
1303 * @see #ENABLED_STATE_SET
1304 * @see #SELECTED_STATE_SET
1305 * @see #WINDOW_FOCUSED_STATE_SET
1306 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001307 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 /**
1309 * Indicates the view is pressed, enabled and focused.
1310 *
1311 * @see #PRESSED_STATE_SET
1312 * @see #ENABLED_STATE_SET
1313 * @see #FOCUSED_STATE_SET
1314 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001315 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 /**
1317 * Indicates the view is pressed, enabled, focused and its window has the
1318 * focus.
1319 *
1320 * @see #PRESSED_STATE_SET
1321 * @see #ENABLED_STATE_SET
1322 * @see #FOCUSED_STATE_SET
1323 * @see #WINDOW_FOCUSED_STATE_SET
1324 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001325 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 /**
1327 * Indicates the view is pressed, enabled, focused and selected.
1328 *
1329 * @see #PRESSED_STATE_SET
1330 * @see #ENABLED_STATE_SET
1331 * @see #SELECTED_STATE_SET
1332 * @see #FOCUSED_STATE_SET
1333 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001334 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 /**
1336 * Indicates the view is pressed, enabled, focused, selected and its window
1337 * has the focus.
1338 *
1339 * @see #PRESSED_STATE_SET
1340 * @see #ENABLED_STATE_SET
1341 * @see #SELECTED_STATE_SET
1342 * @see #FOCUSED_STATE_SET
1343 * @see #WINDOW_FOCUSED_STATE_SET
1344 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001345 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346
1347 /**
1348 * The order here is very important to {@link #getDrawableState()}
1349 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001350 private static final int[][] VIEW_STATE_SETS;
1351
Romain Guyb051e892010-09-28 19:09:36 -07001352 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1353 static final int VIEW_STATE_SELECTED = 1 << 1;
1354 static final int VIEW_STATE_FOCUSED = 1 << 2;
1355 static final int VIEW_STATE_ENABLED = 1 << 3;
1356 static final int VIEW_STATE_PRESSED = 1 << 4;
1357 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001358 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001359 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001360 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1361 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001362
1363 static final int[] VIEW_STATE_IDS = new int[] {
1364 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1365 R.attr.state_selected, VIEW_STATE_SELECTED,
1366 R.attr.state_focused, VIEW_STATE_FOCUSED,
1367 R.attr.state_enabled, VIEW_STATE_ENABLED,
1368 R.attr.state_pressed, VIEW_STATE_PRESSED,
1369 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001370 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001371 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001372 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1373 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 };
1375
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001376 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001377 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1378 throw new IllegalStateException(
1379 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1380 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001381 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001382 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001383 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001384 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001385 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001386 orderedIds[i * 2] = viewState;
1387 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001388 }
1389 }
1390 }
Romain Guyb051e892010-09-28 19:09:36 -07001391 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1392 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1393 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001394 int numBits = Integer.bitCount(i);
1395 int[] set = new int[numBits];
1396 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001397 for (int j = 0; j < orderedIds.length; j += 2) {
1398 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001399 set[pos++] = orderedIds[j];
1400 }
1401 }
1402 VIEW_STATE_SETS[i] = set;
1403 }
1404
1405 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1406 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1407 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1408 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1409 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1410 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1411 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1412 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1413 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1414 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1415 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1416 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1417 | VIEW_STATE_FOCUSED];
1418 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1419 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1421 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1422 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1423 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1424 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1425 | VIEW_STATE_ENABLED];
1426 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1427 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1428 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1429 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1430 | VIEW_STATE_ENABLED];
1431 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1432 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1433 | VIEW_STATE_ENABLED];
1434 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1435 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1436 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1437
1438 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1439 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1440 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1441 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1442 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1443 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1444 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1445 | VIEW_STATE_PRESSED];
1446 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1447 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1448 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1449 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1450 | VIEW_STATE_PRESSED];
1451 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1452 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1453 | VIEW_STATE_PRESSED];
1454 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1455 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1456 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1457 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1459 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1460 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1461 | VIEW_STATE_PRESSED];
1462 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1464 | VIEW_STATE_PRESSED];
1465 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1467 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1468 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1470 | VIEW_STATE_PRESSED];
1471 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1472 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1473 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1474 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1475 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1476 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1477 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1478 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1479 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1480 | VIEW_STATE_PRESSED];
1481 }
1482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001484 * Accessibility event types that are dispatched for text population.
1485 */
1486 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1487 AccessibilityEvent.TYPE_VIEW_CLICKED
1488 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1489 | AccessibilityEvent.TYPE_VIEW_SELECTED
1490 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1491 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1492 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001493 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001494 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
1495 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001496
1497 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 * Temporary Rect currently for use in setBackground(). This will probably
1499 * be extended in the future to hold our own class with more than just
1500 * a Rect. :)
1501 */
1502 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001503
1504 /**
1505 * Map used to store views' tags.
1506 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001507 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001510 * The next available accessiiblity id.
1511 */
1512 private static int sNextAccessibilityViewId;
1513
1514 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 * The animation currently associated with this view.
1516 * @hide
1517 */
1518 protected Animation mCurrentAnimation = null;
1519
1520 /**
1521 * Width as measured during measure pass.
1522 * {@hide}
1523 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001524 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001525 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526
1527 /**
1528 * Height as measured during measure pass.
1529 * {@hide}
1530 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001531 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001532 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533
1534 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001535 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1536 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1537 * its display list. This flag, used only when hw accelerated, allows us to clear the
1538 * flag while retaining this information until it's needed (at getDisplayList() time and
1539 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1540 *
1541 * {@hide}
1542 */
1543 boolean mRecreateDisplayList = false;
1544
1545 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 * The view's identifier.
1547 * {@hide}
1548 *
1549 * @see #setId(int)
1550 * @see #getId()
1551 */
1552 @ViewDebug.ExportedProperty(resolveId = true)
1553 int mID = NO_ID;
1554
1555 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001556 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001557 */
1558 int mAccessibilityViewId = NO_ID;
1559
1560 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 * The view's tag.
1562 * {@hide}
1563 *
1564 * @see #setTag(Object)
1565 * @see #getTag()
1566 */
1567 protected Object mTag;
1568
1569 // for mPrivateFlags:
1570 /** {@hide} */
1571 static final int WANTS_FOCUS = 0x00000001;
1572 /** {@hide} */
1573 static final int FOCUSED = 0x00000002;
1574 /** {@hide} */
1575 static final int SELECTED = 0x00000004;
1576 /** {@hide} */
1577 static final int IS_ROOT_NAMESPACE = 0x00000008;
1578 /** {@hide} */
1579 static final int HAS_BOUNDS = 0x00000010;
1580 /** {@hide} */
1581 static final int DRAWN = 0x00000020;
1582 /**
1583 * When this flag is set, this view is running an animation on behalf of its
1584 * children and should therefore not cancel invalidate requests, even if they
1585 * lie outside of this view's bounds.
1586 *
1587 * {@hide}
1588 */
1589 static final int DRAW_ANIMATION = 0x00000040;
1590 /** {@hide} */
1591 static final int SKIP_DRAW = 0x00000080;
1592 /** {@hide} */
1593 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1594 /** {@hide} */
1595 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1596 /** {@hide} */
1597 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1598 /** {@hide} */
1599 static final int MEASURED_DIMENSION_SET = 0x00000800;
1600 /** {@hide} */
1601 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001602 /** {@hide} */
1603 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604
1605 private static final int PRESSED = 0x00004000;
1606
1607 /** {@hide} */
1608 static final int DRAWING_CACHE_VALID = 0x00008000;
1609 /**
1610 * Flag used to indicate that this view should be drawn once more (and only once
1611 * more) after its animation has completed.
1612 * {@hide}
1613 */
1614 static final int ANIMATION_STARTED = 0x00010000;
1615
1616 private static final int SAVE_STATE_CALLED = 0x00020000;
1617
1618 /**
1619 * Indicates that the View returned true when onSetAlpha() was called and that
1620 * the alpha must be restored.
1621 * {@hide}
1622 */
1623 static final int ALPHA_SET = 0x00040000;
1624
1625 /**
1626 * Set by {@link #setScrollContainer(boolean)}.
1627 */
1628 static final int SCROLL_CONTAINER = 0x00080000;
1629
1630 /**
1631 * Set by {@link #setScrollContainer(boolean)}.
1632 */
1633 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1634
1635 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001636 * View flag indicating whether this view was invalidated (fully or partially.)
1637 *
1638 * @hide
1639 */
1640 static final int DIRTY = 0x00200000;
1641
1642 /**
1643 * View flag indicating whether this view was invalidated by an opaque
1644 * invalidate request.
1645 *
1646 * @hide
1647 */
1648 static final int DIRTY_OPAQUE = 0x00400000;
1649
1650 /**
1651 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1652 *
1653 * @hide
1654 */
1655 static final int DIRTY_MASK = 0x00600000;
1656
1657 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001658 * Indicates whether the background is opaque.
1659 *
1660 * @hide
1661 */
1662 static final int OPAQUE_BACKGROUND = 0x00800000;
1663
1664 /**
1665 * Indicates whether the scrollbars are opaque.
1666 *
1667 * @hide
1668 */
1669 static final int OPAQUE_SCROLLBARS = 0x01000000;
1670
1671 /**
1672 * Indicates whether the view is opaque.
1673 *
1674 * @hide
1675 */
1676 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001677
Adam Powelle14579b2009-12-16 18:39:52 -08001678 /**
1679 * Indicates a prepressed state;
1680 * the short time between ACTION_DOWN and recognizing
1681 * a 'real' press. Prepressed is used to recognize quick taps
1682 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001683 *
Adam Powelle14579b2009-12-16 18:39:52 -08001684 * @hide
1685 */
1686 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001687
Adam Powellc9fbaab2010-02-16 17:16:19 -08001688 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001689 * Indicates whether the view is temporarily detached.
1690 *
1691 * @hide
1692 */
1693 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001694
Adam Powell8568c3a2010-04-19 14:26:11 -07001695 /**
1696 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001697 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001698 * @hide
1699 */
1700 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001701
1702 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001703 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1704 * @hide
1705 */
1706 private static final int HOVERED = 0x10000000;
1707
1708 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001709 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1710 * for transform operations
1711 *
1712 * @hide
1713 */
Adam Powellf37df072010-09-17 16:22:49 -07001714 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001715
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001716 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001717 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001718
Chet Haasefd2b0022010-08-06 13:08:56 -07001719 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001720 * Indicates that this view was specifically invalidated, not just dirtied because some
1721 * child view was invalidated. The flag is used to determine when we need to recreate
1722 * a view's display list (as opposed to just returning a reference to its existing
1723 * display list).
1724 *
1725 * @hide
1726 */
1727 static final int INVALIDATED = 0x80000000;
1728
Christopher Tate3d4bf172011-03-28 16:16:46 -07001729 /* Masks for mPrivateFlags2 */
1730
1731 /**
1732 * Indicates that this view has reported that it can accept the current drag's content.
1733 * Cleared when the drag operation concludes.
1734 * @hide
1735 */
1736 static final int DRAG_CAN_ACCEPT = 0x00000001;
1737
1738 /**
1739 * Indicates that this view is currently directly under the drag location in a
1740 * drag-and-drop operation involving content that it can accept. Cleared when
1741 * the drag exits the view, or when the drag operation concludes.
1742 * @hide
1743 */
1744 static final int DRAG_HOVERED = 0x00000002;
1745
Cibu Johny86666632010-02-22 13:01:02 -08001746 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001747 * Indicates whether the view layout direction has been resolved and drawn to the
1748 * right-to-left direction.
Cibu Johny86666632010-02-22 13:01:02 -08001749 *
1750 * @hide
1751 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001752 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004;
1753
1754 /**
1755 * Indicates whether the view layout direction has been resolved.
1756 *
1757 * @hide
1758 */
1759 static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008;
1760
Cibu Johny86666632010-02-22 13:01:02 -08001761
Adam Powell539ee872012-02-03 19:00:49 -08001762 /**
1763 * Indicates that the view is tracking some sort of transient state
1764 * that the app should not need to be aware of, but that the framework
1765 * should take special care to preserve.
1766 *
1767 * @hide
1768 */
1769 static final int HAS_TRANSIENT_STATE = 0x00000010;
1770
1771
Christopher Tate3d4bf172011-03-28 16:16:46 -07001772 /* End of masks for mPrivateFlags2 */
1773
1774 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1775
Chet Haasedaf98e92011-01-10 14:10:36 -08001776 /**
Adam Powell637d3372010-08-25 14:37:03 -07001777 * Always allow a user to over-scroll this view, provided it is a
1778 * view that can scroll.
1779 *
1780 * @see #getOverScrollMode()
1781 * @see #setOverScrollMode(int)
1782 */
1783 public static final int OVER_SCROLL_ALWAYS = 0;
1784
1785 /**
1786 * Allow a user to over-scroll this view only if the content is large
1787 * enough to meaningfully scroll, provided it is a view that can scroll.
1788 *
1789 * @see #getOverScrollMode()
1790 * @see #setOverScrollMode(int)
1791 */
1792 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1793
1794 /**
1795 * Never allow a user to over-scroll this view.
1796 *
1797 * @see #getOverScrollMode()
1798 * @see #setOverScrollMode(int)
1799 */
1800 public static final int OVER_SCROLL_NEVER = 2;
1801
1802 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001803 * View has requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08001804 *
Joe Malin32736f02011-01-19 16:14:20 -08001805 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001806 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001807 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08001808
1809 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001810 * View has requested the system UI to enter an unobtrusive "low profile" mode.
1811 *
1812 * This is for use in games, book readers, video players, or any other "immersive" application
1813 * where the usual system chrome is deemed too distracting.
1814 *
1815 * In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08001816 *
Joe Malin32736f02011-01-19 16:14:20 -08001817 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001818 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001819 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
1820
1821 /**
1822 * View has requested that the system navigation be temporarily hidden.
1823 *
1824 * This is an even less obtrusive state than that called for by
1825 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
1826 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
1827 * those to disappear. This is useful (in conjunction with the
1828 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
1829 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
1830 * window flags) for displaying content using every last pixel on the display.
1831 *
1832 * There is a limitation: because navigation controls are so important, the least user
1833 * interaction will cause them to reappear immediately.
1834 *
1835 * @see #setSystemUiVisibility(int)
1836 */
1837 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
1838
1839 /**
1840 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
1841 */
1842 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
1843
1844 /**
1845 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
1846 */
1847 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08001848
1849 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001850 * @hide
1851 *
1852 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1853 * out of the public fields to keep the undefined bits out of the developer's way.
1854 *
1855 * Flag to make the status bar not expandable. Unless you also
1856 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1857 */
1858 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1859
1860 /**
1861 * @hide
1862 *
1863 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1864 * out of the public fields to keep the undefined bits out of the developer's way.
1865 *
1866 * Flag to hide notification icons and scrolling ticker text.
1867 */
1868 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1869
1870 /**
1871 * @hide
1872 *
1873 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1874 * out of the public fields to keep the undefined bits out of the developer's way.
1875 *
1876 * Flag to disable incoming notification alerts. This will not block
1877 * icons, but it will block sound, vibrating and other visual or aural notifications.
1878 */
1879 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1880
1881 /**
1882 * @hide
1883 *
1884 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1885 * out of the public fields to keep the undefined bits out of the developer's way.
1886 *
1887 * Flag to hide only the scrolling ticker. Note that
1888 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1889 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1890 */
1891 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1892
1893 /**
1894 * @hide
1895 *
1896 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1897 * out of the public fields to keep the undefined bits out of the developer's way.
1898 *
1899 * Flag to hide the center system info area.
1900 */
1901 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1902
1903 /**
1904 * @hide
1905 *
1906 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1907 * out of the public fields to keep the undefined bits out of the developer's way.
1908 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04001909 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001910 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1911 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04001912 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001913
1914 /**
1915 * @hide
1916 *
1917 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1918 * out of the public fields to keep the undefined bits out of the developer's way.
1919 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04001920 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08001921 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1922 */
1923 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1924
1925 /**
1926 * @hide
1927 *
1928 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1929 * out of the public fields to keep the undefined bits out of the developer's way.
1930 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001931 * Flag to hide only the clock. You might use this if your activity has
1932 * its own clock making the status bar's clock redundant.
1933 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001934 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1935
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001936 /**
1937 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04001938 *
1939 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1940 * out of the public fields to keep the undefined bits out of the developer's way.
1941 *
1942 * Flag to hide only the recent apps button. Don't use this
1943 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1944 */
1945 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
1946
1947 /**
1948 * @hide
1949 *
1950 * NOTE: This flag may only be used in subtreeSystemUiVisibility, etc. etc.
1951 *
1952 * This hides HOME and RECENT and is provided for compatibility with interim implementations.
1953 */
1954 @Deprecated
1955 public static final int STATUS_BAR_DISABLE_NAVIGATION =
1956 STATUS_BAR_DISABLE_HOME | STATUS_BAR_DISABLE_RECENT;
1957
1958 /**
1959 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001960 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001961 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001962
1963 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001964 * These are the system UI flags that can be cleared by events outside
1965 * of an application. Currently this is just the ability to tap on the
1966 * screen while hiding the navigation bar to have it return.
1967 * @hide
1968 */
1969 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
1970 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION;
1971
1972 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001973 * Find views that render the specified text.
1974 *
1975 * @see #findViewsWithText(ArrayList, CharSequence, int)
1976 */
1977 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
1978
1979 /**
1980 * Find find views that contain the specified content description.
1981 *
1982 * @see #findViewsWithText(ArrayList, CharSequence, int)
1983 */
1984 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
1985
1986 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07001987 * Find views that contain {@link AccessibilityNodeProvider}. Such
1988 * a View is a root of virtual view hierarchy and may contain the searched
1989 * text. If this flag is set Views with providers are automatically
1990 * added and it is a responsibility of the client to call the APIs of
1991 * the provider to determine whether the virtual tree rooted at this View
1992 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
1993 * represeting the virtual views with this text.
1994 *
1995 * @see #findViewsWithText(ArrayList, CharSequence, int)
1996 *
1997 * @hide
1998 */
1999 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2000
2001 /**
Adam Powell637d3372010-08-25 14:37:03 -07002002 * Controls the over-scroll mode for this view.
2003 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2004 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2005 * and {@link #OVER_SCROLL_NEVER}.
2006 */
2007 private int mOverScrollMode;
2008
2009 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 * The parent this view is attached to.
2011 * {@hide}
2012 *
2013 * @see #getParent()
2014 */
2015 protected ViewParent mParent;
2016
2017 /**
2018 * {@hide}
2019 */
2020 AttachInfo mAttachInfo;
2021
2022 /**
2023 * {@hide}
2024 */
Romain Guy809a7f62009-05-14 15:44:42 -07002025 @ViewDebug.ExportedProperty(flagMapping = {
2026 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2027 name = "FORCE_LAYOUT"),
2028 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2029 name = "LAYOUT_REQUIRED"),
2030 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002031 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002032 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2033 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2034 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2035 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2036 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002038 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039
2040 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002041 * This view's request for the visibility of the status bar.
2042 * @hide
2043 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002044 @ViewDebug.ExportedProperty(flagMapping = {
2045 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2046 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2047 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2048 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2049 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2050 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2051 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2052 equals = SYSTEM_UI_FLAG_VISIBLE,
2053 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2054 })
Joe Onorato664644d2011-01-23 17:53:23 -08002055 int mSystemUiVisibility;
2056
2057 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 * Count of how many windows this view has been attached to.
2059 */
2060 int mWindowAttachCount;
2061
2062 /**
2063 * The layout parameters associated with this view and used by the parent
2064 * {@link android.view.ViewGroup} to determine how this view should be
2065 * laid out.
2066 * {@hide}
2067 */
2068 protected ViewGroup.LayoutParams mLayoutParams;
2069
2070 /**
2071 * The view flags hold various views states.
2072 * {@hide}
2073 */
2074 @ViewDebug.ExportedProperty
2075 int mViewFlags;
2076
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002077 static class TransformationInfo {
2078 /**
2079 * The transform matrix for the View. This transform is calculated internally
2080 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2081 * is used by default. Do *not* use this variable directly; instead call
2082 * getMatrix(), which will automatically recalculate the matrix if necessary
2083 * to get the correct matrix based on the latest rotation and scale properties.
2084 */
2085 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002086
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002087 /**
2088 * The transform matrix for the View. This transform is calculated internally
2089 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2090 * is used by default. Do *not* use this variable directly; instead call
2091 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2092 * to get the correct matrix based on the latest rotation and scale properties.
2093 */
2094 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002095
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002096 /**
2097 * An internal variable that tracks whether we need to recalculate the
2098 * transform matrix, based on whether the rotation or scaleX/Y properties
2099 * have changed since the matrix was last calculated.
2100 */
2101 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002102
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002103 /**
2104 * An internal variable that tracks whether we need to recalculate the
2105 * transform matrix, based on whether the rotation or scaleX/Y properties
2106 * have changed since the matrix was last calculated.
2107 */
2108 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002109
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002110 /**
2111 * A variable that tracks whether we need to recalculate the
2112 * transform matrix, based on whether the rotation or scaleX/Y properties
2113 * have changed since the matrix was last calculated. This variable
2114 * is only valid after a call to updateMatrix() or to a function that
2115 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2116 */
2117 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002118
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002119 /**
2120 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2121 */
2122 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002123
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002124 /**
2125 * This matrix is used when computing the matrix for 3D rotations.
2126 */
2127 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002128
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002129 /**
2130 * These prev values are used to recalculate a centered pivot point when necessary. The
2131 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2132 * set), so thes values are only used then as well.
2133 */
2134 private int mPrevWidth = -1;
2135 private int mPrevHeight = -1;
2136
2137 /**
2138 * The degrees rotation around the vertical axis through the pivot point.
2139 */
2140 @ViewDebug.ExportedProperty
2141 float mRotationY = 0f;
2142
2143 /**
2144 * The degrees rotation around the horizontal axis through the pivot point.
2145 */
2146 @ViewDebug.ExportedProperty
2147 float mRotationX = 0f;
2148
2149 /**
2150 * The degrees rotation around the pivot point.
2151 */
2152 @ViewDebug.ExportedProperty
2153 float mRotation = 0f;
2154
2155 /**
2156 * The amount of translation of the object away from its left property (post-layout).
2157 */
2158 @ViewDebug.ExportedProperty
2159 float mTranslationX = 0f;
2160
2161 /**
2162 * The amount of translation of the object away from its top property (post-layout).
2163 */
2164 @ViewDebug.ExportedProperty
2165 float mTranslationY = 0f;
2166
2167 /**
2168 * The amount of scale in the x direction around the pivot point. A
2169 * value of 1 means no scaling is applied.
2170 */
2171 @ViewDebug.ExportedProperty
2172 float mScaleX = 1f;
2173
2174 /**
2175 * The amount of scale in the y direction around the pivot point. A
2176 * value of 1 means no scaling is applied.
2177 */
2178 @ViewDebug.ExportedProperty
2179 float mScaleY = 1f;
2180
2181 /**
Chet Haasea33de552012-02-03 16:28:24 -08002182 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002183 */
2184 @ViewDebug.ExportedProperty
2185 float mPivotX = 0f;
2186
2187 /**
Chet Haasea33de552012-02-03 16:28:24 -08002188 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002189 */
2190 @ViewDebug.ExportedProperty
2191 float mPivotY = 0f;
2192
2193 /**
2194 * The opacity of the View. This is a value from 0 to 1, where 0 means
2195 * completely transparent and 1 means completely opaque.
2196 */
2197 @ViewDebug.ExportedProperty
2198 float mAlpha = 1f;
2199 }
2200
2201 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002202
Joe Malin32736f02011-01-19 16:14:20 -08002203 private boolean mLastIsOpaque;
2204
Chet Haasefd2b0022010-08-06 13:08:56 -07002205 /**
2206 * Convenience value to check for float values that are close enough to zero to be considered
2207 * zero.
2208 */
Romain Guy2542d192010-08-18 11:47:12 -07002209 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002210
2211 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 * The distance in pixels from the left edge of this view's parent
2213 * to the left edge of this view.
2214 * {@hide}
2215 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002216 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 protected int mLeft;
2218 /**
2219 * The distance in pixels from the left edge of this view's parent
2220 * to the right edge of this view.
2221 * {@hide}
2222 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002223 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 protected int mRight;
2225 /**
2226 * The distance in pixels from the top edge of this view's parent
2227 * to the top edge of this view.
2228 * {@hide}
2229 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002230 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 protected int mTop;
2232 /**
2233 * The distance in pixels from the top edge of this view's parent
2234 * to the bottom edge of this view.
2235 * {@hide}
2236 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002237 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 protected int mBottom;
2239
2240 /**
2241 * The offset, in pixels, by which the content of this view is scrolled
2242 * horizontally.
2243 * {@hide}
2244 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002245 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 protected int mScrollX;
2247 /**
2248 * The offset, in pixels, by which the content of this view is scrolled
2249 * vertically.
2250 * {@hide}
2251 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002252 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 protected int mScrollY;
2254
2255 /**
2256 * The left padding in pixels, that is the distance in pixels between the
2257 * left edge of this view and the left edge of its content.
2258 * {@hide}
2259 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002260 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 protected int mPaddingLeft;
2262 /**
2263 * The right padding in pixels, that is the distance in pixels between the
2264 * right edge of this view and the right edge of its content.
2265 * {@hide}
2266 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002267 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 protected int mPaddingRight;
2269 /**
2270 * The top padding in pixels, that is the distance in pixels between the
2271 * top edge of this view and the top edge of its content.
2272 * {@hide}
2273 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002274 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 protected int mPaddingTop;
2276 /**
2277 * The bottom padding in pixels, that is the distance in pixels between the
2278 * bottom edge of this view and the bottom edge of its content.
2279 * {@hide}
2280 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002281 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 protected int mPaddingBottom;
2283
2284 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002285 * Briefly describes the view and is primarily used for accessibility support.
2286 */
2287 private CharSequence mContentDescription;
2288
2289 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002291 *
2292 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002294 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002295 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296
2297 /**
2298 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002299 *
2300 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002302 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002303 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002305 /**
Adam Powell20232d02010-12-08 21:08:53 -08002306 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002307 *
2308 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002309 */
2310 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002311 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002312
2313 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002314 * Cache if the user padding is relative.
2315 *
2316 */
2317 @ViewDebug.ExportedProperty(category = "padding")
2318 boolean mUserPaddingRelative;
2319
2320 /**
2321 * Cache the paddingStart set by the user to append to the scrollbar's size.
2322 *
2323 */
2324 @ViewDebug.ExportedProperty(category = "padding")
2325 int mUserPaddingStart;
2326
2327 /**
2328 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2329 *
2330 */
2331 @ViewDebug.ExportedProperty(category = "padding")
2332 int mUserPaddingEnd;
2333
2334 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002335 * @hide
2336 */
2337 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2338 /**
2339 * @hide
2340 */
2341 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 private Drawable mBGDrawable;
2344
2345 private int mBackgroundResource;
2346 private boolean mBackgroundSizeChanged;
2347
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002348 static class ListenerInfo {
2349 /**
2350 * Listener used to dispatch focus change events.
2351 * This field should be made private, so it is hidden from the SDK.
2352 * {@hide}
2353 */
2354 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002356 /**
2357 * Listeners for layout change events.
2358 */
2359 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002360
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002361 /**
2362 * Listeners for attach events.
2363 */
2364 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002365
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002366 /**
2367 * Listener used to dispatch click events.
2368 * This field should be made private, so it is hidden from the SDK.
2369 * {@hide}
2370 */
2371 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002373 /**
2374 * Listener used to dispatch long click events.
2375 * This field should be made private, so it is hidden from the SDK.
2376 * {@hide}
2377 */
2378 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002380 /**
2381 * Listener used to build the context menu.
2382 * This field should be made private, so it is hidden from the SDK.
2383 * {@hide}
2384 */
2385 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002387 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002389 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002391 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002392
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002393 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002394
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002395 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002396
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002397 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2398 }
2399
2400 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 /**
2403 * The application environment this view lives in.
2404 * This field should be made private, so it is hidden from the SDK.
2405 * {@hide}
2406 */
2407 protected Context mContext;
2408
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002409 private final Resources mResources;
2410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 private ScrollabilityCache mScrollCache;
2412
2413 private int[] mDrawableState = null;
2414
Romain Guy0211a0a2011-02-14 16:34:59 -08002415 /**
2416 * Set to true when drawing cache is enabled and cannot be created.
2417 *
2418 * @hide
2419 */
2420 public boolean mCachingFailed;
2421
Romain Guy02890fd2010-08-06 17:58:44 -07002422 private Bitmap mDrawingCache;
2423 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002424 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002425 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426
2427 /**
2428 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2429 * the user may specify which view to go to next.
2430 */
2431 private int mNextFocusLeftId = View.NO_ID;
2432
2433 /**
2434 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2435 * the user may specify which view to go to next.
2436 */
2437 private int mNextFocusRightId = View.NO_ID;
2438
2439 /**
2440 * When this view has focus and the next focus is {@link #FOCUS_UP},
2441 * the user may specify which view to go to next.
2442 */
2443 private int mNextFocusUpId = View.NO_ID;
2444
2445 /**
2446 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2447 * the user may specify which view to go to next.
2448 */
2449 private int mNextFocusDownId = View.NO_ID;
2450
Jeff Brown4e6319b2010-12-13 10:36:51 -08002451 /**
2452 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2453 * the user may specify which view to go to next.
2454 */
2455 int mNextFocusForwardId = View.NO_ID;
2456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002458 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002459 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002460 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 private UnsetPressedState mUnsetPressedState;
2463
2464 /**
2465 * Whether the long press's action has been invoked. The tap's action is invoked on the
2466 * up event while a long press is invoked as soon as the long press duration is reached, so
2467 * a long press could be performed before the tap is checked, in which case the tap's action
2468 * should not be invoked.
2469 */
2470 private boolean mHasPerformedLongPress;
2471
2472 /**
2473 * The minimum height of the view. We'll try our best to have the height
2474 * of this view to at least this amount.
2475 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002476 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 private int mMinHeight;
2478
2479 /**
2480 * The minimum width of the view. We'll try our best to have the width
2481 * of this view to at least this amount.
2482 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002483 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 private int mMinWidth;
2485
2486 /**
2487 * The delegate to handle touch events that are physically in this view
2488 * but should be handled by another view.
2489 */
2490 private TouchDelegate mTouchDelegate = null;
2491
2492 /**
2493 * Solid color to use as a background when creating the drawing cache. Enables
2494 * the cache to use 16 bit bitmaps instead of 32 bit.
2495 */
2496 private int mDrawingCacheBackgroundColor = 0;
2497
2498 /**
2499 * Special tree observer used when mAttachInfo is null.
2500 */
2501 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002502
Adam Powelle14579b2009-12-16 18:39:52 -08002503 /**
2504 * Cache the touch slop from the context that created the view.
2505 */
2506 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002509 * Object that handles automatic animation of view properties.
2510 */
2511 private ViewPropertyAnimator mAnimator = null;
2512
2513 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002514 * Flag indicating that a drag can cross window boundaries. When
2515 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2516 * with this flag set, all visible applications will be able to participate
2517 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002518 *
2519 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002520 */
2521 public static final int DRAG_FLAG_GLOBAL = 1;
2522
2523 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002524 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2525 */
2526 private float mVerticalScrollFactor;
2527
2528 /**
Adam Powell20232d02010-12-08 21:08:53 -08002529 * Position of the vertical scroll bar.
2530 */
2531 private int mVerticalScrollbarPosition;
2532
2533 /**
2534 * Position the scroll bar at the default position as determined by the system.
2535 */
2536 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2537
2538 /**
2539 * Position the scroll bar along the left edge.
2540 */
2541 public static final int SCROLLBAR_POSITION_LEFT = 1;
2542
2543 /**
2544 * Position the scroll bar along the right edge.
2545 */
2546 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2547
2548 /**
Romain Guy171c5922011-01-06 10:04:23 -08002549 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002550 *
2551 * @see #getLayerType()
2552 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002553 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002554 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002555 */
2556 public static final int LAYER_TYPE_NONE = 0;
2557
2558 /**
2559 * <p>Indicates that the view has a software layer. A software layer is backed
2560 * by a bitmap and causes the view to be rendered using Android's software
2561 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002562 *
Romain Guy171c5922011-01-06 10:04:23 -08002563 * <p>Software layers have various usages:</p>
2564 * <p>When the application is not using hardware acceleration, a software layer
2565 * is useful to apply a specific color filter and/or blending mode and/or
2566 * translucency to a view and all its children.</p>
2567 * <p>When the application is using hardware acceleration, a software layer
2568 * is useful to render drawing primitives not supported by the hardware
2569 * accelerated pipeline. It can also be used to cache a complex view tree
2570 * into a texture and reduce the complexity of drawing operations. For instance,
2571 * when animating a complex view tree with a translation, a software layer can
2572 * be used to render the view tree only once.</p>
2573 * <p>Software layers should be avoided when the affected view tree updates
2574 * often. Every update will require to re-render the software layer, which can
2575 * potentially be slow (particularly when hardware acceleration is turned on
2576 * since the layer will have to be uploaded into a hardware texture after every
2577 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002578 *
2579 * @see #getLayerType()
2580 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002581 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002582 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002583 */
2584 public static final int LAYER_TYPE_SOFTWARE = 1;
2585
2586 /**
2587 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2588 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2589 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2590 * rendering pipeline, but only if hardware acceleration is turned on for the
2591 * view hierarchy. When hardware acceleration is turned off, hardware layers
2592 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002593 *
Romain Guy171c5922011-01-06 10:04:23 -08002594 * <p>A hardware layer is useful to apply a specific color filter and/or
2595 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002596 * <p>A hardware layer can be used to cache a complex view tree into a
2597 * texture and reduce the complexity of drawing operations. For instance,
2598 * when animating a complex view tree with a translation, a hardware layer can
2599 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002600 * <p>A hardware layer can also be used to increase the rendering quality when
2601 * rotation transformations are applied on a view. It can also be used to
2602 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002603 *
2604 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002605 * @see #setLayerType(int, android.graphics.Paint)
2606 * @see #LAYER_TYPE_NONE
2607 * @see #LAYER_TYPE_SOFTWARE
2608 */
2609 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002610
Romain Guy3aaff3a2011-01-12 14:18:47 -08002611 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2612 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2613 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2614 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2615 })
Romain Guy171c5922011-01-06 10:04:23 -08002616 int mLayerType = LAYER_TYPE_NONE;
2617 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002618 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002619
2620 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002621 * Set to true when the view is sending hover accessibility events because it
2622 * is the innermost hovered view.
2623 */
2624 private boolean mSendingHoverAccessibilityEvents;
2625
2626 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002627 * Delegate for injecting accessiblity functionality.
2628 */
2629 AccessibilityDelegate mAccessibilityDelegate;
2630
2631 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002632 * Text direction is inherited thru {@link ViewGroup}
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002633 */
2634 public static final int TEXT_DIRECTION_INHERIT = 0;
2635
2636 /**
2637 * Text direction is using "first strong algorithm". The first strong directional character
2638 * determines the paragraph direction. If there is no strong directional character, the
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002639 * paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002640 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002641 */
2642 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2643
2644 /**
2645 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2646 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002647 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002648 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002649 */
2650 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2651
2652 /**
2653 * Text direction is forced to LTR.
2654 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002655 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002656 public static final int TEXT_DIRECTION_LTR = 3;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002657
2658 /**
2659 * Text direction is forced to RTL.
2660 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002661 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002662 public static final int TEXT_DIRECTION_RTL = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002663
2664 /**
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002665 * Text direction is coming from the system Locale.
2666 *
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002667 */
2668 public static final int TEXT_DIRECTION_LOCALE = 5;
2669
2670 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002671 * Default text direction is inherited
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07002672 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002673 */
2674 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2675
2676 /**
2677 * The text direction that has been defined by {@link #setTextDirection(int)}.
2678 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002679 */
2680 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002681 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2682 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2683 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2684 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002685 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
2686 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002687 })
Doug Feltcb3791202011-07-07 11:57:48 -07002688 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002689
2690 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002691 * The resolved text direction. This needs resolution if the value is
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002692 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if it is
Doug Feltcb3791202011-07-07 11:57:48 -07002693 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2694 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002695 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002696 */
2697 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002698 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2699 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2700 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002701 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002702 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
2703 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002704 })
Doug Feltcb3791202011-07-07 11:57:48 -07002705 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002706
2707 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002708 * Consistency verifier for debugging purposes.
2709 * @hide
2710 */
2711 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2712 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2713 new InputEventConsistencyVerifier(this, 0) : null;
2714
2715 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 * Simple constructor to use when creating a view from code.
2717 *
2718 * @param context The Context the view is running in, through which it can
2719 * access the current theme, resources, etc.
2720 */
2721 public View(Context context) {
2722 mContext = context;
2723 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002724 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002725 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002726 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002727 mUserPaddingStart = -1;
2728 mUserPaddingEnd = -1;
2729 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 }
2731
2732 /**
2733 * Constructor that is called when inflating a view from XML. This is called
2734 * when a view is being constructed from an XML file, supplying attributes
2735 * that were specified in the XML file. This version uses a default style of
2736 * 0, so the only attribute values applied are those in the Context's Theme
2737 * and the given AttributeSet.
2738 *
2739 * <p>
2740 * The method onFinishInflate() will be called after all children have been
2741 * added.
2742 *
2743 * @param context The Context the view is running in, through which it can
2744 * access the current theme, resources, etc.
2745 * @param attrs The attributes of the XML tag that is inflating the view.
2746 * @see #View(Context, AttributeSet, int)
2747 */
2748 public View(Context context, AttributeSet attrs) {
2749 this(context, attrs, 0);
2750 }
2751
2752 /**
2753 * Perform inflation from XML and apply a class-specific base style. This
2754 * constructor of View allows subclasses to use their own base style when
2755 * they are inflating. For example, a Button class's constructor would call
2756 * this version of the super class constructor and supply
2757 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2758 * the theme's button style to modify all of the base view attributes (in
2759 * particular its background) as well as the Button class's attributes.
2760 *
2761 * @param context The Context the view is running in, through which it can
2762 * access the current theme, resources, etc.
2763 * @param attrs The attributes of the XML tag that is inflating the view.
2764 * @param defStyle The default style to apply to this view. If 0, no style
2765 * will be applied (beyond what is included in the theme). This may
2766 * either be an attribute resource, whose value will be retrieved
2767 * from the current theme, or an explicit style resource.
2768 * @see #View(Context, AttributeSet)
2769 */
2770 public View(Context context, AttributeSet attrs, int defStyle) {
2771 this(context);
2772
2773 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2774 defStyle, 0);
2775
2776 Drawable background = null;
2777
2778 int leftPadding = -1;
2779 int topPadding = -1;
2780 int rightPadding = -1;
2781 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002782 int startPadding = -1;
2783 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784
2785 int padding = -1;
2786
2787 int viewFlagValues = 0;
2788 int viewFlagMasks = 0;
2789
2790 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 int x = 0;
2793 int y = 0;
2794
Chet Haase73066682010-11-29 15:55:32 -08002795 float tx = 0;
2796 float ty = 0;
2797 float rotation = 0;
2798 float rotationX = 0;
2799 float rotationY = 0;
2800 float sx = 1f;
2801 float sy = 1f;
2802 boolean transformSet = false;
2803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2805
Adam Powell637d3372010-08-25 14:37:03 -07002806 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 final int N = a.getIndexCount();
2808 for (int i = 0; i < N; i++) {
2809 int attr = a.getIndex(i);
2810 switch (attr) {
2811 case com.android.internal.R.styleable.View_background:
2812 background = a.getDrawable(attr);
2813 break;
2814 case com.android.internal.R.styleable.View_padding:
2815 padding = a.getDimensionPixelSize(attr, -1);
2816 break;
2817 case com.android.internal.R.styleable.View_paddingLeft:
2818 leftPadding = a.getDimensionPixelSize(attr, -1);
2819 break;
2820 case com.android.internal.R.styleable.View_paddingTop:
2821 topPadding = a.getDimensionPixelSize(attr, -1);
2822 break;
2823 case com.android.internal.R.styleable.View_paddingRight:
2824 rightPadding = a.getDimensionPixelSize(attr, -1);
2825 break;
2826 case com.android.internal.R.styleable.View_paddingBottom:
2827 bottomPadding = a.getDimensionPixelSize(attr, -1);
2828 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002829 case com.android.internal.R.styleable.View_paddingStart:
2830 startPadding = a.getDimensionPixelSize(attr, -1);
2831 break;
2832 case com.android.internal.R.styleable.View_paddingEnd:
2833 endPadding = a.getDimensionPixelSize(attr, -1);
2834 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 case com.android.internal.R.styleable.View_scrollX:
2836 x = a.getDimensionPixelOffset(attr, 0);
2837 break;
2838 case com.android.internal.R.styleable.View_scrollY:
2839 y = a.getDimensionPixelOffset(attr, 0);
2840 break;
Chet Haase73066682010-11-29 15:55:32 -08002841 case com.android.internal.R.styleable.View_alpha:
2842 setAlpha(a.getFloat(attr, 1f));
2843 break;
2844 case com.android.internal.R.styleable.View_transformPivotX:
2845 setPivotX(a.getDimensionPixelOffset(attr, 0));
2846 break;
2847 case com.android.internal.R.styleable.View_transformPivotY:
2848 setPivotY(a.getDimensionPixelOffset(attr, 0));
2849 break;
2850 case com.android.internal.R.styleable.View_translationX:
2851 tx = a.getDimensionPixelOffset(attr, 0);
2852 transformSet = true;
2853 break;
2854 case com.android.internal.R.styleable.View_translationY:
2855 ty = a.getDimensionPixelOffset(attr, 0);
2856 transformSet = true;
2857 break;
2858 case com.android.internal.R.styleable.View_rotation:
2859 rotation = a.getFloat(attr, 0);
2860 transformSet = true;
2861 break;
2862 case com.android.internal.R.styleable.View_rotationX:
2863 rotationX = a.getFloat(attr, 0);
2864 transformSet = true;
2865 break;
2866 case com.android.internal.R.styleable.View_rotationY:
2867 rotationY = a.getFloat(attr, 0);
2868 transformSet = true;
2869 break;
2870 case com.android.internal.R.styleable.View_scaleX:
2871 sx = a.getFloat(attr, 1f);
2872 transformSet = true;
2873 break;
2874 case com.android.internal.R.styleable.View_scaleY:
2875 sy = a.getFloat(attr, 1f);
2876 transformSet = true;
2877 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 case com.android.internal.R.styleable.View_id:
2879 mID = a.getResourceId(attr, NO_ID);
2880 break;
2881 case com.android.internal.R.styleable.View_tag:
2882 mTag = a.getText(attr);
2883 break;
2884 case com.android.internal.R.styleable.View_fitsSystemWindows:
2885 if (a.getBoolean(attr, false)) {
2886 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2887 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2888 }
2889 break;
2890 case com.android.internal.R.styleable.View_focusable:
2891 if (a.getBoolean(attr, false)) {
2892 viewFlagValues |= FOCUSABLE;
2893 viewFlagMasks |= FOCUSABLE_MASK;
2894 }
2895 break;
2896 case com.android.internal.R.styleable.View_focusableInTouchMode:
2897 if (a.getBoolean(attr, false)) {
2898 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2899 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2900 }
2901 break;
2902 case com.android.internal.R.styleable.View_clickable:
2903 if (a.getBoolean(attr, false)) {
2904 viewFlagValues |= CLICKABLE;
2905 viewFlagMasks |= CLICKABLE;
2906 }
2907 break;
2908 case com.android.internal.R.styleable.View_longClickable:
2909 if (a.getBoolean(attr, false)) {
2910 viewFlagValues |= LONG_CLICKABLE;
2911 viewFlagMasks |= LONG_CLICKABLE;
2912 }
2913 break;
2914 case com.android.internal.R.styleable.View_saveEnabled:
2915 if (!a.getBoolean(attr, true)) {
2916 viewFlagValues |= SAVE_DISABLED;
2917 viewFlagMasks |= SAVE_DISABLED_MASK;
2918 }
2919 break;
2920 case com.android.internal.R.styleable.View_duplicateParentState:
2921 if (a.getBoolean(attr, false)) {
2922 viewFlagValues |= DUPLICATE_PARENT_STATE;
2923 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2924 }
2925 break;
2926 case com.android.internal.R.styleable.View_visibility:
2927 final int visibility = a.getInt(attr, 0);
2928 if (visibility != 0) {
2929 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2930 viewFlagMasks |= VISIBILITY_MASK;
2931 }
2932 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002933 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002934 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002935 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002936 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002937 final int layoutDirection = a.getInt(attr, -1);
2938 if (layoutDirection != -1) {
2939 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002940 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002941 // Set to default (LAYOUT_DIRECTION_INHERIT)
2942 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002943 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002944 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002945 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 case com.android.internal.R.styleable.View_drawingCacheQuality:
2947 final int cacheQuality = a.getInt(attr, 0);
2948 if (cacheQuality != 0) {
2949 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2950 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2951 }
2952 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002953 case com.android.internal.R.styleable.View_contentDescription:
2954 mContentDescription = a.getString(attr);
2955 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2957 if (!a.getBoolean(attr, true)) {
2958 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2959 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2960 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002961 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2963 if (!a.getBoolean(attr, true)) {
2964 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2965 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2966 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002967 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 case R.styleable.View_scrollbars:
2969 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2970 if (scrollbars != SCROLLBARS_NONE) {
2971 viewFlagValues |= scrollbars;
2972 viewFlagMasks |= SCROLLBARS_MASK;
2973 initializeScrollbars(a);
2974 }
2975 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07002976 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07002978 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
2979 // Ignore the attribute starting with ICS
2980 break;
2981 }
2982 // With builds < ICS, fall through and apply fading edges
2983 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2985 if (fadingEdge != FADING_EDGE_NONE) {
2986 viewFlagValues |= fadingEdge;
2987 viewFlagMasks |= FADING_EDGE_MASK;
2988 initializeFadingEdge(a);
2989 }
2990 break;
2991 case R.styleable.View_scrollbarStyle:
2992 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2993 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2994 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2995 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2996 }
2997 break;
2998 case R.styleable.View_isScrollContainer:
2999 setScrollContainer = true;
3000 if (a.getBoolean(attr, false)) {
3001 setScrollContainer(true);
3002 }
3003 break;
3004 case com.android.internal.R.styleable.View_keepScreenOn:
3005 if (a.getBoolean(attr, false)) {
3006 viewFlagValues |= KEEP_SCREEN_ON;
3007 viewFlagMasks |= KEEP_SCREEN_ON;
3008 }
3009 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003010 case R.styleable.View_filterTouchesWhenObscured:
3011 if (a.getBoolean(attr, false)) {
3012 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3013 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3014 }
3015 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 case R.styleable.View_nextFocusLeft:
3017 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3018 break;
3019 case R.styleable.View_nextFocusRight:
3020 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3021 break;
3022 case R.styleable.View_nextFocusUp:
3023 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3024 break;
3025 case R.styleable.View_nextFocusDown:
3026 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3027 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003028 case R.styleable.View_nextFocusForward:
3029 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3030 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003031 case R.styleable.View_minWidth:
3032 mMinWidth = a.getDimensionPixelSize(attr, 0);
3033 break;
3034 case R.styleable.View_minHeight:
3035 mMinHeight = a.getDimensionPixelSize(attr, 0);
3036 break;
Romain Guy9a817362009-05-01 10:57:14 -07003037 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003038 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003039 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003040 + "be used within a restricted context");
3041 }
3042
Romain Guy9a817362009-05-01 10:57:14 -07003043 final String handlerName = a.getString(attr);
3044 if (handlerName != null) {
3045 setOnClickListener(new OnClickListener() {
3046 private Method mHandler;
3047
3048 public void onClick(View v) {
3049 if (mHandler == null) {
3050 try {
3051 mHandler = getContext().getClass().getMethod(handlerName,
3052 View.class);
3053 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003054 int id = getId();
3055 String idText = id == NO_ID ? "" : " with id '"
3056 + getContext().getResources().getResourceEntryName(
3057 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003058 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003059 handlerName + "(View) in the activity "
3060 + getContext().getClass() + " for onClick handler"
3061 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003062 }
3063 }
3064
3065 try {
3066 mHandler.invoke(getContext(), View.this);
3067 } catch (IllegalAccessException e) {
3068 throw new IllegalStateException("Could not execute non "
3069 + "public method of the activity", e);
3070 } catch (InvocationTargetException e) {
3071 throw new IllegalStateException("Could not execute "
3072 + "method of the activity", e);
3073 }
3074 }
3075 });
3076 }
3077 break;
Adam Powell637d3372010-08-25 14:37:03 -07003078 case R.styleable.View_overScrollMode:
3079 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3080 break;
Adam Powell20232d02010-12-08 21:08:53 -08003081 case R.styleable.View_verticalScrollbarPosition:
3082 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3083 break;
Romain Guy171c5922011-01-06 10:04:23 -08003084 case R.styleable.View_layerType:
3085 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3086 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003087 case R.styleable.View_textDirection:
3088 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
3089 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090 }
3091 }
3092
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003093 a.recycle();
3094
Adam Powell637d3372010-08-25 14:37:03 -07003095 setOverScrollMode(overScrollMode);
3096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 if (background != null) {
3098 setBackgroundDrawable(background);
3099 }
3100
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003101 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
3102
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003103 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3104 // layout direction). Those cached values will be used later during padding resolution.
3105 mUserPaddingStart = startPadding;
3106 mUserPaddingEnd = endPadding;
3107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 if (padding >= 0) {
3109 leftPadding = padding;
3110 topPadding = padding;
3111 rightPadding = padding;
3112 bottomPadding = padding;
3113 }
3114
3115 // If the user specified the padding (either with android:padding or
3116 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3117 // use the default padding or the padding from the background drawable
3118 // (stored at this point in mPadding*)
3119 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3120 topPadding >= 0 ? topPadding : mPaddingTop,
3121 rightPadding >= 0 ? rightPadding : mPaddingRight,
3122 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3123
3124 if (viewFlagMasks != 0) {
3125 setFlags(viewFlagValues, viewFlagMasks);
3126 }
3127
3128 // Needs to be called after mViewFlags is set
3129 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3130 recomputePadding();
3131 }
3132
3133 if (x != 0 || y != 0) {
3134 scrollTo(x, y);
3135 }
3136
Chet Haase73066682010-11-29 15:55:32 -08003137 if (transformSet) {
3138 setTranslationX(tx);
3139 setTranslationY(ty);
3140 setRotation(rotation);
3141 setRotationX(rotationX);
3142 setRotationY(rotationY);
3143 setScaleX(sx);
3144 setScaleY(sy);
3145 }
3146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3148 setScrollContainer(true);
3149 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003150
3151 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003152 }
3153
3154 /**
3155 * Non-public constructor for use in testing
3156 */
3157 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003158 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003159 }
3160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 /**
3162 * <p>
3163 * Initializes the fading edges from a given set of styled attributes. This
3164 * method should be called by subclasses that need fading edges and when an
3165 * instance of these subclasses is created programmatically rather than
3166 * being inflated from XML. This method is automatically called when the XML
3167 * is inflated.
3168 * </p>
3169 *
3170 * @param a the styled attributes set to initialize the fading edges from
3171 */
3172 protected void initializeFadingEdge(TypedArray a) {
3173 initScrollCache();
3174
3175 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3176 R.styleable.View_fadingEdgeLength,
3177 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3178 }
3179
3180 /**
3181 * Returns the size of the vertical faded edges used to indicate that more
3182 * content in this view is visible.
3183 *
3184 * @return The size in pixels of the vertical faded edge or 0 if vertical
3185 * faded edges are not enabled for this view.
3186 * @attr ref android.R.styleable#View_fadingEdgeLength
3187 */
3188 public int getVerticalFadingEdgeLength() {
3189 if (isVerticalFadingEdgeEnabled()) {
3190 ScrollabilityCache cache = mScrollCache;
3191 if (cache != null) {
3192 return cache.fadingEdgeLength;
3193 }
3194 }
3195 return 0;
3196 }
3197
3198 /**
3199 * Set the size of the faded edge used to indicate that more content in this
3200 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003201 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3202 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3203 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 *
3205 * @param length The size in pixels of the faded edge used to indicate that more
3206 * content in this view is visible.
3207 */
3208 public void setFadingEdgeLength(int length) {
3209 initScrollCache();
3210 mScrollCache.fadingEdgeLength = length;
3211 }
3212
3213 /**
3214 * Returns the size of the horizontal faded edges used to indicate that more
3215 * content in this view is visible.
3216 *
3217 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3218 * faded edges are not enabled for this view.
3219 * @attr ref android.R.styleable#View_fadingEdgeLength
3220 */
3221 public int getHorizontalFadingEdgeLength() {
3222 if (isHorizontalFadingEdgeEnabled()) {
3223 ScrollabilityCache cache = mScrollCache;
3224 if (cache != null) {
3225 return cache.fadingEdgeLength;
3226 }
3227 }
3228 return 0;
3229 }
3230
3231 /**
3232 * Returns the width of the vertical scrollbar.
3233 *
3234 * @return The width in pixels of the vertical scrollbar or 0 if there
3235 * is no vertical scrollbar.
3236 */
3237 public int getVerticalScrollbarWidth() {
3238 ScrollabilityCache cache = mScrollCache;
3239 if (cache != null) {
3240 ScrollBarDrawable scrollBar = cache.scrollBar;
3241 if (scrollBar != null) {
3242 int size = scrollBar.getSize(true);
3243 if (size <= 0) {
3244 size = cache.scrollBarSize;
3245 }
3246 return size;
3247 }
3248 return 0;
3249 }
3250 return 0;
3251 }
3252
3253 /**
3254 * Returns the height of the horizontal scrollbar.
3255 *
3256 * @return The height in pixels of the horizontal scrollbar or 0 if
3257 * there is no horizontal scrollbar.
3258 */
3259 protected int getHorizontalScrollbarHeight() {
3260 ScrollabilityCache cache = mScrollCache;
3261 if (cache != null) {
3262 ScrollBarDrawable scrollBar = cache.scrollBar;
3263 if (scrollBar != null) {
3264 int size = scrollBar.getSize(false);
3265 if (size <= 0) {
3266 size = cache.scrollBarSize;
3267 }
3268 return size;
3269 }
3270 return 0;
3271 }
3272 return 0;
3273 }
3274
3275 /**
3276 * <p>
3277 * Initializes the scrollbars from a given set of styled attributes. This
3278 * method should be called by subclasses that need scrollbars and when an
3279 * instance of these subclasses is created programmatically rather than
3280 * being inflated from XML. This method is automatically called when the XML
3281 * is inflated.
3282 * </p>
3283 *
3284 * @param a the styled attributes set to initialize the scrollbars from
3285 */
3286 protected void initializeScrollbars(TypedArray a) {
3287 initScrollCache();
3288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003290
Mike Cleronf116bf82009-09-27 19:14:12 -07003291 if (scrollabilityCache.scrollBar == null) {
3292 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3293 }
Joe Malin32736f02011-01-19 16:14:20 -08003294
Romain Guy8bda2482010-03-02 11:42:11 -08003295 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296
Mike Cleronf116bf82009-09-27 19:14:12 -07003297 if (!fadeScrollbars) {
3298 scrollabilityCache.state = ScrollabilityCache.ON;
3299 }
3300 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003301
3302
Mike Cleronf116bf82009-09-27 19:14:12 -07003303 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3304 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3305 .getScrollBarFadeDuration());
3306 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3307 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3308 ViewConfiguration.getScrollDefaultDelay());
3309
Joe Malin32736f02011-01-19 16:14:20 -08003310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3312 com.android.internal.R.styleable.View_scrollbarSize,
3313 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3314
3315 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3316 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3317
3318 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3319 if (thumb != null) {
3320 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3321 }
3322
3323 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3324 false);
3325 if (alwaysDraw) {
3326 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3327 }
3328
3329 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3330 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3331
3332 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3333 if (thumb != null) {
3334 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3335 }
3336
3337 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3338 false);
3339 if (alwaysDraw) {
3340 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3341 }
3342
3343 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003344 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 }
3346
3347 /**
3348 * <p>
3349 * Initalizes the scrollability cache if necessary.
3350 * </p>
3351 */
3352 private void initScrollCache() {
3353 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003354 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003355 }
3356 }
3357
3358 /**
Adam Powell20232d02010-12-08 21:08:53 -08003359 * Set the position of the vertical scroll bar. Should be one of
3360 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3361 * {@link #SCROLLBAR_POSITION_RIGHT}.
3362 *
3363 * @param position Where the vertical scroll bar should be positioned.
3364 */
3365 public void setVerticalScrollbarPosition(int position) {
3366 if (mVerticalScrollbarPosition != position) {
3367 mVerticalScrollbarPosition = position;
3368 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003369 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003370 }
3371 }
3372
3373 /**
3374 * @return The position where the vertical scroll bar will show, if applicable.
3375 * @see #setVerticalScrollbarPosition(int)
3376 */
3377 public int getVerticalScrollbarPosition() {
3378 return mVerticalScrollbarPosition;
3379 }
3380
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003381 ListenerInfo getListenerInfo() {
3382 if (mListenerInfo != null) {
3383 return mListenerInfo;
3384 }
3385 mListenerInfo = new ListenerInfo();
3386 return mListenerInfo;
3387 }
3388
Adam Powell20232d02010-12-08 21:08:53 -08003389 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 * Register a callback to be invoked when focus of this view changed.
3391 *
3392 * @param l The callback that will run.
3393 */
3394 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003395 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396 }
3397
3398 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003399 * Add a listener that will be called when the bounds of the view change due to
3400 * layout processing.
3401 *
3402 * @param listener The listener that will be called when layout bounds change.
3403 */
3404 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003405 ListenerInfo li = getListenerInfo();
3406 if (li.mOnLayoutChangeListeners == null) {
3407 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003408 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003409 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3410 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003411 }
Chet Haase21cd1382010-09-01 17:42:29 -07003412 }
3413
3414 /**
3415 * Remove a listener for layout changes.
3416 *
3417 * @param listener The listener for layout bounds change.
3418 */
3419 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003420 ListenerInfo li = mListenerInfo;
3421 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003422 return;
3423 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003424 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003425 }
3426
3427 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003428 * Add a listener for attach state changes.
3429 *
3430 * This listener will be called whenever this view is attached or detached
3431 * from a window. Remove the listener using
3432 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3433 *
3434 * @param listener Listener to attach
3435 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3436 */
3437 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003438 ListenerInfo li = getListenerInfo();
3439 if (li.mOnAttachStateChangeListeners == null) {
3440 li.mOnAttachStateChangeListeners
3441 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003442 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003443 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003444 }
3445
3446 /**
3447 * Remove a listener for attach state changes. The listener will receive no further
3448 * notification of window attach/detach events.
3449 *
3450 * @param listener Listener to remove
3451 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3452 */
3453 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003454 ListenerInfo li = mListenerInfo;
3455 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003456 return;
3457 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003458 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003459 }
3460
3461 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003462 * Returns the focus-change callback registered for this view.
3463 *
3464 * @return The callback, or null if one is not registered.
3465 */
3466 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003467 ListenerInfo li = mListenerInfo;
3468 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003469 }
3470
3471 /**
3472 * Register a callback to be invoked when this view is clicked. If this view is not
3473 * clickable, it becomes clickable.
3474 *
3475 * @param l The callback that will run
3476 *
3477 * @see #setClickable(boolean)
3478 */
3479 public void setOnClickListener(OnClickListener l) {
3480 if (!isClickable()) {
3481 setClickable(true);
3482 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003483 getListenerInfo().mOnClickListener = l;
3484 }
3485
3486 /**
3487 * Return whether this view has an attached OnClickListener. Returns
3488 * true if there is a listener, false if there is none.
3489 */
3490 public boolean hasOnClickListeners() {
3491 ListenerInfo li = mListenerInfo;
3492 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 }
3494
3495 /**
3496 * Register a callback to be invoked when this view is clicked and held. If this view is not
3497 * long clickable, it becomes long clickable.
3498 *
3499 * @param l The callback that will run
3500 *
3501 * @see #setLongClickable(boolean)
3502 */
3503 public void setOnLongClickListener(OnLongClickListener l) {
3504 if (!isLongClickable()) {
3505 setLongClickable(true);
3506 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003507 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003508 }
3509
3510 /**
3511 * Register a callback to be invoked when the context menu for this view is
3512 * being built. If this view is not long clickable, it becomes long clickable.
3513 *
3514 * @param l The callback that will run
3515 *
3516 */
3517 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3518 if (!isLongClickable()) {
3519 setLongClickable(true);
3520 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003521 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003522 }
3523
3524 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003525 * Call this view's OnClickListener, if it is defined. Performs all normal
3526 * actions associated with clicking: reporting accessibility event, playing
3527 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003528 *
3529 * @return True there was an assigned OnClickListener that was called, false
3530 * otherwise is returned.
3531 */
3532 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003533 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3534
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003535 ListenerInfo li = mListenerInfo;
3536 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003537 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003538 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 return true;
3540 }
3541
3542 return false;
3543 }
3544
3545 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003546 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3547 * this only calls the listener, and does not do any associated clicking
3548 * actions like reporting an accessibility event.
3549 *
3550 * @return True there was an assigned OnClickListener that was called, false
3551 * otherwise is returned.
3552 */
3553 public boolean callOnClick() {
3554 ListenerInfo li = mListenerInfo;
3555 if (li != null && li.mOnClickListener != null) {
3556 li.mOnClickListener.onClick(this);
3557 return true;
3558 }
3559 return false;
3560 }
3561
3562 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003563 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3564 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003565 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003566 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003567 */
3568 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003569 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003572 ListenerInfo li = mListenerInfo;
3573 if (li != null && li.mOnLongClickListener != null) {
3574 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 }
3576 if (!handled) {
3577 handled = showContextMenu();
3578 }
3579 if (handled) {
3580 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3581 }
3582 return handled;
3583 }
3584
3585 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003586 * Performs button-related actions during a touch down event.
3587 *
3588 * @param event The event.
3589 * @return True if the down was consumed.
3590 *
3591 * @hide
3592 */
3593 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3594 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3595 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3596 return true;
3597 }
3598 }
3599 return false;
3600 }
3601
3602 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003603 * Bring up the context menu for this view.
3604 *
3605 * @return Whether a context menu was displayed.
3606 */
3607 public boolean showContextMenu() {
3608 return getParent().showContextMenuForChild(this);
3609 }
3610
3611 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003612 * Bring up the context menu for this view, referring to the item under the specified point.
3613 *
3614 * @param x The referenced x coordinate.
3615 * @param y The referenced y coordinate.
3616 * @param metaState The keyboard modifiers that were pressed.
3617 * @return Whether a context menu was displayed.
3618 *
3619 * @hide
3620 */
3621 public boolean showContextMenu(float x, float y, int metaState) {
3622 return showContextMenu();
3623 }
3624
3625 /**
Adam Powell6e346362010-07-23 10:18:23 -07003626 * Start an action mode.
3627 *
3628 * @param callback Callback that will control the lifecycle of the action mode
3629 * @return The new action mode if it is started, null otherwise
3630 *
3631 * @see ActionMode
3632 */
3633 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08003634 ViewParent parent = getParent();
3635 if (parent == null) return null;
3636 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07003637 }
3638
3639 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003640 * Register a callback to be invoked when a key is pressed in this view.
3641 * @param l the key listener to attach to this view
3642 */
3643 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003644 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003645 }
3646
3647 /**
3648 * Register a callback to be invoked when a touch event is sent to this view.
3649 * @param l the touch listener to attach to this view
3650 */
3651 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003652 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003653 }
3654
3655 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003656 * Register a callback to be invoked when a generic motion event is sent to this view.
3657 * @param l the generic motion listener to attach to this view
3658 */
3659 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003660 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08003661 }
3662
3663 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003664 * Register a callback to be invoked when a hover event is sent to this view.
3665 * @param l the hover listener to attach to this view
3666 */
3667 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003668 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07003669 }
3670
3671 /**
Joe Malin32736f02011-01-19 16:14:20 -08003672 * Register a drag event listener callback object for this View. The parameter is
3673 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3674 * View, the system calls the
3675 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3676 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003677 */
3678 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003679 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07003680 }
3681
3682 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003683 * Give this view focus. This will cause
3684 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685 *
3686 * Note: this does not check whether this {@link View} should get focus, it just
3687 * gives it focus no matter what. It should only be called internally by framework
3688 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3689 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003690 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3691 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003692 * focus moved when requestFocus() is called. It may not always
3693 * apply, in which case use the default View.FOCUS_DOWN.
3694 * @param previouslyFocusedRect The rectangle of the view that had focus
3695 * prior in this View's coordinate system.
3696 */
3697 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3698 if (DBG) {
3699 System.out.println(this + " requestFocus()");
3700 }
3701
3702 if ((mPrivateFlags & FOCUSED) == 0) {
3703 mPrivateFlags |= FOCUSED;
3704
3705 if (mParent != null) {
3706 mParent.requestChildFocus(this, this);
3707 }
3708
3709 onFocusChanged(true, direction, previouslyFocusedRect);
3710 refreshDrawableState();
3711 }
3712 }
3713
3714 /**
3715 * Request that a rectangle of this view be visible on the screen,
3716 * scrolling if necessary just enough.
3717 *
3718 * <p>A View should call this if it maintains some notion of which part
3719 * of its content is interesting. For example, a text editing view
3720 * should call this when its cursor moves.
3721 *
3722 * @param rectangle The rectangle.
3723 * @return Whether any parent scrolled.
3724 */
3725 public boolean requestRectangleOnScreen(Rect rectangle) {
3726 return requestRectangleOnScreen(rectangle, false);
3727 }
3728
3729 /**
3730 * Request that a rectangle of this view be visible on the screen,
3731 * scrolling if necessary just enough.
3732 *
3733 * <p>A View should call this if it maintains some notion of which part
3734 * of its content is interesting. For example, a text editing view
3735 * should call this when its cursor moves.
3736 *
3737 * <p>When <code>immediate</code> is set to true, scrolling will not be
3738 * animated.
3739 *
3740 * @param rectangle The rectangle.
3741 * @param immediate True to forbid animated scrolling, false otherwise
3742 * @return Whether any parent scrolled.
3743 */
3744 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3745 View child = this;
3746 ViewParent parent = mParent;
3747 boolean scrolled = false;
3748 while (parent != null) {
3749 scrolled |= parent.requestChildRectangleOnScreen(child,
3750 rectangle, immediate);
3751
3752 // offset rect so next call has the rectangle in the
3753 // coordinate system of its direct child.
3754 rectangle.offset(child.getLeft(), child.getTop());
3755 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3756
3757 if (!(parent instanceof View)) {
3758 break;
3759 }
Romain Guy8506ab42009-06-11 17:35:47 -07003760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003761 child = (View) parent;
3762 parent = child.getParent();
3763 }
3764 return scrolled;
3765 }
3766
3767 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08003768 * Called when this view wants to give up focus. If focus is cleared
3769 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
3770 * <p>
3771 * <strong>Note:</strong> When a View clears focus the framework is trying
3772 * to give focus to the first focusable View from the top. Hence, if this
3773 * View is the first from the top that can take focus, then its focus will
3774 * not be cleared nor will the focus change callback be invoked.
3775 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003776 */
3777 public void clearFocus() {
3778 if (DBG) {
3779 System.out.println(this + " clearFocus()");
3780 }
3781
3782 if ((mPrivateFlags & FOCUSED) != 0) {
3783 mPrivateFlags &= ~FOCUSED;
3784
3785 if (mParent != null) {
3786 mParent.clearChildFocus(this);
3787 }
3788
3789 onFocusChanged(false, 0, null);
3790 refreshDrawableState();
3791 }
3792 }
3793
3794 /**
3795 * Called to clear the focus of a view that is about to be removed.
3796 * Doesn't call clearChildFocus, which prevents this view from taking
3797 * focus again before it has been removed from the parent
3798 */
3799 void clearFocusForRemoval() {
3800 if ((mPrivateFlags & FOCUSED) != 0) {
3801 mPrivateFlags &= ~FOCUSED;
3802
3803 onFocusChanged(false, 0, null);
3804 refreshDrawableState();
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08003805
3806 // The view cleared focus and invoked the callbacks, so now is the
3807 // time to give focus to the the first focusable from the top to
3808 // ensure that the gain focus is announced after clear focus.
3809 getRootView().requestFocus(FOCUS_FORWARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003810 }
3811 }
3812
3813 /**
3814 * Called internally by the view system when a new view is getting focus.
3815 * This is what clears the old focus.
3816 */
3817 void unFocus() {
3818 if (DBG) {
3819 System.out.println(this + " unFocus()");
3820 }
3821
3822 if ((mPrivateFlags & FOCUSED) != 0) {
3823 mPrivateFlags &= ~FOCUSED;
3824
3825 onFocusChanged(false, 0, null);
3826 refreshDrawableState();
3827 }
3828 }
3829
3830 /**
3831 * Returns true if this view has focus iteself, or is the ancestor of the
3832 * view that has focus.
3833 *
3834 * @return True if this view has or contains focus, false otherwise.
3835 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003836 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003837 public boolean hasFocus() {
3838 return (mPrivateFlags & FOCUSED) != 0;
3839 }
3840
3841 /**
3842 * Returns true if this view is focusable or if it contains a reachable View
3843 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3844 * is a View whose parents do not block descendants focus.
3845 *
3846 * Only {@link #VISIBLE} views are considered focusable.
3847 *
3848 * @return True if the view is focusable or if the view contains a focusable
3849 * View, false otherwise.
3850 *
3851 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3852 */
3853 public boolean hasFocusable() {
3854 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3855 }
3856
3857 /**
3858 * Called by the view system when the focus state of this view changes.
3859 * When the focus change event is caused by directional navigation, direction
3860 * and previouslyFocusedRect provide insight into where the focus is coming from.
3861 * When overriding, be sure to call up through to the super class so that
3862 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003863 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003864 * @param gainFocus True if the View has focus; false otherwise.
3865 * @param direction The direction focus has moved when requestFocus()
3866 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003867 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3868 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3869 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003870 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3871 * system, of the previously focused view. If applicable, this will be
3872 * passed in as finer grained information about where the focus is coming
3873 * from (in addition to direction). Will be <code>null</code> otherwise.
3874 */
3875 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003876 if (gainFocus) {
3877 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3878 }
3879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003880 InputMethodManager imm = InputMethodManager.peekInstance();
3881 if (!gainFocus) {
3882 if (isPressed()) {
3883 setPressed(false);
3884 }
3885 if (imm != null && mAttachInfo != null
3886 && mAttachInfo.mHasWindowFocus) {
3887 imm.focusOut(this);
3888 }
Romain Guya2431d02009-04-30 16:30:00 -07003889 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003890 } else if (imm != null && mAttachInfo != null
3891 && mAttachInfo.mHasWindowFocus) {
3892 imm.focusIn(this);
3893 }
Romain Guy8506ab42009-06-11 17:35:47 -07003894
Romain Guy0fd89bf2011-01-26 15:41:30 -08003895 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003896 ListenerInfo li = mListenerInfo;
3897 if (li != null && li.mOnFocusChangeListener != null) {
3898 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 }
Joe Malin32736f02011-01-19 16:14:20 -08003900
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003901 if (mAttachInfo != null) {
3902 mAttachInfo.mKeyDispatchState.reset(this);
3903 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 }
3905
3906 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003907 * Sends an accessibility event of the given type. If accessiiblity is
3908 * not enabled this method has no effect. The default implementation calls
3909 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3910 * to populate information about the event source (this View), then calls
3911 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3912 * populate the text content of the event source including its descendants,
3913 * and last calls
3914 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3915 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003916 * <p>
3917 * If an {@link AccessibilityDelegate} has been specified via calling
3918 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3919 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
3920 * responsible for handling this call.
3921 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003922 *
Scott Mainb303d832011-10-12 16:45:18 -07003923 * @param eventType The type of the event to send, as defined by several types from
3924 * {@link android.view.accessibility.AccessibilityEvent}, such as
3925 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
3926 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003927 *
3928 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3929 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3930 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003931 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07003932 */
3933 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003934 if (mAccessibilityDelegate != null) {
3935 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
3936 } else {
3937 sendAccessibilityEventInternal(eventType);
3938 }
3939 }
3940
3941 /**
3942 * @see #sendAccessibilityEvent(int)
3943 *
3944 * Note: Called from the default {@link AccessibilityDelegate}.
3945 */
3946 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003947 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3948 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3949 }
3950 }
3951
3952 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003953 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3954 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003955 * perform a check whether accessibility is enabled.
3956 * <p>
3957 * If an {@link AccessibilityDelegate} has been specified via calling
3958 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3959 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
3960 * is responsible for handling this call.
3961 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003962 *
3963 * @param event The event to send.
3964 *
3965 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003966 */
3967 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003968 if (mAccessibilityDelegate != null) {
3969 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
3970 } else {
3971 sendAccessibilityEventUncheckedInternal(event);
3972 }
3973 }
3974
3975 /**
3976 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
3977 *
3978 * Note: Called from the default {@link AccessibilityDelegate}.
3979 */
3980 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003981 if (!isShown()) {
3982 return;
3983 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003984 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07003985 // Only a subset of accessibility events populates text content.
3986 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
3987 dispatchPopulateAccessibilityEvent(event);
3988 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003989 // In the beginning we called #isShown(), so we know that getParent() is not null.
3990 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003991 }
3992
3993 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003994 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3995 * to its children for adding their text content to the event. Note that the
3996 * event text is populated in a separate dispatch path since we add to the
3997 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003998 * A typical implementation will call
3999 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4000 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4001 * on each child. Override this method if custom population of the event text
4002 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004003 * <p>
4004 * If an {@link AccessibilityDelegate} has been specified via calling
4005 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4006 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4007 * is responsible for handling this call.
4008 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004009 * <p>
4010 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4011 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4012 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004013 *
4014 * @param event The event.
4015 *
4016 * @return True if the event population was completed.
4017 */
4018 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004019 if (mAccessibilityDelegate != null) {
4020 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4021 } else {
4022 return dispatchPopulateAccessibilityEventInternal(event);
4023 }
4024 }
4025
4026 /**
4027 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4028 *
4029 * Note: Called from the default {@link AccessibilityDelegate}.
4030 */
4031 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004032 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004033 return false;
4034 }
4035
4036 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004037 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004038 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004039 * text content. While this method is free to modify event
4040 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004041 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4042 * <p>
4043 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004044 * to the text added by the super implementation:
4045 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004046 * super.onPopulateAccessibilityEvent(event);
4047 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4048 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4049 * mCurrentDate.getTimeInMillis(), flags);
4050 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004051 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004052 * <p>
4053 * If an {@link AccessibilityDelegate} has been specified via calling
4054 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4055 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4056 * is responsible for handling this call.
4057 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004058 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4059 * information to the event, in case the default implementation has basic information to add.
4060 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004061 *
4062 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004063 *
4064 * @see #sendAccessibilityEvent(int)
4065 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004066 */
4067 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004068 if (mAccessibilityDelegate != null) {
4069 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4070 } else {
4071 onPopulateAccessibilityEventInternal(event);
4072 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004073 }
4074
4075 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004076 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4077 *
4078 * Note: Called from the default {@link AccessibilityDelegate}.
4079 */
4080 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4081
4082 }
4083
4084 /**
4085 * Initializes an {@link AccessibilityEvent} with information about
4086 * this View which is the event source. In other words, the source of
4087 * an accessibility event is the view whose state change triggered firing
4088 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004089 * <p>
4090 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004091 * to properties set by the super implementation:
4092 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4093 * super.onInitializeAccessibilityEvent(event);
4094 * event.setPassword(true);
4095 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004096 * <p>
4097 * If an {@link AccessibilityDelegate} has been specified via calling
4098 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4099 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4100 * is responsible for handling this call.
4101 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004102 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4103 * information to the event, in case the default implementation has basic information to add.
4104 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004105 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004106 *
4107 * @see #sendAccessibilityEvent(int)
4108 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4109 */
4110 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004111 if (mAccessibilityDelegate != null) {
4112 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4113 } else {
4114 onInitializeAccessibilityEventInternal(event);
4115 }
4116 }
4117
4118 /**
4119 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4120 *
4121 * Note: Called from the default {@link AccessibilityDelegate}.
4122 */
4123 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004124 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004125 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004126 event.setPackageName(getContext().getPackageName());
4127 event.setEnabled(isEnabled());
4128 event.setContentDescription(mContentDescription);
4129
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004130 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
4131 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4132 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4133 FOCUSABLES_ALL);
4134 event.setItemCount(focusablesTempList.size());
4135 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4136 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004137 }
4138 }
4139
4140 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004141 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4142 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4143 * This method is responsible for obtaining an accessibility node info from a
4144 * pool of reusable instances and calling
4145 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4146 * initialize the former.
4147 * <p>
4148 * Note: The client is responsible for recycling the obtained instance by calling
4149 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4150 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004151 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004152 * @return A populated {@link AccessibilityNodeInfo}.
4153 *
4154 * @see AccessibilityNodeInfo
4155 */
4156 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004157 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4158 if (provider != null) {
4159 return provider.createAccessibilityNodeInfo(View.NO_ID);
4160 } else {
4161 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4162 onInitializeAccessibilityNodeInfo(info);
4163 return info;
4164 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004165 }
4166
4167 /**
4168 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4169 * The base implementation sets:
4170 * <ul>
4171 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004172 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4173 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004174 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4175 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4176 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4177 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4178 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4179 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4180 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4181 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4182 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4183 * </ul>
4184 * <p>
4185 * Subclasses should override this method, call the super implementation,
4186 * and set additional attributes.
4187 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004188 * <p>
4189 * If an {@link AccessibilityDelegate} has been specified via calling
4190 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4191 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4192 * is responsible for handling this call.
4193 * </p>
4194 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004195 * @param info The instance to initialize.
4196 */
4197 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004198 if (mAccessibilityDelegate != null) {
4199 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4200 } else {
4201 onInitializeAccessibilityNodeInfoInternal(info);
4202 }
4203 }
4204
4205 /**
4206 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4207 *
4208 * Note: Called from the default {@link AccessibilityDelegate}.
4209 */
4210 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004211 Rect bounds = mAttachInfo.mTmpInvalRect;
4212 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004213 info.setBoundsInParent(bounds);
4214
4215 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4216 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004217 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004218 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4219 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004220
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004221 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
4222 ViewParent parent = getParent();
4223 if (parent instanceof View) {
4224 View parentView = (View) parent;
4225 info.setParent(parentView);
4226 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004227 }
4228
4229 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004230 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004231 info.setContentDescription(getContentDescription());
4232
4233 info.setEnabled(isEnabled());
4234 info.setClickable(isClickable());
4235 info.setFocusable(isFocusable());
4236 info.setFocused(isFocused());
4237 info.setSelected(isSelected());
4238 info.setLongClickable(isLongClickable());
4239
4240 // TODO: These make sense only if we are in an AdapterView but all
4241 // views can be selected. Maybe from accessiiblity perspective
4242 // we should report as selectable view in an AdapterView.
4243 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4244 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4245
4246 if (isFocusable()) {
4247 if (isFocused()) {
4248 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4249 } else {
4250 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4251 }
4252 }
4253 }
4254
4255 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004256 * Sets a delegate for implementing accessibility support via compositon as
4257 * opposed to inheritance. The delegate's primary use is for implementing
4258 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4259 *
4260 * @param delegate The delegate instance.
4261 *
4262 * @see AccessibilityDelegate
4263 */
4264 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4265 mAccessibilityDelegate = delegate;
4266 }
4267
4268 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004269 * Gets the provider for managing a virtual view hierarchy rooted at this View
4270 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4271 * that explore the window content.
4272 * <p>
4273 * If this method returns an instance, this instance is responsible for managing
4274 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4275 * View including the one representing the View itself. Similarly the returned
4276 * instance is responsible for performing accessibility actions on any virtual
4277 * view or the root view itself.
4278 * </p>
4279 * <p>
4280 * If an {@link AccessibilityDelegate} has been specified via calling
4281 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4282 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4283 * is responsible for handling this call.
4284 * </p>
4285 *
4286 * @return The provider.
4287 *
4288 * @see AccessibilityNodeProvider
4289 */
4290 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4291 if (mAccessibilityDelegate != null) {
4292 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4293 } else {
4294 return null;
4295 }
4296 }
4297
4298 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004299 * Gets the unique identifier of this view on the screen for accessibility purposes.
4300 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4301 *
4302 * @return The view accessibility id.
4303 *
4304 * @hide
4305 */
4306 public int getAccessibilityViewId() {
4307 if (mAccessibilityViewId == NO_ID) {
4308 mAccessibilityViewId = sNextAccessibilityViewId++;
4309 }
4310 return mAccessibilityViewId;
4311 }
4312
4313 /**
4314 * Gets the unique identifier of the window in which this View reseides.
4315 *
4316 * @return The window accessibility id.
4317 *
4318 * @hide
4319 */
4320 public int getAccessibilityWindowId() {
4321 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4322 }
4323
4324 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004325 * Gets the {@link View} description. It briefly describes the view and is
4326 * primarily used for accessibility support. Set this property to enable
4327 * better accessibility support for your application. This is especially
4328 * true for views that do not have textual representation (For example,
4329 * ImageButton).
4330 *
4331 * @return The content descriptiopn.
4332 *
4333 * @attr ref android.R.styleable#View_contentDescription
4334 */
4335 public CharSequence getContentDescription() {
4336 return mContentDescription;
4337 }
4338
4339 /**
4340 * Sets the {@link View} description. It briefly describes the view and is
4341 * primarily used for accessibility support. Set this property to enable
4342 * better accessibility support for your application. This is especially
4343 * true for views that do not have textual representation (For example,
4344 * ImageButton).
4345 *
4346 * @param contentDescription The content description.
4347 *
4348 * @attr ref android.R.styleable#View_contentDescription
4349 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004350 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004351 public void setContentDescription(CharSequence contentDescription) {
4352 mContentDescription = contentDescription;
4353 }
4354
4355 /**
Romain Guya2431d02009-04-30 16:30:00 -07004356 * Invoked whenever this view loses focus, either by losing window focus or by losing
4357 * focus within its window. This method can be used to clear any state tied to the
4358 * focus. For instance, if a button is held pressed with the trackball and the window
4359 * loses focus, this method can be used to cancel the press.
4360 *
4361 * Subclasses of View overriding this method should always call super.onFocusLost().
4362 *
4363 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004364 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004365 *
4366 * @hide pending API council approval
4367 */
4368 protected void onFocusLost() {
4369 resetPressedState();
4370 }
4371
4372 private void resetPressedState() {
4373 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4374 return;
4375 }
4376
4377 if (isPressed()) {
4378 setPressed(false);
4379
4380 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004381 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004382 }
4383 }
4384 }
4385
4386 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004387 * Returns true if this view has focus
4388 *
4389 * @return True if this view has focus, false otherwise.
4390 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004391 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004392 public boolean isFocused() {
4393 return (mPrivateFlags & FOCUSED) != 0;
4394 }
4395
4396 /**
4397 * Find the view in the hierarchy rooted at this view that currently has
4398 * focus.
4399 *
4400 * @return The view that currently has focus, or null if no focused view can
4401 * be found.
4402 */
4403 public View findFocus() {
4404 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4405 }
4406
4407 /**
4408 * Change whether this view is one of the set of scrollable containers in
4409 * its window. This will be used to determine whether the window can
4410 * resize or must pan when a soft input area is open -- scrollable
4411 * containers allow the window to use resize mode since the container
4412 * will appropriately shrink.
4413 */
4414 public void setScrollContainer(boolean isScrollContainer) {
4415 if (isScrollContainer) {
4416 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4417 mAttachInfo.mScrollContainers.add(this);
4418 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4419 }
4420 mPrivateFlags |= SCROLL_CONTAINER;
4421 } else {
4422 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4423 mAttachInfo.mScrollContainers.remove(this);
4424 }
4425 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4426 }
4427 }
4428
4429 /**
4430 * Returns the quality of the drawing cache.
4431 *
4432 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4433 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4434 *
4435 * @see #setDrawingCacheQuality(int)
4436 * @see #setDrawingCacheEnabled(boolean)
4437 * @see #isDrawingCacheEnabled()
4438 *
4439 * @attr ref android.R.styleable#View_drawingCacheQuality
4440 */
4441 public int getDrawingCacheQuality() {
4442 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4443 }
4444
4445 /**
4446 * Set the drawing cache quality of this view. This value is used only when the
4447 * drawing cache is enabled
4448 *
4449 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4450 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4451 *
4452 * @see #getDrawingCacheQuality()
4453 * @see #setDrawingCacheEnabled(boolean)
4454 * @see #isDrawingCacheEnabled()
4455 *
4456 * @attr ref android.R.styleable#View_drawingCacheQuality
4457 */
4458 public void setDrawingCacheQuality(int quality) {
4459 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4460 }
4461
4462 /**
4463 * Returns whether the screen should remain on, corresponding to the current
4464 * value of {@link #KEEP_SCREEN_ON}.
4465 *
4466 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4467 *
4468 * @see #setKeepScreenOn(boolean)
4469 *
4470 * @attr ref android.R.styleable#View_keepScreenOn
4471 */
4472 public boolean getKeepScreenOn() {
4473 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4474 }
4475
4476 /**
4477 * Controls whether the screen should remain on, modifying the
4478 * value of {@link #KEEP_SCREEN_ON}.
4479 *
4480 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4481 *
4482 * @see #getKeepScreenOn()
4483 *
4484 * @attr ref android.R.styleable#View_keepScreenOn
4485 */
4486 public void setKeepScreenOn(boolean keepScreenOn) {
4487 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4488 }
4489
4490 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004491 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4492 * @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 -08004493 *
4494 * @attr ref android.R.styleable#View_nextFocusLeft
4495 */
4496 public int getNextFocusLeftId() {
4497 return mNextFocusLeftId;
4498 }
4499
4500 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004501 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4502 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4503 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004504 *
4505 * @attr ref android.R.styleable#View_nextFocusLeft
4506 */
4507 public void setNextFocusLeftId(int nextFocusLeftId) {
4508 mNextFocusLeftId = nextFocusLeftId;
4509 }
4510
4511 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004512 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4513 * @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 -08004514 *
4515 * @attr ref android.R.styleable#View_nextFocusRight
4516 */
4517 public int getNextFocusRightId() {
4518 return mNextFocusRightId;
4519 }
4520
4521 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004522 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4523 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4524 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004525 *
4526 * @attr ref android.R.styleable#View_nextFocusRight
4527 */
4528 public void setNextFocusRightId(int nextFocusRightId) {
4529 mNextFocusRightId = nextFocusRightId;
4530 }
4531
4532 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004533 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4534 * @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 -08004535 *
4536 * @attr ref android.R.styleable#View_nextFocusUp
4537 */
4538 public int getNextFocusUpId() {
4539 return mNextFocusUpId;
4540 }
4541
4542 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004543 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4544 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4545 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004546 *
4547 * @attr ref android.R.styleable#View_nextFocusUp
4548 */
4549 public void setNextFocusUpId(int nextFocusUpId) {
4550 mNextFocusUpId = nextFocusUpId;
4551 }
4552
4553 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004554 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4555 * @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 -08004556 *
4557 * @attr ref android.R.styleable#View_nextFocusDown
4558 */
4559 public int getNextFocusDownId() {
4560 return mNextFocusDownId;
4561 }
4562
4563 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004564 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4565 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4566 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004567 *
4568 * @attr ref android.R.styleable#View_nextFocusDown
4569 */
4570 public void setNextFocusDownId(int nextFocusDownId) {
4571 mNextFocusDownId = nextFocusDownId;
4572 }
4573
4574 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004575 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4576 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4577 *
4578 * @attr ref android.R.styleable#View_nextFocusForward
4579 */
4580 public int getNextFocusForwardId() {
4581 return mNextFocusForwardId;
4582 }
4583
4584 /**
4585 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4586 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4587 * decide automatically.
4588 *
4589 * @attr ref android.R.styleable#View_nextFocusForward
4590 */
4591 public void setNextFocusForwardId(int nextFocusForwardId) {
4592 mNextFocusForwardId = nextFocusForwardId;
4593 }
4594
4595 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004596 * Returns the visibility of this view and all of its ancestors
4597 *
4598 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4599 */
4600 public boolean isShown() {
4601 View current = this;
4602 //noinspection ConstantConditions
4603 do {
4604 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4605 return false;
4606 }
4607 ViewParent parent = current.mParent;
4608 if (parent == null) {
4609 return false; // We are not attached to the view root
4610 }
4611 if (!(parent instanceof View)) {
4612 return true;
4613 }
4614 current = (View) parent;
4615 } while (current != null);
4616
4617 return false;
4618 }
4619
4620 /**
4621 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4622 * is set
4623 *
4624 * @param insets Insets for system windows
4625 *
4626 * @return True if this view applied the insets, false otherwise
4627 */
4628 protected boolean fitSystemWindows(Rect insets) {
4629 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4630 mPaddingLeft = insets.left;
4631 mPaddingTop = insets.top;
4632 mPaddingRight = insets.right;
4633 mPaddingBottom = insets.bottom;
4634 requestLayout();
4635 return true;
4636 }
4637 return false;
4638 }
4639
4640 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004641 * Set whether or not this view should account for system screen decorations
4642 * such as the status bar and inset its content. This allows this view to be
4643 * positioned in absolute screen coordinates and remain visible to the user.
4644 *
4645 * <p>This should only be used by top-level window decor views.
4646 *
4647 * @param fitSystemWindows true to inset content for system screen decorations, false for
4648 * default behavior.
4649 *
4650 * @attr ref android.R.styleable#View_fitsSystemWindows
4651 */
4652 public void setFitsSystemWindows(boolean fitSystemWindows) {
4653 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4654 }
4655
4656 /**
4657 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4658 * will account for system screen decorations such as the status bar and inset its
4659 * content. This allows the view to be positioned in absolute screen coordinates
4660 * and remain visible to the user.
4661 *
4662 * @return true if this view will adjust its content bounds for system screen decorations.
4663 *
4664 * @attr ref android.R.styleable#View_fitsSystemWindows
4665 */
4666 public boolean fitsSystemWindows() {
4667 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4668 }
4669
4670 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004671 * Returns the visibility status for this view.
4672 *
4673 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4674 * @attr ref android.R.styleable#View_visibility
4675 */
4676 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004677 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4678 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4679 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004680 })
4681 public int getVisibility() {
4682 return mViewFlags & VISIBILITY_MASK;
4683 }
4684
4685 /**
4686 * Set the enabled state of this view.
4687 *
4688 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4689 * @attr ref android.R.styleable#View_visibility
4690 */
4691 @RemotableViewMethod
4692 public void setVisibility(int visibility) {
4693 setFlags(visibility, VISIBILITY_MASK);
4694 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4695 }
4696
4697 /**
4698 * Returns the enabled status for this view. The interpretation of the
4699 * enabled state varies by subclass.
4700 *
4701 * @return True if this view is enabled, false otherwise.
4702 */
4703 @ViewDebug.ExportedProperty
4704 public boolean isEnabled() {
4705 return (mViewFlags & ENABLED_MASK) == ENABLED;
4706 }
4707
4708 /**
4709 * Set the enabled state of this view. The interpretation of the enabled
4710 * state varies by subclass.
4711 *
4712 * @param enabled True if this view is enabled, false otherwise.
4713 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004714 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004715 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004716 if (enabled == isEnabled()) return;
4717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004718 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4719
4720 /*
4721 * The View most likely has to change its appearance, so refresh
4722 * the drawable state.
4723 */
4724 refreshDrawableState();
4725
4726 // Invalidate too, since the default behavior for views is to be
4727 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004728 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004729 }
4730
4731 /**
4732 * Set whether this view can receive the focus.
4733 *
4734 * Setting this to false will also ensure that this view is not focusable
4735 * in touch mode.
4736 *
4737 * @param focusable If true, this view can receive the focus.
4738 *
4739 * @see #setFocusableInTouchMode(boolean)
4740 * @attr ref android.R.styleable#View_focusable
4741 */
4742 public void setFocusable(boolean focusable) {
4743 if (!focusable) {
4744 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4745 }
4746 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4747 }
4748
4749 /**
4750 * Set whether this view can receive focus while in touch mode.
4751 *
4752 * Setting this to true will also ensure that this view is focusable.
4753 *
4754 * @param focusableInTouchMode If true, this view can receive the focus while
4755 * in touch mode.
4756 *
4757 * @see #setFocusable(boolean)
4758 * @attr ref android.R.styleable#View_focusableInTouchMode
4759 */
4760 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4761 // Focusable in touch mode should always be set before the focusable flag
4762 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4763 // which, in touch mode, will not successfully request focus on this view
4764 // because the focusable in touch mode flag is not set
4765 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4766 if (focusableInTouchMode) {
4767 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4768 }
4769 }
4770
4771 /**
4772 * Set whether this view should have sound effects enabled for events such as
4773 * clicking and touching.
4774 *
4775 * <p>You may wish to disable sound effects for a view if you already play sounds,
4776 * for instance, a dial key that plays dtmf tones.
4777 *
4778 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4779 * @see #isSoundEffectsEnabled()
4780 * @see #playSoundEffect(int)
4781 * @attr ref android.R.styleable#View_soundEffectsEnabled
4782 */
4783 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4784 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4785 }
4786
4787 /**
4788 * @return whether this view should have sound effects enabled for events such as
4789 * clicking and touching.
4790 *
4791 * @see #setSoundEffectsEnabled(boolean)
4792 * @see #playSoundEffect(int)
4793 * @attr ref android.R.styleable#View_soundEffectsEnabled
4794 */
4795 @ViewDebug.ExportedProperty
4796 public boolean isSoundEffectsEnabled() {
4797 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4798 }
4799
4800 /**
4801 * Set whether this view should have haptic feedback for events such as
4802 * long presses.
4803 *
4804 * <p>You may wish to disable haptic feedback if your view already controls
4805 * its own haptic feedback.
4806 *
4807 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4808 * @see #isHapticFeedbackEnabled()
4809 * @see #performHapticFeedback(int)
4810 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4811 */
4812 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4813 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4814 }
4815
4816 /**
4817 * @return whether this view should have haptic feedback enabled for events
4818 * long presses.
4819 *
4820 * @see #setHapticFeedbackEnabled(boolean)
4821 * @see #performHapticFeedback(int)
4822 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4823 */
4824 @ViewDebug.ExportedProperty
4825 public boolean isHapticFeedbackEnabled() {
4826 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4827 }
4828
4829 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004830 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004831 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004832 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4833 * {@link #LAYOUT_DIRECTION_RTL},
4834 * {@link #LAYOUT_DIRECTION_INHERIT} or
4835 * {@link #LAYOUT_DIRECTION_LOCALE}.
4836 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004837 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004838 * @hide
4839 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004840 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004841 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4842 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4843 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4844 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004845 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004846 public int getLayoutDirection() {
4847 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004848 }
4849
4850 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004851 * Set the layout direction for this view. This will propagate a reset of layout direction
4852 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004853 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004854 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4855 * {@link #LAYOUT_DIRECTION_RTL},
4856 * {@link #LAYOUT_DIRECTION_INHERIT} or
4857 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004858 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004859 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004860 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004861 * @hide
4862 */
4863 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004864 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004865 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004866 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004867 // Setting the flag will also request a layout.
4868 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4869 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004870 }
4871
4872 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004873 * Returns the resolved layout direction for this view.
4874 *
4875 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4876 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4877 *
4878 * @hide
4879 */
4880 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4881 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4882 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4883 })
4884 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004885 resolveLayoutDirectionIfNeeded();
4886 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004887 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4888 }
4889
4890 /**
4891 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4892 * layout attribute and/or the inherited value from the parent.</p>
4893 *
4894 * @return true if the layout is right-to-left.
4895 *
4896 * @hide
4897 */
4898 @ViewDebug.ExportedProperty(category = "layout")
4899 public boolean isLayoutRtl() {
4900 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4901 }
4902
4903 /**
Adam Powell539ee872012-02-03 19:00:49 -08004904 * Indicates whether the view is currently tracking transient state that the
4905 * app should not need to concern itself with saving and restoring, but that
4906 * the framework should take special note to preserve when possible.
4907 *
4908 * @return true if the view has transient state
4909 *
4910 * @hide
4911 */
4912 @ViewDebug.ExportedProperty(category = "layout")
4913 public boolean hasTransientState() {
4914 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
4915 }
4916
4917 /**
4918 * Set whether this view is currently tracking transient state that the
4919 * framework should attempt to preserve when possible.
4920 *
4921 * @param hasTransientState true if this view has transient state
4922 *
4923 * @hide
4924 */
4925 public void setHasTransientState(boolean hasTransientState) {
4926 if (hasTransientState() == hasTransientState) return;
4927
4928 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
4929 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
4930 if (mParent != null) {
4931 try {
4932 mParent.childHasTransientStateChanged(this, hasTransientState);
4933 } catch (AbstractMethodError e) {
4934 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
4935 " does not fully implement ViewParent", e);
4936 }
4937 }
4938 }
4939
4940 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004941 * If this view doesn't do any drawing on its own, set this flag to
4942 * allow further optimizations. By default, this flag is not set on
4943 * View, but could be set on some View subclasses such as ViewGroup.
4944 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004945 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4946 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004947 *
4948 * @param willNotDraw whether or not this View draw on its own
4949 */
4950 public void setWillNotDraw(boolean willNotDraw) {
4951 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4952 }
4953
4954 /**
4955 * Returns whether or not this View draws on its own.
4956 *
4957 * @return true if this view has nothing to draw, false otherwise
4958 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004959 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004960 public boolean willNotDraw() {
4961 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4962 }
4963
4964 /**
4965 * When a View's drawing cache is enabled, drawing is redirected to an
4966 * offscreen bitmap. Some views, like an ImageView, must be able to
4967 * bypass this mechanism if they already draw a single bitmap, to avoid
4968 * unnecessary usage of the memory.
4969 *
4970 * @param willNotCacheDrawing true if this view does not cache its
4971 * drawing, false otherwise
4972 */
4973 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4974 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4975 }
4976
4977 /**
4978 * Returns whether or not this View can cache its drawing or not.
4979 *
4980 * @return true if this view does not cache its drawing, false otherwise
4981 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004982 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004983 public boolean willNotCacheDrawing() {
4984 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4985 }
4986
4987 /**
4988 * Indicates whether this view reacts to click events or not.
4989 *
4990 * @return true if the view is clickable, false otherwise
4991 *
4992 * @see #setClickable(boolean)
4993 * @attr ref android.R.styleable#View_clickable
4994 */
4995 @ViewDebug.ExportedProperty
4996 public boolean isClickable() {
4997 return (mViewFlags & CLICKABLE) == CLICKABLE;
4998 }
4999
5000 /**
5001 * Enables or disables click events for this view. When a view
5002 * is clickable it will change its state to "pressed" on every click.
5003 * Subclasses should set the view clickable to visually react to
5004 * user's clicks.
5005 *
5006 * @param clickable true to make the view clickable, false otherwise
5007 *
5008 * @see #isClickable()
5009 * @attr ref android.R.styleable#View_clickable
5010 */
5011 public void setClickable(boolean clickable) {
5012 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5013 }
5014
5015 /**
5016 * Indicates whether this view reacts to long click events or not.
5017 *
5018 * @return true if the view is long clickable, false otherwise
5019 *
5020 * @see #setLongClickable(boolean)
5021 * @attr ref android.R.styleable#View_longClickable
5022 */
5023 public boolean isLongClickable() {
5024 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5025 }
5026
5027 /**
5028 * Enables or disables long click events for this view. When a view is long
5029 * clickable it reacts to the user holding down the button for a longer
5030 * duration than a tap. This event can either launch the listener or a
5031 * context menu.
5032 *
5033 * @param longClickable true to make the view long clickable, false otherwise
5034 * @see #isLongClickable()
5035 * @attr ref android.R.styleable#View_longClickable
5036 */
5037 public void setLongClickable(boolean longClickable) {
5038 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5039 }
5040
5041 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005042 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005043 *
5044 * @see #isClickable()
5045 * @see #setClickable(boolean)
5046 *
5047 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5048 * the View's internal state from a previously set "pressed" state.
5049 */
5050 public void setPressed(boolean pressed) {
Adam Powell4d6f0662012-02-21 15:11:11 -08005051 if (pressed == ((mPrivateFlags & PRESSED) == PRESSED)) {
5052 return;
5053 }
5054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005055 if (pressed) {
5056 mPrivateFlags |= PRESSED;
5057 } else {
5058 mPrivateFlags &= ~PRESSED;
5059 }
5060 refreshDrawableState();
5061 dispatchSetPressed(pressed);
5062 }
5063
5064 /**
5065 * Dispatch setPressed to all of this View's children.
5066 *
5067 * @see #setPressed(boolean)
5068 *
5069 * @param pressed The new pressed state
5070 */
5071 protected void dispatchSetPressed(boolean pressed) {
5072 }
5073
5074 /**
5075 * Indicates whether the view is currently in pressed state. Unless
5076 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5077 * the pressed state.
5078 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005079 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005080 * @see #isClickable()
5081 * @see #setClickable(boolean)
5082 *
5083 * @return true if the view is currently pressed, false otherwise
5084 */
5085 public boolean isPressed() {
5086 return (mPrivateFlags & PRESSED) == PRESSED;
5087 }
5088
5089 /**
5090 * Indicates whether this view will save its state (that is,
5091 * whether its {@link #onSaveInstanceState} method will be called).
5092 *
5093 * @return Returns true if the view state saving is enabled, else false.
5094 *
5095 * @see #setSaveEnabled(boolean)
5096 * @attr ref android.R.styleable#View_saveEnabled
5097 */
5098 public boolean isSaveEnabled() {
5099 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5100 }
5101
5102 /**
5103 * Controls whether the saving of this view's state is
5104 * enabled (that is, whether its {@link #onSaveInstanceState} method
5105 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005106 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005107 * for its state to be saved. This flag can only disable the
5108 * saving of this view; any child views may still have their state saved.
5109 *
5110 * @param enabled Set to false to <em>disable</em> state saving, or true
5111 * (the default) to allow it.
5112 *
5113 * @see #isSaveEnabled()
5114 * @see #setId(int)
5115 * @see #onSaveInstanceState()
5116 * @attr ref android.R.styleable#View_saveEnabled
5117 */
5118 public void setSaveEnabled(boolean enabled) {
5119 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5120 }
5121
Jeff Brown85a31762010-09-01 17:01:00 -07005122 /**
5123 * Gets whether the framework should discard touches when the view's
5124 * window is obscured by another visible window.
5125 * Refer to the {@link View} security documentation for more details.
5126 *
5127 * @return True if touch filtering is enabled.
5128 *
5129 * @see #setFilterTouchesWhenObscured(boolean)
5130 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5131 */
5132 @ViewDebug.ExportedProperty
5133 public boolean getFilterTouchesWhenObscured() {
5134 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5135 }
5136
5137 /**
5138 * Sets whether the framework should discard touches when the view's
5139 * window is obscured by another visible window.
5140 * Refer to the {@link View} security documentation for more details.
5141 *
5142 * @param enabled True if touch filtering should be enabled.
5143 *
5144 * @see #getFilterTouchesWhenObscured
5145 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5146 */
5147 public void setFilterTouchesWhenObscured(boolean enabled) {
5148 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5149 FILTER_TOUCHES_WHEN_OBSCURED);
5150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005151
5152 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005153 * Indicates whether the entire hierarchy under this view will save its
5154 * state when a state saving traversal occurs from its parent. The default
5155 * is true; if false, these views will not be saved unless
5156 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5157 *
5158 * @return Returns true if the view state saving from parent is enabled, else false.
5159 *
5160 * @see #setSaveFromParentEnabled(boolean)
5161 */
5162 public boolean isSaveFromParentEnabled() {
5163 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5164 }
5165
5166 /**
5167 * Controls whether the entire hierarchy under this view will save its
5168 * state when a state saving traversal occurs from its parent. The default
5169 * is true; if false, these views will not be saved unless
5170 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5171 *
5172 * @param enabled Set to false to <em>disable</em> state saving, or true
5173 * (the default) to allow it.
5174 *
5175 * @see #isSaveFromParentEnabled()
5176 * @see #setId(int)
5177 * @see #onSaveInstanceState()
5178 */
5179 public void setSaveFromParentEnabled(boolean enabled) {
5180 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5181 }
5182
5183
5184 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005185 * Returns whether this View is able to take focus.
5186 *
5187 * @return True if this view can take focus, or false otherwise.
5188 * @attr ref android.R.styleable#View_focusable
5189 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005190 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005191 public final boolean isFocusable() {
5192 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5193 }
5194
5195 /**
5196 * When a view is focusable, it may not want to take focus when in touch mode.
5197 * For example, a button would like focus when the user is navigating via a D-pad
5198 * so that the user can click on it, but once the user starts touching the screen,
5199 * the button shouldn't take focus
5200 * @return Whether the view is focusable in touch mode.
5201 * @attr ref android.R.styleable#View_focusableInTouchMode
5202 */
5203 @ViewDebug.ExportedProperty
5204 public final boolean isFocusableInTouchMode() {
5205 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5206 }
5207
5208 /**
5209 * Find the nearest view in the specified direction that can take focus.
5210 * This does not actually give focus to that view.
5211 *
5212 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5213 *
5214 * @return The nearest focusable in the specified direction, or null if none
5215 * can be found.
5216 */
5217 public View focusSearch(int direction) {
5218 if (mParent != null) {
5219 return mParent.focusSearch(this, direction);
5220 } else {
5221 return null;
5222 }
5223 }
5224
5225 /**
5226 * This method is the last chance for the focused view and its ancestors to
5227 * respond to an arrow key. This is called when the focused view did not
5228 * consume the key internally, nor could the view system find a new view in
5229 * the requested direction to give focus to.
5230 *
5231 * @param focused The currently focused view.
5232 * @param direction The direction focus wants to move. One of FOCUS_UP,
5233 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5234 * @return True if the this view consumed this unhandled move.
5235 */
5236 public boolean dispatchUnhandledMove(View focused, int direction) {
5237 return false;
5238 }
5239
5240 /**
5241 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005242 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005243 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005244 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5245 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005246 * @return The user specified next view, or null if there is none.
5247 */
5248 View findUserSetNextFocus(View root, int direction) {
5249 switch (direction) {
5250 case FOCUS_LEFT:
5251 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005252 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005253 case FOCUS_RIGHT:
5254 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005255 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005256 case FOCUS_UP:
5257 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005258 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005259 case FOCUS_DOWN:
5260 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005261 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005262 case FOCUS_FORWARD:
5263 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005264 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005265 case FOCUS_BACKWARD: {
5266 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005267 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005268 @Override
5269 public boolean apply(View t) {
5270 return t.mNextFocusForwardId == id;
5271 }
5272 });
5273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005274 }
5275 return null;
5276 }
5277
Jeff Brown4dfbec22011-08-15 14:55:37 -07005278 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5279 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5280 @Override
5281 public boolean apply(View t) {
5282 return t.mID == childViewId;
5283 }
5284 });
5285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005286 if (result == null) {
5287 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5288 + "by user for id " + childViewId);
5289 }
5290 return result;
5291 }
5292
5293 /**
5294 * Find and return all focusable views that are descendants of this view,
5295 * possibly including this view if it is focusable itself.
5296 *
5297 * @param direction The direction of the focus
5298 * @return A list of focusable views
5299 */
5300 public ArrayList<View> getFocusables(int direction) {
5301 ArrayList<View> result = new ArrayList<View>(24);
5302 addFocusables(result, direction);
5303 return result;
5304 }
5305
5306 /**
5307 * Add any focusable views that are descendants of this view (possibly
5308 * including this view if it is focusable itself) to views. If we are in touch mode,
5309 * only add views that are also focusable in touch mode.
5310 *
5311 * @param views Focusable views found so far
5312 * @param direction The direction of the focus
5313 */
5314 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005315 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005317
svetoslavganov75986cf2009-05-14 22:28:01 -07005318 /**
5319 * Adds any focusable views that are descendants of this view (possibly
5320 * including this view if it is focusable itself) to views. This method
5321 * adds all focusable views regardless if we are in touch mode or
5322 * only views focusable in touch mode if we are in touch mode depending on
5323 * the focusable mode paramater.
5324 *
5325 * @param views Focusable views found so far or null if all we are interested is
5326 * the number of focusables.
5327 * @param direction The direction of the focus.
5328 * @param focusableMode The type of focusables to be added.
5329 *
5330 * @see #FOCUSABLES_ALL
5331 * @see #FOCUSABLES_TOUCH_MODE
5332 */
5333 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5334 if (!isFocusable()) {
5335 return;
5336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005337
svetoslavganov75986cf2009-05-14 22:28:01 -07005338 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5339 isInTouchMode() && !isFocusableInTouchMode()) {
5340 return;
5341 }
5342
5343 if (views != null) {
5344 views.add(this);
5345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005346 }
5347
5348 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005349 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005350 * The search is performed by either the text that the View renders or the content
5351 * description that describes the view for accessibility purposes and the view does
5352 * not render or both. Clients can specify how the search is to be performed via
5353 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5354 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005355 *
5356 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005357 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005358 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005359 * @see #FIND_VIEWS_WITH_TEXT
5360 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5361 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005362 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005363 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005364 if (getAccessibilityNodeProvider() != null) {
5365 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5366 outViews.add(this);
5367 }
5368 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5369 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005370 String searchedLowerCase = searched.toString().toLowerCase();
5371 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5372 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5373 outViews.add(this);
5374 }
5375 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005376 }
5377
5378 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005379 * Find and return all touchable views that are descendants of this view,
5380 * possibly including this view if it is touchable itself.
5381 *
5382 * @return A list of touchable views
5383 */
5384 public ArrayList<View> getTouchables() {
5385 ArrayList<View> result = new ArrayList<View>();
5386 addTouchables(result);
5387 return result;
5388 }
5389
5390 /**
5391 * Add any touchable views that are descendants of this view (possibly
5392 * including this view if it is touchable itself) to views.
5393 *
5394 * @param views Touchable views found so far
5395 */
5396 public void addTouchables(ArrayList<View> views) {
5397 final int viewFlags = mViewFlags;
5398
5399 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5400 && (viewFlags & ENABLED_MASK) == ENABLED) {
5401 views.add(this);
5402 }
5403 }
5404
5405 /**
5406 * Call this to try to give focus to a specific view or to one of its
5407 * descendants.
5408 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005409 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5410 * false), or if it is focusable and it is not focusable in touch mode
5411 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005412 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005413 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005414 * have focus, and you want your parent to look for the next one.
5415 *
5416 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5417 * {@link #FOCUS_DOWN} and <code>null</code>.
5418 *
5419 * @return Whether this view or one of its descendants actually took focus.
5420 */
5421 public final boolean requestFocus() {
5422 return requestFocus(View.FOCUS_DOWN);
5423 }
5424
5425
5426 /**
5427 * Call this to try to give focus to a specific view or to one of its
5428 * descendants and give it a hint about what direction focus is heading.
5429 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005430 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5431 * false), or if it is focusable and it is not focusable in touch mode
5432 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005433 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005434 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005435 * have focus, and you want your parent to look for the next one.
5436 *
5437 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5438 * <code>null</code> set for the previously focused rectangle.
5439 *
5440 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5441 * @return Whether this view or one of its descendants actually took focus.
5442 */
5443 public final boolean requestFocus(int direction) {
5444 return requestFocus(direction, null);
5445 }
5446
5447 /**
5448 * Call this to try to give focus to a specific view or to one of its descendants
5449 * and give it hints about the direction and a specific rectangle that the focus
5450 * is coming from. The rectangle can help give larger views a finer grained hint
5451 * about where focus is coming from, and therefore, where to show selection, or
5452 * forward focus change internally.
5453 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005454 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5455 * false), or if it is focusable and it is not focusable in touch mode
5456 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005457 *
5458 * A View will not take focus if it is not visible.
5459 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005460 * A View will not take focus if one of its parents has
5461 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5462 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005463 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005464 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005465 * have focus, and you want your parent to look for the next one.
5466 *
5467 * You may wish to override this method if your custom {@link View} has an internal
5468 * {@link View} that it wishes to forward the request to.
5469 *
5470 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5471 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5472 * to give a finer grained hint about where focus is coming from. May be null
5473 * if there is no hint.
5474 * @return Whether this view or one of its descendants actually took focus.
5475 */
5476 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5477 // need to be focusable
5478 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5479 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5480 return false;
5481 }
5482
5483 // need to be focusable in touch mode if in touch mode
5484 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005485 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5486 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005487 }
5488
5489 // need to not have any parents blocking us
5490 if (hasAncestorThatBlocksDescendantFocus()) {
5491 return false;
5492 }
5493
5494 handleFocusGainInternal(direction, previouslyFocusedRect);
5495 return true;
5496 }
5497
5498 /**
5499 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5500 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5501 * touch mode to request focus when they are touched.
5502 *
5503 * @return Whether this view or one of its descendants actually took focus.
5504 *
5505 * @see #isInTouchMode()
5506 *
5507 */
5508 public final boolean requestFocusFromTouch() {
5509 // Leave touch mode if we need to
5510 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005511 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005512 if (viewRoot != null) {
5513 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005514 }
5515 }
5516 return requestFocus(View.FOCUS_DOWN);
5517 }
5518
5519 /**
5520 * @return Whether any ancestor of this view blocks descendant focus.
5521 */
5522 private boolean hasAncestorThatBlocksDescendantFocus() {
5523 ViewParent ancestor = mParent;
5524 while (ancestor instanceof ViewGroup) {
5525 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5526 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5527 return true;
5528 } else {
5529 ancestor = vgAncestor.getParent();
5530 }
5531 }
5532 return false;
5533 }
5534
5535 /**
Romain Guya440b002010-02-24 15:57:54 -08005536 * @hide
5537 */
5538 public void dispatchStartTemporaryDetach() {
5539 onStartTemporaryDetach();
5540 }
5541
5542 /**
5543 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005544 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5545 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005546 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005547 */
5548 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005549 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005550 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005551 }
5552
5553 /**
5554 * @hide
5555 */
5556 public void dispatchFinishTemporaryDetach() {
5557 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005558 }
Romain Guy8506ab42009-06-11 17:35:47 -07005559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005560 /**
5561 * Called after {@link #onStartTemporaryDetach} when the container is done
5562 * changing the view.
5563 */
5564 public void onFinishTemporaryDetach() {
5565 }
Romain Guy8506ab42009-06-11 17:35:47 -07005566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005567 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005568 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5569 * for this view's window. Returns null if the view is not currently attached
5570 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005571 * just use the standard high-level event callbacks like
5572 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005573 */
5574 public KeyEvent.DispatcherState getKeyDispatcherState() {
5575 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5576 }
Joe Malin32736f02011-01-19 16:14:20 -08005577
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005578 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005579 * Dispatch a key event before it is processed by any input method
5580 * associated with the view hierarchy. This can be used to intercept
5581 * key events in special situations before the IME consumes them; a
5582 * typical example would be handling the BACK key to update the application's
5583 * UI instead of allowing the IME to see it and close itself.
5584 *
5585 * @param event The key event to be dispatched.
5586 * @return True if the event was handled, false otherwise.
5587 */
5588 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5589 return onKeyPreIme(event.getKeyCode(), event);
5590 }
5591
5592 /**
5593 * Dispatch a key event to the next view on the focus path. This path runs
5594 * from the top of the view tree down to the currently focused view. If this
5595 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5596 * the next node down the focus path. This method also fires any key
5597 * listeners.
5598 *
5599 * @param event The key event to be dispatched.
5600 * @return True if the event was handled, false otherwise.
5601 */
5602 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005603 if (mInputEventConsistencyVerifier != null) {
5604 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5605 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005606
Jeff Brown21bc5c92011-02-28 18:27:14 -08005607 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005608 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005609 ListenerInfo li = mListenerInfo;
5610 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5611 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005612 return true;
5613 }
5614
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005615 if (event.dispatch(this, mAttachInfo != null
5616 ? mAttachInfo.mKeyDispatchState : null, this)) {
5617 return true;
5618 }
5619
5620 if (mInputEventConsistencyVerifier != null) {
5621 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5622 }
5623 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005624 }
5625
5626 /**
5627 * Dispatches a key shortcut event.
5628 *
5629 * @param event The key event to be dispatched.
5630 * @return True if the event was handled by the view, false otherwise.
5631 */
5632 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5633 return onKeyShortcut(event.getKeyCode(), event);
5634 }
5635
5636 /**
5637 * Pass the touch screen motion event down to the target view, or this
5638 * view if it is the target.
5639 *
5640 * @param event The motion event to be dispatched.
5641 * @return True if the event was handled by the view, false otherwise.
5642 */
5643 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005644 if (mInputEventConsistencyVerifier != null) {
5645 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5646 }
5647
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005648 if (onFilterTouchEventForSecurity(event)) {
5649 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005650 ListenerInfo li = mListenerInfo;
5651 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5652 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005653 return true;
5654 }
5655
5656 if (onTouchEvent(event)) {
5657 return true;
5658 }
Jeff Brown85a31762010-09-01 17:01:00 -07005659 }
5660
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005661 if (mInputEventConsistencyVerifier != null) {
5662 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005663 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005664 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005665 }
5666
5667 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005668 * Filter the touch event to apply security policies.
5669 *
5670 * @param event The motion event to be filtered.
5671 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005672 *
Jeff Brown85a31762010-09-01 17:01:00 -07005673 * @see #getFilterTouchesWhenObscured
5674 */
5675 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005676 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005677 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5678 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5679 // Window is obscured, drop this touch.
5680 return false;
5681 }
5682 return true;
5683 }
5684
5685 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005686 * Pass a trackball motion event down to the focused view.
5687 *
5688 * @param event The motion event to be dispatched.
5689 * @return True if the event was handled by the view, false otherwise.
5690 */
5691 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005692 if (mInputEventConsistencyVerifier != null) {
5693 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5694 }
5695
Romain Guy02ccac62011-06-24 13:20:23 -07005696 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005697 }
5698
5699 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005700 * Dispatch a generic motion event.
5701 * <p>
5702 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5703 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005704 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005705 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005706 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005707 *
5708 * @param event The motion event to be dispatched.
5709 * @return True if the event was handled by the view, false otherwise.
5710 */
5711 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005712 if (mInputEventConsistencyVerifier != null) {
5713 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5714 }
5715
Jeff Browna032cc02011-03-07 16:56:21 -08005716 final int source = event.getSource();
5717 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5718 final int action = event.getAction();
5719 if (action == MotionEvent.ACTION_HOVER_ENTER
5720 || action == MotionEvent.ACTION_HOVER_MOVE
5721 || action == MotionEvent.ACTION_HOVER_EXIT) {
5722 if (dispatchHoverEvent(event)) {
5723 return true;
5724 }
5725 } else if (dispatchGenericPointerEvent(event)) {
5726 return true;
5727 }
5728 } else if (dispatchGenericFocusedEvent(event)) {
5729 return true;
5730 }
5731
Jeff Brown10b62902011-06-20 16:40:37 -07005732 if (dispatchGenericMotionEventInternal(event)) {
5733 return true;
5734 }
5735
5736 if (mInputEventConsistencyVerifier != null) {
5737 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5738 }
5739 return false;
5740 }
5741
5742 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005743 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005744 ListenerInfo li = mListenerInfo;
5745 if (li != null && li.mOnGenericMotionListener != null
5746 && (mViewFlags & ENABLED_MASK) == ENABLED
5747 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005748 return true;
5749 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005750
5751 if (onGenericMotionEvent(event)) {
5752 return true;
5753 }
5754
5755 if (mInputEventConsistencyVerifier != null) {
5756 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5757 }
5758 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005759 }
5760
5761 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005762 * Dispatch a hover event.
5763 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005764 * Do not call this method directly.
5765 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005766 * </p>
5767 *
5768 * @param event The motion event to be dispatched.
5769 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005770 */
5771 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005772 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005773 ListenerInfo li = mListenerInfo;
5774 if (li != null && li.mOnHoverListener != null
5775 && (mViewFlags & ENABLED_MASK) == ENABLED
5776 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07005777 return true;
5778 }
5779
Jeff Browna032cc02011-03-07 16:56:21 -08005780 return onHoverEvent(event);
5781 }
5782
5783 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005784 * Returns true if the view has a child to which it has recently sent
5785 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5786 * it does not have a hovered child, then it must be the innermost hovered view.
5787 * @hide
5788 */
5789 protected boolean hasHoveredChild() {
5790 return false;
5791 }
5792
5793 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005794 * Dispatch a generic motion event to the view under the first pointer.
5795 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005796 * Do not call this method directly.
5797 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005798 * </p>
5799 *
5800 * @param event The motion event to be dispatched.
5801 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005802 */
5803 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5804 return false;
5805 }
5806
5807 /**
5808 * Dispatch a generic motion event to the currently focused view.
5809 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005810 * Do not call this method directly.
5811 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005812 * </p>
5813 *
5814 * @param event The motion event to be dispatched.
5815 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005816 */
5817 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5818 return false;
5819 }
5820
5821 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005822 * Dispatch a pointer event.
5823 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005824 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5825 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5826 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005827 * and should not be expected to handle other pointing device features.
5828 * </p>
5829 *
5830 * @param event The motion event to be dispatched.
5831 * @return True if the event was handled by the view, false otherwise.
5832 * @hide
5833 */
5834 public final boolean dispatchPointerEvent(MotionEvent event) {
5835 if (event.isTouchEvent()) {
5836 return dispatchTouchEvent(event);
5837 } else {
5838 return dispatchGenericMotionEvent(event);
5839 }
5840 }
5841
5842 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005843 * Called when the window containing this view gains or loses window focus.
5844 * ViewGroups should override to route to their children.
5845 *
5846 * @param hasFocus True if the window containing this view now has focus,
5847 * false otherwise.
5848 */
5849 public void dispatchWindowFocusChanged(boolean hasFocus) {
5850 onWindowFocusChanged(hasFocus);
5851 }
5852
5853 /**
5854 * Called when the window containing this view gains or loses focus. Note
5855 * that this is separate from view focus: to receive key events, both
5856 * your view and its window must have focus. If a window is displayed
5857 * on top of yours that takes input focus, then your own window will lose
5858 * focus but the view focus will remain unchanged.
5859 *
5860 * @param hasWindowFocus True if the window containing this view now has
5861 * focus, false otherwise.
5862 */
5863 public void onWindowFocusChanged(boolean hasWindowFocus) {
5864 InputMethodManager imm = InputMethodManager.peekInstance();
5865 if (!hasWindowFocus) {
5866 if (isPressed()) {
5867 setPressed(false);
5868 }
5869 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5870 imm.focusOut(this);
5871 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005872 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005873 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005874 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005875 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5876 imm.focusIn(this);
5877 }
5878 refreshDrawableState();
5879 }
5880
5881 /**
5882 * Returns true if this view is in a window that currently has window focus.
5883 * Note that this is not the same as the view itself having focus.
5884 *
5885 * @return True if this view is in a window that currently has window focus.
5886 */
5887 public boolean hasWindowFocus() {
5888 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5889 }
5890
5891 /**
Adam Powell326d8082009-12-09 15:10:07 -08005892 * Dispatch a view visibility change down the view hierarchy.
5893 * ViewGroups should override to route to their children.
5894 * @param changedView The view whose visibility changed. Could be 'this' or
5895 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005896 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5897 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005898 */
5899 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5900 onVisibilityChanged(changedView, visibility);
5901 }
5902
5903 /**
5904 * Called when the visibility of the view or an ancestor of the view is changed.
5905 * @param changedView The view whose visibility changed. Could be 'this' or
5906 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005907 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5908 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005909 */
5910 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005911 if (visibility == VISIBLE) {
5912 if (mAttachInfo != null) {
5913 initialAwakenScrollBars();
5914 } else {
5915 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5916 }
5917 }
Adam Powell326d8082009-12-09 15:10:07 -08005918 }
5919
5920 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005921 * Dispatch a hint about whether this view is displayed. For instance, when
5922 * a View moves out of the screen, it might receives a display hint indicating
5923 * the view is not displayed. Applications should not <em>rely</em> on this hint
5924 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005925 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005926 * @param hint A hint about whether or not this view is displayed:
5927 * {@link #VISIBLE} or {@link #INVISIBLE}.
5928 */
5929 public void dispatchDisplayHint(int hint) {
5930 onDisplayHint(hint);
5931 }
5932
5933 /**
5934 * Gives this view a hint about whether is displayed or not. For instance, when
5935 * a View moves out of the screen, it might receives a display hint indicating
5936 * the view is not displayed. Applications should not <em>rely</em> on this hint
5937 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005938 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005939 * @param hint A hint about whether or not this view is displayed:
5940 * {@link #VISIBLE} or {@link #INVISIBLE}.
5941 */
5942 protected void onDisplayHint(int hint) {
5943 }
5944
5945 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005946 * Dispatch a window visibility change down the view hierarchy.
5947 * ViewGroups should override to route to their children.
5948 *
5949 * @param visibility The new visibility of the window.
5950 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005951 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005952 */
5953 public void dispatchWindowVisibilityChanged(int visibility) {
5954 onWindowVisibilityChanged(visibility);
5955 }
5956
5957 /**
5958 * Called when the window containing has change its visibility
5959 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5960 * that this tells you whether or not your window is being made visible
5961 * to the window manager; this does <em>not</em> tell you whether or not
5962 * your window is obscured by other windows on the screen, even if it
5963 * is itself visible.
5964 *
5965 * @param visibility The new visibility of the window.
5966 */
5967 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005968 if (visibility == VISIBLE) {
5969 initialAwakenScrollBars();
5970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005971 }
5972
5973 /**
5974 * Returns the current visibility of the window this view is attached to
5975 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5976 *
5977 * @return Returns the current visibility of the view's window.
5978 */
5979 public int getWindowVisibility() {
5980 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5981 }
5982
5983 /**
5984 * Retrieve the overall visible display size in which the window this view is
5985 * attached to has been positioned in. This takes into account screen
5986 * decorations above the window, for both cases where the window itself
5987 * is being position inside of them or the window is being placed under
5988 * then and covered insets are used for the window to position its content
5989 * inside. In effect, this tells you the available area where content can
5990 * be placed and remain visible to users.
5991 *
5992 * <p>This function requires an IPC back to the window manager to retrieve
5993 * the requested information, so should not be used in performance critical
5994 * code like drawing.
5995 *
5996 * @param outRect Filled in with the visible display frame. If the view
5997 * is not attached to a window, this is simply the raw display size.
5998 */
5999 public void getWindowVisibleDisplayFrame(Rect outRect) {
6000 if (mAttachInfo != null) {
6001 try {
6002 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
6003 } catch (RemoteException e) {
6004 return;
6005 }
6006 // XXX This is really broken, and probably all needs to be done
6007 // in the window manager, and we need to know more about whether
6008 // we want the area behind or in front of the IME.
6009 final Rect insets = mAttachInfo.mVisibleInsets;
6010 outRect.left += insets.left;
6011 outRect.top += insets.top;
6012 outRect.right -= insets.right;
6013 outRect.bottom -= insets.bottom;
6014 return;
6015 }
6016 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006017 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006018 }
6019
6020 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006021 * Dispatch a notification about a resource configuration change down
6022 * the view hierarchy.
6023 * ViewGroups should override to route to their children.
6024 *
6025 * @param newConfig The new resource configuration.
6026 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006027 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006028 */
6029 public void dispatchConfigurationChanged(Configuration newConfig) {
6030 onConfigurationChanged(newConfig);
6031 }
6032
6033 /**
6034 * Called when the current configuration of the resources being used
6035 * by the application have changed. You can use this to decide when
6036 * to reload resources that can changed based on orientation and other
6037 * configuration characterstics. You only need to use this if you are
6038 * not relying on the normal {@link android.app.Activity} mechanism of
6039 * recreating the activity instance upon a configuration change.
6040 *
6041 * @param newConfig The new resource configuration.
6042 */
6043 protected void onConfigurationChanged(Configuration newConfig) {
6044 }
6045
6046 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006047 * Private function to aggregate all per-view attributes in to the view
6048 * root.
6049 */
6050 void dispatchCollectViewAttributes(int visibility) {
6051 performCollectViewAttributes(visibility);
6052 }
6053
6054 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08006055 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006056 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
6057 mAttachInfo.mKeepScreenOn = true;
6058 }
6059 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006060 ListenerInfo li = mListenerInfo;
6061 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006062 mAttachInfo.mHasSystemUiListeners = true;
6063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006064 }
6065 }
6066
6067 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006068 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006069 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006070 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6071 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006072 ai.mRecomputeGlobalAttributes = true;
6073 }
6074 }
6075 }
6076
6077 /**
6078 * Returns whether the device is currently in touch mode. Touch mode is entered
6079 * once the user begins interacting with the device by touch, and affects various
6080 * things like whether focus is always visible to the user.
6081 *
6082 * @return Whether the device is in touch mode.
6083 */
6084 @ViewDebug.ExportedProperty
6085 public boolean isInTouchMode() {
6086 if (mAttachInfo != null) {
6087 return mAttachInfo.mInTouchMode;
6088 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006089 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006090 }
6091 }
6092
6093 /**
6094 * Returns the context the view is running in, through which it can
6095 * access the current theme, resources, etc.
6096 *
6097 * @return The view's Context.
6098 */
6099 @ViewDebug.CapturedViewProperty
6100 public final Context getContext() {
6101 return mContext;
6102 }
6103
6104 /**
6105 * Handle a key event before it is processed by any input method
6106 * associated with the view hierarchy. This can be used to intercept
6107 * key events in special situations before the IME consumes them; a
6108 * typical example would be handling the BACK key to update the application's
6109 * UI instead of allowing the IME to see it and close itself.
6110 *
6111 * @param keyCode The value in event.getKeyCode().
6112 * @param event Description of the key event.
6113 * @return If you handled the event, return true. If you want to allow the
6114 * event to be handled by the next receiver, return false.
6115 */
6116 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
6117 return false;
6118 }
6119
6120 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006121 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
6122 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006123 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
6124 * is released, if the view is enabled and clickable.
6125 *
6126 * @param keyCode A key code that represents the button pressed, from
6127 * {@link android.view.KeyEvent}.
6128 * @param event The KeyEvent object that defines the button action.
6129 */
6130 public boolean onKeyDown(int keyCode, KeyEvent event) {
6131 boolean result = false;
6132
6133 switch (keyCode) {
6134 case KeyEvent.KEYCODE_DPAD_CENTER:
6135 case KeyEvent.KEYCODE_ENTER: {
6136 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6137 return true;
6138 }
6139 // Long clickable items don't necessarily have to be clickable
6140 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
6141 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
6142 (event.getRepeatCount() == 0)) {
6143 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006144 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006145 return true;
6146 }
6147 break;
6148 }
6149 }
6150 return result;
6151 }
6152
6153 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006154 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
6155 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
6156 * the event).
6157 */
6158 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
6159 return false;
6160 }
6161
6162 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006163 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
6164 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006165 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
6166 * {@link KeyEvent#KEYCODE_ENTER} is released.
6167 *
6168 * @param keyCode A key code that represents the button pressed, from
6169 * {@link android.view.KeyEvent}.
6170 * @param event The KeyEvent object that defines the button action.
6171 */
6172 public boolean onKeyUp(int keyCode, KeyEvent event) {
6173 boolean result = false;
6174
6175 switch (keyCode) {
6176 case KeyEvent.KEYCODE_DPAD_CENTER:
6177 case KeyEvent.KEYCODE_ENTER: {
6178 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6179 return true;
6180 }
6181 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
6182 setPressed(false);
6183
6184 if (!mHasPerformedLongPress) {
6185 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006186 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006187
6188 result = performClick();
6189 }
6190 }
6191 break;
6192 }
6193 }
6194 return result;
6195 }
6196
6197 /**
6198 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
6199 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
6200 * the event).
6201 *
6202 * @param keyCode A key code that represents the button pressed, from
6203 * {@link android.view.KeyEvent}.
6204 * @param repeatCount The number of times the action was made.
6205 * @param event The KeyEvent object that defines the button action.
6206 */
6207 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6208 return false;
6209 }
6210
6211 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006212 * Called on the focused view when a key shortcut event is not handled.
6213 * Override this method to implement local key shortcuts for the View.
6214 * Key shortcuts can also be implemented by setting the
6215 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006216 *
6217 * @param keyCode The value in event.getKeyCode().
6218 * @param event Description of the key event.
6219 * @return If you handled the event, return true. If you want to allow the
6220 * event to be handled by the next receiver, return false.
6221 */
6222 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6223 return false;
6224 }
6225
6226 /**
6227 * Check whether the called view is a text editor, in which case it
6228 * would make sense to automatically display a soft input window for
6229 * it. Subclasses should override this if they implement
6230 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006231 * a call on that method would return a non-null InputConnection, and
6232 * they are really a first-class editor that the user would normally
6233 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006234 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006235 * <p>The default implementation always returns false. This does
6236 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6237 * will not be called or the user can not otherwise perform edits on your
6238 * view; it is just a hint to the system that this is not the primary
6239 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006240 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006241 * @return Returns true if this view is a text editor, else false.
6242 */
6243 public boolean onCheckIsTextEditor() {
6244 return false;
6245 }
Romain Guy8506ab42009-06-11 17:35:47 -07006246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006247 /**
6248 * Create a new InputConnection for an InputMethod to interact
6249 * with the view. The default implementation returns null, since it doesn't
6250 * support input methods. You can override this to implement such support.
6251 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006252 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006253 * <p>When implementing this, you probably also want to implement
6254 * {@link #onCheckIsTextEditor()} to indicate you will return a
6255 * non-null InputConnection.
6256 *
6257 * @param outAttrs Fill in with attribute information about the connection.
6258 */
6259 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6260 return null;
6261 }
6262
6263 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006264 * Called by the {@link android.view.inputmethod.InputMethodManager}
6265 * when a view who is not the current
6266 * input connection target is trying to make a call on the manager. The
6267 * default implementation returns false; you can override this to return
6268 * true for certain views if you are performing InputConnection proxying
6269 * to them.
6270 * @param view The View that is making the InputMethodManager call.
6271 * @return Return true to allow the call, false to reject.
6272 */
6273 public boolean checkInputConnectionProxy(View view) {
6274 return false;
6275 }
Romain Guy8506ab42009-06-11 17:35:47 -07006276
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006277 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006278 * Show the context menu for this view. It is not safe to hold on to the
6279 * menu after returning from this method.
6280 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006281 * You should normally not overload this method. Overload
6282 * {@link #onCreateContextMenu(ContextMenu)} or define an
6283 * {@link OnCreateContextMenuListener} to add items to the context menu.
6284 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006285 * @param menu The context menu to populate
6286 */
6287 public void createContextMenu(ContextMenu menu) {
6288 ContextMenuInfo menuInfo = getContextMenuInfo();
6289
6290 // Sets the current menu info so all items added to menu will have
6291 // my extra info set.
6292 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6293
6294 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006295 ListenerInfo li = mListenerInfo;
6296 if (li != null && li.mOnCreateContextMenuListener != null) {
6297 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006298 }
6299
6300 // Clear the extra information so subsequent items that aren't mine don't
6301 // have my extra info.
6302 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6303
6304 if (mParent != null) {
6305 mParent.createContextMenu(menu);
6306 }
6307 }
6308
6309 /**
6310 * Views should implement this if they have extra information to associate
6311 * with the context menu. The return result is supplied as a parameter to
6312 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6313 * callback.
6314 *
6315 * @return Extra information about the item for which the context menu
6316 * should be shown. This information will vary across different
6317 * subclasses of View.
6318 */
6319 protected ContextMenuInfo getContextMenuInfo() {
6320 return null;
6321 }
6322
6323 /**
6324 * Views should implement this if the view itself is going to add items to
6325 * the context menu.
6326 *
6327 * @param menu the context menu to populate
6328 */
6329 protected void onCreateContextMenu(ContextMenu menu) {
6330 }
6331
6332 /**
6333 * Implement this method to handle trackball motion events. The
6334 * <em>relative</em> movement of the trackball since the last event
6335 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6336 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6337 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6338 * they will often be fractional values, representing the more fine-grained
6339 * movement information available from a trackball).
6340 *
6341 * @param event The motion event.
6342 * @return True if the event was handled, false otherwise.
6343 */
6344 public boolean onTrackballEvent(MotionEvent event) {
6345 return false;
6346 }
6347
6348 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006349 * Implement this method to handle generic motion events.
6350 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006351 * Generic motion events describe joystick movements, mouse hovers, track pad
6352 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006353 * {@link MotionEvent#getSource() source} of the motion event specifies
6354 * the class of input that was received. Implementations of this method
6355 * must examine the bits in the source before processing the event.
6356 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006357 * </p><p>
6358 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6359 * are delivered to the view under the pointer. All other generic motion events are
6360 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006361 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07006362 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006363 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006364 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6365 * // process the joystick movement...
6366 * return true;
6367 * }
6368 * }
6369 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6370 * switch (event.getAction()) {
6371 * case MotionEvent.ACTION_HOVER_MOVE:
6372 * // process the mouse hover movement...
6373 * return true;
6374 * case MotionEvent.ACTION_SCROLL:
6375 * // process the scroll wheel movement...
6376 * return true;
6377 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006378 * }
6379 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07006380 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08006381 *
6382 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006383 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006384 */
6385 public boolean onGenericMotionEvent(MotionEvent event) {
6386 return false;
6387 }
6388
6389 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006390 * Implement this method to handle hover events.
6391 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006392 * This method is called whenever a pointer is hovering into, over, or out of the
6393 * bounds of a view and the view is not currently being touched.
6394 * Hover events are represented as pointer events with action
6395 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6396 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6397 * </p>
6398 * <ul>
6399 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6400 * when the pointer enters the bounds of the view.</li>
6401 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6402 * when the pointer has already entered the bounds of the view and has moved.</li>
6403 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6404 * when the pointer has exited the bounds of the view or when the pointer is
6405 * about to go down due to a button click, tap, or similar user action that
6406 * causes the view to be touched.</li>
6407 * </ul>
6408 * <p>
6409 * The view should implement this method to return true to indicate that it is
6410 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006411 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006412 * The default implementation calls {@link #setHovered} to update the hovered state
6413 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006414 * is enabled and is clickable. The default implementation also sends hover
6415 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006416 * </p>
6417 *
6418 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006419 * @return True if the view handled the hover event.
6420 *
6421 * @see #isHovered
6422 * @see #setHovered
6423 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006424 */
6425 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006426 // The root view may receive hover (or touch) events that are outside the bounds of
6427 // the window. This code ensures that we only send accessibility events for
6428 // hovers that are actually within the bounds of the root view.
6429 final int action = event.getAction();
6430 if (!mSendingHoverAccessibilityEvents) {
6431 if ((action == MotionEvent.ACTION_HOVER_ENTER
6432 || action == MotionEvent.ACTION_HOVER_MOVE)
6433 && !hasHoveredChild()
6434 && pointInView(event.getX(), event.getY())) {
6435 mSendingHoverAccessibilityEvents = true;
6436 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6437 }
6438 } else {
6439 if (action == MotionEvent.ACTION_HOVER_EXIT
6440 || (action == MotionEvent.ACTION_HOVER_MOVE
6441 && !pointInView(event.getX(), event.getY()))) {
6442 mSendingHoverAccessibilityEvents = false;
6443 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6444 }
Jeff Browna1b24182011-07-28 13:38:24 -07006445 }
6446
Jeff Brown87b7f802011-06-21 18:35:45 -07006447 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006448 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006449 case MotionEvent.ACTION_HOVER_ENTER:
6450 setHovered(true);
6451 break;
6452 case MotionEvent.ACTION_HOVER_EXIT:
6453 setHovered(false);
6454 break;
6455 }
Jeff Browna1b24182011-07-28 13:38:24 -07006456
6457 // Dispatch the event to onGenericMotionEvent before returning true.
6458 // This is to provide compatibility with existing applications that
6459 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6460 // break because of the new default handling for hoverable views
6461 // in onHoverEvent.
6462 // Note that onGenericMotionEvent will be called by default when
6463 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6464 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006465 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006466 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006467 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006468 }
6469
6470 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006471 * Returns true if the view should handle {@link #onHoverEvent}
6472 * by calling {@link #setHovered} to change its hovered state.
6473 *
6474 * @return True if the view is hoverable.
6475 */
6476 private boolean isHoverable() {
6477 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006478 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006479 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6480 return false;
6481 }
6482
6483 return (viewFlags & CLICKABLE) == CLICKABLE
6484 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6485 }
6486
6487 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006488 * Returns true if the view is currently hovered.
6489 *
6490 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006491 *
6492 * @see #setHovered
6493 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006494 */
Jeff Brown10b62902011-06-20 16:40:37 -07006495 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006496 public boolean isHovered() {
6497 return (mPrivateFlags & HOVERED) != 0;
6498 }
6499
6500 /**
6501 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006502 * <p>
6503 * Calling this method also changes the drawable state of the view. This
6504 * enables the view to react to hover by using different drawable resources
6505 * to change its appearance.
6506 * </p><p>
6507 * The {@link #onHoverChanged} method is called when the hovered state changes.
6508 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006509 *
6510 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006511 *
6512 * @see #isHovered
6513 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006514 */
6515 public void setHovered(boolean hovered) {
6516 if (hovered) {
6517 if ((mPrivateFlags & HOVERED) == 0) {
6518 mPrivateFlags |= HOVERED;
6519 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006520 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006521 }
6522 } else {
6523 if ((mPrivateFlags & HOVERED) != 0) {
6524 mPrivateFlags &= ~HOVERED;
6525 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006526 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006527 }
6528 }
6529 }
6530
6531 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006532 * Implement this method to handle hover state changes.
6533 * <p>
6534 * This method is called whenever the hover state changes as a result of a
6535 * call to {@link #setHovered}.
6536 * </p>
6537 *
6538 * @param hovered The current hover state, as returned by {@link #isHovered}.
6539 *
6540 * @see #isHovered
6541 * @see #setHovered
6542 */
6543 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006544 }
6545
6546 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006547 * Implement this method to handle touch screen motion events.
6548 *
6549 * @param event The motion event.
6550 * @return True if the event was handled, false otherwise.
6551 */
6552 public boolean onTouchEvent(MotionEvent event) {
6553 final int viewFlags = mViewFlags;
6554
6555 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006556 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08006557 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006559 // A disabled view that is clickable still consumes the touch
6560 // events, it just doesn't respond to them.
6561 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6562 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6563 }
6564
6565 if (mTouchDelegate != null) {
6566 if (mTouchDelegate.onTouchEvent(event)) {
6567 return true;
6568 }
6569 }
6570
6571 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6572 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6573 switch (event.getAction()) {
6574 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006575 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6576 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006577 // take focus if we don't have it already and we should in
6578 // touch mode.
6579 boolean focusTaken = false;
6580 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6581 focusTaken = requestFocus();
6582 }
6583
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006584 if (prepressed) {
6585 // The button is being released before we actually
6586 // showed it as pressed. Make it show the pressed
6587 // state now (before scheduling the click) to ensure
6588 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08006589 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006590 }
Joe Malin32736f02011-01-19 16:14:20 -08006591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006592 if (!mHasPerformedLongPress) {
6593 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006594 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006595
6596 // Only perform take click actions if we were in the pressed state
6597 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006598 // Use a Runnable and post this rather than calling
6599 // performClick directly. This lets other visual state
6600 // of the view update before click actions start.
6601 if (mPerformClick == null) {
6602 mPerformClick = new PerformClick();
6603 }
6604 if (!post(mPerformClick)) {
6605 performClick();
6606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006607 }
6608 }
6609
6610 if (mUnsetPressedState == null) {
6611 mUnsetPressedState = new UnsetPressedState();
6612 }
6613
Adam Powelle14579b2009-12-16 18:39:52 -08006614 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006615 postDelayed(mUnsetPressedState,
6616 ViewConfiguration.getPressedStateDuration());
6617 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006618 // If the post failed, unpress right now
6619 mUnsetPressedState.run();
6620 }
Adam Powelle14579b2009-12-16 18:39:52 -08006621 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006622 }
6623 break;
6624
6625 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006626 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006627
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006628 if (performButtonActionOnTouchDown(event)) {
6629 break;
6630 }
6631
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006632 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006633 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006634
6635 // For views inside a scrolling container, delay the pressed feedback for
6636 // a short period in case this is a scroll.
6637 if (isInScrollingContainer) {
6638 mPrivateFlags |= PREPRESSED;
6639 if (mPendingCheckForTap == null) {
6640 mPendingCheckForTap = new CheckForTap();
6641 }
6642 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6643 } else {
6644 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08006645 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006646 checkForLongClick(0);
6647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006648 break;
6649
6650 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08006651 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08006652 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006653 break;
6654
6655 case MotionEvent.ACTION_MOVE:
6656 final int x = (int) event.getX();
6657 final int y = (int) event.getY();
6658
6659 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006660 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006661 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006662 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006663 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006664 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006665 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006666
Adam Powell4d6f0662012-02-21 15:11:11 -08006667 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006668 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006669 }
6670 break;
6671 }
6672 return true;
6673 }
6674
6675 return false;
6676 }
6677
6678 /**
Adam Powell10298662011-08-14 18:26:30 -07006679 * @hide
6680 */
6681 public boolean isInScrollingContainer() {
6682 ViewParent p = getParent();
6683 while (p != null && p instanceof ViewGroup) {
6684 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6685 return true;
6686 }
6687 p = p.getParent();
6688 }
6689 return false;
6690 }
6691
6692 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006693 * Remove the longpress detection timer.
6694 */
6695 private void removeLongPressCallback() {
6696 if (mPendingCheckForLongPress != null) {
6697 removeCallbacks(mPendingCheckForLongPress);
6698 }
6699 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006700
6701 /**
6702 * Remove the pending click action
6703 */
6704 private void removePerformClickCallback() {
6705 if (mPerformClick != null) {
6706 removeCallbacks(mPerformClick);
6707 }
6708 }
6709
Adam Powelle14579b2009-12-16 18:39:52 -08006710 /**
Romain Guya440b002010-02-24 15:57:54 -08006711 * Remove the prepress detection timer.
6712 */
6713 private void removeUnsetPressCallback() {
6714 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6715 setPressed(false);
6716 removeCallbacks(mUnsetPressedState);
6717 }
6718 }
6719
6720 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006721 * Remove the tap detection timer.
6722 */
6723 private void removeTapCallback() {
6724 if (mPendingCheckForTap != null) {
6725 mPrivateFlags &= ~PREPRESSED;
6726 removeCallbacks(mPendingCheckForTap);
6727 }
6728 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006729
6730 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006731 * Cancels a pending long press. Your subclass can use this if you
6732 * want the context menu to come up if the user presses and holds
6733 * at the same place, but you don't want it to come up if they press
6734 * and then move around enough to cause scrolling.
6735 */
6736 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006737 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006738
6739 /*
6740 * The prepressed state handled by the tap callback is a display
6741 * construct, but the tap callback will post a long press callback
6742 * less its own timeout. Remove it here.
6743 */
6744 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006745 }
6746
6747 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006748 * Remove the pending callback for sending a
6749 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6750 */
6751 private void removeSendViewScrolledAccessibilityEventCallback() {
6752 if (mSendViewScrolledAccessibilityEvent != null) {
6753 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6754 }
6755 }
6756
6757 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006758 * Sets the TouchDelegate for this View.
6759 */
6760 public void setTouchDelegate(TouchDelegate delegate) {
6761 mTouchDelegate = delegate;
6762 }
6763
6764 /**
6765 * Gets the TouchDelegate for this View.
6766 */
6767 public TouchDelegate getTouchDelegate() {
6768 return mTouchDelegate;
6769 }
6770
6771 /**
6772 * Set flags controlling behavior of this view.
6773 *
6774 * @param flags Constant indicating the value which should be set
6775 * @param mask Constant indicating the bit range that should be changed
6776 */
6777 void setFlags(int flags, int mask) {
6778 int old = mViewFlags;
6779 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6780
6781 int changed = mViewFlags ^ old;
6782 if (changed == 0) {
6783 return;
6784 }
6785 int privateFlags = mPrivateFlags;
6786
6787 /* Check if the FOCUSABLE bit has changed */
6788 if (((changed & FOCUSABLE_MASK) != 0) &&
6789 ((privateFlags & HAS_BOUNDS) !=0)) {
6790 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6791 && ((privateFlags & FOCUSED) != 0)) {
6792 /* Give up focus if we are no longer focusable */
6793 clearFocus();
6794 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6795 && ((privateFlags & FOCUSED) == 0)) {
6796 /*
6797 * Tell the view system that we are now available to take focus
6798 * if no one else already has it.
6799 */
6800 if (mParent != null) mParent.focusableViewAvailable(this);
6801 }
6802 }
6803
6804 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6805 if ((changed & VISIBILITY_MASK) != 0) {
6806 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006807 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006808 * it was not visible. Marking it drawn ensures that the invalidation will
6809 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006810 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006811 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006812 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006813
6814 needGlobalAttributesUpdate(true);
6815
6816 // a view becoming visible is worth notifying the parent
6817 // about in case nothing has focus. even if this specific view
6818 // isn't focusable, it may contain something that is, so let
6819 // the root view try to give this focus if nothing else does.
6820 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6821 mParent.focusableViewAvailable(this);
6822 }
6823 }
6824 }
6825
6826 /* Check if the GONE bit has changed */
6827 if ((changed & GONE) != 0) {
6828 needGlobalAttributesUpdate(false);
6829 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006830
Romain Guyecd80ee2009-12-03 17:13:02 -08006831 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6832 if (hasFocus()) clearFocus();
6833 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006834 if (mParent instanceof View) {
6835 // GONE views noop invalidation, so invalidate the parent
6836 ((View) mParent).invalidate(true);
6837 }
6838 // Mark the view drawn to ensure that it gets invalidated properly the next
6839 // time it is visible and gets invalidated
6840 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006841 }
6842 if (mAttachInfo != null) {
6843 mAttachInfo.mViewVisibilityChanged = true;
6844 }
6845 }
6846
6847 /* Check if the VISIBLE bit has changed */
6848 if ((changed & INVISIBLE) != 0) {
6849 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006850 /*
6851 * If this view is becoming invisible, set the DRAWN flag so that
6852 * the next invalidate() will not be skipped.
6853 */
6854 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006855
6856 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6857 // root view becoming invisible shouldn't clear focus
6858 if (getRootView() != this) {
6859 clearFocus();
6860 }
6861 }
6862 if (mAttachInfo != null) {
6863 mAttachInfo.mViewVisibilityChanged = true;
6864 }
6865 }
6866
Adam Powell326d8082009-12-09 15:10:07 -08006867 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006868 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08006869 ((ViewGroup) mParent).onChildVisibilityChanged(this,
6870 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08006871 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006872 } else if (mParent != null) {
6873 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006874 }
Adam Powell326d8082009-12-09 15:10:07 -08006875 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6876 }
6877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006878 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6879 destroyDrawingCache();
6880 }
6881
6882 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6883 destroyDrawingCache();
6884 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006885 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006886 }
6887
6888 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6889 destroyDrawingCache();
6890 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6891 }
6892
6893 if ((changed & DRAW_MASK) != 0) {
6894 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6895 if (mBGDrawable != null) {
6896 mPrivateFlags &= ~SKIP_DRAW;
6897 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6898 } else {
6899 mPrivateFlags |= SKIP_DRAW;
6900 }
6901 } else {
6902 mPrivateFlags &= ~SKIP_DRAW;
6903 }
6904 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006905 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006906 }
6907
6908 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006909 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006910 mParent.recomputeViewAttributes(this);
6911 }
6912 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006913
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006914 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006915 requestLayout();
6916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006917 }
6918
6919 /**
6920 * Change the view's z order in the tree, so it's on top of other sibling
6921 * views
6922 */
6923 public void bringToFront() {
6924 if (mParent != null) {
6925 mParent.bringChildToFront(this);
6926 }
6927 }
6928
6929 /**
6930 * This is called in response to an internal scroll in this view (i.e., the
6931 * view scrolled its own contents). This is typically as a result of
6932 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6933 * called.
6934 *
6935 * @param l Current horizontal scroll origin.
6936 * @param t Current vertical scroll origin.
6937 * @param oldl Previous horizontal scroll origin.
6938 * @param oldt Previous vertical scroll origin.
6939 */
6940 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006941 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6942 postSendViewScrolledAccessibilityEventCallback();
6943 }
6944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006945 mBackgroundSizeChanged = true;
6946
6947 final AttachInfo ai = mAttachInfo;
6948 if (ai != null) {
6949 ai.mViewScrollChanged = true;
6950 }
6951 }
6952
6953 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006954 * Interface definition for a callback to be invoked when the layout bounds of a view
6955 * changes due to layout processing.
6956 */
6957 public interface OnLayoutChangeListener {
6958 /**
6959 * Called when the focus state of a view has changed.
6960 *
6961 * @param v The view whose state has changed.
6962 * @param left The new value of the view's left property.
6963 * @param top The new value of the view's top property.
6964 * @param right The new value of the view's right property.
6965 * @param bottom The new value of the view's bottom property.
6966 * @param oldLeft The previous value of the view's left property.
6967 * @param oldTop The previous value of the view's top property.
6968 * @param oldRight The previous value of the view's right property.
6969 * @param oldBottom The previous value of the view's bottom property.
6970 */
6971 void onLayoutChange(View v, int left, int top, int right, int bottom,
6972 int oldLeft, int oldTop, int oldRight, int oldBottom);
6973 }
6974
6975 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006976 * This is called during layout when the size of this view has changed. If
6977 * you were just added to the view hierarchy, you're called with the old
6978 * values of 0.
6979 *
6980 * @param w Current width of this view.
6981 * @param h Current height of this view.
6982 * @param oldw Old width of this view.
6983 * @param oldh Old height of this view.
6984 */
6985 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6986 }
6987
6988 /**
6989 * Called by draw to draw the child views. This may be overridden
6990 * by derived classes to gain control just before its children are drawn
6991 * (but after its own view has been drawn).
6992 * @param canvas the canvas on which to draw the view
6993 */
6994 protected void dispatchDraw(Canvas canvas) {
6995 }
6996
6997 /**
6998 * Gets the parent of this view. Note that the parent is a
6999 * ViewParent and not necessarily a View.
7000 *
7001 * @return Parent of this view.
7002 */
7003 public final ViewParent getParent() {
7004 return mParent;
7005 }
7006
7007 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007008 * Set the horizontal scrolled position of your view. This will cause a call to
7009 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7010 * invalidated.
7011 * @param value the x position to scroll to
7012 */
7013 public void setScrollX(int value) {
7014 scrollTo(value, mScrollY);
7015 }
7016
7017 /**
7018 * Set the vertical scrolled position of your view. This will cause a call to
7019 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7020 * invalidated.
7021 * @param value the y position to scroll to
7022 */
7023 public void setScrollY(int value) {
7024 scrollTo(mScrollX, value);
7025 }
7026
7027 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007028 * Return the scrolled left position of this view. This is the left edge of
7029 * the displayed part of your view. You do not need to draw any pixels
7030 * farther left, since those are outside of the frame of your view on
7031 * screen.
7032 *
7033 * @return The left edge of the displayed part of your view, in pixels.
7034 */
7035 public final int getScrollX() {
7036 return mScrollX;
7037 }
7038
7039 /**
7040 * Return the scrolled top position of this view. This is the top edge of
7041 * the displayed part of your view. You do not need to draw any pixels above
7042 * it, since those are outside of the frame of your view on screen.
7043 *
7044 * @return The top edge of the displayed part of your view, in pixels.
7045 */
7046 public final int getScrollY() {
7047 return mScrollY;
7048 }
7049
7050 /**
7051 * Return the width of the your view.
7052 *
7053 * @return The width of your view, in pixels.
7054 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007055 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007056 public final int getWidth() {
7057 return mRight - mLeft;
7058 }
7059
7060 /**
7061 * Return the height of your view.
7062 *
7063 * @return The height of your view, in pixels.
7064 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007065 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007066 public final int getHeight() {
7067 return mBottom - mTop;
7068 }
7069
7070 /**
7071 * Return the visible drawing bounds of your view. Fills in the output
7072 * rectangle with the values from getScrollX(), getScrollY(),
7073 * getWidth(), and getHeight().
7074 *
7075 * @param outRect The (scrolled) drawing bounds of the view.
7076 */
7077 public void getDrawingRect(Rect outRect) {
7078 outRect.left = mScrollX;
7079 outRect.top = mScrollY;
7080 outRect.right = mScrollX + (mRight - mLeft);
7081 outRect.bottom = mScrollY + (mBottom - mTop);
7082 }
7083
7084 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007085 * Like {@link #getMeasuredWidthAndState()}, but only returns the
7086 * raw width component (that is the result is masked by
7087 * {@link #MEASURED_SIZE_MASK}).
7088 *
7089 * @return The raw measured width of this view.
7090 */
7091 public final int getMeasuredWidth() {
7092 return mMeasuredWidth & MEASURED_SIZE_MASK;
7093 }
7094
7095 /**
7096 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007097 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007098 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007099 * This should be used during measurement and layout calculations only. Use
7100 * {@link #getWidth()} to see how wide a view is after layout.
7101 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007102 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007103 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08007104 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007105 return mMeasuredWidth;
7106 }
7107
7108 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007109 * Like {@link #getMeasuredHeightAndState()}, but only returns the
7110 * raw width component (that is the result is masked by
7111 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007112 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007113 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007114 */
7115 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08007116 return mMeasuredHeight & MEASURED_SIZE_MASK;
7117 }
7118
7119 /**
7120 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007121 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007122 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
7123 * This should be used during measurement and layout calculations only. Use
7124 * {@link #getHeight()} to see how wide a view is after layout.
7125 *
7126 * @return The measured width of this view as a bit mask.
7127 */
7128 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007129 return mMeasuredHeight;
7130 }
7131
7132 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007133 * Return only the state bits of {@link #getMeasuredWidthAndState()}
7134 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
7135 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
7136 * and the height component is at the shifted bits
7137 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
7138 */
7139 public final int getMeasuredState() {
7140 return (mMeasuredWidth&MEASURED_STATE_MASK)
7141 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
7142 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
7143 }
7144
7145 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007146 * The transform matrix of this view, which is calculated based on the current
7147 * roation, scale, and pivot properties.
7148 *
7149 * @see #getRotation()
7150 * @see #getScaleX()
7151 * @see #getScaleY()
7152 * @see #getPivotX()
7153 * @see #getPivotY()
7154 * @return The current transform matrix for the view
7155 */
7156 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007157 if (mTransformationInfo != null) {
7158 updateMatrix();
7159 return mTransformationInfo.mMatrix;
7160 }
7161 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07007162 }
7163
7164 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007165 * Utility function to determine if the value is far enough away from zero to be
7166 * considered non-zero.
7167 * @param value A floating point value to check for zero-ness
7168 * @return whether the passed-in value is far enough away from zero to be considered non-zero
7169 */
7170 private static boolean nonzero(float value) {
7171 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
7172 }
7173
7174 /**
Jeff Brown86671742010-09-30 20:00:15 -07007175 * Returns true if the transform matrix is the identity matrix.
7176 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08007177 *
Romain Guy33e72ae2010-07-17 12:40:29 -07007178 * @return True if the transform matrix is the identity matrix, false otherwise.
7179 */
Jeff Brown86671742010-09-30 20:00:15 -07007180 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007181 if (mTransformationInfo != null) {
7182 updateMatrix();
7183 return mTransformationInfo.mMatrixIsIdentity;
7184 }
7185 return true;
7186 }
7187
7188 void ensureTransformationInfo() {
7189 if (mTransformationInfo == null) {
7190 mTransformationInfo = new TransformationInfo();
7191 }
Jeff Brown86671742010-09-30 20:00:15 -07007192 }
7193
7194 /**
7195 * Recomputes the transform matrix if necessary.
7196 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007197 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007198 final TransformationInfo info = mTransformationInfo;
7199 if (info == null) {
7200 return;
7201 }
7202 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007203 // transform-related properties have changed since the last time someone
7204 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007205
7206 // Figure out if we need to update the pivot point
7207 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007208 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7209 info.mPrevWidth = mRight - mLeft;
7210 info.mPrevHeight = mBottom - mTop;
7211 info.mPivotX = info.mPrevWidth / 2f;
7212 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007213 }
7214 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007215 info.mMatrix.reset();
7216 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7217 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7218 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7219 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007220 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007221 if (info.mCamera == null) {
7222 info.mCamera = new Camera();
7223 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007224 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007225 info.mCamera.save();
7226 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7227 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7228 info.mCamera.getMatrix(info.matrix3D);
7229 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7230 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7231 info.mPivotY + info.mTranslationY);
7232 info.mMatrix.postConcat(info.matrix3D);
7233 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007234 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007235 info.mMatrixDirty = false;
7236 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7237 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007238 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007239 }
7240
7241 /**
7242 * Utility method to retrieve the inverse of the current mMatrix property.
7243 * We cache the matrix to avoid recalculating it when transform properties
7244 * have not changed.
7245 *
7246 * @return The inverse of the current matrix of this view.
7247 */
Jeff Brown86671742010-09-30 20:00:15 -07007248 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007249 final TransformationInfo info = mTransformationInfo;
7250 if (info != null) {
7251 updateMatrix();
7252 if (info.mInverseMatrixDirty) {
7253 if (info.mInverseMatrix == null) {
7254 info.mInverseMatrix = new Matrix();
7255 }
7256 info.mMatrix.invert(info.mInverseMatrix);
7257 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007258 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007259 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007260 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007261 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007262 }
7263
7264 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007265 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7266 * views are drawn) from the camera to this view. The camera's distance
7267 * affects 3D transformations, for instance rotations around the X and Y
7268 * axis. If the rotationX or rotationY properties are changed and this view is
7269 * large (more than half the size of the screen), it is recommended to always
7270 * use a camera distance that's greater than the height (X axis rotation) or
7271 * the width (Y axis rotation) of this view.</p>
7272 *
7273 * <p>The distance of the camera from the view plane can have an affect on the
7274 * perspective distortion of the view when it is rotated around the x or y axis.
7275 * For example, a large distance will result in a large viewing angle, and there
7276 * will not be much perspective distortion of the view as it rotates. A short
7277 * distance may cause much more perspective distortion upon rotation, and can
7278 * also result in some drawing artifacts if the rotated view ends up partially
7279 * behind the camera (which is why the recommendation is to use a distance at
7280 * least as far as the size of the view, if the view is to be rotated.)</p>
7281 *
7282 * <p>The distance is expressed in "depth pixels." The default distance depends
7283 * on the screen density. For instance, on a medium density display, the
7284 * default distance is 1280. On a high density display, the default distance
7285 * is 1920.</p>
7286 *
7287 * <p>If you want to specify a distance that leads to visually consistent
7288 * results across various densities, use the following formula:</p>
7289 * <pre>
7290 * float scale = context.getResources().getDisplayMetrics().density;
7291 * view.setCameraDistance(distance * scale);
7292 * </pre>
7293 *
7294 * <p>The density scale factor of a high density display is 1.5,
7295 * and 1920 = 1280 * 1.5.</p>
7296 *
7297 * @param distance The distance in "depth pixels", if negative the opposite
7298 * value is used
7299 *
7300 * @see #setRotationX(float)
7301 * @see #setRotationY(float)
7302 */
7303 public void setCameraDistance(float distance) {
7304 invalidateParentCaches();
7305 invalidate(false);
7306
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007307 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007308 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007309 final TransformationInfo info = mTransformationInfo;
7310 if (info.mCamera == null) {
7311 info.mCamera = new Camera();
7312 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007313 }
7314
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007315 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7316 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007317
7318 invalidate(false);
7319 }
7320
7321 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007322 * The degrees that the view is rotated around the pivot point.
7323 *
Romain Guya5364ee2011-02-24 14:46:04 -08007324 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007325 * @see #getPivotX()
7326 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007327 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007328 * @return The degrees of rotation.
7329 */
Chet Haasea5531132012-02-02 13:41:44 -08007330 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007331 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007332 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007333 }
7334
7335 /**
Chet Haase897247b2010-09-09 14:54:47 -07007336 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7337 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007338 *
7339 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007340 *
7341 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007342 * @see #getPivotX()
7343 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007344 * @see #setRotationX(float)
7345 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007346 *
7347 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007348 */
7349 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007350 ensureTransformationInfo();
7351 final TransformationInfo info = mTransformationInfo;
7352 if (info.mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007353 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007354 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007355 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007356 info.mRotation = rotation;
7357 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007358 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007359 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007360 }
7361 }
7362
7363 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007364 * The degrees that the view is rotated around the vertical axis through the pivot point.
7365 *
7366 * @see #getPivotX()
7367 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007368 * @see #setRotationY(float)
7369 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007370 * @return The degrees of Y rotation.
7371 */
Chet Haasea5531132012-02-02 13:41:44 -08007372 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007373 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007374 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007375 }
7376
7377 /**
Chet Haase897247b2010-09-09 14:54:47 -07007378 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7379 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7380 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007381 *
7382 * When rotating large views, it is recommended to adjust the camera distance
7383 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007384 *
7385 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007386 *
7387 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007388 * @see #getPivotX()
7389 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007390 * @see #setRotation(float)
7391 * @see #setRotationX(float)
7392 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007393 *
7394 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007395 */
7396 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007397 ensureTransformationInfo();
7398 final TransformationInfo info = mTransformationInfo;
7399 if (info.mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007400 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007401 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007402 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007403 info.mRotationY = rotationY;
7404 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007405 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007406 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007407 }
7408 }
7409
7410 /**
7411 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7412 *
7413 * @see #getPivotX()
7414 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007415 * @see #setRotationX(float)
7416 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007417 * @return The degrees of X rotation.
7418 */
Chet Haasea5531132012-02-02 13:41:44 -08007419 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007420 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007421 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007422 }
7423
7424 /**
Chet Haase897247b2010-09-09 14:54:47 -07007425 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7426 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7427 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007428 *
7429 * When rotating large views, it is recommended to adjust the camera distance
7430 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007431 *
7432 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007433 *
7434 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007435 * @see #getPivotX()
7436 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007437 * @see #setRotation(float)
7438 * @see #setRotationY(float)
7439 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007440 *
7441 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007442 */
7443 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007444 ensureTransformationInfo();
7445 final TransformationInfo info = mTransformationInfo;
7446 if (info.mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007447 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007448 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007449 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007450 info.mRotationX = rotationX;
7451 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007452 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007453 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007454 }
7455 }
7456
7457 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007458 * The amount that the view is scaled in x around the pivot point, as a proportion of
7459 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7460 *
Joe Onorato93162322010-09-16 15:42:01 -04007461 * <p>By default, this is 1.0f.
7462 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007463 * @see #getPivotX()
7464 * @see #getPivotY()
7465 * @return The scaling factor.
7466 */
Chet Haasea5531132012-02-02 13:41:44 -08007467 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007468 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007469 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007470 }
7471
7472 /**
7473 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7474 * the view's unscaled width. A value of 1 means that no scaling is applied.
7475 *
7476 * @param scaleX The scaling factor.
7477 * @see #getPivotX()
7478 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007479 *
7480 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007481 */
7482 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007483 ensureTransformationInfo();
7484 final TransformationInfo info = mTransformationInfo;
7485 if (info.mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007486 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007487 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007488 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007489 info.mScaleX = scaleX;
7490 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007491 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007492 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007493 }
7494 }
7495
7496 /**
7497 * The amount that the view is scaled in y around the pivot point, as a proportion of
7498 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7499 *
Joe Onorato93162322010-09-16 15:42:01 -04007500 * <p>By default, this is 1.0f.
7501 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007502 * @see #getPivotX()
7503 * @see #getPivotY()
7504 * @return The scaling factor.
7505 */
Chet Haasea5531132012-02-02 13:41:44 -08007506 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007507 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007508 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007509 }
7510
7511 /**
7512 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7513 * the view's unscaled width. A value of 1 means that no scaling is applied.
7514 *
7515 * @param scaleY The scaling factor.
7516 * @see #getPivotX()
7517 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007518 *
7519 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007520 */
7521 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007522 ensureTransformationInfo();
7523 final TransformationInfo info = mTransformationInfo;
7524 if (info.mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007525 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007526 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007527 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007528 info.mScaleY = scaleY;
7529 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007530 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007531 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007532 }
7533 }
7534
7535 /**
7536 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7537 * and {@link #setScaleX(float) scaled}.
7538 *
7539 * @see #getRotation()
7540 * @see #getScaleX()
7541 * @see #getScaleY()
7542 * @see #getPivotY()
7543 * @return The x location of the pivot point.
7544 */
Chet Haasea5531132012-02-02 13:41:44 -08007545 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007546 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007547 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007548 }
7549
7550 /**
7551 * Sets the x location of the point around which the view is
7552 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007553 * By default, the pivot point is centered on the object.
7554 * Setting this property disables this behavior and causes the view to use only the
7555 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007556 *
7557 * @param pivotX The x location of the pivot point.
7558 * @see #getRotation()
7559 * @see #getScaleX()
7560 * @see #getScaleY()
7561 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007562 *
7563 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007564 */
7565 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007566 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007567 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007568 final TransformationInfo info = mTransformationInfo;
7569 if (info.mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007570 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007571 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007572 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007573 info.mPivotX = pivotX;
7574 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007575 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007576 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007577 }
7578 }
7579
7580 /**
7581 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7582 * and {@link #setScaleY(float) scaled}.
7583 *
7584 * @see #getRotation()
7585 * @see #getScaleX()
7586 * @see #getScaleY()
7587 * @see #getPivotY()
7588 * @return The y location of the pivot point.
7589 */
Chet Haasea5531132012-02-02 13:41:44 -08007590 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007591 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007592 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007593 }
7594
7595 /**
7596 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007597 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7598 * Setting this property disables this behavior and causes the view to use only the
7599 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007600 *
7601 * @param pivotY The y location of the pivot point.
7602 * @see #getRotation()
7603 * @see #getScaleX()
7604 * @see #getScaleY()
7605 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007606 *
7607 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007608 */
7609 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007610 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007611 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007612 final TransformationInfo info = mTransformationInfo;
7613 if (info.mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007614 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007615 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007616 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007617 info.mPivotY = pivotY;
7618 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007619 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007620 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007621 }
7622 }
7623
7624 /**
7625 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7626 * completely transparent and 1 means the view is completely opaque.
7627 *
Joe Onorato93162322010-09-16 15:42:01 -04007628 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007629 * @return The opacity of the view.
7630 */
Chet Haasea5531132012-02-02 13:41:44 -08007631 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007632 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007633 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007634 }
7635
7636 /**
Romain Guy171c5922011-01-06 10:04:23 -08007637 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7638 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007639 *
Romain Guy171c5922011-01-06 10:04:23 -08007640 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7641 * responsible for applying the opacity itself. Otherwise, calling this method is
7642 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007643 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007644 *
Chet Haasea5531132012-02-02 13:41:44 -08007645 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
7646 * performance implications. It is generally best to use the alpha property sparingly and
7647 * transiently, as in the case of fading animations.</p>
7648 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007649 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007650 *
Joe Malin32736f02011-01-19 16:14:20 -08007651 * @see #setLayerType(int, android.graphics.Paint)
7652 *
Chet Haase73066682010-11-29 15:55:32 -08007653 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007654 */
7655 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007656 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007657 if (mTransformationInfo.mAlpha != alpha) {
7658 mTransformationInfo.mAlpha = alpha;
7659 invalidateParentCaches();
7660 if (onSetAlpha((int) (alpha * 255))) {
7661 mPrivateFlags |= ALPHA_SET;
7662 // subclass is handling alpha - don't optimize rendering cache invalidation
7663 invalidate(true);
7664 } else {
7665 mPrivateFlags &= ~ALPHA_SET;
7666 invalidate(false);
7667 }
Chet Haaseed032702010-10-01 14:05:54 -07007668 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007669 }
7670
7671 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007672 * Faster version of setAlpha() which performs the same steps except there are
7673 * no calls to invalidate(). The caller of this function should perform proper invalidation
7674 * on the parent and this object. The return value indicates whether the subclass handles
7675 * alpha (the return value for onSetAlpha()).
7676 *
7677 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007678 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
7679 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08007680 */
7681 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007682 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007683 if (mTransformationInfo.mAlpha != alpha) {
7684 mTransformationInfo.mAlpha = alpha;
7685 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7686 if (subclassHandlesAlpha) {
7687 mPrivateFlags |= ALPHA_SET;
7688 return true;
7689 } else {
7690 mPrivateFlags &= ~ALPHA_SET;
7691 }
Chet Haasea00f3862011-02-22 06:34:40 -08007692 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007693 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08007694 }
7695
7696 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007697 * Top position of this view relative to its parent.
7698 *
7699 * @return The top of this view, in pixels.
7700 */
7701 @ViewDebug.CapturedViewProperty
7702 public final int getTop() {
7703 return mTop;
7704 }
7705
7706 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007707 * Sets the top position of this view relative to its parent. This method is meant to be called
7708 * by the layout system and should not generally be called otherwise, because the property
7709 * may be changed at any time by the layout.
7710 *
7711 * @param top The top of this view, in pixels.
7712 */
7713 public final void setTop(int top) {
7714 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007715 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007716 final boolean matrixIsIdentity = mTransformationInfo == null
7717 || mTransformationInfo.mMatrixIsIdentity;
7718 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007719 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007720 int minTop;
7721 int yLoc;
7722 if (top < mTop) {
7723 minTop = top;
7724 yLoc = top - mTop;
7725 } else {
7726 minTop = mTop;
7727 yLoc = 0;
7728 }
Chet Haasee9140a72011-02-16 16:23:29 -08007729 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007730 }
7731 } else {
7732 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007733 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007734 }
7735
Chet Haaseed032702010-10-01 14:05:54 -07007736 int width = mRight - mLeft;
7737 int oldHeight = mBottom - mTop;
7738
Chet Haase21cd1382010-09-01 17:42:29 -07007739 mTop = top;
7740
Chet Haaseed032702010-10-01 14:05:54 -07007741 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7742
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007743 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007744 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7745 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007746 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007747 }
Chet Haase21cd1382010-09-01 17:42:29 -07007748 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007749 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007750 }
Chet Haase55dbb652010-12-21 20:15:08 -08007751 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007752 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007753 }
7754 }
7755
7756 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007757 * Bottom position of this view relative to its parent.
7758 *
7759 * @return The bottom of this view, in pixels.
7760 */
7761 @ViewDebug.CapturedViewProperty
7762 public final int getBottom() {
7763 return mBottom;
7764 }
7765
7766 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007767 * True if this view has changed since the last time being drawn.
7768 *
7769 * @return The dirty state of this view.
7770 */
7771 public boolean isDirty() {
7772 return (mPrivateFlags & DIRTY_MASK) != 0;
7773 }
7774
7775 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007776 * Sets the bottom position of this view relative to its parent. This method is meant to be
7777 * called by the layout system and should not generally be called otherwise, because the
7778 * property may be changed at any time by the layout.
7779 *
7780 * @param bottom The bottom of this view, in pixels.
7781 */
7782 public final void setBottom(int bottom) {
7783 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007784 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007785 final boolean matrixIsIdentity = mTransformationInfo == null
7786 || mTransformationInfo.mMatrixIsIdentity;
7787 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007788 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007789 int maxBottom;
7790 if (bottom < mBottom) {
7791 maxBottom = mBottom;
7792 } else {
7793 maxBottom = bottom;
7794 }
Chet Haasee9140a72011-02-16 16:23:29 -08007795 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007796 }
7797 } else {
7798 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007799 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007800 }
7801
Chet Haaseed032702010-10-01 14:05:54 -07007802 int width = mRight - mLeft;
7803 int oldHeight = mBottom - mTop;
7804
Chet Haase21cd1382010-09-01 17:42:29 -07007805 mBottom = bottom;
7806
Chet Haaseed032702010-10-01 14:05:54 -07007807 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7808
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007809 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007810 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7811 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007812 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007813 }
Chet Haase21cd1382010-09-01 17:42:29 -07007814 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007815 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007816 }
Chet Haase55dbb652010-12-21 20:15:08 -08007817 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007818 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007819 }
7820 }
7821
7822 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007823 * Left position of this view relative to its parent.
7824 *
7825 * @return The left edge of this view, in pixels.
7826 */
7827 @ViewDebug.CapturedViewProperty
7828 public final int getLeft() {
7829 return mLeft;
7830 }
7831
7832 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007833 * Sets the left position of this view relative to its parent. This method is meant to be called
7834 * by the layout system and should not generally be called otherwise, because the property
7835 * may be changed at any time by the layout.
7836 *
7837 * @param left The bottom of this view, in pixels.
7838 */
7839 public final void setLeft(int left) {
7840 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007841 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007842 final boolean matrixIsIdentity = mTransformationInfo == null
7843 || mTransformationInfo.mMatrixIsIdentity;
7844 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007845 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007846 int minLeft;
7847 int xLoc;
7848 if (left < mLeft) {
7849 minLeft = left;
7850 xLoc = left - mLeft;
7851 } else {
7852 minLeft = mLeft;
7853 xLoc = 0;
7854 }
Chet Haasee9140a72011-02-16 16:23:29 -08007855 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007856 }
7857 } else {
7858 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007859 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007860 }
7861
Chet Haaseed032702010-10-01 14:05:54 -07007862 int oldWidth = mRight - mLeft;
7863 int height = mBottom - mTop;
7864
Chet Haase21cd1382010-09-01 17:42:29 -07007865 mLeft = left;
7866
Chet Haaseed032702010-10-01 14:05:54 -07007867 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7868
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007869 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007870 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7871 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007872 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007873 }
Chet Haase21cd1382010-09-01 17:42:29 -07007874 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007875 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007876 }
Chet Haase55dbb652010-12-21 20:15:08 -08007877 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007878 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007879 }
7880 }
7881
7882 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007883 * Right position of this view relative to its parent.
7884 *
7885 * @return The right edge of this view, in pixels.
7886 */
7887 @ViewDebug.CapturedViewProperty
7888 public final int getRight() {
7889 return mRight;
7890 }
7891
7892 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007893 * Sets the right position of this view relative to its parent. This method is meant to be called
7894 * by the layout system and should not generally be called otherwise, because the property
7895 * may be changed at any time by the layout.
7896 *
7897 * @param right The bottom of this view, in pixels.
7898 */
7899 public final void setRight(int right) {
7900 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007901 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007902 final boolean matrixIsIdentity = mTransformationInfo == null
7903 || mTransformationInfo.mMatrixIsIdentity;
7904 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007905 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007906 int maxRight;
7907 if (right < mRight) {
7908 maxRight = mRight;
7909 } else {
7910 maxRight = right;
7911 }
Chet Haasee9140a72011-02-16 16:23:29 -08007912 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007913 }
7914 } else {
7915 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007916 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007917 }
7918
Chet Haaseed032702010-10-01 14:05:54 -07007919 int oldWidth = mRight - mLeft;
7920 int height = mBottom - mTop;
7921
Chet Haase21cd1382010-09-01 17:42:29 -07007922 mRight = right;
7923
Chet Haaseed032702010-10-01 14:05:54 -07007924 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7925
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007926 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007927 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7928 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007929 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007930 }
Chet Haase21cd1382010-09-01 17:42:29 -07007931 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007932 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007933 }
Chet Haase55dbb652010-12-21 20:15:08 -08007934 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007935 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007936 }
7937 }
7938
7939 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007940 * The visual x position of this view, in pixels. This is equivalent to the
7941 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007942 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007943 *
Chet Haasedf030d22010-07-30 17:22:38 -07007944 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007945 */
Chet Haasea5531132012-02-02 13:41:44 -08007946 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007947 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007948 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007949 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007950
Chet Haasedf030d22010-07-30 17:22:38 -07007951 /**
7952 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7953 * {@link #setTranslationX(float) translationX} property to be the difference between
7954 * the x value passed in and the current {@link #getLeft() left} property.
7955 *
7956 * @param x The visual x position of this view, in pixels.
7957 */
7958 public void setX(float x) {
7959 setTranslationX(x - mLeft);
7960 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007961
Chet Haasedf030d22010-07-30 17:22:38 -07007962 /**
7963 * The visual y position of this view, in pixels. This is equivalent to the
7964 * {@link #setTranslationY(float) translationY} property plus the current
7965 * {@link #getTop() top} property.
7966 *
7967 * @return The visual y position of this view, in pixels.
7968 */
Chet Haasea5531132012-02-02 13:41:44 -08007969 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007970 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007971 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007972 }
7973
7974 /**
7975 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7976 * {@link #setTranslationY(float) translationY} property to be the difference between
7977 * the y value passed in and the current {@link #getTop() top} property.
7978 *
7979 * @param y The visual y position of this view, in pixels.
7980 */
7981 public void setY(float y) {
7982 setTranslationY(y - mTop);
7983 }
7984
7985
7986 /**
7987 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7988 * This position is post-layout, in addition to wherever the object's
7989 * layout placed it.
7990 *
7991 * @return The horizontal position of this view relative to its left position, in pixels.
7992 */
Chet Haasea5531132012-02-02 13:41:44 -08007993 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007994 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007995 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07007996 }
7997
7998 /**
7999 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
8000 * This effectively positions the object post-layout, in addition to wherever the object's
8001 * layout placed it.
8002 *
8003 * @param translationX The horizontal position of this view relative to its left position,
8004 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008005 *
8006 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07008007 */
8008 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008009 ensureTransformationInfo();
8010 final TransformationInfo info = mTransformationInfo;
8011 if (info.mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008012 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07008013 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07008014 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008015 info.mTranslationX = translationX;
8016 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07008017 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008018 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008019 }
8020 }
8021
8022 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008023 * The horizontal location of this view relative to its {@link #getTop() top} position.
8024 * This position is post-layout, in addition to wherever the object's
8025 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008026 *
Chet Haasedf030d22010-07-30 17:22:38 -07008027 * @return The vertical position of this view relative to its top position,
8028 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008029 */
Chet Haasea5531132012-02-02 13:41:44 -08008030 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008031 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008032 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008033 }
8034
8035 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008036 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
8037 * This effectively positions the object post-layout, in addition to wherever the object's
8038 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008039 *
Chet Haasedf030d22010-07-30 17:22:38 -07008040 * @param translationY The vertical position of this view relative to its top position,
8041 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008042 *
8043 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07008044 */
Chet Haasedf030d22010-07-30 17:22:38 -07008045 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008046 ensureTransformationInfo();
8047 final TransformationInfo info = mTransformationInfo;
8048 if (info.mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008049 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07008050 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07008051 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008052 info.mTranslationY = translationY;
8053 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07008054 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008055 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07008056 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008057 }
8058
8059 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008060 * Hit rectangle in parent's coordinates
8061 *
8062 * @param outRect The hit rectangle of the view.
8063 */
8064 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07008065 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008066 final TransformationInfo info = mTransformationInfo;
8067 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008068 outRect.set(mLeft, mTop, mRight, mBottom);
8069 } else {
8070 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008071 tmpRect.set(-info.mPivotX, -info.mPivotY,
8072 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
8073 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07008074 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
8075 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07008076 }
8077 }
8078
8079 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07008080 * Determines whether the given point, in local coordinates is inside the view.
8081 */
8082 /*package*/ final boolean pointInView(float localX, float localY) {
8083 return localX >= 0 && localX < (mRight - mLeft)
8084 && localY >= 0 && localY < (mBottom - mTop);
8085 }
8086
8087 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008088 * Utility method to determine whether the given point, in local coordinates,
8089 * is inside the view, where the area of the view is expanded by the slop factor.
8090 * This method is called while processing touch-move events to determine if the event
8091 * is still within the view.
8092 */
8093 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07008094 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07008095 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008096 }
8097
8098 /**
8099 * When a view has focus and the user navigates away from it, the next view is searched for
8100 * starting from the rectangle filled in by this method.
8101 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008102 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
8103 * of the view. However, if your view maintains some idea of internal selection,
8104 * such as a cursor, or a selected row or column, you should override this method and
8105 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008106 *
8107 * @param r The rectangle to fill in, in this view's coordinates.
8108 */
8109 public void getFocusedRect(Rect r) {
8110 getDrawingRect(r);
8111 }
8112
8113 /**
8114 * If some part of this view is not clipped by any of its parents, then
8115 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01008116 * coordinates (without taking possible View rotations into account), offset
8117 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
8118 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008119 *
8120 * @param r If true is returned, r holds the global coordinates of the
8121 * visible portion of this view.
8122 * @param globalOffset If true is returned, globalOffset holds the dx,dy
8123 * between this view and its root. globalOffet may be null.
8124 * @return true if r is non-empty (i.e. part of the view is visible at the
8125 * root level.
8126 */
8127 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
8128 int width = mRight - mLeft;
8129 int height = mBottom - mTop;
8130 if (width > 0 && height > 0) {
8131 r.set(0, 0, width, height);
8132 if (globalOffset != null) {
8133 globalOffset.set(-mScrollX, -mScrollY);
8134 }
8135 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
8136 }
8137 return false;
8138 }
8139
8140 public final boolean getGlobalVisibleRect(Rect r) {
8141 return getGlobalVisibleRect(r, null);
8142 }
8143
8144 public final boolean getLocalVisibleRect(Rect r) {
8145 Point offset = new Point();
8146 if (getGlobalVisibleRect(r, offset)) {
8147 r.offset(-offset.x, -offset.y); // make r local
8148 return true;
8149 }
8150 return false;
8151 }
8152
8153 /**
8154 * Offset this view's vertical location by the specified number of pixels.
8155 *
8156 * @param offset the number of pixels to offset the view by
8157 */
8158 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008159 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008160 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008161 final boolean matrixIsIdentity = mTransformationInfo == null
8162 || mTransformationInfo.mMatrixIsIdentity;
8163 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008164 final ViewParent p = mParent;
8165 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008166 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008167 int minTop;
8168 int maxBottom;
8169 int yLoc;
8170 if (offset < 0) {
8171 minTop = mTop + offset;
8172 maxBottom = mBottom;
8173 yLoc = offset;
8174 } else {
8175 minTop = mTop;
8176 maxBottom = mBottom + offset;
8177 yLoc = 0;
8178 }
8179 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8180 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008181 }
8182 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008183 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008184 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008185
Chet Haasec3aa3612010-06-17 08:50:37 -07008186 mTop += offset;
8187 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008188
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008189 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008190 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008191 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008192 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008193 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008195 }
8196
8197 /**
8198 * Offset this view's horizontal location by the specified amount of pixels.
8199 *
8200 * @param offset the numer of pixels to offset the view by
8201 */
8202 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008203 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008204 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008205 final boolean matrixIsIdentity = mTransformationInfo == null
8206 || mTransformationInfo.mMatrixIsIdentity;
8207 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008208 final ViewParent p = mParent;
8209 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008210 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008211 int minLeft;
8212 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008213 if (offset < 0) {
8214 minLeft = mLeft + offset;
8215 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008216 } else {
8217 minLeft = mLeft;
8218 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008219 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008220 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008221 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008222 }
8223 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008224 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008225 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008226
Chet Haasec3aa3612010-06-17 08:50:37 -07008227 mLeft += offset;
8228 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008229
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008230 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008231 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008232 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008233 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008234 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008236 }
8237
8238 /**
8239 * Get the LayoutParams associated with this view. All views should have
8240 * layout parameters. These supply parameters to the <i>parent</i> of this
8241 * view specifying how it should be arranged. There are many subclasses of
8242 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8243 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008244 *
8245 * This method may return null if this View is not attached to a parent
8246 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8247 * was not invoked successfully. When a View is attached to a parent
8248 * ViewGroup, this method must not return null.
8249 *
8250 * @return The LayoutParams associated with this view, or null if no
8251 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008252 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008253 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008254 public ViewGroup.LayoutParams getLayoutParams() {
8255 return mLayoutParams;
8256 }
8257
8258 /**
8259 * Set the layout parameters associated with this view. These supply
8260 * parameters to the <i>parent</i> of this view specifying how it should be
8261 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8262 * correspond to the different subclasses of ViewGroup that are responsible
8263 * for arranging their children.
8264 *
Romain Guy01c174b2011-02-22 11:51:06 -08008265 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008266 */
8267 public void setLayoutParams(ViewGroup.LayoutParams params) {
8268 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008269 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008270 }
8271 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08008272 if (mParent instanceof ViewGroup) {
8273 ((ViewGroup) mParent).onSetLayoutParams(this, params);
8274 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008275 requestLayout();
8276 }
8277
8278 /**
8279 * Set the scrolled position of your view. This will cause a call to
8280 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8281 * invalidated.
8282 * @param x the x position to scroll to
8283 * @param y the y position to scroll to
8284 */
8285 public void scrollTo(int x, int y) {
8286 if (mScrollX != x || mScrollY != y) {
8287 int oldX = mScrollX;
8288 int oldY = mScrollY;
8289 mScrollX = x;
8290 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008291 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008292 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008293 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008294 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008295 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008296 }
8297 }
8298
8299 /**
8300 * Move the scrolled position of your view. This will cause a call to
8301 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8302 * invalidated.
8303 * @param x the amount of pixels to scroll by horizontally
8304 * @param y the amount of pixels to scroll by vertically
8305 */
8306 public void scrollBy(int x, int y) {
8307 scrollTo(mScrollX + x, mScrollY + y);
8308 }
8309
8310 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008311 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8312 * animation to fade the scrollbars out after a default delay. If a subclass
8313 * provides animated scrolling, the start delay should equal the duration
8314 * of the scrolling animation.</p>
8315 *
8316 * <p>The animation starts only if at least one of the scrollbars is
8317 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8318 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8319 * this method returns true, and false otherwise. If the animation is
8320 * started, this method calls {@link #invalidate()}; in that case the
8321 * caller should not call {@link #invalidate()}.</p>
8322 *
8323 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008324 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008325 *
8326 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8327 * and {@link #scrollTo(int, int)}.</p>
8328 *
8329 * @return true if the animation is played, false otherwise
8330 *
8331 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008332 * @see #scrollBy(int, int)
8333 * @see #scrollTo(int, int)
8334 * @see #isHorizontalScrollBarEnabled()
8335 * @see #isVerticalScrollBarEnabled()
8336 * @see #setHorizontalScrollBarEnabled(boolean)
8337 * @see #setVerticalScrollBarEnabled(boolean)
8338 */
8339 protected boolean awakenScrollBars() {
8340 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008341 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008342 }
8343
8344 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008345 * Trigger the scrollbars to draw.
8346 * This method differs from awakenScrollBars() only in its default duration.
8347 * initialAwakenScrollBars() will show the scroll bars for longer than
8348 * usual to give the user more of a chance to notice them.
8349 *
8350 * @return true if the animation is played, false otherwise.
8351 */
8352 private boolean initialAwakenScrollBars() {
8353 return mScrollCache != null &&
8354 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8355 }
8356
8357 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008358 * <p>
8359 * Trigger the scrollbars to draw. When invoked this method starts an
8360 * animation to fade the scrollbars out after a fixed delay. If a subclass
8361 * provides animated scrolling, the start delay should equal the duration of
8362 * the scrolling animation.
8363 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008364 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008365 * <p>
8366 * The animation starts only if at least one of the scrollbars is enabled,
8367 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8368 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8369 * this method returns true, and false otherwise. If the animation is
8370 * started, this method calls {@link #invalidate()}; in that case the caller
8371 * should not call {@link #invalidate()}.
8372 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008373 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008374 * <p>
8375 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008376 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008377 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008378 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008379 * @param startDelay the delay, in milliseconds, after which the animation
8380 * should start; when the delay is 0, the animation starts
8381 * immediately
8382 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008383 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008384 * @see #scrollBy(int, int)
8385 * @see #scrollTo(int, int)
8386 * @see #isHorizontalScrollBarEnabled()
8387 * @see #isVerticalScrollBarEnabled()
8388 * @see #setHorizontalScrollBarEnabled(boolean)
8389 * @see #setVerticalScrollBarEnabled(boolean)
8390 */
8391 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008392 return awakenScrollBars(startDelay, true);
8393 }
Joe Malin32736f02011-01-19 16:14:20 -08008394
Mike Cleron290947b2009-09-29 18:34:32 -07008395 /**
8396 * <p>
8397 * Trigger the scrollbars to draw. When invoked this method starts an
8398 * animation to fade the scrollbars out after a fixed delay. If a subclass
8399 * provides animated scrolling, the start delay should equal the duration of
8400 * the scrolling animation.
8401 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008402 *
Mike Cleron290947b2009-09-29 18:34:32 -07008403 * <p>
8404 * The animation starts only if at least one of the scrollbars is enabled,
8405 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8406 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8407 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008408 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008409 * is set to true; in that case the caller
8410 * should not call {@link #invalidate()}.
8411 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008412 *
Mike Cleron290947b2009-09-29 18:34:32 -07008413 * <p>
8414 * This method should be invoked everytime a subclass directly updates the
8415 * scroll parameters.
8416 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008417 *
Mike Cleron290947b2009-09-29 18:34:32 -07008418 * @param startDelay the delay, in milliseconds, after which the animation
8419 * should start; when the delay is 0, the animation starts
8420 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008421 *
Mike Cleron290947b2009-09-29 18:34:32 -07008422 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008423 *
Mike Cleron290947b2009-09-29 18:34:32 -07008424 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008425 *
Mike Cleron290947b2009-09-29 18:34:32 -07008426 * @see #scrollBy(int, int)
8427 * @see #scrollTo(int, int)
8428 * @see #isHorizontalScrollBarEnabled()
8429 * @see #isVerticalScrollBarEnabled()
8430 * @see #setHorizontalScrollBarEnabled(boolean)
8431 * @see #setVerticalScrollBarEnabled(boolean)
8432 */
8433 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008434 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008435
Mike Cleronf116bf82009-09-27 19:14:12 -07008436 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8437 return false;
8438 }
8439
8440 if (scrollCache.scrollBar == null) {
8441 scrollCache.scrollBar = new ScrollBarDrawable();
8442 }
8443
8444 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8445
Mike Cleron290947b2009-09-29 18:34:32 -07008446 if (invalidate) {
8447 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008448 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008449 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008450
8451 if (scrollCache.state == ScrollabilityCache.OFF) {
8452 // FIXME: this is copied from WindowManagerService.
8453 // We should get this value from the system when it
8454 // is possible to do so.
8455 final int KEY_REPEAT_FIRST_DELAY = 750;
8456 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8457 }
8458
8459 // Tell mScrollCache when we should start fading. This may
8460 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008461 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008462 scrollCache.fadeStartTime = fadeStartTime;
8463 scrollCache.state = ScrollabilityCache.ON;
8464
8465 // Schedule our fader to run, unscheduling any old ones first
8466 if (mAttachInfo != null) {
8467 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8468 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8469 }
8470
8471 return true;
8472 }
8473
8474 return false;
8475 }
8476
8477 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008478 * Do not invalidate views which are not visible and which are not running an animation. They
8479 * will not get drawn and they should not set dirty flags as if they will be drawn
8480 */
8481 private boolean skipInvalidate() {
8482 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8483 (!(mParent instanceof ViewGroup) ||
8484 !((ViewGroup) mParent).isViewTransitioning(this));
8485 }
8486 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008487 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008488 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8489 * in the future. This must be called from a UI thread. To call from a non-UI
8490 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008491 *
8492 * WARNING: This method is destructive to dirty.
8493 * @param dirty the rectangle representing the bounds of the dirty region
8494 */
8495 public void invalidate(Rect dirty) {
8496 if (ViewDebug.TRACE_HIERARCHY) {
8497 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8498 }
8499
Chet Haaseaceafe62011-08-26 15:44:33 -07008500 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008501 return;
8502 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008503 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008504 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8505 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008506 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008507 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008508 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008509 final ViewParent p = mParent;
8510 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008511 //noinspection PointlessBooleanExpression,ConstantConditions
8512 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8513 if (p != null && ai != null && ai.mHardwareAccelerated) {
8514 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008515 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008516 p.invalidateChild(this, null);
8517 return;
8518 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008520 if (p != null && ai != null) {
8521 final int scrollX = mScrollX;
8522 final int scrollY = mScrollY;
8523 final Rect r = ai.mTmpInvalRect;
8524 r.set(dirty.left - scrollX, dirty.top - scrollY,
8525 dirty.right - scrollX, dirty.bottom - scrollY);
8526 mParent.invalidateChild(this, r);
8527 }
8528 }
8529 }
8530
8531 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008532 * 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 -08008533 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008534 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8535 * will be called at some point in the future. This must be called from
8536 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008537 * @param l the left position of the dirty region
8538 * @param t the top position of the dirty region
8539 * @param r the right position of the dirty region
8540 * @param b the bottom position of the dirty region
8541 */
8542 public void invalidate(int l, int t, int r, int b) {
8543 if (ViewDebug.TRACE_HIERARCHY) {
8544 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8545 }
8546
Chet Haaseaceafe62011-08-26 15:44:33 -07008547 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008548 return;
8549 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008550 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008551 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8552 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008553 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008554 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008555 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008556 final ViewParent p = mParent;
8557 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008558 //noinspection PointlessBooleanExpression,ConstantConditions
8559 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8560 if (p != null && ai != null && ai.mHardwareAccelerated) {
8561 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008562 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008563 p.invalidateChild(this, null);
8564 return;
8565 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008567 if (p != null && ai != null && l < r && t < b) {
8568 final int scrollX = mScrollX;
8569 final int scrollY = mScrollY;
8570 final Rect tmpr = ai.mTmpInvalRect;
8571 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8572 p.invalidateChild(this, tmpr);
8573 }
8574 }
8575 }
8576
8577 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008578 * Invalidate the whole view. If the view is visible,
8579 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8580 * the future. This must be called from a UI thread. To call from a non-UI thread,
8581 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008582 */
8583 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008584 invalidate(true);
8585 }
Joe Malin32736f02011-01-19 16:14:20 -08008586
Chet Haaseed032702010-10-01 14:05:54 -07008587 /**
8588 * This is where the invalidate() work actually happens. A full invalidate()
8589 * causes the drawing cache to be invalidated, but this function can be called with
8590 * invalidateCache set to false to skip that invalidation step for cases that do not
8591 * need it (for example, a component that remains at the same dimensions with the same
8592 * content).
8593 *
8594 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8595 * well. This is usually true for a full invalidate, but may be set to false if the
8596 * View's contents or dimensions have not changed.
8597 */
Romain Guy849d0a32011-02-01 17:20:48 -08008598 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008599 if (ViewDebug.TRACE_HIERARCHY) {
8600 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8601 }
8602
Chet Haaseaceafe62011-08-26 15:44:33 -07008603 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008604 return;
8605 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008606 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008607 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008608 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8609 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008610 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008611 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008612 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008613 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008614 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008616 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008617 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008618 //noinspection PointlessBooleanExpression,ConstantConditions
8619 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8620 if (p != null && ai != null && ai.mHardwareAccelerated) {
8621 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008622 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008623 p.invalidateChild(this, null);
8624 return;
8625 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008626 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008628 if (p != null && ai != null) {
8629 final Rect r = ai.mTmpInvalRect;
8630 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8631 // Don't call invalidate -- we don't want to internally scroll
8632 // our own bounds
8633 p.invalidateChild(this, r);
8634 }
8635 }
8636 }
8637
8638 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008639 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008640 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8641 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008642 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8643 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008644 *
8645 * @hide
8646 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008647 protected void invalidateParentCaches() {
8648 if (mParent instanceof View) {
8649 ((View) mParent).mPrivateFlags |= INVALIDATED;
8650 }
8651 }
Joe Malin32736f02011-01-19 16:14:20 -08008652
Romain Guy0fd89bf2011-01-26 15:41:30 -08008653 /**
8654 * Used to indicate that the parent of this view should be invalidated. This functionality
8655 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8656 * which is necessary when various parent-managed properties of the view change, such as
8657 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8658 * an invalidation event to the parent.
8659 *
8660 * @hide
8661 */
8662 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008663 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008664 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008665 }
8666 }
8667
8668 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008669 * Indicates whether this View is opaque. An opaque View guarantees that it will
8670 * draw all the pixels overlapping its bounds using a fully opaque color.
8671 *
8672 * Subclasses of View should override this method whenever possible to indicate
8673 * whether an instance is opaque. Opaque Views are treated in a special way by
8674 * the View hierarchy, possibly allowing it to perform optimizations during
8675 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008676 *
Romain Guy24443ea2009-05-11 11:56:30 -07008677 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008678 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008679 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008680 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008681 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008682 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8683 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008684 }
8685
Adam Powell20232d02010-12-08 21:08:53 -08008686 /**
8687 * @hide
8688 */
8689 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008690 // Opaque if:
8691 // - Has a background
8692 // - Background is opaque
8693 // - Doesn't have scrollbars or scrollbars are inside overlay
8694
8695 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8696 mPrivateFlags |= OPAQUE_BACKGROUND;
8697 } else {
8698 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8699 }
8700
8701 final int flags = mViewFlags;
8702 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8703 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8704 mPrivateFlags |= OPAQUE_SCROLLBARS;
8705 } else {
8706 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8707 }
8708 }
8709
8710 /**
8711 * @hide
8712 */
8713 protected boolean hasOpaqueScrollbars() {
8714 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008715 }
8716
8717 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008718 * @return A handler associated with the thread running the View. This
8719 * handler can be used to pump events in the UI events queue.
8720 */
8721 public Handler getHandler() {
8722 if (mAttachInfo != null) {
8723 return mAttachInfo.mHandler;
8724 }
8725 return null;
8726 }
8727
8728 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08008729 * Gets the view root associated with the View.
8730 * @return The view root, or null if none.
8731 * @hide
8732 */
8733 public ViewRootImpl getViewRootImpl() {
8734 if (mAttachInfo != null) {
8735 return mAttachInfo.mViewRootImpl;
8736 }
8737 return null;
8738 }
8739
8740 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008741 * <p>Causes the Runnable to be added to the message queue.
8742 * The runnable will be run on the user interface thread.</p>
8743 *
8744 * <p>This method can be invoked from outside of the UI thread
8745 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008746 *
8747 * @param action The Runnable that will be executed.
8748 *
8749 * @return Returns true if the Runnable was successfully placed in to the
8750 * message queue. Returns false on failure, usually because the
8751 * looper processing the message queue is exiting.
8752 */
8753 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008754 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008755 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008756 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008757 }
Jeff Browna175a5b2012-02-15 19:18:31 -08008758 // Assume that post will succeed later
8759 ViewRootImpl.getRunQueue().post(action);
8760 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008761 }
8762
8763 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008764 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008765 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008766 * The runnable will be run on the user interface thread.</p>
8767 *
8768 * <p>This method can be invoked from outside of the UI thread
8769 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008770 *
8771 * @param action The Runnable that will be executed.
8772 * @param delayMillis The delay (in milliseconds) until the Runnable
8773 * will be executed.
8774 *
8775 * @return true if the Runnable was successfully placed in to the
8776 * message queue. Returns false on failure, usually because the
8777 * looper processing the message queue is exiting. Note that a
8778 * result of true does not mean the Runnable will be processed --
8779 * if the looper is quit before the delivery time of the message
8780 * occurs then the message will be dropped.
8781 */
8782 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008783 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008784 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008785 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008786 }
Jeff Browna175a5b2012-02-15 19:18:31 -08008787 // Assume that post will succeed later
8788 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
8789 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008790 }
8791
8792 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008793 * <p>Removes the specified Runnable from the message queue.</p>
8794 *
8795 * <p>This method can be invoked from outside of the UI thread
8796 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008797 *
8798 * @param action The Runnable to remove from the message handling queue
8799 *
8800 * @return true if this view could ask the Handler to remove the Runnable,
8801 * false otherwise. When the returned value is true, the Runnable
8802 * may or may not have been actually removed from the message queue
8803 * (for instance, if the Runnable was not in the queue already.)
8804 */
8805 public boolean removeCallbacks(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008806 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008807 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008808 attachInfo.mHandler.removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008809 } else {
8810 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008811 ViewRootImpl.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008812 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008813 return true;
8814 }
8815
8816 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008817 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
8818 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008819 *
Romain Guye63a4f32011-08-11 11:33:31 -07008820 * <p>This method can be invoked from outside of the UI thread
8821 * only when this View is attached to a window.</p>
8822 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008823 * @see #invalidate()
8824 */
8825 public void postInvalidate() {
8826 postInvalidateDelayed(0);
8827 }
8828
8829 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008830 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8831 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
8832 *
8833 * <p>This method can be invoked from outside of the UI thread
8834 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008835 *
8836 * @param left The left coordinate of the rectangle to invalidate.
8837 * @param top The top coordinate of the rectangle to invalidate.
8838 * @param right The right coordinate of the rectangle to invalidate.
8839 * @param bottom The bottom coordinate of the rectangle to invalidate.
8840 *
8841 * @see #invalidate(int, int, int, int)
8842 * @see #invalidate(Rect)
8843 */
8844 public void postInvalidate(int left, int top, int right, int bottom) {
8845 postInvalidateDelayed(0, left, top, right, bottom);
8846 }
8847
8848 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008849 * <p>Cause an invalidate to happen on a subsequent cycle through the event
8850 * loop. Waits for the specified amount of time.</p>
8851 *
8852 * <p>This method can be invoked from outside of the UI thread
8853 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008854 *
8855 * @param delayMilliseconds the duration in milliseconds to delay the
8856 * invalidation by
8857 */
8858 public void postInvalidateDelayed(long delayMilliseconds) {
8859 // We try only with the AttachInfo because there's no point in invalidating
8860 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08008861 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008862 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008863 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008864 }
8865 }
8866
8867 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008868 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8869 * through the event loop. Waits for the specified amount of time.</p>
8870 *
8871 * <p>This method can be invoked from outside of the UI thread
8872 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008873 *
8874 * @param delayMilliseconds the duration in milliseconds to delay the
8875 * invalidation by
8876 * @param left The left coordinate of the rectangle to invalidate.
8877 * @param top The top coordinate of the rectangle to invalidate.
8878 * @param right The right coordinate of the rectangle to invalidate.
8879 * @param bottom The bottom coordinate of the rectangle to invalidate.
8880 */
8881 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8882 int right, int bottom) {
8883
8884 // We try only with the AttachInfo because there's no point in invalidating
8885 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08008886 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008887 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008888 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8889 info.target = this;
8890 info.left = left;
8891 info.top = top;
8892 info.right = right;
8893 info.bottom = bottom;
8894
Jeff Browna175a5b2012-02-15 19:18:31 -08008895 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008896 }
8897 }
8898
8899 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008900 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8901 * This event is sent at most once every
8902 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8903 */
8904 private void postSendViewScrolledAccessibilityEventCallback() {
8905 if (mSendViewScrolledAccessibilityEvent == null) {
8906 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8907 }
8908 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8909 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8910 postDelayed(mSendViewScrolledAccessibilityEvent,
8911 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8912 }
8913 }
8914
8915 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008916 * Called by a parent to request that a child update its values for mScrollX
8917 * and mScrollY if necessary. This will typically be done if the child is
8918 * animating a scroll using a {@link android.widget.Scroller Scroller}
8919 * object.
8920 */
8921 public void computeScroll() {
8922 }
8923
8924 /**
8925 * <p>Indicate whether the horizontal edges are faded when the view is
8926 * scrolled horizontally.</p>
8927 *
8928 * @return true if the horizontal edges should are faded on scroll, false
8929 * otherwise
8930 *
8931 * @see #setHorizontalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008932 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008933 */
8934 public boolean isHorizontalFadingEdgeEnabled() {
8935 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8936 }
8937
8938 /**
8939 * <p>Define whether the horizontal edges should be faded when this view
8940 * is scrolled horizontally.</p>
8941 *
8942 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8943 * be faded when the view is scrolled
8944 * horizontally
8945 *
8946 * @see #isHorizontalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008947 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008948 */
8949 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8950 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8951 if (horizontalFadingEdgeEnabled) {
8952 initScrollCache();
8953 }
8954
8955 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8956 }
8957 }
8958
8959 /**
8960 * <p>Indicate whether the vertical edges are faded when the view is
8961 * scrolled horizontally.</p>
8962 *
8963 * @return true if the vertical edges should are faded on scroll, false
8964 * otherwise
8965 *
8966 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008967 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008968 */
8969 public boolean isVerticalFadingEdgeEnabled() {
8970 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8971 }
8972
8973 /**
8974 * <p>Define whether the vertical edges should be faded when this view
8975 * is scrolled vertically.</p>
8976 *
8977 * @param verticalFadingEdgeEnabled true if the vertical edges should
8978 * be faded when the view is scrolled
8979 * vertically
8980 *
8981 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008982 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008983 */
8984 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8985 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8986 if (verticalFadingEdgeEnabled) {
8987 initScrollCache();
8988 }
8989
8990 mViewFlags ^= FADING_EDGE_VERTICAL;
8991 }
8992 }
8993
8994 /**
8995 * Returns the strength, or intensity, of the top faded edge. The strength is
8996 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8997 * returns 0.0 or 1.0 but no value in between.
8998 *
8999 * Subclasses should override this method to provide a smoother fade transition
9000 * when scrolling occurs.
9001 *
9002 * @return the intensity of the top fade as a float between 0.0f and 1.0f
9003 */
9004 protected float getTopFadingEdgeStrength() {
9005 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
9006 }
9007
9008 /**
9009 * Returns the strength, or intensity, of the bottom faded edge. The strength is
9010 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9011 * returns 0.0 or 1.0 but no value in between.
9012 *
9013 * Subclasses should override this method to provide a smoother fade transition
9014 * when scrolling occurs.
9015 *
9016 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
9017 */
9018 protected float getBottomFadingEdgeStrength() {
9019 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
9020 computeVerticalScrollRange() ? 1.0f : 0.0f;
9021 }
9022
9023 /**
9024 * Returns the strength, or intensity, of the left faded edge. The strength is
9025 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9026 * returns 0.0 or 1.0 but no value in between.
9027 *
9028 * Subclasses should override this method to provide a smoother fade transition
9029 * when scrolling occurs.
9030 *
9031 * @return the intensity of the left fade as a float between 0.0f and 1.0f
9032 */
9033 protected float getLeftFadingEdgeStrength() {
9034 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
9035 }
9036
9037 /**
9038 * Returns the strength, or intensity, of the right faded edge. The strength is
9039 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9040 * returns 0.0 or 1.0 but no value in between.
9041 *
9042 * Subclasses should override this method to provide a smoother fade transition
9043 * when scrolling occurs.
9044 *
9045 * @return the intensity of the right fade as a float between 0.0f and 1.0f
9046 */
9047 protected float getRightFadingEdgeStrength() {
9048 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
9049 computeHorizontalScrollRange() ? 1.0f : 0.0f;
9050 }
9051
9052 /**
9053 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
9054 * scrollbar is not drawn by default.</p>
9055 *
9056 * @return true if the horizontal scrollbar should be painted, false
9057 * otherwise
9058 *
9059 * @see #setHorizontalScrollBarEnabled(boolean)
9060 */
9061 public boolean isHorizontalScrollBarEnabled() {
9062 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9063 }
9064
9065 /**
9066 * <p>Define whether the horizontal scrollbar should be drawn or not. The
9067 * scrollbar is not drawn by default.</p>
9068 *
9069 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
9070 * be painted
9071 *
9072 * @see #isHorizontalScrollBarEnabled()
9073 */
9074 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
9075 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
9076 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009077 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009078 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009079 }
9080 }
9081
9082 /**
9083 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
9084 * scrollbar is not drawn by default.</p>
9085 *
9086 * @return true if the vertical scrollbar should be painted, false
9087 * otherwise
9088 *
9089 * @see #setVerticalScrollBarEnabled(boolean)
9090 */
9091 public boolean isVerticalScrollBarEnabled() {
9092 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
9093 }
9094
9095 /**
9096 * <p>Define whether the vertical scrollbar should be drawn or not. The
9097 * scrollbar is not drawn by default.</p>
9098 *
9099 * @param verticalScrollBarEnabled true if the vertical scrollbar should
9100 * be painted
9101 *
9102 * @see #isVerticalScrollBarEnabled()
9103 */
9104 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9105 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9106 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009107 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009108 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009109 }
9110 }
9111
Adam Powell20232d02010-12-08 21:08:53 -08009112 /**
9113 * @hide
9114 */
9115 protected void recomputePadding() {
9116 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009117 }
Joe Malin32736f02011-01-19 16:14:20 -08009118
Mike Cleronfe81d382009-09-28 14:22:16 -07009119 /**
9120 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009121 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009122 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009123 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009124 */
9125 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9126 initScrollCache();
9127 final ScrollabilityCache scrollabilityCache = mScrollCache;
9128 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009129 if (fadeScrollbars) {
9130 scrollabilityCache.state = ScrollabilityCache.OFF;
9131 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009132 scrollabilityCache.state = ScrollabilityCache.ON;
9133 }
9134 }
Joe Malin32736f02011-01-19 16:14:20 -08009135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009136 /**
Joe Malin32736f02011-01-19 16:14:20 -08009137 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009138 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009139 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009140 * @return true if scrollbar fading is enabled
9141 */
9142 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009143 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009144 }
Joe Malin32736f02011-01-19 16:14:20 -08009145
Mike Cleron52f0a642009-09-28 18:21:37 -07009146 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009147 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9148 * inset. When inset, they add to the padding of the view. And the scrollbars
9149 * can be drawn inside the padding area or on the edge of the view. For example,
9150 * if a view has a background drawable and you want to draw the scrollbars
9151 * inside the padding specified by the drawable, you can use
9152 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9153 * appear at the edge of the view, ignoring the padding, then you can use
9154 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9155 * @param style the style of the scrollbars. Should be one of
9156 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9157 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9158 * @see #SCROLLBARS_INSIDE_OVERLAY
9159 * @see #SCROLLBARS_INSIDE_INSET
9160 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9161 * @see #SCROLLBARS_OUTSIDE_INSET
9162 */
9163 public void setScrollBarStyle(int style) {
9164 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9165 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009166 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009167 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009168 }
9169 }
9170
9171 /**
9172 * <p>Returns the current scrollbar style.</p>
9173 * @return the current scrollbar style
9174 * @see #SCROLLBARS_INSIDE_OVERLAY
9175 * @see #SCROLLBARS_INSIDE_INSET
9176 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9177 * @see #SCROLLBARS_OUTSIDE_INSET
9178 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009179 @ViewDebug.ExportedProperty(mapping = {
9180 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9181 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9182 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9183 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9184 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009185 public int getScrollBarStyle() {
9186 return mViewFlags & SCROLLBARS_STYLE_MASK;
9187 }
9188
9189 /**
9190 * <p>Compute the horizontal range that the horizontal scrollbar
9191 * represents.</p>
9192 *
9193 * <p>The range is expressed in arbitrary units that must be the same as the
9194 * units used by {@link #computeHorizontalScrollExtent()} and
9195 * {@link #computeHorizontalScrollOffset()}.</p>
9196 *
9197 * <p>The default range is the drawing width of this view.</p>
9198 *
9199 * @return the total horizontal range represented by the horizontal
9200 * scrollbar
9201 *
9202 * @see #computeHorizontalScrollExtent()
9203 * @see #computeHorizontalScrollOffset()
9204 * @see android.widget.ScrollBarDrawable
9205 */
9206 protected int computeHorizontalScrollRange() {
9207 return getWidth();
9208 }
9209
9210 /**
9211 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9212 * within the horizontal range. This value is used to compute the position
9213 * of the thumb within the scrollbar's track.</p>
9214 *
9215 * <p>The range is expressed in arbitrary units that must be the same as the
9216 * units used by {@link #computeHorizontalScrollRange()} and
9217 * {@link #computeHorizontalScrollExtent()}.</p>
9218 *
9219 * <p>The default offset is the scroll offset of this view.</p>
9220 *
9221 * @return the horizontal offset of the scrollbar's thumb
9222 *
9223 * @see #computeHorizontalScrollRange()
9224 * @see #computeHorizontalScrollExtent()
9225 * @see android.widget.ScrollBarDrawable
9226 */
9227 protected int computeHorizontalScrollOffset() {
9228 return mScrollX;
9229 }
9230
9231 /**
9232 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9233 * within the horizontal range. This value is used to compute the length
9234 * of the thumb within the scrollbar's track.</p>
9235 *
9236 * <p>The range is expressed in arbitrary units that must be the same as the
9237 * units used by {@link #computeHorizontalScrollRange()} and
9238 * {@link #computeHorizontalScrollOffset()}.</p>
9239 *
9240 * <p>The default extent is the drawing width of this view.</p>
9241 *
9242 * @return the horizontal extent of the scrollbar's thumb
9243 *
9244 * @see #computeHorizontalScrollRange()
9245 * @see #computeHorizontalScrollOffset()
9246 * @see android.widget.ScrollBarDrawable
9247 */
9248 protected int computeHorizontalScrollExtent() {
9249 return getWidth();
9250 }
9251
9252 /**
9253 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9254 *
9255 * <p>The range is expressed in arbitrary units that must be the same as the
9256 * units used by {@link #computeVerticalScrollExtent()} and
9257 * {@link #computeVerticalScrollOffset()}.</p>
9258 *
9259 * @return the total vertical range represented by the vertical scrollbar
9260 *
9261 * <p>The default range is the drawing height of this view.</p>
9262 *
9263 * @see #computeVerticalScrollExtent()
9264 * @see #computeVerticalScrollOffset()
9265 * @see android.widget.ScrollBarDrawable
9266 */
9267 protected int computeVerticalScrollRange() {
9268 return getHeight();
9269 }
9270
9271 /**
9272 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9273 * within the horizontal range. This value is used to compute the position
9274 * of the thumb within the scrollbar's track.</p>
9275 *
9276 * <p>The range is expressed in arbitrary units that must be the same as the
9277 * units used by {@link #computeVerticalScrollRange()} and
9278 * {@link #computeVerticalScrollExtent()}.</p>
9279 *
9280 * <p>The default offset is the scroll offset of this view.</p>
9281 *
9282 * @return the vertical offset of the scrollbar's thumb
9283 *
9284 * @see #computeVerticalScrollRange()
9285 * @see #computeVerticalScrollExtent()
9286 * @see android.widget.ScrollBarDrawable
9287 */
9288 protected int computeVerticalScrollOffset() {
9289 return mScrollY;
9290 }
9291
9292 /**
9293 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9294 * within the vertical range. This value is used to compute the length
9295 * of the thumb within the scrollbar's track.</p>
9296 *
9297 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009298 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009299 * {@link #computeVerticalScrollOffset()}.</p>
9300 *
9301 * <p>The default extent is the drawing height of this view.</p>
9302 *
9303 * @return the vertical extent of the scrollbar's thumb
9304 *
9305 * @see #computeVerticalScrollRange()
9306 * @see #computeVerticalScrollOffset()
9307 * @see android.widget.ScrollBarDrawable
9308 */
9309 protected int computeVerticalScrollExtent() {
9310 return getHeight();
9311 }
9312
9313 /**
Adam Powell69159442011-06-13 17:53:06 -07009314 * Check if this view can be scrolled horizontally in a certain direction.
9315 *
9316 * @param direction Negative to check scrolling left, positive to check scrolling right.
9317 * @return true if this view can be scrolled in the specified direction, false otherwise.
9318 */
9319 public boolean canScrollHorizontally(int direction) {
9320 final int offset = computeHorizontalScrollOffset();
9321 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9322 if (range == 0) return false;
9323 if (direction < 0) {
9324 return offset > 0;
9325 } else {
9326 return offset < range - 1;
9327 }
9328 }
9329
9330 /**
9331 * Check if this view can be scrolled vertically in a certain direction.
9332 *
9333 * @param direction Negative to check scrolling up, positive to check scrolling down.
9334 * @return true if this view can be scrolled in the specified direction, false otherwise.
9335 */
9336 public boolean canScrollVertically(int direction) {
9337 final int offset = computeVerticalScrollOffset();
9338 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9339 if (range == 0) return false;
9340 if (direction < 0) {
9341 return offset > 0;
9342 } else {
9343 return offset < range - 1;
9344 }
9345 }
9346
9347 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009348 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9349 * scrollbars are painted only if they have been awakened first.</p>
9350 *
9351 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009352 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009353 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009354 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009355 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009356 // scrollbars are drawn only when the animation is running
9357 final ScrollabilityCache cache = mScrollCache;
9358 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009359
Mike Cleronf116bf82009-09-27 19:14:12 -07009360 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009361
Mike Cleronf116bf82009-09-27 19:14:12 -07009362 if (state == ScrollabilityCache.OFF) {
9363 return;
9364 }
Joe Malin32736f02011-01-19 16:14:20 -08009365
Mike Cleronf116bf82009-09-27 19:14:12 -07009366 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009367
Mike Cleronf116bf82009-09-27 19:14:12 -07009368 if (state == ScrollabilityCache.FADING) {
9369 // We're fading -- get our fade interpolation
9370 if (cache.interpolatorValues == null) {
9371 cache.interpolatorValues = new float[1];
9372 }
Joe Malin32736f02011-01-19 16:14:20 -08009373
Mike Cleronf116bf82009-09-27 19:14:12 -07009374 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009375
Mike Cleronf116bf82009-09-27 19:14:12 -07009376 // Stops the animation if we're done
9377 if (cache.scrollBarInterpolator.timeToValues(values) ==
9378 Interpolator.Result.FREEZE_END) {
9379 cache.state = ScrollabilityCache.OFF;
9380 } else {
9381 cache.scrollBar.setAlpha(Math.round(values[0]));
9382 }
Joe Malin32736f02011-01-19 16:14:20 -08009383
9384 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009385 // drawing. We only want this when we're fading so that
9386 // we prevent excessive redraws
9387 invalidate = true;
9388 } else {
9389 // We're just on -- but we may have been fading before so
9390 // reset alpha
9391 cache.scrollBar.setAlpha(255);
9392 }
9393
Joe Malin32736f02011-01-19 16:14:20 -08009394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009395 final int viewFlags = mViewFlags;
9396
9397 final boolean drawHorizontalScrollBar =
9398 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9399 final boolean drawVerticalScrollBar =
9400 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9401 && !isVerticalScrollBarHidden();
9402
9403 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9404 final int width = mRight - mLeft;
9405 final int height = mBottom - mTop;
9406
9407 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009408
Mike Reede8853fc2009-09-04 14:01:48 -04009409 final int scrollX = mScrollX;
9410 final int scrollY = mScrollY;
9411 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9412
Mike Cleronf116bf82009-09-27 19:14:12 -07009413 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009415 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009416 int size = scrollBar.getSize(false);
9417 if (size <= 0) {
9418 size = cache.scrollBarSize;
9419 }
9420
Mike Cleronf116bf82009-09-27 19:14:12 -07009421 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009422 computeHorizontalScrollOffset(),
9423 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009424 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009425 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009426 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009427 left = scrollX + (mPaddingLeft & inside);
9428 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9429 bottom = top + size;
9430 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9431 if (invalidate) {
9432 invalidate(left, top, right, bottom);
9433 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009434 }
9435
9436 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009437 int size = scrollBar.getSize(true);
9438 if (size <= 0) {
9439 size = cache.scrollBarSize;
9440 }
9441
Mike Reede8853fc2009-09-04 14:01:48 -04009442 scrollBar.setParameters(computeVerticalScrollRange(),
9443 computeVerticalScrollOffset(),
9444 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009445 switch (mVerticalScrollbarPosition) {
9446 default:
9447 case SCROLLBAR_POSITION_DEFAULT:
9448 case SCROLLBAR_POSITION_RIGHT:
9449 left = scrollX + width - size - (mUserPaddingRight & inside);
9450 break;
9451 case SCROLLBAR_POSITION_LEFT:
9452 left = scrollX + (mUserPaddingLeft & inside);
9453 break;
9454 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009455 top = scrollY + (mPaddingTop & inside);
9456 right = left + size;
9457 bottom = scrollY + height - (mUserPaddingBottom & inside);
9458 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9459 if (invalidate) {
9460 invalidate(left, top, right, bottom);
9461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009462 }
9463 }
9464 }
9465 }
Romain Guy8506ab42009-06-11 17:35:47 -07009466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009467 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009468 * 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 -08009469 * FastScroller is visible.
9470 * @return whether to temporarily hide the vertical scrollbar
9471 * @hide
9472 */
9473 protected boolean isVerticalScrollBarHidden() {
9474 return false;
9475 }
9476
9477 /**
9478 * <p>Draw the horizontal scrollbar if
9479 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9480 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009481 * @param canvas the canvas on which to draw the scrollbar
9482 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009483 *
9484 * @see #isHorizontalScrollBarEnabled()
9485 * @see #computeHorizontalScrollRange()
9486 * @see #computeHorizontalScrollExtent()
9487 * @see #computeHorizontalScrollOffset()
9488 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009489 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009490 */
Romain Guy8fb95422010-08-17 18:38:51 -07009491 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9492 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009493 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009494 scrollBar.draw(canvas);
9495 }
Mike Reede8853fc2009-09-04 14:01:48 -04009496
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009497 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009498 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9499 * returns true.</p>
9500 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009501 * @param canvas the canvas on which to draw the scrollbar
9502 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009503 *
9504 * @see #isVerticalScrollBarEnabled()
9505 * @see #computeVerticalScrollRange()
9506 * @see #computeVerticalScrollExtent()
9507 * @see #computeVerticalScrollOffset()
9508 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009509 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009510 */
Romain Guy8fb95422010-08-17 18:38:51 -07009511 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9512 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009513 scrollBar.setBounds(l, t, r, b);
9514 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009515 }
9516
9517 /**
9518 * Implement this to do your drawing.
9519 *
9520 * @param canvas the canvas on which the background will be drawn
9521 */
9522 protected void onDraw(Canvas canvas) {
9523 }
9524
9525 /*
9526 * Caller is responsible for calling requestLayout if necessary.
9527 * (This allows addViewInLayout to not request a new layout.)
9528 */
9529 void assignParent(ViewParent parent) {
9530 if (mParent == null) {
9531 mParent = parent;
9532 } else if (parent == null) {
9533 mParent = null;
9534 } else {
9535 throw new RuntimeException("view " + this + " being added, but"
9536 + " it already has a parent");
9537 }
9538 }
9539
9540 /**
9541 * This is called when the view is attached to a window. At this point it
9542 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009543 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9544 * however it may be called any time before the first onDraw -- including
9545 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009546 *
9547 * @see #onDetachedFromWindow()
9548 */
9549 protected void onAttachedToWindow() {
9550 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9551 mParent.requestTransparentRegion(this);
9552 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009553 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9554 initialAwakenScrollBars();
9555 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9556 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009557 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009558 // Order is important here: LayoutDirection MUST be resolved before Padding
9559 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009560 resolveLayoutDirectionIfNeeded();
9561 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009562 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009563 if (isFocused()) {
9564 InputMethodManager imm = InputMethodManager.peekInstance();
9565 imm.focusIn(this);
9566 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009567 }
Cibu Johny86666632010-02-22 13:01:02 -08009568
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009569 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009570 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9571 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009572 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009573 private void resolveLayoutDirectionIfNeeded() {
9574 // Do not resolve if it is not needed
9575 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9576
9577 // Clear any previous layout direction resolution
9578 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9579
9580 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009581 switch (getLayoutDirection()) {
9582 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009583 // We cannot do the resolution if there is no parent
9584 if (mParent == null) return;
9585
Cibu Johny86666632010-02-22 13:01:02 -08009586 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009587 if (mParent instanceof ViewGroup) {
9588 ViewGroup viewGroup = ((ViewGroup) mParent);
9589
9590 // Check if the parent view group can resolve
9591 if (! viewGroup.canResolveLayoutDirection()) {
9592 return;
9593 }
9594 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9595 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9596 }
Cibu Johny86666632010-02-22 13:01:02 -08009597 }
9598 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009599 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009600 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009601 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009602 case LAYOUT_DIRECTION_LOCALE:
9603 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009604 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009605 }
9606 break;
9607 default:
9608 // Nothing to do, LTR by default
9609 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009610
9611 // Set to resolved
9612 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
9613 }
9614
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009615 /**
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009616 * Force padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009617 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009618 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009619 // If the user specified the absolute padding (either with android:padding or
9620 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9621 // use the default padding or the padding from the background drawable
9622 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009623 int resolvedLayoutDirection = getResolvedLayoutDirection();
9624 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009625 case LAYOUT_DIRECTION_RTL:
9626 // Start user padding override Right user padding. Otherwise, if Right user
9627 // padding is not defined, use the default Right padding. If Right user padding
9628 // is defined, just use it.
9629 if (mUserPaddingStart >= 0) {
9630 mUserPaddingRight = mUserPaddingStart;
9631 } else if (mUserPaddingRight < 0) {
9632 mUserPaddingRight = mPaddingRight;
9633 }
9634 if (mUserPaddingEnd >= 0) {
9635 mUserPaddingLeft = mUserPaddingEnd;
9636 } else if (mUserPaddingLeft < 0) {
9637 mUserPaddingLeft = mPaddingLeft;
9638 }
9639 break;
9640 case LAYOUT_DIRECTION_LTR:
9641 default:
9642 // Start user padding override Left user padding. Otherwise, if Left user
9643 // padding is not defined, use the default left padding. If Left user padding
9644 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009645 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009646 mUserPaddingLeft = mUserPaddingStart;
9647 } else if (mUserPaddingLeft < 0) {
9648 mUserPaddingLeft = mPaddingLeft;
9649 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009650 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009651 mUserPaddingRight = mUserPaddingEnd;
9652 } else if (mUserPaddingRight < 0) {
9653 mUserPaddingRight = mPaddingRight;
9654 }
9655 }
9656
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009657 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9658
9659 recomputePadding();
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009660 onResolvePadding(resolvedLayoutDirection);
9661 }
9662
9663 /**
9664 * Resolve padding depending on the layout direction. Subclasses that care about
9665 * padding resolution should override this method. The default implementation does
9666 * nothing.
9667 *
9668 * @param layoutDirection the direction of the layout
9669 *
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009670 */
9671 public void onResolvePadding(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009672 }
9673
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009674 /**
9675 * Return true if layout direction resolution can be done
9676 *
9677 * @hide
9678 */
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009679 protected boolean canResolveLayoutDirection() {
9680 switch (getLayoutDirection()) {
9681 case LAYOUT_DIRECTION_INHERIT:
9682 return (mParent != null);
9683 default:
9684 return true;
9685 }
9686 }
9687
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009688 /**
Doug Feltc0ccf0c2011-06-23 16:13:18 -07009689 * Reset the resolved layout direction.
9690 *
9691 * Subclasses need to override this method to clear cached information that depends on the
9692 * resolved layout direction, or to inform child views that inherit their layout direction.
9693 * Overrides must also call the superclass implementation at the start of their implementation.
9694 *
9695 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009696 */
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009697 protected void resetResolvedLayoutDirection() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -08009698 // Reset the layout direction resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009699 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -08009700 // Reset also the text direction
9701 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009702 }
9703
9704 /**
9705 * Check if a Locale is corresponding to a RTL script.
9706 *
9707 * @param locale Locale to check
9708 * @return true if a Locale is corresponding to a RTL script.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009709 *
9710 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009711 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009712 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07009713 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
9714 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009715 }
9716
9717 /**
9718 * This is called when the view is detached from a window. At this point it
9719 * no longer has a surface for drawing.
9720 *
9721 * @see #onAttachedToWindow()
9722 */
9723 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009724 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009725
Romain Guya440b002010-02-24 15:57:54 -08009726 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009727 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009728 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009729 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009731 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009732
Romain Guy6d7475d2011-07-27 16:28:21 -07009733 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009734
Chet Haasedaf98e92011-01-10 14:10:36 -08009735 if (mDisplayList != null) {
9736 mDisplayList.invalidate();
9737 }
9738
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009739 if (mAttachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009740 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009741 }
9742
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009743 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009744
9745 resetResolvedLayoutDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009746 }
9747
9748 /**
9749 * @return The number of times this view has been attached to a window
9750 */
9751 protected int getWindowAttachCount() {
9752 return mWindowAttachCount;
9753 }
9754
9755 /**
9756 * Retrieve a unique token identifying the window this view is attached to.
9757 * @return Return the window's token for use in
9758 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9759 */
9760 public IBinder getWindowToken() {
9761 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9762 }
9763
9764 /**
9765 * Retrieve a unique token identifying the top-level "real" window of
9766 * the window that this view is attached to. That is, this is like
9767 * {@link #getWindowToken}, except if the window this view in is a panel
9768 * window (attached to another containing window), then the token of
9769 * the containing window is returned instead.
9770 *
9771 * @return Returns the associated window token, either
9772 * {@link #getWindowToken()} or the containing window's token.
9773 */
9774 public IBinder getApplicationWindowToken() {
9775 AttachInfo ai = mAttachInfo;
9776 if (ai != null) {
9777 IBinder appWindowToken = ai.mPanelParentWindowToken;
9778 if (appWindowToken == null) {
9779 appWindowToken = ai.mWindowToken;
9780 }
9781 return appWindowToken;
9782 }
9783 return null;
9784 }
9785
9786 /**
9787 * Retrieve private session object this view hierarchy is using to
9788 * communicate with the window manager.
9789 * @return the session object to communicate with the window manager
9790 */
9791 /*package*/ IWindowSession getWindowSession() {
9792 return mAttachInfo != null ? mAttachInfo.mSession : null;
9793 }
9794
9795 /**
9796 * @param info the {@link android.view.View.AttachInfo} to associated with
9797 * this view
9798 */
9799 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9800 //System.out.println("Attached! " + this);
9801 mAttachInfo = info;
9802 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009803 // We will need to evaluate the drawable state at least once.
9804 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009805 if (mFloatingTreeObserver != null) {
9806 info.mTreeObserver.merge(mFloatingTreeObserver);
9807 mFloatingTreeObserver = null;
9808 }
9809 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9810 mAttachInfo.mScrollContainers.add(this);
9811 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9812 }
9813 performCollectViewAttributes(visibility);
9814 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009815
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009816 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009817 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009818 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009819 if (listeners != null && listeners.size() > 0) {
9820 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9821 // perform the dispatching. The iterator is a safe guard against listeners that
9822 // could mutate the list by calling the various add/remove methods. This prevents
9823 // the array from being modified while we iterate it.
9824 for (OnAttachStateChangeListener listener : listeners) {
9825 listener.onViewAttachedToWindow(this);
9826 }
9827 }
9828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009829 int vis = info.mWindowVisibility;
9830 if (vis != GONE) {
9831 onWindowVisibilityChanged(vis);
9832 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009833 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9834 // If nobody has evaluated the drawable state yet, then do it now.
9835 refreshDrawableState();
9836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009837 }
9838
9839 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009840 AttachInfo info = mAttachInfo;
9841 if (info != null) {
9842 int vis = info.mWindowVisibility;
9843 if (vis != GONE) {
9844 onWindowVisibilityChanged(GONE);
9845 }
9846 }
9847
9848 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009849
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009850 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009851 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009852 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009853 if (listeners != null && listeners.size() > 0) {
9854 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9855 // perform the dispatching. The iterator is a safe guard against listeners that
9856 // could mutate the list by calling the various add/remove methods. This prevents
9857 // the array from being modified while we iterate it.
9858 for (OnAttachStateChangeListener listener : listeners) {
9859 listener.onViewDetachedFromWindow(this);
9860 }
9861 }
9862
Romain Guy01d5edc2011-01-28 11:28:53 -08009863 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009864 mAttachInfo.mScrollContainers.remove(this);
9865 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9866 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009868 mAttachInfo = null;
9869 }
9870
9871 /**
9872 * Store this view hierarchy's frozen state into the given container.
9873 *
9874 * @param container The SparseArray in which to save the view's state.
9875 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009876 * @see #restoreHierarchyState(android.util.SparseArray)
9877 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9878 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009879 */
9880 public void saveHierarchyState(SparseArray<Parcelable> container) {
9881 dispatchSaveInstanceState(container);
9882 }
9883
9884 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009885 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9886 * this view and its children. May be overridden to modify how freezing happens to a
9887 * 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 -08009888 *
9889 * @param container The SparseArray in which to save the view's state.
9890 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009891 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9892 * @see #saveHierarchyState(android.util.SparseArray)
9893 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009894 */
9895 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9896 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9897 mPrivateFlags &= ~SAVE_STATE_CALLED;
9898 Parcelable state = onSaveInstanceState();
9899 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9900 throw new IllegalStateException(
9901 "Derived class did not call super.onSaveInstanceState()");
9902 }
9903 if (state != null) {
9904 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9905 // + ": " + state);
9906 container.put(mID, state);
9907 }
9908 }
9909 }
9910
9911 /**
9912 * Hook allowing a view to generate a representation of its internal state
9913 * that can later be used to create a new instance with that same state.
9914 * This state should only contain information that is not persistent or can
9915 * not be reconstructed later. For example, you will never store your
9916 * current position on screen because that will be computed again when a
9917 * new instance of the view is placed in its view hierarchy.
9918 * <p>
9919 * Some examples of things you may store here: the current cursor position
9920 * in a text view (but usually not the text itself since that is stored in a
9921 * content provider or other persistent storage), the currently selected
9922 * item in a list view.
9923 *
9924 * @return Returns a Parcelable object containing the view's current dynamic
9925 * state, or null if there is nothing interesting to save. The
9926 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009927 * @see #onRestoreInstanceState(android.os.Parcelable)
9928 * @see #saveHierarchyState(android.util.SparseArray)
9929 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009930 * @see #setSaveEnabled(boolean)
9931 */
9932 protected Parcelable onSaveInstanceState() {
9933 mPrivateFlags |= SAVE_STATE_CALLED;
9934 return BaseSavedState.EMPTY_STATE;
9935 }
9936
9937 /**
9938 * Restore this view hierarchy's frozen state from the given container.
9939 *
9940 * @param container The SparseArray which holds previously frozen states.
9941 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009942 * @see #saveHierarchyState(android.util.SparseArray)
9943 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9944 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009945 */
9946 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9947 dispatchRestoreInstanceState(container);
9948 }
9949
9950 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009951 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9952 * state for this view and its children. May be overridden to modify how restoring
9953 * happens to a view's children; for example, some views may want to not store state
9954 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009955 *
9956 * @param container The SparseArray which holds previously saved state.
9957 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009958 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9959 * @see #restoreHierarchyState(android.util.SparseArray)
9960 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009961 */
9962 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9963 if (mID != NO_ID) {
9964 Parcelable state = container.get(mID);
9965 if (state != null) {
9966 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9967 // + ": " + state);
9968 mPrivateFlags &= ~SAVE_STATE_CALLED;
9969 onRestoreInstanceState(state);
9970 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9971 throw new IllegalStateException(
9972 "Derived class did not call super.onRestoreInstanceState()");
9973 }
9974 }
9975 }
9976 }
9977
9978 /**
9979 * Hook allowing a view to re-apply a representation of its internal state that had previously
9980 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9981 * null state.
9982 *
9983 * @param state The frozen state that had previously been returned by
9984 * {@link #onSaveInstanceState}.
9985 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009986 * @see #onSaveInstanceState()
9987 * @see #restoreHierarchyState(android.util.SparseArray)
9988 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009989 */
9990 protected void onRestoreInstanceState(Parcelable state) {
9991 mPrivateFlags |= SAVE_STATE_CALLED;
9992 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009993 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9994 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009995 + "when two views of different type have the same id in the same hierarchy. "
9996 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009997 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009998 }
9999 }
10000
10001 /**
10002 * <p>Return the time at which the drawing of the view hierarchy started.</p>
10003 *
10004 * @return the drawing start time in milliseconds
10005 */
10006 public long getDrawingTime() {
10007 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
10008 }
10009
10010 /**
10011 * <p>Enables or disables the duplication of the parent's state into this view. When
10012 * duplication is enabled, this view gets its drawable state from its parent rather
10013 * than from its own internal properties.</p>
10014 *
10015 * <p>Note: in the current implementation, setting this property to true after the
10016 * view was added to a ViewGroup might have no effect at all. This property should
10017 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
10018 *
10019 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
10020 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010021 *
Gilles Debunnefb817032011-01-13 13:52:49 -080010022 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
10023 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010024 *
10025 * @param enabled True to enable duplication of the parent's drawable state, false
10026 * to disable it.
10027 *
10028 * @see #getDrawableState()
10029 * @see #isDuplicateParentStateEnabled()
10030 */
10031 public void setDuplicateParentStateEnabled(boolean enabled) {
10032 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
10033 }
10034
10035 /**
10036 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
10037 *
10038 * @return True if this view's drawable state is duplicated from the parent,
10039 * false otherwise
10040 *
10041 * @see #getDrawableState()
10042 * @see #setDuplicateParentStateEnabled(boolean)
10043 */
10044 public boolean isDuplicateParentStateEnabled() {
10045 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
10046 }
10047
10048 /**
Romain Guy171c5922011-01-06 10:04:23 -080010049 * <p>Specifies the type of layer backing this view. The layer can be
10050 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
10051 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010052 *
Romain Guy171c5922011-01-06 10:04:23 -080010053 * <p>A layer is associated with an optional {@link android.graphics.Paint}
10054 * instance that controls how the layer is composed on screen. The following
10055 * properties of the paint are taken into account when composing the layer:</p>
10056 * <ul>
10057 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
10058 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
10059 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
10060 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080010061 *
Romain Guy171c5922011-01-06 10:04:23 -080010062 * <p>If this view has an alpha value set to < 1.0 by calling
10063 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
10064 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
10065 * equivalent to setting a hardware layer on this view and providing a paint with
10066 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080010067 *
Romain Guy171c5922011-01-06 10:04:23 -080010068 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
10069 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
10070 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010071 *
Romain Guy171c5922011-01-06 10:04:23 -080010072 * @param layerType The ype of layer to use with this view, must be one of
10073 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10074 * {@link #LAYER_TYPE_HARDWARE}
10075 * @param paint The paint used to compose the layer. This argument is optional
10076 * and can be null. It is ignored when the layer type is
10077 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080010078 *
10079 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080010080 * @see #LAYER_TYPE_NONE
10081 * @see #LAYER_TYPE_SOFTWARE
10082 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080010083 * @see #setAlpha(float)
10084 *
Romain Guy171c5922011-01-06 10:04:23 -080010085 * @attr ref android.R.styleable#View_layerType
10086 */
10087 public void setLayerType(int layerType, Paint paint) {
10088 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080010089 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080010090 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
10091 }
Chet Haasedaf98e92011-01-10 14:10:36 -080010092
Romain Guyd6cd5722011-01-17 14:42:41 -080010093 if (layerType == mLayerType) {
10094 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
10095 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010096 invalidateParentCaches();
10097 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080010098 }
10099 return;
10100 }
Romain Guy171c5922011-01-06 10:04:23 -080010101
10102 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080010103 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070010104 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010105 destroyLayer();
Romain Guy31f2c2e2011-11-21 10:55:41 -080010106 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080010107 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010108 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010109 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080010110 default:
10111 break;
Romain Guy171c5922011-01-06 10:04:23 -080010112 }
10113
10114 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010115 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10116 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10117 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010118
Romain Guy0fd89bf2011-01-26 15:41:30 -080010119 invalidateParentCaches();
10120 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010121 }
10122
10123 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010124 * Indicates whether this view has a static layer. A view with layer type
10125 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10126 * dynamic.
10127 */
10128 boolean hasStaticLayer() {
10129 return mLayerType == LAYER_TYPE_NONE;
10130 }
10131
10132 /**
Romain Guy171c5922011-01-06 10:04:23 -080010133 * Indicates what type of layer is currently associated with this view. By default
10134 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10135 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10136 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010137 *
Romain Guy171c5922011-01-06 10:04:23 -080010138 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10139 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010140 *
10141 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010142 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010143 * @see #LAYER_TYPE_NONE
10144 * @see #LAYER_TYPE_SOFTWARE
10145 * @see #LAYER_TYPE_HARDWARE
10146 */
10147 public int getLayerType() {
10148 return mLayerType;
10149 }
Joe Malin32736f02011-01-19 16:14:20 -080010150
Romain Guy6c319ca2011-01-11 14:29:25 -080010151 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010152 * Forces this view's layer to be created and this view to be rendered
10153 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10154 * invoking this method will have no effect.
10155 *
10156 * This method can for instance be used to render a view into its layer before
10157 * starting an animation. If this view is complex, rendering into the layer
10158 * before starting the animation will avoid skipping frames.
10159 *
10160 * @throws IllegalStateException If this view is not attached to a window
10161 *
10162 * @see #setLayerType(int, android.graphics.Paint)
10163 */
10164 public void buildLayer() {
10165 if (mLayerType == LAYER_TYPE_NONE) return;
10166
10167 if (mAttachInfo == null) {
10168 throw new IllegalStateException("This view must be attached to a window first");
10169 }
10170
10171 switch (mLayerType) {
10172 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080010173 if (mAttachInfo.mHardwareRenderer != null &&
10174 mAttachInfo.mHardwareRenderer.isEnabled() &&
10175 mAttachInfo.mHardwareRenderer.validate()) {
10176 getHardwareLayer();
10177 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010178 break;
10179 case LAYER_TYPE_SOFTWARE:
10180 buildDrawingCache(true);
10181 break;
10182 }
10183 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010184
10185 // Make sure the HardwareRenderer.validate() was invoked before calling this method
10186 void flushLayer() {
10187 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
10188 mHardwareLayer.flush();
10189 }
10190 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010191
10192 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010193 * <p>Returns a hardware layer that can be used to draw this view again
10194 * without executing its draw method.</p>
10195 *
10196 * @return A HardwareLayer ready to render, or null if an error occurred.
10197 */
Romain Guy5e7f7662011-01-24 22:35:56 -080010198 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010199 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10200 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010201 return null;
10202 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010203
10204 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080010205
10206 final int width = mRight - mLeft;
10207 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010208
Romain Guy6c319ca2011-01-11 14:29:25 -080010209 if (width == 0 || height == 0) {
10210 return null;
10211 }
10212
10213 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10214 if (mHardwareLayer == null) {
10215 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10216 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -080010217 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010218 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10219 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -080010220 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010221 }
10222
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010223 // The layer is not valid if the underlying GPU resources cannot be allocated
10224 if (!mHardwareLayer.isValid()) {
10225 return null;
10226 }
10227
Romain Guy59a12ca2011-06-09 17:48:21 -070010228 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -080010229 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010230
10231 // Make sure all the GPU resources have been properly allocated
10232 if (canvas == null) {
10233 mHardwareLayer.end(currentCanvas);
10234 return null;
10235 }
10236
Romain Guy5e7f7662011-01-24 22:35:56 -080010237 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010238 try {
10239 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080010240 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -080010241 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010242
Chet Haase88172fe2011-03-07 17:36:33 -080010243 final int restoreCount = canvas.save();
10244
Romain Guy6c319ca2011-01-11 14:29:25 -080010245 computeScroll();
10246 canvas.translate(-mScrollX, -mScrollY);
10247
Romain Guy6c319ca2011-01-11 14:29:25 -080010248 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -080010249
Romain Guy6c319ca2011-01-11 14:29:25 -080010250 // Fast path for layouts with no backgrounds
10251 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10252 mPrivateFlags &= ~DIRTY_MASK;
10253 dispatchDraw(canvas);
10254 } else {
10255 draw(canvas);
10256 }
Joe Malin32736f02011-01-19 16:14:20 -080010257
Chet Haase88172fe2011-03-07 17:36:33 -080010258 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -080010259 } finally {
10260 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -080010261 mHardwareLayer.end(currentCanvas);
10262 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010263 }
10264 }
10265
10266 return mHardwareLayer;
10267 }
Romain Guy171c5922011-01-06 10:04:23 -080010268
Romain Guy589b0bb2011-10-10 13:57:47 -070010269 /**
10270 * Destroys this View's hardware layer if possible.
10271 *
10272 * @return True if the layer was destroyed, false otherwise.
10273 *
10274 * @see #setLayerType(int, android.graphics.Paint)
10275 * @see #LAYER_TYPE_HARDWARE
10276 */
Romain Guy65b345f2011-07-27 18:51:50 -070010277 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -070010278 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010279 AttachInfo info = mAttachInfo;
10280 if (info != null && info.mHardwareRenderer != null &&
10281 info.mHardwareRenderer.isEnabled() && info.mHardwareRenderer.validate()) {
10282 mHardwareLayer.destroy();
10283 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080010284
Romain Guy9c4b79a2011-11-10 19:23:58 -080010285 invalidate(true);
10286 invalidateParentCaches();
10287 }
Romain Guy65b345f2011-07-27 18:51:50 -070010288 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010289 }
Romain Guy65b345f2011-07-27 18:51:50 -070010290 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010291 }
10292
Romain Guy171c5922011-01-06 10:04:23 -080010293 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080010294 * Destroys all hardware rendering resources. This method is invoked
10295 * when the system needs to reclaim resources. Upon execution of this
10296 * method, you should free any OpenGL resources created by the view.
10297 *
10298 * Note: you <strong>must</strong> call
10299 * <code>super.destroyHardwareResources()</code> when overriding
10300 * this method.
10301 *
10302 * @hide
10303 */
10304 protected void destroyHardwareResources() {
10305 destroyLayer();
10306 }
10307
10308 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010309 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10310 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10311 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10312 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10313 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10314 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010315 *
Romain Guy171c5922011-01-06 10:04:23 -080010316 * <p>Enabling the drawing cache is similar to
10317 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010318 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10319 * drawing cache has no effect on rendering because the system uses a different mechanism
10320 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10321 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10322 * for information on how to enable software and hardware layers.</p>
10323 *
10324 * <p>This API can be used to manually generate
10325 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10326 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010327 *
10328 * @param enabled true to enable the drawing cache, false otherwise
10329 *
10330 * @see #isDrawingCacheEnabled()
10331 * @see #getDrawingCache()
10332 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010333 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010334 */
10335 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010336 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010337 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10338 }
10339
10340 /**
10341 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10342 *
10343 * @return true if the drawing cache is enabled
10344 *
10345 * @see #setDrawingCacheEnabled(boolean)
10346 * @see #getDrawingCache()
10347 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010348 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010349 public boolean isDrawingCacheEnabled() {
10350 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10351 }
10352
10353 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010354 * Debugging utility which recursively outputs the dirty state of a view and its
10355 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010356 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010357 * @hide
10358 */
Romain Guy676b1732011-02-14 14:45:33 -080010359 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010360 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10361 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10362 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10363 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10364 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10365 if (clear) {
10366 mPrivateFlags &= clearMask;
10367 }
10368 if (this instanceof ViewGroup) {
10369 ViewGroup parent = (ViewGroup) this;
10370 final int count = parent.getChildCount();
10371 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010372 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010373 child.outputDirtyFlags(indent + " ", clear, clearMask);
10374 }
10375 }
10376 }
10377
10378 /**
10379 * This method is used by ViewGroup to cause its children to restore or recreate their
10380 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10381 * to recreate its own display list, which would happen if it went through the normal
10382 * draw/dispatchDraw mechanisms.
10383 *
10384 * @hide
10385 */
10386 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010387
10388 /**
10389 * A view that is not attached or hardware accelerated cannot create a display list.
10390 * This method checks these conditions and returns the appropriate result.
10391 *
10392 * @return true if view has the ability to create a display list, false otherwise.
10393 *
10394 * @hide
10395 */
10396 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010397 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010398 }
Joe Malin32736f02011-01-19 16:14:20 -080010399
Chet Haasedaf98e92011-01-10 14:10:36 -080010400 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080010401 * @return The HardwareRenderer associated with that view or null if hardware rendering
10402 * is not supported or this this has not been attached to a window.
10403 *
10404 * @hide
10405 */
10406 public HardwareRenderer getHardwareRenderer() {
10407 if (mAttachInfo != null) {
10408 return mAttachInfo.mHardwareRenderer;
10409 }
10410 return null;
10411 }
10412
10413 /**
Romain Guyb051e892010-09-28 19:09:36 -070010414 * <p>Returns a display list that can be used to draw this view again
10415 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010416 *
Romain Guyb051e892010-09-28 19:09:36 -070010417 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010418 *
10419 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010420 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010421 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -080010422 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -070010423 return null;
10424 }
10425
Chet Haasedaf98e92011-01-10 14:10:36 -080010426 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10427 mDisplayList == null || !mDisplayList.isValid() ||
10428 mRecreateDisplayList)) {
10429 // Don't need to recreate the display list, just need to tell our
10430 // children to restore/recreate theirs
10431 if (mDisplayList != null && mDisplayList.isValid() &&
10432 !mRecreateDisplayList) {
10433 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10434 mPrivateFlags &= ~DIRTY_MASK;
10435 dispatchGetDisplayList();
10436
10437 return mDisplayList;
10438 }
10439
10440 // If we got here, we're recreating it. Mark it as such to ensure that
10441 // we copy in child display lists into ours in drawChild()
10442 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -080010443 if (mDisplayList == null) {
Romain Guy13631f32012-01-30 17:41:55 -080010444 final String name = getClass().getSimpleName();
10445 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
Chet Haasedaf98e92011-01-10 14:10:36 -080010446 // If we're creating a new display list, make sure our parent gets invalidated
10447 // since they will need to recreate their display list to account for this
10448 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -080010449 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -080010450 }
Romain Guyb051e892010-09-28 19:09:36 -070010451
10452 final HardwareCanvas canvas = mDisplayList.start();
Romain Guye080af32011-09-08 15:03:39 -070010453 int restoreCount = 0;
Romain Guyb051e892010-09-28 19:09:36 -070010454 try {
10455 int width = mRight - mLeft;
10456 int height = mBottom - mTop;
10457
10458 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -080010459 // The dirty rect should always be null for a display list
10460 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -070010461
Chet Haasedaf98e92011-01-10 14:10:36 -080010462 computeScroll();
Romain Guye080af32011-09-08 15:03:39 -070010463
10464 restoreCount = canvas.save();
Chet Haasedaf98e92011-01-10 14:10:36 -080010465 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010466 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -070010467 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -080010468
Romain Guyb051e892010-09-28 19:09:36 -070010469 // Fast path for layouts with no backgrounds
10470 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -070010471 dispatchDraw(canvas);
10472 } else {
10473 draw(canvas);
10474 }
Romain Guyb051e892010-09-28 19:09:36 -070010475 } finally {
Romain Guye080af32011-09-08 15:03:39 -070010476 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -070010477 canvas.onPostDraw();
10478
10479 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -070010480 }
Chet Haase77785f92011-01-25 23:22:09 -080010481 } else {
10482 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10483 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -070010484 }
10485
10486 return mDisplayList;
10487 }
10488
10489 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010490 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010491 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010492 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010493 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010494 * @see #getDrawingCache(boolean)
10495 */
10496 public Bitmap getDrawingCache() {
10497 return getDrawingCache(false);
10498 }
10499
10500 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010501 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10502 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10503 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10504 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10505 * request the drawing cache by calling this method and draw it on screen if the
10506 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010507 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010508 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10509 * this method will create a bitmap of the same size as this view. Because this bitmap
10510 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10511 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10512 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10513 * size than the view. This implies that your application must be able to handle this
10514 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010515 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010516 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10517 * the current density of the screen when the application is in compatibility
10518 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010519 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010520 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010521 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010522 * @see #setDrawingCacheEnabled(boolean)
10523 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010524 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010525 * @see #destroyDrawingCache()
10526 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010527 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010528 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10529 return null;
10530 }
10531 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010532 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010533 }
Romain Guy02890fd2010-08-06 17:58:44 -070010534 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010535 }
10536
10537 /**
10538 * <p>Frees the resources used by the drawing cache. If you call
10539 * {@link #buildDrawingCache()} manually without calling
10540 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10541 * should cleanup the cache with this method afterwards.</p>
10542 *
10543 * @see #setDrawingCacheEnabled(boolean)
10544 * @see #buildDrawingCache()
10545 * @see #getDrawingCache()
10546 */
10547 public void destroyDrawingCache() {
10548 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010549 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010550 mDrawingCache = null;
10551 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010552 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010553 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010554 mUnscaledDrawingCache = null;
10555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010556 }
10557
10558 /**
10559 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070010560 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010561 * view will always be drawn on top of a solid color.
10562 *
10563 * @param color The background color to use for the drawing cache's bitmap
10564 *
10565 * @see #setDrawingCacheEnabled(boolean)
10566 * @see #buildDrawingCache()
10567 * @see #getDrawingCache()
10568 */
10569 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010570 if (color != mDrawingCacheBackgroundColor) {
10571 mDrawingCacheBackgroundColor = color;
10572 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10573 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010574 }
10575
10576 /**
10577 * @see #setDrawingCacheBackgroundColor(int)
10578 *
10579 * @return The background color to used for the drawing cache's bitmap
10580 */
10581 public int getDrawingCacheBackgroundColor() {
10582 return mDrawingCacheBackgroundColor;
10583 }
10584
10585 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010586 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010587 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010588 * @see #buildDrawingCache(boolean)
10589 */
10590 public void buildDrawingCache() {
10591 buildDrawingCache(false);
10592 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010593
Romain Guyfbd8f692009-06-26 14:51:58 -070010594 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010595 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10596 *
10597 * <p>If you call {@link #buildDrawingCache()} manually without calling
10598 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10599 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010600 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010601 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10602 * this method will create a bitmap of the same size as this view. Because this bitmap
10603 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10604 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10605 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10606 * size than the view. This implies that your application must be able to handle this
10607 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010608 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010609 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10610 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010611 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010612 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010613 *
10614 * @see #getDrawingCache()
10615 * @see #destroyDrawingCache()
10616 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010617 public void buildDrawingCache(boolean autoScale) {
10618 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010619 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010620 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010621
10622 if (ViewDebug.TRACE_HIERARCHY) {
10623 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10624 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010625
Romain Guy8506ab42009-06-11 17:35:47 -070010626 int width = mRight - mLeft;
10627 int height = mBottom - mTop;
10628
10629 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010630 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010631
Romain Guye1123222009-06-29 14:24:56 -070010632 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010633 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10634 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010636
10637 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010638 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010639 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010640
10641 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010642 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010643 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010644 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10645 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010646 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010647 return;
10648 }
10649
10650 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010651 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010652
10653 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010654 Bitmap.Config quality;
10655 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010656 // Never pick ARGB_4444 because it looks awful
10657 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010658 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10659 case DRAWING_CACHE_QUALITY_AUTO:
10660 quality = Bitmap.Config.ARGB_8888;
10661 break;
10662 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010663 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010664 break;
10665 case DRAWING_CACHE_QUALITY_HIGH:
10666 quality = Bitmap.Config.ARGB_8888;
10667 break;
10668 default:
10669 quality = Bitmap.Config.ARGB_8888;
10670 break;
10671 }
10672 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010673 // Optimization for translucent windows
10674 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010675 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010676 }
10677
10678 // Try to cleanup memory
10679 if (bitmap != null) bitmap.recycle();
10680
10681 try {
10682 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010683 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010684 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010685 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010686 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010687 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010688 }
Adam Powell26153a32010-11-08 15:22:27 -080010689 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010690 } catch (OutOfMemoryError e) {
10691 // If there is not enough memory to create the bitmap cache, just
10692 // ignore the issue as bitmap caches are not required to draw the
10693 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010694 if (autoScale) {
10695 mDrawingCache = null;
10696 } else {
10697 mUnscaledDrawingCache = null;
10698 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010699 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010700 return;
10701 }
10702
10703 clear = drawingCacheBackgroundColor != 0;
10704 }
10705
10706 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010707 if (attachInfo != null) {
10708 canvas = attachInfo.mCanvas;
10709 if (canvas == null) {
10710 canvas = new Canvas();
10711 }
10712 canvas.setBitmap(bitmap);
10713 // Temporarily clobber the cached Canvas in case one of our children
10714 // is also using a drawing cache. Without this, the children would
10715 // steal the canvas by attaching their own bitmap to it and bad, bad
10716 // thing would happen (invisible views, corrupted drawings, etc.)
10717 attachInfo.mCanvas = null;
10718 } else {
10719 // This case should hopefully never or seldom happen
10720 canvas = new Canvas(bitmap);
10721 }
10722
10723 if (clear) {
10724 bitmap.eraseColor(drawingCacheBackgroundColor);
10725 }
10726
10727 computeScroll();
10728 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010729
Romain Guye1123222009-06-29 14:24:56 -070010730 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010731 final float scale = attachInfo.mApplicationScale;
10732 canvas.scale(scale, scale);
10733 }
Joe Malin32736f02011-01-19 16:14:20 -080010734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010735 canvas.translate(-mScrollX, -mScrollY);
10736
Romain Guy5bcdff42009-05-14 21:27:18 -070010737 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010738 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10739 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010740 mPrivateFlags |= DRAWING_CACHE_VALID;
10741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010742
10743 // Fast path for layouts with no backgrounds
10744 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10745 if (ViewDebug.TRACE_HIERARCHY) {
10746 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10747 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010748 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010749 dispatchDraw(canvas);
10750 } else {
10751 draw(canvas);
10752 }
10753
10754 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010755 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010756
10757 if (attachInfo != null) {
10758 // Restore the cached Canvas for our siblings
10759 attachInfo.mCanvas = canvas;
10760 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010761 }
10762 }
10763
10764 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010765 * Create a snapshot of the view into a bitmap. We should probably make
10766 * some form of this public, but should think about the API.
10767 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010768 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010769 int width = mRight - mLeft;
10770 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010771
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010772 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010773 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010774 width = (int) ((width * scale) + 0.5f);
10775 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010776
Romain Guy8c11e312009-09-14 15:15:30 -070010777 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010778 if (bitmap == null) {
10779 throw new OutOfMemoryError();
10780 }
10781
Romain Guyc529d8d2011-09-06 15:01:39 -070010782 Resources resources = getResources();
10783 if (resources != null) {
10784 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
10785 }
Joe Malin32736f02011-01-19 16:14:20 -080010786
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010787 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010788 if (attachInfo != null) {
10789 canvas = attachInfo.mCanvas;
10790 if (canvas == null) {
10791 canvas = new Canvas();
10792 }
10793 canvas.setBitmap(bitmap);
10794 // Temporarily clobber the cached Canvas in case one of our children
10795 // is also using a drawing cache. Without this, the children would
10796 // steal the canvas by attaching their own bitmap to it and bad, bad
10797 // things would happen (invisible views, corrupted drawings, etc.)
10798 attachInfo.mCanvas = null;
10799 } else {
10800 // This case should hopefully never or seldom happen
10801 canvas = new Canvas(bitmap);
10802 }
10803
Romain Guy5bcdff42009-05-14 21:27:18 -070010804 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010805 bitmap.eraseColor(backgroundColor);
10806 }
10807
10808 computeScroll();
10809 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010810 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010811 canvas.translate(-mScrollX, -mScrollY);
10812
Romain Guy5bcdff42009-05-14 21:27:18 -070010813 // Temporarily remove the dirty mask
10814 int flags = mPrivateFlags;
10815 mPrivateFlags &= ~DIRTY_MASK;
10816
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010817 // Fast path for layouts with no backgrounds
10818 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10819 dispatchDraw(canvas);
10820 } else {
10821 draw(canvas);
10822 }
10823
Romain Guy5bcdff42009-05-14 21:27:18 -070010824 mPrivateFlags = flags;
10825
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010826 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010827 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010828
10829 if (attachInfo != null) {
10830 // Restore the cached Canvas for our siblings
10831 attachInfo.mCanvas = canvas;
10832 }
Romain Guy8506ab42009-06-11 17:35:47 -070010833
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010834 return bitmap;
10835 }
10836
10837 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010838 * Indicates whether this View is currently in edit mode. A View is usually
10839 * in edit mode when displayed within a developer tool. For instance, if
10840 * this View is being drawn by a visual user interface builder, this method
10841 * should return true.
10842 *
10843 * Subclasses should check the return value of this method to provide
10844 * different behaviors if their normal behavior might interfere with the
10845 * host environment. For instance: the class spawns a thread in its
10846 * constructor, the drawing code relies on device-specific features, etc.
10847 *
10848 * This method is usually checked in the drawing code of custom widgets.
10849 *
10850 * @return True if this View is in edit mode, false otherwise.
10851 */
10852 public boolean isInEditMode() {
10853 return false;
10854 }
10855
10856 /**
10857 * If the View draws content inside its padding and enables fading edges,
10858 * it needs to support padding offsets. Padding offsets are added to the
10859 * fading edges to extend the length of the fade so that it covers pixels
10860 * drawn inside the padding.
10861 *
10862 * Subclasses of this class should override this method if they need
10863 * to draw content inside the padding.
10864 *
10865 * @return True if padding offset must be applied, false otherwise.
10866 *
10867 * @see #getLeftPaddingOffset()
10868 * @see #getRightPaddingOffset()
10869 * @see #getTopPaddingOffset()
10870 * @see #getBottomPaddingOffset()
10871 *
10872 * @since CURRENT
10873 */
10874 protected boolean isPaddingOffsetRequired() {
10875 return false;
10876 }
10877
10878 /**
10879 * Amount by which to extend the left fading region. Called only when
10880 * {@link #isPaddingOffsetRequired()} returns true.
10881 *
10882 * @return The left padding offset in pixels.
10883 *
10884 * @see #isPaddingOffsetRequired()
10885 *
10886 * @since CURRENT
10887 */
10888 protected int getLeftPaddingOffset() {
10889 return 0;
10890 }
10891
10892 /**
10893 * Amount by which to extend the right fading region. Called only when
10894 * {@link #isPaddingOffsetRequired()} returns true.
10895 *
10896 * @return The right padding offset in pixels.
10897 *
10898 * @see #isPaddingOffsetRequired()
10899 *
10900 * @since CURRENT
10901 */
10902 protected int getRightPaddingOffset() {
10903 return 0;
10904 }
10905
10906 /**
10907 * Amount by which to extend the top fading region. Called only when
10908 * {@link #isPaddingOffsetRequired()} returns true.
10909 *
10910 * @return The top padding offset in pixels.
10911 *
10912 * @see #isPaddingOffsetRequired()
10913 *
10914 * @since CURRENT
10915 */
10916 protected int getTopPaddingOffset() {
10917 return 0;
10918 }
10919
10920 /**
10921 * Amount by which to extend the bottom fading region. Called only when
10922 * {@link #isPaddingOffsetRequired()} returns true.
10923 *
10924 * @return The bottom padding offset in pixels.
10925 *
10926 * @see #isPaddingOffsetRequired()
10927 *
10928 * @since CURRENT
10929 */
10930 protected int getBottomPaddingOffset() {
10931 return 0;
10932 }
10933
10934 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070010935 * @hide
10936 * @param offsetRequired
10937 */
10938 protected int getFadeTop(boolean offsetRequired) {
10939 int top = mPaddingTop;
10940 if (offsetRequired) top += getTopPaddingOffset();
10941 return top;
10942 }
10943
10944 /**
10945 * @hide
10946 * @param offsetRequired
10947 */
10948 protected int getFadeHeight(boolean offsetRequired) {
10949 int padding = mPaddingTop;
10950 if (offsetRequired) padding += getTopPaddingOffset();
10951 return mBottom - mTop - mPaddingBottom - padding;
10952 }
10953
10954 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010955 * <p>Indicates whether this view is attached to an hardware accelerated
10956 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010957 *
Romain Guy2bffd262010-09-12 17:40:02 -070010958 * <p>Even if this method returns true, it does not mean that every call
10959 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10960 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10961 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10962 * window is hardware accelerated,
10963 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10964 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010965 *
Romain Guy2bffd262010-09-12 17:40:02 -070010966 * @return True if the view is attached to a window and the window is
10967 * hardware accelerated; false in any other case.
10968 */
10969 public boolean isHardwareAccelerated() {
10970 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10971 }
Joe Malin32736f02011-01-19 16:14:20 -080010972
Romain Guy2bffd262010-09-12 17:40:02 -070010973 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080010974 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
10975 * case of an active Animation being run on the view.
10976 */
10977 private boolean drawAnimation(ViewGroup parent, long drawingTime,
10978 Animation a, boolean scalingRequired) {
10979 Transformation invalidationTransform;
10980 final int flags = parent.mGroupFlags;
10981 final boolean initialized = a.isInitialized();
10982 if (!initialized) {
10983 a.initialize(mRight - mLeft, mBottom - mTop, getWidth(), getHeight());
10984 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
10985 onAnimationStart();
10986 }
10987
10988 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
10989 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
10990 if (parent.mInvalidationTransformation == null) {
10991 parent.mInvalidationTransformation = new Transformation();
10992 }
10993 invalidationTransform = parent.mInvalidationTransformation;
10994 a.getTransformation(drawingTime, invalidationTransform, 1f);
10995 } else {
10996 invalidationTransform = parent.mChildTransformation;
10997 }
10998 if (more) {
10999 if (!a.willChangeBounds()) {
11000 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
11001 parent.FLAG_OPTIMIZE_INVALIDATE) {
11002 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
11003 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
11004 // The child need to draw an animation, potentially offscreen, so
11005 // make sure we do not cancel invalidate requests
11006 parent.mPrivateFlags |= DRAW_ANIMATION;
11007 parent.invalidate(mLeft, mTop, mRight, mBottom);
11008 }
11009 } else {
11010 if (parent.mInvalidateRegion == null) {
11011 parent.mInvalidateRegion = new RectF();
11012 }
11013 final RectF region = parent.mInvalidateRegion;
11014 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
11015 invalidationTransform);
11016
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
11021 final int left = mLeft + (int) region.left;
11022 final int top = mTop + (int) region.top;
11023 parent.invalidate(left, top, left + (int) (region.width() + .5f),
11024 top + (int) (region.height() + .5f));
11025 }
11026 }
11027 return more;
11028 }
11029
11030 /**
Chet Haase64a48c12012-02-13 16:33:29 -080011031 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
11032 * This draw() method is an implementation detail and is not intended to be overridden or
11033 * to be called from anywhere else other than ViewGroup.drawChild().
11034 */
11035 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
11036 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080011037 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080011038 final int flags = parent.mGroupFlags;
11039
11040 if ((flags & parent.FLAG_CLEAR_TRANSFORMATION) == parent.FLAG_CLEAR_TRANSFORMATION) {
11041 parent.mChildTransformation.clear();
11042 parent.mGroupFlags &= ~parent.FLAG_CLEAR_TRANSFORMATION;
11043 }
11044
11045 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080011046 boolean concatMatrix = false;
11047
11048 boolean scalingRequired = false;
11049 boolean caching;
11050 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
11051
11052 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
11053 if ((flags & parent.FLAG_CHILDREN_DRAWN_WITH_CACHE) == parent.FLAG_CHILDREN_DRAWN_WITH_CACHE ||
11054 (flags & parent.FLAG_ALWAYS_DRAWN_WITH_CACHE) == parent.FLAG_ALWAYS_DRAWN_WITH_CACHE) {
11055 caching = true;
11056 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
11057 } else {
11058 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
11059 }
11060
Chet Haasebcca79a2012-02-14 08:45:14 -080011061 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080011062 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011063 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080011064 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080011065 transformToApply = parent.mChildTransformation;
Chet Haase64a48c12012-02-13 16:33:29 -080011066 } else if ((flags & parent.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) ==
11067 parent.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011068 final boolean hasTransform =
11069 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080011070 if (hasTransform) {
11071 final int transformType = parent.mChildTransformation.getTransformationType();
11072 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
11073 parent.mChildTransformation : null;
11074 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
11075 }
11076 }
11077
11078 concatMatrix |= !childHasIdentityMatrix;
11079
11080 // Sets the flag as early as possible to allow draw() implementations
11081 // to call invalidate() successfully when doing animations
11082 mPrivateFlags |= DRAWN;
11083
Chet Haasebcca79a2012-02-14 08:45:14 -080011084 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080011085 (mPrivateFlags & DRAW_ANIMATION) == 0) {
11086 return more;
11087 }
11088
11089 if (hardwareAccelerated) {
11090 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
11091 // retain the flag's value temporarily in the mRecreateDisplayList flag
11092 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
11093 mPrivateFlags &= ~INVALIDATED;
11094 }
11095
11096 computeScroll();
11097
11098 final int sx = mScrollX;
11099 final int sy = mScrollY;
11100
11101 DisplayList displayList = null;
11102 Bitmap cache = null;
11103 boolean hasDisplayList = false;
11104 if (caching) {
11105 if (!hardwareAccelerated) {
11106 if (layerType != LAYER_TYPE_NONE) {
11107 layerType = LAYER_TYPE_SOFTWARE;
11108 buildDrawingCache(true);
11109 }
11110 cache = getDrawingCache(true);
11111 } else {
11112 switch (layerType) {
11113 case LAYER_TYPE_SOFTWARE:
11114 buildDrawingCache(true);
11115 cache = getDrawingCache(true);
11116 break;
11117 case LAYER_TYPE_NONE:
11118 // Delay getting the display list until animation-driven alpha values are
11119 // set up and possibly passed on to the view
11120 hasDisplayList = canHaveDisplayList();
11121 break;
11122 }
11123 }
11124 }
11125
11126 final boolean hasNoCache = cache == null || hasDisplayList;
11127 final boolean offsetForScroll = cache == null && !hasDisplayList &&
11128 layerType != LAYER_TYPE_HARDWARE;
11129
11130 final int restoreTo = canvas.save();
11131 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011132 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080011133 } else {
Chet Haasebcca79a2012-02-14 08:45:14 -080011134 canvas.translate(mLeft, mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080011135 if (scalingRequired) {
11136 // mAttachInfo cannot be null, otherwise scalingRequired == false
11137 final float scale = 1.0f / mAttachInfo.mApplicationScale;
11138 canvas.scale(scale, scale);
11139 }
11140 }
11141
11142 float alpha = getAlpha();
11143 if (transformToApply != null || alpha < 1.0f || !hasIdentityMatrix()) {
11144 if (transformToApply != null || !childHasIdentityMatrix) {
11145 int transX = 0;
11146 int transY = 0;
11147
11148 if (offsetForScroll) {
11149 transX = -sx;
11150 transY = -sy;
11151 }
11152
11153 if (transformToApply != null) {
11154 if (concatMatrix) {
11155 // Undo the scroll translation, apply the transformation matrix,
11156 // then redo the scroll translate to get the correct result.
11157 canvas.translate(-transX, -transY);
11158 canvas.concat(transformToApply.getMatrix());
11159 canvas.translate(transX, transY);
11160 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11161 }
11162
11163 float transformAlpha = transformToApply.getAlpha();
11164 if (transformAlpha < 1.0f) {
11165 alpha *= transformToApply.getAlpha();
11166 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11167 }
11168 }
11169
11170 if (!childHasIdentityMatrix) {
11171 canvas.translate(-transX, -transY);
11172 canvas.concat(getMatrix());
11173 canvas.translate(transX, transY);
11174 }
11175 }
11176
11177 if (alpha < 1.0f) {
11178 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11179 if (hasNoCache) {
11180 final int multipliedAlpha = (int) (255 * alpha);
11181 if (!onSetAlpha(multipliedAlpha)) {
11182 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11183 if ((flags & parent.FLAG_CLIP_CHILDREN) == parent.FLAG_CLIP_CHILDREN ||
11184 layerType != LAYER_TYPE_NONE) {
11185 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
11186 }
11187 if (layerType == LAYER_TYPE_NONE) {
11188 final int scrollX = hasDisplayList ? 0 : sx;
11189 final int scrollY = hasDisplayList ? 0 : sy;
Chet Haasebcca79a2012-02-14 08:45:14 -080011190 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
11191 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080011192 }
11193 } else {
11194 // Alpha is handled by the child directly, clobber the layer's alpha
11195 mPrivateFlags |= ALPHA_SET;
11196 }
11197 }
11198 }
11199 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11200 onSetAlpha(255);
11201 mPrivateFlags &= ~ALPHA_SET;
11202 }
11203
11204 if ((flags & parent.FLAG_CLIP_CHILDREN) == parent.FLAG_CLIP_CHILDREN) {
11205 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011206 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080011207 } else {
11208 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011209 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080011210 } else {
11211 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
11212 }
11213 }
11214 }
11215
11216 if (hasDisplayList) {
11217 displayList = getDisplayList();
11218 if (!displayList.isValid()) {
11219 // Uncommon, but possible. If a view is removed from the hierarchy during the call
11220 // to getDisplayList(), the display list will be marked invalid and we should not
11221 // try to use it again.
11222 displayList = null;
11223 hasDisplayList = false;
11224 }
11225 }
11226
11227 if (hasNoCache) {
11228 boolean layerRendered = false;
11229 if (layerType == LAYER_TYPE_HARDWARE) {
11230 final HardwareLayer layer = getHardwareLayer();
11231 if (layer != null && layer.isValid()) {
11232 mLayerPaint.setAlpha((int) (alpha * 255));
11233 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
11234 layerRendered = true;
11235 } else {
11236 final int scrollX = hasDisplayList ? 0 : sx;
11237 final int scrollY = hasDisplayList ? 0 : sy;
11238 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080011239 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080011240 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11241 }
11242 }
11243
11244 if (!layerRendered) {
11245 if (!hasDisplayList) {
11246 // Fast path for layouts with no backgrounds
11247 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11248 if (ViewDebug.TRACE_HIERARCHY) {
11249 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
11250 }
11251 mPrivateFlags &= ~DIRTY_MASK;
11252 dispatchDraw(canvas);
11253 } else {
11254 draw(canvas);
11255 }
11256 } else {
11257 mPrivateFlags &= ~DIRTY_MASK;
Chet Haasebcca79a2012-02-14 08:45:14 -080011258 ((HardwareCanvas) canvas).drawDisplayList(displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -080011259 mRight - mLeft, mBottom - mTop, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080011260 }
11261 }
11262 } else if (cache != null) {
11263 mPrivateFlags &= ~DIRTY_MASK;
11264 Paint cachePaint;
11265
11266 if (layerType == LAYER_TYPE_NONE) {
11267 cachePaint = parent.mCachePaint;
11268 if (cachePaint == null) {
11269 cachePaint = new Paint();
11270 cachePaint.setDither(false);
11271 parent.mCachePaint = cachePaint;
11272 }
11273 if (alpha < 1.0f) {
11274 cachePaint.setAlpha((int) (alpha * 255));
11275 parent.mGroupFlags |= parent.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haasebcca79a2012-02-14 08:45:14 -080011276 } else if ((flags & parent.FLAG_ALPHA_LOWER_THAN_ONE) ==
11277 parent.FLAG_ALPHA_LOWER_THAN_ONE) {
Chet Haase64a48c12012-02-13 16:33:29 -080011278 cachePaint.setAlpha(255);
11279 parent.mGroupFlags &= ~parent.FLAG_ALPHA_LOWER_THAN_ONE;
11280 }
11281 } else {
11282 cachePaint = mLayerPaint;
11283 cachePaint.setAlpha((int) (alpha * 255));
11284 }
11285 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
11286 }
11287
11288 canvas.restoreToCount(restoreTo);
11289
11290 if (a != null && !more) {
11291 if (!hardwareAccelerated && !a.getFillAfter()) {
11292 onSetAlpha(255);
11293 }
11294 parent.finishAnimatingView(this, a);
11295 }
11296
11297 if (more && hardwareAccelerated) {
11298 // invalidation is the trigger to recreate display lists, so if we're using
11299 // display lists to render, force an invalidate to allow the animation to
11300 // continue drawing another frame
11301 parent.invalidate(true);
11302 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11303 // alpha animations should cause the child to recreate its display list
11304 invalidate(true);
11305 }
11306 }
11307
11308 mRecreateDisplayList = false;
11309
11310 return more;
11311 }
11312
11313 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011314 * Manually render this view (and all of its children) to the given Canvas.
11315 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011316 * called. When implementing a view, implement
11317 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
11318 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011319 *
11320 * @param canvas The Canvas to which the View is rendered.
11321 */
11322 public void draw(Canvas canvas) {
11323 if (ViewDebug.TRACE_HIERARCHY) {
11324 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
11325 }
11326
Romain Guy5bcdff42009-05-14 21:27:18 -070011327 final int privateFlags = mPrivateFlags;
11328 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
11329 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
11330 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070011331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011332 /*
11333 * Draw traversal performs several drawing steps which must be executed
11334 * in the appropriate order:
11335 *
11336 * 1. Draw the background
11337 * 2. If necessary, save the canvas' layers to prepare for fading
11338 * 3. Draw view's content
11339 * 4. Draw children
11340 * 5. If necessary, draw the fading edges and restore layers
11341 * 6. Draw decorations (scrollbars for instance)
11342 */
11343
11344 // Step 1, draw the background, if needed
11345 int saveCount;
11346
Romain Guy24443ea2009-05-11 11:56:30 -070011347 if (!dirtyOpaque) {
11348 final Drawable background = mBGDrawable;
11349 if (background != null) {
11350 final int scrollX = mScrollX;
11351 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011352
Romain Guy24443ea2009-05-11 11:56:30 -070011353 if (mBackgroundSizeChanged) {
11354 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
11355 mBackgroundSizeChanged = false;
11356 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011357
Romain Guy24443ea2009-05-11 11:56:30 -070011358 if ((scrollX | scrollY) == 0) {
11359 background.draw(canvas);
11360 } else {
11361 canvas.translate(scrollX, scrollY);
11362 background.draw(canvas);
11363 canvas.translate(-scrollX, -scrollY);
11364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011365 }
11366 }
11367
11368 // skip step 2 & 5 if possible (common case)
11369 final int viewFlags = mViewFlags;
11370 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
11371 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
11372 if (!verticalEdges && !horizontalEdges) {
11373 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011374 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011375
11376 // Step 4, draw the children
11377 dispatchDraw(canvas);
11378
11379 // Step 6, draw decorations (scrollbars)
11380 onDrawScrollBars(canvas);
11381
11382 // we're done...
11383 return;
11384 }
11385
11386 /*
11387 * Here we do the full fledged routine...
11388 * (this is an uncommon case where speed matters less,
11389 * this is why we repeat some of the tests that have been
11390 * done above)
11391 */
11392
11393 boolean drawTop = false;
11394 boolean drawBottom = false;
11395 boolean drawLeft = false;
11396 boolean drawRight = false;
11397
11398 float topFadeStrength = 0.0f;
11399 float bottomFadeStrength = 0.0f;
11400 float leftFadeStrength = 0.0f;
11401 float rightFadeStrength = 0.0f;
11402
11403 // Step 2, save the canvas' layers
11404 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011405
11406 final boolean offsetRequired = isPaddingOffsetRequired();
11407 if (offsetRequired) {
11408 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011409 }
11410
11411 int left = mScrollX + paddingLeft;
11412 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070011413 int top = mScrollY + getFadeTop(offsetRequired);
11414 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011415
11416 if (offsetRequired) {
11417 right += getRightPaddingOffset();
11418 bottom += getBottomPaddingOffset();
11419 }
11420
11421 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011422 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
11423 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011424
11425 // clip the fade length if top and bottom fades overlap
11426 // overlapping fades produce odd-looking artifacts
11427 if (verticalEdges && (top + length > bottom - length)) {
11428 length = (bottom - top) / 2;
11429 }
11430
11431 // also clip horizontal fades if necessary
11432 if (horizontalEdges && (left + length > right - length)) {
11433 length = (right - left) / 2;
11434 }
11435
11436 if (verticalEdges) {
11437 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011438 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011439 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011440 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011441 }
11442
11443 if (horizontalEdges) {
11444 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011445 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011446 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011447 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011448 }
11449
11450 saveCount = canvas.getSaveCount();
11451
11452 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070011453 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011454 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11455
11456 if (drawTop) {
11457 canvas.saveLayer(left, top, right, top + length, null, flags);
11458 }
11459
11460 if (drawBottom) {
11461 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
11462 }
11463
11464 if (drawLeft) {
11465 canvas.saveLayer(left, top, left + length, bottom, null, flags);
11466 }
11467
11468 if (drawRight) {
11469 canvas.saveLayer(right - length, top, right, bottom, null, flags);
11470 }
11471 } else {
11472 scrollabilityCache.setFadeColor(solidColor);
11473 }
11474
11475 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011476 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011477
11478 // Step 4, draw the children
11479 dispatchDraw(canvas);
11480
11481 // Step 5, draw the fade effect and restore layers
11482 final Paint p = scrollabilityCache.paint;
11483 final Matrix matrix = scrollabilityCache.matrix;
11484 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011485
11486 if (drawTop) {
11487 matrix.setScale(1, fadeHeight * topFadeStrength);
11488 matrix.postTranslate(left, top);
11489 fade.setLocalMatrix(matrix);
11490 canvas.drawRect(left, top, right, top + length, p);
11491 }
11492
11493 if (drawBottom) {
11494 matrix.setScale(1, fadeHeight * bottomFadeStrength);
11495 matrix.postRotate(180);
11496 matrix.postTranslate(left, bottom);
11497 fade.setLocalMatrix(matrix);
11498 canvas.drawRect(left, bottom - length, right, bottom, p);
11499 }
11500
11501 if (drawLeft) {
11502 matrix.setScale(1, fadeHeight * leftFadeStrength);
11503 matrix.postRotate(-90);
11504 matrix.postTranslate(left, top);
11505 fade.setLocalMatrix(matrix);
11506 canvas.drawRect(left, top, left + length, bottom, p);
11507 }
11508
11509 if (drawRight) {
11510 matrix.setScale(1, fadeHeight * rightFadeStrength);
11511 matrix.postRotate(90);
11512 matrix.postTranslate(right, top);
11513 fade.setLocalMatrix(matrix);
11514 canvas.drawRect(right - length, top, right, bottom, p);
11515 }
11516
11517 canvas.restoreToCount(saveCount);
11518
11519 // Step 6, draw decorations (scrollbars)
11520 onDrawScrollBars(canvas);
11521 }
11522
11523 /**
11524 * Override this if your view is known to always be drawn on top of a solid color background,
11525 * and needs to draw fading edges. Returning a non-zero color enables the view system to
11526 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
11527 * should be set to 0xFF.
11528 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011529 * @see #setVerticalFadingEdgeEnabled(boolean)
11530 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011531 *
11532 * @return The known solid color background for this view, or 0 if the color may vary
11533 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011534 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011535 public int getSolidColor() {
11536 return 0;
11537 }
11538
11539 /**
11540 * Build a human readable string representation of the specified view flags.
11541 *
11542 * @param flags the view flags to convert to a string
11543 * @return a String representing the supplied flags
11544 */
11545 private static String printFlags(int flags) {
11546 String output = "";
11547 int numFlags = 0;
11548 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
11549 output += "TAKES_FOCUS";
11550 numFlags++;
11551 }
11552
11553 switch (flags & VISIBILITY_MASK) {
11554 case INVISIBLE:
11555 if (numFlags > 0) {
11556 output += " ";
11557 }
11558 output += "INVISIBLE";
11559 // USELESS HERE numFlags++;
11560 break;
11561 case GONE:
11562 if (numFlags > 0) {
11563 output += " ";
11564 }
11565 output += "GONE";
11566 // USELESS HERE numFlags++;
11567 break;
11568 default:
11569 break;
11570 }
11571 return output;
11572 }
11573
11574 /**
11575 * Build a human readable string representation of the specified private
11576 * view flags.
11577 *
11578 * @param privateFlags the private view flags to convert to a string
11579 * @return a String representing the supplied flags
11580 */
11581 private static String printPrivateFlags(int privateFlags) {
11582 String output = "";
11583 int numFlags = 0;
11584
11585 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
11586 output += "WANTS_FOCUS";
11587 numFlags++;
11588 }
11589
11590 if ((privateFlags & FOCUSED) == FOCUSED) {
11591 if (numFlags > 0) {
11592 output += " ";
11593 }
11594 output += "FOCUSED";
11595 numFlags++;
11596 }
11597
11598 if ((privateFlags & SELECTED) == SELECTED) {
11599 if (numFlags > 0) {
11600 output += " ";
11601 }
11602 output += "SELECTED";
11603 numFlags++;
11604 }
11605
11606 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
11607 if (numFlags > 0) {
11608 output += " ";
11609 }
11610 output += "IS_ROOT_NAMESPACE";
11611 numFlags++;
11612 }
11613
11614 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
11615 if (numFlags > 0) {
11616 output += " ";
11617 }
11618 output += "HAS_BOUNDS";
11619 numFlags++;
11620 }
11621
11622 if ((privateFlags & DRAWN) == DRAWN) {
11623 if (numFlags > 0) {
11624 output += " ";
11625 }
11626 output += "DRAWN";
11627 // USELESS HERE numFlags++;
11628 }
11629 return output;
11630 }
11631
11632 /**
11633 * <p>Indicates whether or not this view's layout will be requested during
11634 * the next hierarchy layout pass.</p>
11635 *
11636 * @return true if the layout will be forced during next layout pass
11637 */
11638 public boolean isLayoutRequested() {
11639 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
11640 }
11641
11642 /**
11643 * Assign a size and position to a view and all of its
11644 * descendants
11645 *
11646 * <p>This is the second phase of the layout mechanism.
11647 * (The first is measuring). In this phase, each parent calls
11648 * layout on all of its children to position them.
11649 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080011650 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011651 *
Chet Haase9c087442011-01-12 16:20:16 -080011652 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011653 * Derived classes with children should override
11654 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080011655 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011656 *
11657 * @param l Left position, relative to parent
11658 * @param t Top position, relative to parent
11659 * @param r Right position, relative to parent
11660 * @param b Bottom position, relative to parent
11661 */
Romain Guy5429e1d2010-09-07 12:38:00 -070011662 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080011663 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070011664 int oldL = mLeft;
11665 int oldT = mTop;
11666 int oldB = mBottom;
11667 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011668 boolean changed = setFrame(l, t, r, b);
11669 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
11670 if (ViewDebug.TRACE_HIERARCHY) {
11671 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
11672 }
11673
11674 onLayout(changed, l, t, r, b);
11675 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070011676
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011677 ListenerInfo li = mListenerInfo;
11678 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070011679 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011680 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070011681 int numListeners = listenersCopy.size();
11682 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070011683 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070011684 }
11685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011686 }
11687 mPrivateFlags &= ~FORCE_LAYOUT;
11688 }
11689
11690 /**
11691 * Called from layout when this view should
11692 * assign a size and position to each of its children.
11693 *
11694 * Derived classes with children should override
11695 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070011696 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011697 * @param changed This is a new size or position for this view
11698 * @param left Left position, relative to parent
11699 * @param top Top position, relative to parent
11700 * @param right Right position, relative to parent
11701 * @param bottom Bottom position, relative to parent
11702 */
11703 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
11704 }
11705
11706 /**
11707 * Assign a size and position to this view.
11708 *
11709 * This is called from layout.
11710 *
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 * @return true if the new size and position are different than the
11716 * previous ones
11717 * {@hide}
11718 */
11719 protected boolean setFrame(int left, int top, int right, int bottom) {
11720 boolean changed = false;
11721
11722 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070011723 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011724 + right + "," + bottom + ")");
11725 }
11726
11727 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
11728 changed = true;
11729
11730 // Remember our drawn bit
11731 int drawn = mPrivateFlags & DRAWN;
11732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011733 int oldWidth = mRight - mLeft;
11734 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070011735 int newWidth = right - left;
11736 int newHeight = bottom - top;
11737 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
11738
11739 // Invalidate our old position
11740 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011741
11742 mLeft = left;
11743 mTop = top;
11744 mRight = right;
11745 mBottom = bottom;
11746
11747 mPrivateFlags |= HAS_BOUNDS;
11748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011749
Chet Haase75755e22011-07-18 17:48:25 -070011750 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011751 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
11752 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070011753 if (mTransformationInfo != null) {
11754 mTransformationInfo.mMatrixDirty = true;
11755 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011757 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
11758 }
11759
11760 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
11761 // If we are visible, force the DRAWN bit to on so that
11762 // this invalidate will go through (at least to our parent).
11763 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011764 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011765 // the DRAWN bit.
11766 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070011767 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080011768 // parent display list may need to be recreated based on a change in the bounds
11769 // of any child
11770 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011771 }
11772
11773 // Reset drawn bit to original value (invalidate turns it off)
11774 mPrivateFlags |= drawn;
11775
11776 mBackgroundSizeChanged = true;
11777 }
11778 return changed;
11779 }
11780
11781 /**
11782 * Finalize inflating a view from XML. This is called as the last phase
11783 * of inflation, after all child views have been added.
11784 *
11785 * <p>Even if the subclass overrides onFinishInflate, they should always be
11786 * sure to call the super method, so that we get called.
11787 */
11788 protected void onFinishInflate() {
11789 }
11790
11791 /**
11792 * Returns the resources associated with this view.
11793 *
11794 * @return Resources object.
11795 */
11796 public Resources getResources() {
11797 return mResources;
11798 }
11799
11800 /**
11801 * Invalidates the specified Drawable.
11802 *
11803 * @param drawable the drawable to invalidate
11804 */
11805 public void invalidateDrawable(Drawable drawable) {
11806 if (verifyDrawable(drawable)) {
11807 final Rect dirty = drawable.getBounds();
11808 final int scrollX = mScrollX;
11809 final int scrollY = mScrollY;
11810
11811 invalidate(dirty.left + scrollX, dirty.top + scrollY,
11812 dirty.right + scrollX, dirty.bottom + scrollY);
11813 }
11814 }
11815
11816 /**
11817 * Schedules an action on a drawable to occur at a specified time.
11818 *
11819 * @param who the recipient of the action
11820 * @param what the action to run on the drawable
11821 * @param when the time at which the action must occur. Uses the
11822 * {@link SystemClock#uptimeMillis} timebase.
11823 */
11824 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080011825 if (verifyDrawable(who) && what != null) {
11826 if (mAttachInfo != null) {
11827 mAttachInfo.mHandler.postAtTime(what, who, when);
11828 } else {
11829 ViewRootImpl.getRunQueue().postDelayed(what, when - SystemClock.uptimeMillis());
11830 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011831 }
11832 }
11833
11834 /**
11835 * Cancels a scheduled action on a drawable.
11836 *
11837 * @param who the recipient of the action
11838 * @param what the action to cancel
11839 */
11840 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080011841 if (verifyDrawable(who) && what != null) {
11842 if (mAttachInfo != null) {
11843 mAttachInfo.mHandler.removeCallbacks(what, who);
11844 } else {
11845 ViewRootImpl.getRunQueue().removeCallbacks(what);
11846 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011847 }
11848 }
11849
11850 /**
11851 * Unschedule any events associated with the given Drawable. This can be
11852 * used when selecting a new Drawable into a view, so that the previous
11853 * one is completely unscheduled.
11854 *
11855 * @param who The Drawable to unschedule.
11856 *
11857 * @see #drawableStateChanged
11858 */
11859 public void unscheduleDrawable(Drawable who) {
11860 if (mAttachInfo != null) {
11861 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
11862 }
11863 }
11864
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011865 /**
11866 * Return the layout direction of a given Drawable.
11867 *
11868 * @param who the Drawable to query
11869 *
11870 * @hide
11871 */
11872 public int getResolvedLayoutDirection(Drawable who) {
11873 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070011874 }
11875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011876 /**
11877 * If your view subclass is displaying its own Drawable objects, it should
11878 * override this function and return true for any Drawable it is
11879 * displaying. This allows animations for those drawables to be
11880 * scheduled.
11881 *
11882 * <p>Be sure to call through to the super class when overriding this
11883 * function.
11884 *
11885 * @param who The Drawable to verify. Return true if it is one you are
11886 * displaying, else return the result of calling through to the
11887 * super class.
11888 *
11889 * @return boolean If true than the Drawable is being displayed in the
11890 * view; else false and it is not allowed to animate.
11891 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011892 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
11893 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011894 */
11895 protected boolean verifyDrawable(Drawable who) {
11896 return who == mBGDrawable;
11897 }
11898
11899 /**
11900 * This function is called whenever the state of the view changes in such
11901 * a way that it impacts the state of drawables being shown.
11902 *
11903 * <p>Be sure to call through to the superclass when overriding this
11904 * function.
11905 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011906 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011907 */
11908 protected void drawableStateChanged() {
11909 Drawable d = mBGDrawable;
11910 if (d != null && d.isStateful()) {
11911 d.setState(getDrawableState());
11912 }
11913 }
11914
11915 /**
11916 * Call this to force a view to update its drawable state. This will cause
11917 * drawableStateChanged to be called on this view. Views that are interested
11918 * in the new state should call getDrawableState.
11919 *
11920 * @see #drawableStateChanged
11921 * @see #getDrawableState
11922 */
11923 public void refreshDrawableState() {
11924 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
11925 drawableStateChanged();
11926
11927 ViewParent parent = mParent;
11928 if (parent != null) {
11929 parent.childDrawableStateChanged(this);
11930 }
11931 }
11932
11933 /**
11934 * Return an array of resource IDs of the drawable states representing the
11935 * current state of the view.
11936 *
11937 * @return The current drawable state
11938 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011939 * @see Drawable#setState(int[])
11940 * @see #drawableStateChanged()
11941 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011942 */
11943 public final int[] getDrawableState() {
11944 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
11945 return mDrawableState;
11946 } else {
11947 mDrawableState = onCreateDrawableState(0);
11948 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
11949 return mDrawableState;
11950 }
11951 }
11952
11953 /**
11954 * Generate the new {@link android.graphics.drawable.Drawable} state for
11955 * this view. This is called by the view
11956 * system when the cached Drawable state is determined to be invalid. To
11957 * retrieve the current state, you should use {@link #getDrawableState}.
11958 *
11959 * @param extraSpace if non-zero, this is the number of extra entries you
11960 * would like in the returned array in which you can place your own
11961 * states.
11962 *
11963 * @return Returns an array holding the current {@link Drawable} state of
11964 * the view.
11965 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011966 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011967 */
11968 protected int[] onCreateDrawableState(int extraSpace) {
11969 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
11970 mParent instanceof View) {
11971 return ((View) mParent).onCreateDrawableState(extraSpace);
11972 }
11973
11974 int[] drawableState;
11975
11976 int privateFlags = mPrivateFlags;
11977
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011978 int viewStateIndex = 0;
11979 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
11980 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
11981 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070011982 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011983 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
11984 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070011985 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
11986 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011987 // This is set if HW acceleration is requested, even if the current
11988 // process doesn't allow it. This is just to allow app preview
11989 // windows to better match their app.
11990 viewStateIndex |= VIEW_STATE_ACCELERATED;
11991 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070011992 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011993
Christopher Tate3d4bf172011-03-28 16:16:46 -070011994 final int privateFlags2 = mPrivateFlags2;
11995 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
11996 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
11997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011998 drawableState = VIEW_STATE_SETS[viewStateIndex];
11999
12000 //noinspection ConstantIfStatement
12001 if (false) {
12002 Log.i("View", "drawableStateIndex=" + viewStateIndex);
12003 Log.i("View", toString()
12004 + " pressed=" + ((privateFlags & PRESSED) != 0)
12005 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
12006 + " fo=" + hasFocus()
12007 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012008 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012009 + ": " + Arrays.toString(drawableState));
12010 }
12011
12012 if (extraSpace == 0) {
12013 return drawableState;
12014 }
12015
12016 final int[] fullState;
12017 if (drawableState != null) {
12018 fullState = new int[drawableState.length + extraSpace];
12019 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
12020 } else {
12021 fullState = new int[extraSpace];
12022 }
12023
12024 return fullState;
12025 }
12026
12027 /**
12028 * Merge your own state values in <var>additionalState</var> into the base
12029 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012030 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012031 *
12032 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012033 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012034 * own additional state values.
12035 *
12036 * @param additionalState The additional state values you would like
12037 * added to <var>baseState</var>; this array is not modified.
12038 *
12039 * @return As a convenience, the <var>baseState</var> array you originally
12040 * passed into the function is returned.
12041 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012042 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012043 */
12044 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
12045 final int N = baseState.length;
12046 int i = N - 1;
12047 while (i >= 0 && baseState[i] == 0) {
12048 i--;
12049 }
12050 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
12051 return baseState;
12052 }
12053
12054 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070012055 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
12056 * on all Drawable objects associated with this view.
12057 */
12058 public void jumpDrawablesToCurrentState() {
12059 if (mBGDrawable != null) {
12060 mBGDrawable.jumpToCurrentState();
12061 }
12062 }
12063
12064 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012065 * Sets the background color for this view.
12066 * @param color the color of the background
12067 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012068 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012069 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070012070 if (mBGDrawable instanceof ColorDrawable) {
12071 ((ColorDrawable) mBGDrawable).setColor(color);
12072 } else {
12073 setBackgroundDrawable(new ColorDrawable(color));
12074 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012075 }
12076
12077 /**
12078 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070012079 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012080 * @param resid The identifier of the resource.
12081 * @attr ref android.R.styleable#View_background
12082 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012083 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012084 public void setBackgroundResource(int resid) {
12085 if (resid != 0 && resid == mBackgroundResource) {
12086 return;
12087 }
12088
12089 Drawable d= null;
12090 if (resid != 0) {
12091 d = mResources.getDrawable(resid);
12092 }
12093 setBackgroundDrawable(d);
12094
12095 mBackgroundResource = resid;
12096 }
12097
12098 /**
12099 * Set the background to a given Drawable, or remove the background. If the
12100 * background has padding, this View's padding is set to the background's
12101 * padding. However, when a background is removed, this View's padding isn't
12102 * touched. If setting the padding is desired, please use
12103 * {@link #setPadding(int, int, int, int)}.
12104 *
12105 * @param d The Drawable to use as the background, or null to remove the
12106 * background
12107 */
12108 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070012109 if (d == mBGDrawable) {
12110 return;
12111 }
12112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012113 boolean requestLayout = false;
12114
12115 mBackgroundResource = 0;
12116
12117 /*
12118 * Regardless of whether we're setting a new background or not, we want
12119 * to clear the previous drawable.
12120 */
12121 if (mBGDrawable != null) {
12122 mBGDrawable.setCallback(null);
12123 unscheduleDrawable(mBGDrawable);
12124 }
12125
12126 if (d != null) {
12127 Rect padding = sThreadLocal.get();
12128 if (padding == null) {
12129 padding = new Rect();
12130 sThreadLocal.set(padding);
12131 }
12132 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012133 switch (d.getResolvedLayoutDirectionSelf()) {
12134 case LAYOUT_DIRECTION_RTL:
12135 setPadding(padding.right, padding.top, padding.left, padding.bottom);
12136 break;
12137 case LAYOUT_DIRECTION_LTR:
12138 default:
12139 setPadding(padding.left, padding.top, padding.right, padding.bottom);
12140 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012141 }
12142
12143 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
12144 // if it has a different minimum size, we should layout again
12145 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
12146 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
12147 requestLayout = true;
12148 }
12149
12150 d.setCallback(this);
12151 if (d.isStateful()) {
12152 d.setState(getDrawableState());
12153 }
12154 d.setVisible(getVisibility() == VISIBLE, false);
12155 mBGDrawable = d;
12156
12157 if ((mPrivateFlags & SKIP_DRAW) != 0) {
12158 mPrivateFlags &= ~SKIP_DRAW;
12159 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
12160 requestLayout = true;
12161 }
12162 } else {
12163 /* Remove the background */
12164 mBGDrawable = null;
12165
12166 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
12167 /*
12168 * This view ONLY drew the background before and we're removing
12169 * the background, so now it won't draw anything
12170 * (hence we SKIP_DRAW)
12171 */
12172 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
12173 mPrivateFlags |= SKIP_DRAW;
12174 }
12175
12176 /*
12177 * When the background is set, we try to apply its padding to this
12178 * View. When the background is removed, we don't touch this View's
12179 * padding. This is noted in the Javadocs. Hence, we don't need to
12180 * requestLayout(), the invalidate() below is sufficient.
12181 */
12182
12183 // The old background's minimum size could have affected this
12184 // View's layout, so let's requestLayout
12185 requestLayout = true;
12186 }
12187
Romain Guy8f1344f52009-05-15 16:03:59 -070012188 computeOpaqueFlags();
12189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012190 if (requestLayout) {
12191 requestLayout();
12192 }
12193
12194 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012195 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012196 }
12197
12198 /**
12199 * Gets the background drawable
12200 * @return The drawable used as the background for this view, if any.
12201 */
12202 public Drawable getBackground() {
12203 return mBGDrawable;
12204 }
12205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012206 /**
12207 * Sets the padding. The view may add on the space required to display
12208 * the scrollbars, depending on the style and visibility of the scrollbars.
12209 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
12210 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
12211 * from the values set in this call.
12212 *
12213 * @attr ref android.R.styleable#View_padding
12214 * @attr ref android.R.styleable#View_paddingBottom
12215 * @attr ref android.R.styleable#View_paddingLeft
12216 * @attr ref android.R.styleable#View_paddingRight
12217 * @attr ref android.R.styleable#View_paddingTop
12218 * @param left the left padding in pixels
12219 * @param top the top padding in pixels
12220 * @param right the right padding in pixels
12221 * @param bottom the bottom padding in pixels
12222 */
12223 public void setPadding(int left, int top, int right, int bottom) {
12224 boolean changed = false;
12225
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012226 mUserPaddingRelative = false;
12227
Adam Powell20232d02010-12-08 21:08:53 -080012228 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012229 mUserPaddingRight = right;
12230 mUserPaddingBottom = bottom;
12231
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012232 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070012233
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012234 // Common case is there are no scroll bars.
12235 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012236 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080012237 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012238 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080012239 switch (mVerticalScrollbarPosition) {
12240 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012241 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
12242 left += offset;
12243 } else {
12244 right += offset;
12245 }
12246 break;
Adam Powell20232d02010-12-08 21:08:53 -080012247 case SCROLLBAR_POSITION_RIGHT:
12248 right += offset;
12249 break;
12250 case SCROLLBAR_POSITION_LEFT:
12251 left += offset;
12252 break;
12253 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012254 }
Adam Powell20232d02010-12-08 21:08:53 -080012255 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012256 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
12257 ? 0 : getHorizontalScrollbarHeight();
12258 }
12259 }
Romain Guy8506ab42009-06-11 17:35:47 -070012260
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012261 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012262 changed = true;
12263 mPaddingLeft = left;
12264 }
12265 if (mPaddingTop != top) {
12266 changed = true;
12267 mPaddingTop = top;
12268 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012269 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012270 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012271 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012272 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012273 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012274 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012275 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012276 }
12277
12278 if (changed) {
12279 requestLayout();
12280 }
12281 }
12282
12283 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012284 * Sets the relative padding. The view may add on the space required to display
12285 * the scrollbars, depending on the style and visibility of the scrollbars.
12286 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
12287 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
12288 * from the values set in this call.
12289 *
12290 * @attr ref android.R.styleable#View_padding
12291 * @attr ref android.R.styleable#View_paddingBottom
12292 * @attr ref android.R.styleable#View_paddingStart
12293 * @attr ref android.R.styleable#View_paddingEnd
12294 * @attr ref android.R.styleable#View_paddingTop
12295 * @param start the start padding in pixels
12296 * @param top the top padding in pixels
12297 * @param end the end padding in pixels
12298 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012299 */
12300 public void setPaddingRelative(int start, int top, int end, int bottom) {
12301 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070012302
12303 mUserPaddingStart = start;
12304 mUserPaddingEnd = end;
12305
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012306 switch(getResolvedLayoutDirection()) {
12307 case LAYOUT_DIRECTION_RTL:
12308 setPadding(end, top, start, bottom);
12309 break;
12310 case LAYOUT_DIRECTION_LTR:
12311 default:
12312 setPadding(start, top, end, bottom);
12313 }
12314 }
12315
12316 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012317 * Returns the top padding of this view.
12318 *
12319 * @return the top padding in pixels
12320 */
12321 public int getPaddingTop() {
12322 return mPaddingTop;
12323 }
12324
12325 /**
12326 * Returns the bottom padding of this view. If there are inset and enabled
12327 * scrollbars, this value may include the space required to display the
12328 * scrollbars as well.
12329 *
12330 * @return the bottom padding in pixels
12331 */
12332 public int getPaddingBottom() {
12333 return mPaddingBottom;
12334 }
12335
12336 /**
12337 * Returns the left 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 left padding in pixels
12342 */
12343 public int getPaddingLeft() {
12344 return mPaddingLeft;
12345 }
12346
12347 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012348 * Returns the start 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 start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012353 */
12354 public int getPaddingStart() {
12355 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12356 mPaddingRight : mPaddingLeft;
12357 }
12358
12359 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012360 * Returns the right padding of this view. If there are inset and enabled
12361 * scrollbars, this value may include the space required to display the
12362 * scrollbars as well.
12363 *
12364 * @return the right padding in pixels
12365 */
12366 public int getPaddingRight() {
12367 return mPaddingRight;
12368 }
12369
12370 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012371 * Returns the end 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 end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012376 */
12377 public int getPaddingEnd() {
12378 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12379 mPaddingLeft : mPaddingRight;
12380 }
12381
12382 /**
12383 * Return if the padding as been set thru relative values
12384 * {@link #setPaddingRelative(int, int, int, int)} or thru
12385 * @attr ref android.R.styleable#View_paddingStart or
12386 * @attr ref android.R.styleable#View_paddingEnd
12387 *
12388 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012389 */
12390 public boolean isPaddingRelative() {
12391 return mUserPaddingRelative;
12392 }
12393
12394 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012395 * Changes the selection state of this view. A view can be selected or not.
12396 * Note that selection is not the same as focus. Views are typically
12397 * selected in the context of an AdapterView like ListView or GridView;
12398 * the selected view is the view that is highlighted.
12399 *
12400 * @param selected true if the view must be selected, false otherwise
12401 */
12402 public void setSelected(boolean selected) {
12403 if (((mPrivateFlags & SELECTED) != 0) != selected) {
12404 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070012405 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080012406 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012407 refreshDrawableState();
12408 dispatchSetSelected(selected);
12409 }
12410 }
12411
12412 /**
12413 * Dispatch setSelected to all of this View's children.
12414 *
12415 * @see #setSelected(boolean)
12416 *
12417 * @param selected The new selected state
12418 */
12419 protected void dispatchSetSelected(boolean selected) {
12420 }
12421
12422 /**
12423 * Indicates the selection state of this view.
12424 *
12425 * @return true if the view is selected, false otherwise
12426 */
12427 @ViewDebug.ExportedProperty
12428 public boolean isSelected() {
12429 return (mPrivateFlags & SELECTED) != 0;
12430 }
12431
12432 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012433 * Changes the activated state of this view. A view can be activated or not.
12434 * Note that activation is not the same as selection. Selection is
12435 * a transient property, representing the view (hierarchy) the user is
12436 * currently interacting with. Activation is a longer-term state that the
12437 * user can move views in and out of. For example, in a list view with
12438 * single or multiple selection enabled, the views in the current selection
12439 * set are activated. (Um, yeah, we are deeply sorry about the terminology
12440 * here.) The activated state is propagated down to children of the view it
12441 * is set on.
12442 *
12443 * @param activated true if the view must be activated, false otherwise
12444 */
12445 public void setActivated(boolean activated) {
12446 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
12447 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012448 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012449 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070012450 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012451 }
12452 }
12453
12454 /**
12455 * Dispatch setActivated to all of this View's children.
12456 *
12457 * @see #setActivated(boolean)
12458 *
12459 * @param activated The new activated state
12460 */
12461 protected void dispatchSetActivated(boolean activated) {
12462 }
12463
12464 /**
12465 * Indicates the activation state of this view.
12466 *
12467 * @return true if the view is activated, false otherwise
12468 */
12469 @ViewDebug.ExportedProperty
12470 public boolean isActivated() {
12471 return (mPrivateFlags & ACTIVATED) != 0;
12472 }
12473
12474 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012475 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
12476 * observer can be used to get notifications when global events, like
12477 * layout, happen.
12478 *
12479 * The returned ViewTreeObserver observer is not guaranteed to remain
12480 * valid for the lifetime of this View. If the caller of this method keeps
12481 * a long-lived reference to ViewTreeObserver, it should always check for
12482 * the return value of {@link ViewTreeObserver#isAlive()}.
12483 *
12484 * @return The ViewTreeObserver for this view's hierarchy.
12485 */
12486 public ViewTreeObserver getViewTreeObserver() {
12487 if (mAttachInfo != null) {
12488 return mAttachInfo.mTreeObserver;
12489 }
12490 if (mFloatingTreeObserver == null) {
12491 mFloatingTreeObserver = new ViewTreeObserver();
12492 }
12493 return mFloatingTreeObserver;
12494 }
12495
12496 /**
12497 * <p>Finds the topmost view in the current view hierarchy.</p>
12498 *
12499 * @return the topmost view containing this view
12500 */
12501 public View getRootView() {
12502 if (mAttachInfo != null) {
12503 final View v = mAttachInfo.mRootView;
12504 if (v != null) {
12505 return v;
12506 }
12507 }
Romain Guy8506ab42009-06-11 17:35:47 -070012508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012509 View parent = this;
12510
12511 while (parent.mParent != null && parent.mParent instanceof View) {
12512 parent = (View) parent.mParent;
12513 }
12514
12515 return parent;
12516 }
12517
12518 /**
12519 * <p>Computes the coordinates of this view on the screen. The argument
12520 * must be an array of two integers. After the method returns, the array
12521 * contains the x and y location in that order.</p>
12522 *
12523 * @param location an array of two integers in which to hold the coordinates
12524 */
12525 public void getLocationOnScreen(int[] location) {
12526 getLocationInWindow(location);
12527
12528 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070012529 if (info != null) {
12530 location[0] += info.mWindowLeft;
12531 location[1] += info.mWindowTop;
12532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012533 }
12534
12535 /**
12536 * <p>Computes the coordinates of this view in its window. The argument
12537 * must be an array of two integers. After the method returns, the array
12538 * contains the x and y location in that order.</p>
12539 *
12540 * @param location an array of two integers in which to hold the coordinates
12541 */
12542 public void getLocationInWindow(int[] location) {
12543 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012544 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012545 }
12546
Gilles Debunne6583ce52011-12-06 18:09:02 -080012547 if (mAttachInfo == null) {
12548 // When the view is not attached to a window, this method does not make sense
12549 location[0] = location[1] = 0;
12550 return;
12551 }
12552
Gilles Debunnecea45132011-11-24 02:19:27 +010012553 float[] position = mAttachInfo.mTmpTransformLocation;
12554 position[0] = position[1] = 0.0f;
12555
12556 if (!hasIdentityMatrix()) {
12557 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012559
Gilles Debunnecea45132011-11-24 02:19:27 +010012560 position[0] += mLeft;
12561 position[1] += mTop;
12562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012563 ViewParent viewParent = mParent;
12564 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012565 final View view = (View) viewParent;
12566
12567 position[0] -= view.mScrollX;
12568 position[1] -= view.mScrollY;
12569
12570 if (!view.hasIdentityMatrix()) {
12571 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012572 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012573
12574 position[0] += view.mLeft;
12575 position[1] += view.mTop;
12576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012577 viewParent = view.mParent;
12578 }
Romain Guy8506ab42009-06-11 17:35:47 -070012579
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012580 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012581 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010012582 final ViewRootImpl vr = (ViewRootImpl) viewParent;
12583 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012584 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012585
12586 location[0] = (int) (position[0] + 0.5f);
12587 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012588 }
12589
12590 /**
12591 * {@hide}
12592 * @param id the id of the view to be found
12593 * @return the view of the specified id, null if cannot be found
12594 */
12595 protected View findViewTraversal(int id) {
12596 if (id == mID) {
12597 return this;
12598 }
12599 return null;
12600 }
12601
12602 /**
12603 * {@hide}
12604 * @param tag the tag of the view to be found
12605 * @return the view of specified tag, null if cannot be found
12606 */
12607 protected View findViewWithTagTraversal(Object tag) {
12608 if (tag != null && tag.equals(mTag)) {
12609 return this;
12610 }
12611 return null;
12612 }
12613
12614 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012615 * {@hide}
12616 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070012617 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080012618 * @return The first view that matches the predicate or null.
12619 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070012620 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080012621 if (predicate.apply(this)) {
12622 return this;
12623 }
12624 return null;
12625 }
12626
12627 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012628 * Look for a child view with the given id. If this view has the given
12629 * id, return this view.
12630 *
12631 * @param id The id to search for.
12632 * @return The view that has the given id in the hierarchy or null
12633 */
12634 public final View findViewById(int id) {
12635 if (id < 0) {
12636 return null;
12637 }
12638 return findViewTraversal(id);
12639 }
12640
12641 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070012642 * Finds a view by its unuque and stable accessibility id.
12643 *
12644 * @param accessibilityId The searched accessibility id.
12645 * @return The found view.
12646 */
12647 final View findViewByAccessibilityId(int accessibilityId) {
12648 if (accessibilityId < 0) {
12649 return null;
12650 }
12651 return findViewByAccessibilityIdTraversal(accessibilityId);
12652 }
12653
12654 /**
12655 * Performs the traversal to find a view by its unuque and stable accessibility id.
12656 *
12657 * <strong>Note:</strong>This method does not stop at the root namespace
12658 * boundary since the user can touch the screen at an arbitrary location
12659 * potentially crossing the root namespace bounday which will send an
12660 * accessibility event to accessibility services and they should be able
12661 * to obtain the event source. Also accessibility ids are guaranteed to be
12662 * unique in the window.
12663 *
12664 * @param accessibilityId The accessibility id.
12665 * @return The found view.
12666 */
12667 View findViewByAccessibilityIdTraversal(int accessibilityId) {
12668 if (getAccessibilityViewId() == accessibilityId) {
12669 return this;
12670 }
12671 return null;
12672 }
12673
12674 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012675 * Look for a child view with the given tag. If this view has the given
12676 * tag, return this view.
12677 *
12678 * @param tag The tag to search for, using "tag.equals(getTag())".
12679 * @return The View that has the given tag in the hierarchy or null
12680 */
12681 public final View findViewWithTag(Object tag) {
12682 if (tag == null) {
12683 return null;
12684 }
12685 return findViewWithTagTraversal(tag);
12686 }
12687
12688 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012689 * {@hide}
12690 * Look for a child view that matches the specified predicate.
12691 * If this view matches the predicate, return this view.
12692 *
12693 * @param predicate The predicate to evaluate.
12694 * @return The first view that matches the predicate or null.
12695 */
12696 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070012697 return findViewByPredicateTraversal(predicate, null);
12698 }
12699
12700 /**
12701 * {@hide}
12702 * Look for a child view that matches the specified predicate,
12703 * starting with the specified view and its descendents and then
12704 * recusively searching the ancestors and siblings of that view
12705 * until this view is reached.
12706 *
12707 * This method is useful in cases where the predicate does not match
12708 * a single unique view (perhaps multiple views use the same id)
12709 * and we are trying to find the view that is "closest" in scope to the
12710 * starting view.
12711 *
12712 * @param start The view to start from.
12713 * @param predicate The predicate to evaluate.
12714 * @return The first view that matches the predicate or null.
12715 */
12716 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
12717 View childToSkip = null;
12718 for (;;) {
12719 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
12720 if (view != null || start == this) {
12721 return view;
12722 }
12723
12724 ViewParent parent = start.getParent();
12725 if (parent == null || !(parent instanceof View)) {
12726 return null;
12727 }
12728
12729 childToSkip = start;
12730 start = (View) parent;
12731 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080012732 }
12733
12734 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012735 * Sets the identifier for this view. The identifier does not have to be
12736 * unique in this view's hierarchy. The identifier should be a positive
12737 * number.
12738 *
12739 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070012740 * @see #getId()
12741 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012742 *
12743 * @param id a number used to identify the view
12744 *
12745 * @attr ref android.R.styleable#View_id
12746 */
12747 public void setId(int id) {
12748 mID = id;
12749 }
12750
12751 /**
12752 * {@hide}
12753 *
12754 * @param isRoot true if the view belongs to the root namespace, false
12755 * otherwise
12756 */
12757 public void setIsRootNamespace(boolean isRoot) {
12758 if (isRoot) {
12759 mPrivateFlags |= IS_ROOT_NAMESPACE;
12760 } else {
12761 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
12762 }
12763 }
12764
12765 /**
12766 * {@hide}
12767 *
12768 * @return true if the view belongs to the root namespace, false otherwise
12769 */
12770 public boolean isRootNamespace() {
12771 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
12772 }
12773
12774 /**
12775 * Returns this view's identifier.
12776 *
12777 * @return a positive integer used to identify the view or {@link #NO_ID}
12778 * if the view has no ID
12779 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012780 * @see #setId(int)
12781 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012782 * @attr ref android.R.styleable#View_id
12783 */
12784 @ViewDebug.CapturedViewProperty
12785 public int getId() {
12786 return mID;
12787 }
12788
12789 /**
12790 * Returns this view's tag.
12791 *
12792 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070012793 *
12794 * @see #setTag(Object)
12795 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012796 */
12797 @ViewDebug.ExportedProperty
12798 public Object getTag() {
12799 return mTag;
12800 }
12801
12802 /**
12803 * Sets the tag associated with this view. A tag can be used to mark
12804 * a view in its hierarchy and does not have to be unique within the
12805 * hierarchy. Tags can also be used to store data within a view without
12806 * resorting to another data structure.
12807 *
12808 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070012809 *
12810 * @see #getTag()
12811 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012812 */
12813 public void setTag(final Object tag) {
12814 mTag = tag;
12815 }
12816
12817 /**
Romain Guyd90a3312009-05-06 14:54:28 -070012818 * Returns the tag associated with this view and the specified key.
12819 *
12820 * @param key The key identifying the tag
12821 *
12822 * @return the Object stored in this view as a tag
12823 *
12824 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070012825 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070012826 */
12827 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012828 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070012829 return null;
12830 }
12831
12832 /**
12833 * Sets a tag associated with this view and a key. A tag can be used
12834 * to mark a view in its hierarchy and does not have to be unique within
12835 * the hierarchy. Tags can also be used to store data within a view
12836 * without resorting to another data structure.
12837 *
12838 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070012839 * application to ensure it is unique (see the <a
12840 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
12841 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070012842 * the Android framework or not associated with any package will cause
12843 * an {@link IllegalArgumentException} to be thrown.
12844 *
12845 * @param key The key identifying the tag
12846 * @param tag An Object to tag the view with
12847 *
12848 * @throws IllegalArgumentException If they specified key is not valid
12849 *
12850 * @see #setTag(Object)
12851 * @see #getTag(int)
12852 */
12853 public void setTag(int key, final Object tag) {
12854 // If the package id is 0x00 or 0x01, it's either an undefined package
12855 // or a framework id
12856 if ((key >>> 24) < 2) {
12857 throw new IllegalArgumentException("The key must be an application-specific "
12858 + "resource id.");
12859 }
12860
Adam Powell2b2f6d62011-09-23 11:15:39 -070012861 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012862 }
12863
12864 /**
12865 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
12866 * framework id.
12867 *
12868 * @hide
12869 */
12870 public void setTagInternal(int key, Object tag) {
12871 if ((key >>> 24) != 0x1) {
12872 throw new IllegalArgumentException("The key must be a framework-specific "
12873 + "resource id.");
12874 }
12875
Adam Powell2b2f6d62011-09-23 11:15:39 -070012876 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012877 }
12878
Adam Powell2b2f6d62011-09-23 11:15:39 -070012879 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012880 if (mKeyedTags == null) {
12881 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070012882 }
12883
Adam Powell7db82ac2011-09-22 19:44:04 -070012884 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012885 }
12886
12887 /**
Romain Guy13922e02009-05-12 17:56:14 -070012888 * @param consistency The type of consistency. See ViewDebug for more information.
12889 *
12890 * @hide
12891 */
12892 protected boolean dispatchConsistencyCheck(int consistency) {
12893 return onConsistencyCheck(consistency);
12894 }
12895
12896 /**
12897 * Method that subclasses should implement to check their consistency. The type of
12898 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070012899 *
Romain Guy13922e02009-05-12 17:56:14 -070012900 * @param consistency The type of consistency. See ViewDebug for more information.
12901 *
12902 * @throws IllegalStateException if the view is in an inconsistent state.
12903 *
12904 * @hide
12905 */
12906 protected boolean onConsistencyCheck(int consistency) {
12907 boolean result = true;
12908
12909 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
12910 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
12911
12912 if (checkLayout) {
12913 if (getParent() == null) {
12914 result = false;
12915 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12916 "View " + this + " does not have a parent.");
12917 }
12918
12919 if (mAttachInfo == null) {
12920 result = false;
12921 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12922 "View " + this + " is not attached to a window.");
12923 }
12924 }
12925
12926 if (checkDrawing) {
12927 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
12928 // from their draw() method
12929
12930 if ((mPrivateFlags & DRAWN) != DRAWN &&
12931 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
12932 result = false;
12933 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12934 "View " + this + " was invalidated but its drawing cache is valid.");
12935 }
12936 }
12937
12938 return result;
12939 }
12940
12941 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012942 * Prints information about this view in the log output, with the tag
12943 * {@link #VIEW_LOG_TAG}.
12944 *
12945 * @hide
12946 */
12947 public void debug() {
12948 debug(0);
12949 }
12950
12951 /**
12952 * Prints information about this view in the log output, with the tag
12953 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
12954 * indentation defined by the <code>depth</code>.
12955 *
12956 * @param depth the indentation level
12957 *
12958 * @hide
12959 */
12960 protected void debug(int depth) {
12961 String output = debugIndent(depth - 1);
12962
12963 output += "+ " + this;
12964 int id = getId();
12965 if (id != -1) {
12966 output += " (id=" + id + ")";
12967 }
12968 Object tag = getTag();
12969 if (tag != null) {
12970 output += " (tag=" + tag + ")";
12971 }
12972 Log.d(VIEW_LOG_TAG, output);
12973
12974 if ((mPrivateFlags & FOCUSED) != 0) {
12975 output = debugIndent(depth) + " FOCUSED";
12976 Log.d(VIEW_LOG_TAG, output);
12977 }
12978
12979 output = debugIndent(depth);
12980 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
12981 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
12982 + "} ";
12983 Log.d(VIEW_LOG_TAG, output);
12984
12985 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
12986 || mPaddingBottom != 0) {
12987 output = debugIndent(depth);
12988 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
12989 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
12990 Log.d(VIEW_LOG_TAG, output);
12991 }
12992
12993 output = debugIndent(depth);
12994 output += "mMeasureWidth=" + mMeasuredWidth +
12995 " mMeasureHeight=" + mMeasuredHeight;
12996 Log.d(VIEW_LOG_TAG, output);
12997
12998 output = debugIndent(depth);
12999 if (mLayoutParams == null) {
13000 output += "BAD! no layout params";
13001 } else {
13002 output = mLayoutParams.debug(output);
13003 }
13004 Log.d(VIEW_LOG_TAG, output);
13005
13006 output = debugIndent(depth);
13007 output += "flags={";
13008 output += View.printFlags(mViewFlags);
13009 output += "}";
13010 Log.d(VIEW_LOG_TAG, output);
13011
13012 output = debugIndent(depth);
13013 output += "privateFlags={";
13014 output += View.printPrivateFlags(mPrivateFlags);
13015 output += "}";
13016 Log.d(VIEW_LOG_TAG, output);
13017 }
13018
13019 /**
13020 * Creates an string of whitespaces used for indentation.
13021 *
13022 * @param depth the indentation level
13023 * @return a String containing (depth * 2 + 3) * 2 white spaces
13024 *
13025 * @hide
13026 */
13027 protected static String debugIndent(int depth) {
13028 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
13029 for (int i = 0; i < (depth * 2) + 3; i++) {
13030 spaces.append(' ').append(' ');
13031 }
13032 return spaces.toString();
13033 }
13034
13035 /**
13036 * <p>Return the offset of the widget's text baseline from the widget's top
13037 * boundary. If this widget does not support baseline alignment, this
13038 * method returns -1. </p>
13039 *
13040 * @return the offset of the baseline within the widget's bounds or -1
13041 * if baseline alignment is not supported
13042 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070013043 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013044 public int getBaseline() {
13045 return -1;
13046 }
13047
13048 /**
13049 * Call this when something has changed which has invalidated the
13050 * layout of this view. This will schedule a layout pass of the view
13051 * tree.
13052 */
13053 public void requestLayout() {
13054 if (ViewDebug.TRACE_HIERARCHY) {
13055 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
13056 }
13057
Svetoslav Ganov79311c42012-01-17 20:24:26 -080013058 if (getAccessibilityNodeProvider() != null) {
13059 throw new IllegalStateException("Views with AccessibilityNodeProvider"
13060 + " can't have children.");
13061 }
13062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013063 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013064 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013065
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070013066 if (mParent != null) {
13067 if (mLayoutParams != null) {
13068 mLayoutParams.resolveWithDirection(getResolvedLayoutDirection());
13069 }
13070 if (!mParent.isLayoutRequested()) {
13071 mParent.requestLayout();
13072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013073 }
13074 }
13075
13076 /**
13077 * Forces this view to be laid out during the next layout pass.
13078 * This method does not call requestLayout() or forceLayout()
13079 * on the parent.
13080 */
13081 public void forceLayout() {
13082 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013083 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013084 }
13085
13086 /**
13087 * <p>
13088 * This is called to find out how big a view should be. The parent
13089 * supplies constraint information in the width and height parameters.
13090 * </p>
13091 *
13092 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080013093 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013094 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080013095 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013096 * </p>
13097 *
13098 *
13099 * @param widthMeasureSpec Horizontal space requirements as imposed by the
13100 * parent
13101 * @param heightMeasureSpec Vertical space requirements as imposed by the
13102 * parent
13103 *
13104 * @see #onMeasure(int, int)
13105 */
13106 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
13107 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
13108 widthMeasureSpec != mOldWidthMeasureSpec ||
13109 heightMeasureSpec != mOldHeightMeasureSpec) {
13110
13111 // first clears the measured dimension flag
13112 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
13113
13114 if (ViewDebug.TRACE_HIERARCHY) {
13115 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
13116 }
13117
13118 // measure ourselves, this should set the measured dimension flag back
13119 onMeasure(widthMeasureSpec, heightMeasureSpec);
13120
13121 // flag not set, setMeasuredDimension() was not invoked, we raise
13122 // an exception to warn the developer
13123 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
13124 throw new IllegalStateException("onMeasure() did not set the"
13125 + " measured dimension by calling"
13126 + " setMeasuredDimension()");
13127 }
13128
13129 mPrivateFlags |= LAYOUT_REQUIRED;
13130 }
13131
13132 mOldWidthMeasureSpec = widthMeasureSpec;
13133 mOldHeightMeasureSpec = heightMeasureSpec;
13134 }
13135
13136 /**
13137 * <p>
13138 * Measure the view and its content to determine the measured width and the
13139 * measured height. This method is invoked by {@link #measure(int, int)} and
13140 * should be overriden by subclasses to provide accurate and efficient
13141 * measurement of their contents.
13142 * </p>
13143 *
13144 * <p>
13145 * <strong>CONTRACT:</strong> When overriding this method, you
13146 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
13147 * measured width and height of this view. Failure to do so will trigger an
13148 * <code>IllegalStateException</code>, thrown by
13149 * {@link #measure(int, int)}. Calling the superclass'
13150 * {@link #onMeasure(int, int)} is a valid use.
13151 * </p>
13152 *
13153 * <p>
13154 * The base class implementation of measure defaults to the background size,
13155 * unless a larger size is allowed by the MeasureSpec. Subclasses should
13156 * override {@link #onMeasure(int, int)} to provide better measurements of
13157 * their content.
13158 * </p>
13159 *
13160 * <p>
13161 * If this method is overridden, it is the subclass's responsibility to make
13162 * sure the measured height and width are at least the view's minimum height
13163 * and width ({@link #getSuggestedMinimumHeight()} and
13164 * {@link #getSuggestedMinimumWidth()}).
13165 * </p>
13166 *
13167 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
13168 * The requirements are encoded with
13169 * {@link android.view.View.MeasureSpec}.
13170 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
13171 * The requirements are encoded with
13172 * {@link android.view.View.MeasureSpec}.
13173 *
13174 * @see #getMeasuredWidth()
13175 * @see #getMeasuredHeight()
13176 * @see #setMeasuredDimension(int, int)
13177 * @see #getSuggestedMinimumHeight()
13178 * @see #getSuggestedMinimumWidth()
13179 * @see android.view.View.MeasureSpec#getMode(int)
13180 * @see android.view.View.MeasureSpec#getSize(int)
13181 */
13182 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
13183 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
13184 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
13185 }
13186
13187 /**
13188 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
13189 * measured width and measured height. Failing to do so will trigger an
13190 * exception at measurement time.</p>
13191 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080013192 * @param measuredWidth The measured width of this view. May be a complex
13193 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13194 * {@link #MEASURED_STATE_TOO_SMALL}.
13195 * @param measuredHeight The measured height of this view. May be a complex
13196 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13197 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013198 */
13199 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
13200 mMeasuredWidth = measuredWidth;
13201 mMeasuredHeight = measuredHeight;
13202
13203 mPrivateFlags |= MEASURED_DIMENSION_SET;
13204 }
13205
13206 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080013207 * Merge two states as returned by {@link #getMeasuredState()}.
13208 * @param curState The current state as returned from a view or the result
13209 * of combining multiple views.
13210 * @param newState The new view state to combine.
13211 * @return Returns a new integer reflecting the combination of the two
13212 * states.
13213 */
13214 public static int combineMeasuredStates(int curState, int newState) {
13215 return curState | newState;
13216 }
13217
13218 /**
13219 * Version of {@link #resolveSizeAndState(int, int, int)}
13220 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
13221 */
13222 public static int resolveSize(int size, int measureSpec) {
13223 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
13224 }
13225
13226 /**
13227 * Utility to reconcile a desired size and state, with constraints imposed
13228 * by a MeasureSpec. Will take the desired size, unless a different size
13229 * is imposed by the constraints. The returned value is a compound integer,
13230 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
13231 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
13232 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013233 *
13234 * @param size How big the view wants to be
13235 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080013236 * @return Size information bit mask as defined by
13237 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013238 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080013239 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013240 int result = size;
13241 int specMode = MeasureSpec.getMode(measureSpec);
13242 int specSize = MeasureSpec.getSize(measureSpec);
13243 switch (specMode) {
13244 case MeasureSpec.UNSPECIFIED:
13245 result = size;
13246 break;
13247 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080013248 if (specSize < size) {
13249 result = specSize | MEASURED_STATE_TOO_SMALL;
13250 } else {
13251 result = size;
13252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013253 break;
13254 case MeasureSpec.EXACTLY:
13255 result = specSize;
13256 break;
13257 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080013258 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013259 }
13260
13261 /**
13262 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070013263 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013264 * by the MeasureSpec.
13265 *
13266 * @param size Default size for this view
13267 * @param measureSpec Constraints imposed by the parent
13268 * @return The size this view should be.
13269 */
13270 public static int getDefaultSize(int size, int measureSpec) {
13271 int result = size;
13272 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070013273 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013274
13275 switch (specMode) {
13276 case MeasureSpec.UNSPECIFIED:
13277 result = size;
13278 break;
13279 case MeasureSpec.AT_MOST:
13280 case MeasureSpec.EXACTLY:
13281 result = specSize;
13282 break;
13283 }
13284 return result;
13285 }
13286
13287 /**
13288 * Returns the suggested minimum height that the view should use. This
13289 * returns the maximum of the view's minimum height
13290 * and the background's minimum height
13291 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
13292 * <p>
13293 * When being used in {@link #onMeasure(int, int)}, the caller should still
13294 * ensure the returned height is within the requirements of the parent.
13295 *
13296 * @return The suggested minimum height of the view.
13297 */
13298 protected int getSuggestedMinimumHeight() {
13299 int suggestedMinHeight = mMinHeight;
13300
13301 if (mBGDrawable != null) {
13302 final int bgMinHeight = mBGDrawable.getMinimumHeight();
13303 if (suggestedMinHeight < bgMinHeight) {
13304 suggestedMinHeight = bgMinHeight;
13305 }
13306 }
13307
13308 return suggestedMinHeight;
13309 }
13310
13311 /**
13312 * Returns the suggested minimum width that the view should use. This
13313 * returns the maximum of the view's minimum width)
13314 * and the background's minimum width
13315 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
13316 * <p>
13317 * When being used in {@link #onMeasure(int, int)}, the caller should still
13318 * ensure the returned width is within the requirements of the parent.
13319 *
13320 * @return The suggested minimum width of the view.
13321 */
13322 protected int getSuggestedMinimumWidth() {
13323 int suggestedMinWidth = mMinWidth;
13324
13325 if (mBGDrawable != null) {
13326 final int bgMinWidth = mBGDrawable.getMinimumWidth();
13327 if (suggestedMinWidth < bgMinWidth) {
13328 suggestedMinWidth = bgMinWidth;
13329 }
13330 }
13331
13332 return suggestedMinWidth;
13333 }
13334
13335 /**
13336 * Sets the minimum height of the view. It is not guaranteed the view will
13337 * be able to achieve this minimum height (for example, if its parent layout
13338 * constrains it with less available height).
13339 *
13340 * @param minHeight The minimum height the view will try to be.
13341 */
13342 public void setMinimumHeight(int minHeight) {
13343 mMinHeight = minHeight;
13344 }
13345
13346 /**
13347 * Sets the minimum width of the view. It is not guaranteed the view will
13348 * be able to achieve this minimum width (for example, if its parent layout
13349 * constrains it with less available width).
13350 *
13351 * @param minWidth The minimum width the view will try to be.
13352 */
13353 public void setMinimumWidth(int minWidth) {
13354 mMinWidth = minWidth;
13355 }
13356
13357 /**
13358 * Get the animation currently associated with this view.
13359 *
13360 * @return The animation that is currently playing or
13361 * scheduled to play for this view.
13362 */
13363 public Animation getAnimation() {
13364 return mCurrentAnimation;
13365 }
13366
13367 /**
13368 * Start the specified animation now.
13369 *
13370 * @param animation the animation to start now
13371 */
13372 public void startAnimation(Animation animation) {
13373 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
13374 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013375 invalidateParentCaches();
13376 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013377 }
13378
13379 /**
13380 * Cancels any animations for this view.
13381 */
13382 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080013383 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080013384 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080013385 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013386 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013387 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013388 }
13389
13390 /**
13391 * Sets the next animation to play for this view.
13392 * If you want the animation to play immediately, use
13393 * startAnimation. This method provides allows fine-grained
13394 * control over the start time and invalidation, but you
13395 * must make sure that 1) the animation has a start time set, and
13396 * 2) the view will be invalidated when the animation is supposed to
13397 * start.
13398 *
13399 * @param animation The next animation, or null.
13400 */
13401 public void setAnimation(Animation animation) {
13402 mCurrentAnimation = animation;
13403 if (animation != null) {
13404 animation.reset();
13405 }
13406 }
13407
13408 /**
13409 * Invoked by a parent ViewGroup to notify the start of the animation
13410 * currently associated with this view. If you override this method,
13411 * always call super.onAnimationStart();
13412 *
13413 * @see #setAnimation(android.view.animation.Animation)
13414 * @see #getAnimation()
13415 */
13416 protected void onAnimationStart() {
13417 mPrivateFlags |= ANIMATION_STARTED;
13418 }
13419
13420 /**
13421 * Invoked by a parent ViewGroup to notify the end of the animation
13422 * currently associated with this view. If you override this method,
13423 * always call super.onAnimationEnd();
13424 *
13425 * @see #setAnimation(android.view.animation.Animation)
13426 * @see #getAnimation()
13427 */
13428 protected void onAnimationEnd() {
13429 mPrivateFlags &= ~ANIMATION_STARTED;
13430 }
13431
13432 /**
13433 * Invoked if there is a Transform that involves alpha. Subclass that can
13434 * draw themselves with the specified alpha should return true, and then
13435 * respect that alpha when their onDraw() is called. If this returns false
13436 * then the view may be redirected to draw into an offscreen buffer to
13437 * fulfill the request, which will look fine, but may be slower than if the
13438 * subclass handles it internally. The default implementation returns false.
13439 *
13440 * @param alpha The alpha (0..255) to apply to the view's drawing
13441 * @return true if the view can draw with the specified alpha.
13442 */
13443 protected boolean onSetAlpha(int alpha) {
13444 return false;
13445 }
13446
13447 /**
13448 * This is used by the RootView to perform an optimization when
13449 * the view hierarchy contains one or several SurfaceView.
13450 * SurfaceView is always considered transparent, but its children are not,
13451 * therefore all View objects remove themselves from the global transparent
13452 * region (passed as a parameter to this function).
13453 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013454 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013455 *
13456 * @return Returns true if the effective visibility of the view at this
13457 * point is opaque, regardless of the transparent region; returns false
13458 * if it is possible for underlying windows to be seen behind the view.
13459 *
13460 * {@hide}
13461 */
13462 public boolean gatherTransparentRegion(Region region) {
13463 final AttachInfo attachInfo = mAttachInfo;
13464 if (region != null && attachInfo != null) {
13465 final int pflags = mPrivateFlags;
13466 if ((pflags & SKIP_DRAW) == 0) {
13467 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
13468 // remove it from the transparent region.
13469 final int[] location = attachInfo.mTransparentLocation;
13470 getLocationInWindow(location);
13471 region.op(location[0], location[1], location[0] + mRight - mLeft,
13472 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
13473 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
13474 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
13475 // exists, so we remove the background drawable's non-transparent
13476 // parts from this transparent region.
13477 applyDrawableToTransparentRegion(mBGDrawable, region);
13478 }
13479 }
13480 return true;
13481 }
13482
13483 /**
13484 * Play a sound effect for this view.
13485 *
13486 * <p>The framework will play sound effects for some built in actions, such as
13487 * clicking, but you may wish to play these effects in your widget,
13488 * for instance, for internal navigation.
13489 *
13490 * <p>The sound effect will only be played if sound effects are enabled by the user, and
13491 * {@link #isSoundEffectsEnabled()} is true.
13492 *
13493 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
13494 */
13495 public void playSoundEffect(int soundConstant) {
13496 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
13497 return;
13498 }
13499 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
13500 }
13501
13502 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013503 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013504 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013505 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013506 *
13507 * <p>The framework will provide haptic feedback for some built in actions,
13508 * such as long presses, but you may wish to provide feedback for your
13509 * own widget.
13510 *
13511 * <p>The feedback will only be performed if
13512 * {@link #isHapticFeedbackEnabled()} is true.
13513 *
13514 * @param feedbackConstant One of the constants defined in
13515 * {@link HapticFeedbackConstants}
13516 */
13517 public boolean performHapticFeedback(int feedbackConstant) {
13518 return performHapticFeedback(feedbackConstant, 0);
13519 }
13520
13521 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013522 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013523 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013524 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013525 *
13526 * @param feedbackConstant One of the constants defined in
13527 * {@link HapticFeedbackConstants}
13528 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
13529 */
13530 public boolean performHapticFeedback(int feedbackConstant, int flags) {
13531 if (mAttachInfo == null) {
13532 return false;
13533 }
Romain Guyf607bdc2010-09-10 19:20:06 -070013534 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070013535 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013536 && !isHapticFeedbackEnabled()) {
13537 return false;
13538 }
Romain Guy812ccbe2010-06-01 14:07:24 -070013539 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
13540 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013541 }
13542
13543 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013544 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013545 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13546 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013547 */
13548 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040013549 if (visibility != mSystemUiVisibility) {
13550 mSystemUiVisibility = visibility;
13551 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13552 mParent.recomputeViewAttributes(this);
13553 }
Joe Onorato664644d2011-01-23 17:53:23 -080013554 }
13555 }
13556
13557 /**
13558 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013559 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13560 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013561 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080013562 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080013563 return mSystemUiVisibility;
13564 }
13565
Scott Mainec6331b2011-05-24 16:55:56 -070013566 /**
13567 * Set a listener to receive callbacks when the visibility of the system bar changes.
13568 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
13569 */
Joe Onorato664644d2011-01-23 17:53:23 -080013570 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013571 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080013572 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13573 mParent.recomputeViewAttributes(this);
13574 }
13575 }
13576
13577 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013578 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
13579 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080013580 */
13581 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013582 ListenerInfo li = mListenerInfo;
13583 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
13584 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080013585 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080013586 }
13587 }
13588
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013589 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
13590 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
13591 if (val != mSystemUiVisibility) {
13592 setSystemUiVisibility(val);
13593 }
13594 }
13595
Joe Onorato664644d2011-01-23 17:53:23 -080013596 /**
Joe Malin32736f02011-01-19 16:14:20 -080013597 * Creates an image that the system displays during the drag and drop
13598 * operation. This is called a &quot;drag shadow&quot;. The default implementation
13599 * for a DragShadowBuilder based on a View returns an image that has exactly the same
13600 * appearance as the given View. The default also positions the center of the drag shadow
13601 * directly under the touch point. If no View is provided (the constructor with no parameters
13602 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
13603 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
13604 * default is an invisible drag shadow.
13605 * <p>
13606 * You are not required to use the View you provide to the constructor as the basis of the
13607 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
13608 * anything you want as the drag shadow.
13609 * </p>
13610 * <p>
13611 * You pass a DragShadowBuilder object to the system when you start the drag. The system
13612 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
13613 * size and position of the drag shadow. It uses this data to construct a
13614 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
13615 * so that your application can draw the shadow image in the Canvas.
13616 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070013617 *
13618 * <div class="special reference">
13619 * <h3>Developer Guides</h3>
13620 * <p>For a guide to implementing drag and drop features, read the
13621 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
13622 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070013623 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013624 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070013625 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070013626
13627 /**
Joe Malin32736f02011-01-19 16:14:20 -080013628 * Constructs a shadow image builder based on a View. By default, the resulting drag
13629 * shadow will have the same appearance and dimensions as the View, with the touch point
13630 * over the center of the View.
13631 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070013632 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013633 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070013634 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070013635 }
13636
Christopher Tate17ed60c2011-01-18 12:50:26 -080013637 /**
13638 * Construct a shadow builder object with no associated View. This
13639 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
13640 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
13641 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080013642 * reference to any View object. If they are not overridden, then the result is an
13643 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080013644 */
13645 public DragShadowBuilder() {
13646 mView = new WeakReference<View>(null);
13647 }
13648
13649 /**
13650 * Returns the View object that had been passed to the
13651 * {@link #View.DragShadowBuilder(View)}
13652 * constructor. If that View parameter was {@code null} or if the
13653 * {@link #View.DragShadowBuilder()}
13654 * constructor was used to instantiate the builder object, this method will return
13655 * null.
13656 *
13657 * @return The View object associate with this builder object.
13658 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070013659 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070013660 final public View getView() {
13661 return mView.get();
13662 }
13663
Christopher Tate2c095f32010-10-04 14:13:40 -070013664 /**
Joe Malin32736f02011-01-19 16:14:20 -080013665 * Provides the metrics for the shadow image. These include the dimensions of
13666 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070013667 * be centered under the touch location while dragging.
13668 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013669 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080013670 * same as the dimensions of the View itself and centers the shadow under
13671 * the touch point.
13672 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013673 *
Joe Malin32736f02011-01-19 16:14:20 -080013674 * @param shadowSize A {@link android.graphics.Point} containing the width and height
13675 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
13676 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
13677 * image.
13678 *
13679 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
13680 * shadow image that should be underneath the touch point during the drag and drop
13681 * operation. Your application must set {@link android.graphics.Point#x} to the
13682 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070013683 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013684 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070013685 final View view = mView.get();
13686 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013687 shadowSize.set(view.getWidth(), view.getHeight());
13688 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070013689 } else {
13690 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
13691 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013692 }
13693
13694 /**
Joe Malin32736f02011-01-19 16:14:20 -080013695 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
13696 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013697 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070013698 *
Joe Malin32736f02011-01-19 16:14:20 -080013699 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070013700 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013701 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070013702 final View view = mView.get();
13703 if (view != null) {
13704 view.draw(canvas);
13705 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013706 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070013707 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013708 }
13709 }
13710
13711 /**
Joe Malin32736f02011-01-19 16:14:20 -080013712 * Starts a drag and drop operation. When your application calls this method, it passes a
13713 * {@link android.view.View.DragShadowBuilder} object to the system. The
13714 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
13715 * to get metrics for the drag shadow, and then calls the object's
13716 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
13717 * <p>
13718 * Once the system has the drag shadow, it begins the drag and drop operation by sending
13719 * drag events to all the View objects in your application that are currently visible. It does
13720 * this either by calling the View object's drag listener (an implementation of
13721 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
13722 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
13723 * Both are passed a {@link android.view.DragEvent} object that has a
13724 * {@link android.view.DragEvent#getAction()} value of
13725 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
13726 * </p>
13727 * <p>
13728 * Your application can invoke startDrag() on any attached View object. The View object does not
13729 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
13730 * be related to the View the user selected for dragging.
13731 * </p>
13732 * @param data A {@link android.content.ClipData} object pointing to the data to be
13733 * transferred by the drag and drop operation.
13734 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
13735 * drag shadow.
13736 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
13737 * drop operation. This Object is put into every DragEvent object sent by the system during the
13738 * current drag.
13739 * <p>
13740 * myLocalState is a lightweight mechanism for the sending information from the dragged View
13741 * to the target Views. For example, it can contain flags that differentiate between a
13742 * a copy operation and a move operation.
13743 * </p>
13744 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
13745 * so the parameter should be set to 0.
13746 * @return {@code true} if the method completes successfully, or
13747 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
13748 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070013749 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013750 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013751 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070013752 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013753 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070013754 }
13755 boolean okay = false;
13756
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013757 Point shadowSize = new Point();
13758 Point shadowTouchPoint = new Point();
13759 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070013760
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013761 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
13762 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
13763 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070013764 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013765
Chris Tatea32dcf72010-10-14 12:13:50 -070013766 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013767 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
13768 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070013769 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013770 Surface surface = new Surface();
13771 try {
13772 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013773 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070013774 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070013775 + " surface=" + surface);
13776 if (token != null) {
13777 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070013778 try {
Chris Tate6b391282010-10-14 15:48:59 -070013779 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013780 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070013781 } finally {
13782 surface.unlockCanvasAndPost(canvas);
13783 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013784
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013785 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080013786
13787 // Cache the local state object for delivery with DragEvents
13788 root.setLocalDragState(myLocalState);
13789
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013790 // repurpose 'shadowSize' for the last touch point
13791 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070013792
Christopher Tatea53146c2010-09-07 11:57:52 -070013793 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013794 shadowSize.x, shadowSize.y,
13795 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070013796 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070013797
13798 // Off and running! Release our local surface instance; the drag
13799 // shadow surface is now managed by the system process.
13800 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070013801 }
13802 } catch (Exception e) {
13803 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
13804 surface.destroy();
13805 }
13806
13807 return okay;
13808 }
13809
Christopher Tatea53146c2010-09-07 11:57:52 -070013810 /**
Joe Malin32736f02011-01-19 16:14:20 -080013811 * Handles drag events sent by the system following a call to
13812 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
13813 *<p>
13814 * When the system calls this method, it passes a
13815 * {@link android.view.DragEvent} object. A call to
13816 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
13817 * in DragEvent. The method uses these to determine what is happening in the drag and drop
13818 * operation.
13819 * @param event The {@link android.view.DragEvent} sent by the system.
13820 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
13821 * in DragEvent, indicating the type of drag event represented by this object.
13822 * @return {@code true} if the method was successful, otherwise {@code false}.
13823 * <p>
13824 * The method should return {@code true} in response to an action type of
13825 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
13826 * operation.
13827 * </p>
13828 * <p>
13829 * The method should also return {@code true} in response to an action type of
13830 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
13831 * {@code false} if it didn't.
13832 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013833 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070013834 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070013835 return false;
13836 }
13837
13838 /**
Joe Malin32736f02011-01-19 16:14:20 -080013839 * Detects if this View is enabled and has a drag event listener.
13840 * If both are true, then it calls the drag event listener with the
13841 * {@link android.view.DragEvent} it received. If the drag event listener returns
13842 * {@code true}, then dispatchDragEvent() returns {@code true}.
13843 * <p>
13844 * For all other cases, the method calls the
13845 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
13846 * method and returns its result.
13847 * </p>
13848 * <p>
13849 * This ensures that a drag event is always consumed, even if the View does not have a drag
13850 * event listener. However, if the View has a listener and the listener returns true, then
13851 * onDragEvent() is not called.
13852 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013853 */
13854 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080013855 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013856 ListenerInfo li = mListenerInfo;
13857 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
13858 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070013859 return true;
13860 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013861 return onDragEvent(event);
13862 }
13863
Christopher Tate3d4bf172011-03-28 16:16:46 -070013864 boolean canAcceptDrag() {
13865 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
13866 }
13867
Christopher Tatea53146c2010-09-07 11:57:52 -070013868 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070013869 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
13870 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070013871 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070013872 */
13873 public void onCloseSystemDialogs(String reason) {
13874 }
Joe Malin32736f02011-01-19 16:14:20 -080013875
Dianne Hackbornffa42482009-09-23 22:20:11 -070013876 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013877 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070013878 * update a Region being computed for
13879 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013880 * that any non-transparent parts of the Drawable are removed from the
13881 * given transparent region.
13882 *
13883 * @param dr The Drawable whose transparency is to be applied to the region.
13884 * @param region A Region holding the current transparency information,
13885 * where any parts of the region that are set are considered to be
13886 * transparent. On return, this region will be modified to have the
13887 * transparency information reduced by the corresponding parts of the
13888 * Drawable that are not transparent.
13889 * {@hide}
13890 */
13891 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
13892 if (DBG) {
13893 Log.i("View", "Getting transparent region for: " + this);
13894 }
13895 final Region r = dr.getTransparentRegion();
13896 final Rect db = dr.getBounds();
13897 final AttachInfo attachInfo = mAttachInfo;
13898 if (r != null && attachInfo != null) {
13899 final int w = getRight()-getLeft();
13900 final int h = getBottom()-getTop();
13901 if (db.left > 0) {
13902 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
13903 r.op(0, 0, db.left, h, Region.Op.UNION);
13904 }
13905 if (db.right < w) {
13906 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
13907 r.op(db.right, 0, w, h, Region.Op.UNION);
13908 }
13909 if (db.top > 0) {
13910 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
13911 r.op(0, 0, w, db.top, Region.Op.UNION);
13912 }
13913 if (db.bottom < h) {
13914 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
13915 r.op(0, db.bottom, w, h, Region.Op.UNION);
13916 }
13917 final int[] location = attachInfo.mTransparentLocation;
13918 getLocationInWindow(location);
13919 r.translate(location[0], location[1]);
13920 region.op(r, Region.Op.INTERSECT);
13921 } else {
13922 region.op(db, Region.Op.DIFFERENCE);
13923 }
13924 }
13925
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013926 private void checkForLongClick(int delayOffset) {
13927 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
13928 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013929
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013930 if (mPendingCheckForLongPress == null) {
13931 mPendingCheckForLongPress = new CheckForLongPress();
13932 }
13933 mPendingCheckForLongPress.rememberWindowAttachCount();
13934 postDelayed(mPendingCheckForLongPress,
13935 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013937 }
13938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013939 /**
13940 * Inflate a view from an XML resource. This convenience method wraps the {@link
13941 * LayoutInflater} class, which provides a full range of options for view inflation.
13942 *
13943 * @param context The Context object for your activity or application.
13944 * @param resource The resource ID to inflate
13945 * @param root A view group that will be the parent. Used to properly inflate the
13946 * layout_* parameters.
13947 * @see LayoutInflater
13948 */
13949 public static View inflate(Context context, int resource, ViewGroup root) {
13950 LayoutInflater factory = LayoutInflater.from(context);
13951 return factory.inflate(resource, root);
13952 }
Romain Guy33e72ae2010-07-17 12:40:29 -070013953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013954 /**
Adam Powell637d3372010-08-25 14:37:03 -070013955 * Scroll the view with standard behavior for scrolling beyond the normal
13956 * content boundaries. Views that call this method should override
13957 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
13958 * results of an over-scroll operation.
13959 *
13960 * Views can use this method to handle any touch or fling-based scrolling.
13961 *
13962 * @param deltaX Change in X in pixels
13963 * @param deltaY Change in Y in pixels
13964 * @param scrollX Current X scroll value in pixels before applying deltaX
13965 * @param scrollY Current Y scroll value in pixels before applying deltaY
13966 * @param scrollRangeX Maximum content scroll range along the X axis
13967 * @param scrollRangeY Maximum content scroll range along the Y axis
13968 * @param maxOverScrollX Number of pixels to overscroll by in either direction
13969 * along the X axis.
13970 * @param maxOverScrollY Number of pixels to overscroll by in either direction
13971 * along the Y axis.
13972 * @param isTouchEvent true if this scroll operation is the result of a touch event.
13973 * @return true if scrolling was clamped to an over-scroll boundary along either
13974 * axis, false otherwise.
13975 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013976 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070013977 protected boolean overScrollBy(int deltaX, int deltaY,
13978 int scrollX, int scrollY,
13979 int scrollRangeX, int scrollRangeY,
13980 int maxOverScrollX, int maxOverScrollY,
13981 boolean isTouchEvent) {
13982 final int overScrollMode = mOverScrollMode;
13983 final boolean canScrollHorizontal =
13984 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
13985 final boolean canScrollVertical =
13986 computeVerticalScrollRange() > computeVerticalScrollExtent();
13987 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
13988 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
13989 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
13990 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
13991
13992 int newScrollX = scrollX + deltaX;
13993 if (!overScrollHorizontal) {
13994 maxOverScrollX = 0;
13995 }
13996
13997 int newScrollY = scrollY + deltaY;
13998 if (!overScrollVertical) {
13999 maxOverScrollY = 0;
14000 }
14001
14002 // Clamp values if at the limits and record
14003 final int left = -maxOverScrollX;
14004 final int right = maxOverScrollX + scrollRangeX;
14005 final int top = -maxOverScrollY;
14006 final int bottom = maxOverScrollY + scrollRangeY;
14007
14008 boolean clampedX = false;
14009 if (newScrollX > right) {
14010 newScrollX = right;
14011 clampedX = true;
14012 } else if (newScrollX < left) {
14013 newScrollX = left;
14014 clampedX = true;
14015 }
14016
14017 boolean clampedY = false;
14018 if (newScrollY > bottom) {
14019 newScrollY = bottom;
14020 clampedY = true;
14021 } else if (newScrollY < top) {
14022 newScrollY = top;
14023 clampedY = true;
14024 }
14025
14026 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
14027
14028 return clampedX || clampedY;
14029 }
14030
14031 /**
14032 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
14033 * respond to the results of an over-scroll operation.
14034 *
14035 * @param scrollX New X scroll value in pixels
14036 * @param scrollY New Y scroll value in pixels
14037 * @param clampedX True if scrollX was clamped to an over-scroll boundary
14038 * @param clampedY True if scrollY was clamped to an over-scroll boundary
14039 */
14040 protected void onOverScrolled(int scrollX, int scrollY,
14041 boolean clampedX, boolean clampedY) {
14042 // Intentionally empty.
14043 }
14044
14045 /**
14046 * Returns the over-scroll mode for this view. The result will be
14047 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14048 * (allow over-scrolling only if the view content is larger than the container),
14049 * or {@link #OVER_SCROLL_NEVER}.
14050 *
14051 * @return This view's over-scroll mode.
14052 */
14053 public int getOverScrollMode() {
14054 return mOverScrollMode;
14055 }
14056
14057 /**
14058 * Set the over-scroll mode for this view. Valid over-scroll modes are
14059 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14060 * (allow over-scrolling only if the view content is larger than the container),
14061 * or {@link #OVER_SCROLL_NEVER}.
14062 *
14063 * Setting the over-scroll mode of a view will have an effect only if the
14064 * view is capable of scrolling.
14065 *
14066 * @param overScrollMode The new over-scroll mode for this view.
14067 */
14068 public void setOverScrollMode(int overScrollMode) {
14069 if (overScrollMode != OVER_SCROLL_ALWAYS &&
14070 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
14071 overScrollMode != OVER_SCROLL_NEVER) {
14072 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
14073 }
14074 mOverScrollMode = overScrollMode;
14075 }
14076
14077 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014078 * Gets a scale factor that determines the distance the view should scroll
14079 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
14080 * @return The vertical scroll scale factor.
14081 * @hide
14082 */
14083 protected float getVerticalScrollFactor() {
14084 if (mVerticalScrollFactor == 0) {
14085 TypedValue outValue = new TypedValue();
14086 if (!mContext.getTheme().resolveAttribute(
14087 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
14088 throw new IllegalStateException(
14089 "Expected theme to define listPreferredItemHeight.");
14090 }
14091 mVerticalScrollFactor = outValue.getDimension(
14092 mContext.getResources().getDisplayMetrics());
14093 }
14094 return mVerticalScrollFactor;
14095 }
14096
14097 /**
14098 * Gets a scale factor that determines the distance the view should scroll
14099 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
14100 * @return The horizontal scroll scale factor.
14101 * @hide
14102 */
14103 protected float getHorizontalScrollFactor() {
14104 // TODO: Should use something else.
14105 return getVerticalScrollFactor();
14106 }
14107
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014108 /**
14109 * Return the value specifying the text direction or policy that was set with
14110 * {@link #setTextDirection(int)}.
14111 *
14112 * @return the defined text direction. It can be one of:
14113 *
14114 * {@link #TEXT_DIRECTION_INHERIT},
14115 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14116 * {@link #TEXT_DIRECTION_ANY_RTL},
14117 * {@link #TEXT_DIRECTION_LTR},
14118 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014119 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014120 */
14121 public int getTextDirection() {
14122 return mTextDirection;
14123 }
14124
14125 /**
14126 * Set the text direction.
14127 *
14128 * @param textDirection the direction to set. Should be one of:
14129 *
14130 * {@link #TEXT_DIRECTION_INHERIT},
14131 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14132 * {@link #TEXT_DIRECTION_ANY_RTL},
14133 * {@link #TEXT_DIRECTION_LTR},
14134 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014135 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014136 */
14137 public void setTextDirection(int textDirection) {
14138 if (textDirection != mTextDirection) {
14139 mTextDirection = textDirection;
14140 resetResolvedTextDirection();
14141 requestLayout();
14142 }
14143 }
14144
14145 /**
14146 * Return the resolved text direction.
14147 *
14148 * @return the resolved text direction. Return one of:
14149 *
Doug Feltcb3791202011-07-07 11:57:48 -070014150 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14151 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014152 * {@link #TEXT_DIRECTION_LTR},
14153 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014154 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014155 */
14156 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070014157 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014158 resolveTextDirection();
14159 }
14160 return mResolvedTextDirection;
14161 }
14162
14163 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014164 * Resolve the text direction. Will call {@link View#onResolveTextDirection()} when resolution
14165 * is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014166 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014167 public void resolveTextDirection() {
14168 if (mResolvedTextDirection != TEXT_DIRECTION_INHERIT) {
14169 // Resolution has already been done.
14170 return;
14171 }
Doug Feltcb3791202011-07-07 11:57:48 -070014172 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
14173 mResolvedTextDirection = mTextDirection;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014174 } else if (mParent != null && mParent instanceof ViewGroup) {
Doug Feltcb3791202011-07-07 11:57:48 -070014175 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014176 } else {
14177 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Doug Feltcb3791202011-07-07 11:57:48 -070014178 }
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014179 onResolveTextDirection();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014180 }
14181
14182 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014183 * Called when text direction has been resolved. Subclasses that care about text direction
14184 * resolution should override this method. The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014185 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014186 public void onResolveTextDirection() {
14187 }
14188
14189 /**
14190 * Reset resolved text direction. Text direction can be resolved with a call to
14191 * getResolvedTextDirection(). Will call {@link View#onResetResolvedTextDirection()} when
14192 * reset is done.
14193 */
14194 public void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070014195 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014196 onResetResolvedTextDirection();
14197 }
14198
14199 /**
14200 * Called when text direction is reset. Subclasses that care about text direction reset should
14201 * override this method and do a reset of the text direction of their children. The default
14202 * implementation does nothing.
14203 */
14204 public void onResetResolvedTextDirection() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014205 }
14206
Chet Haaseb39f0512011-05-24 14:36:40 -070014207 //
14208 // Properties
14209 //
14210 /**
14211 * A Property wrapper around the <code>alpha</code> functionality handled by the
14212 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
14213 */
Chet Haased47f1532011-12-16 11:18:52 -080014214 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014215 @Override
14216 public void setValue(View object, float value) {
14217 object.setAlpha(value);
14218 }
14219
14220 @Override
14221 public Float get(View object) {
14222 return object.getAlpha();
14223 }
14224 };
14225
14226 /**
14227 * A Property wrapper around the <code>translationX</code> functionality handled by the
14228 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
14229 */
Chet Haased47f1532011-12-16 11:18:52 -080014230 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014231 @Override
14232 public void setValue(View object, float value) {
14233 object.setTranslationX(value);
14234 }
14235
14236 @Override
14237 public Float get(View object) {
14238 return object.getTranslationX();
14239 }
14240 };
14241
14242 /**
14243 * A Property wrapper around the <code>translationY</code> functionality handled by the
14244 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
14245 */
Chet Haased47f1532011-12-16 11:18:52 -080014246 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014247 @Override
14248 public void setValue(View object, float value) {
14249 object.setTranslationY(value);
14250 }
14251
14252 @Override
14253 public Float get(View object) {
14254 return object.getTranslationY();
14255 }
14256 };
14257
14258 /**
14259 * A Property wrapper around the <code>x</code> functionality handled by the
14260 * {@link View#setX(float)} and {@link View#getX()} methods.
14261 */
Chet Haased47f1532011-12-16 11:18:52 -080014262 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014263 @Override
14264 public void setValue(View object, float value) {
14265 object.setX(value);
14266 }
14267
14268 @Override
14269 public Float get(View object) {
14270 return object.getX();
14271 }
14272 };
14273
14274 /**
14275 * A Property wrapper around the <code>y</code> functionality handled by the
14276 * {@link View#setY(float)} and {@link View#getY()} methods.
14277 */
Chet Haased47f1532011-12-16 11:18:52 -080014278 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014279 @Override
14280 public void setValue(View object, float value) {
14281 object.setY(value);
14282 }
14283
14284 @Override
14285 public Float get(View object) {
14286 return object.getY();
14287 }
14288 };
14289
14290 /**
14291 * A Property wrapper around the <code>rotation</code> functionality handled by the
14292 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
14293 */
Chet Haased47f1532011-12-16 11:18:52 -080014294 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014295 @Override
14296 public void setValue(View object, float value) {
14297 object.setRotation(value);
14298 }
14299
14300 @Override
14301 public Float get(View object) {
14302 return object.getRotation();
14303 }
14304 };
14305
14306 /**
14307 * A Property wrapper around the <code>rotationX</code> functionality handled by the
14308 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
14309 */
Chet Haased47f1532011-12-16 11:18:52 -080014310 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014311 @Override
14312 public void setValue(View object, float value) {
14313 object.setRotationX(value);
14314 }
14315
14316 @Override
14317 public Float get(View object) {
14318 return object.getRotationX();
14319 }
14320 };
14321
14322 /**
14323 * A Property wrapper around the <code>rotationY</code> functionality handled by the
14324 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
14325 */
Chet Haased47f1532011-12-16 11:18:52 -080014326 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014327 @Override
14328 public void setValue(View object, float value) {
14329 object.setRotationY(value);
14330 }
14331
14332 @Override
14333 public Float get(View object) {
14334 return object.getRotationY();
14335 }
14336 };
14337
14338 /**
14339 * A Property wrapper around the <code>scaleX</code> functionality handled by the
14340 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
14341 */
Chet Haased47f1532011-12-16 11:18:52 -080014342 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014343 @Override
14344 public void setValue(View object, float value) {
14345 object.setScaleX(value);
14346 }
14347
14348 @Override
14349 public Float get(View object) {
14350 return object.getScaleX();
14351 }
14352 };
14353
14354 /**
14355 * A Property wrapper around the <code>scaleY</code> functionality handled by the
14356 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
14357 */
Chet Haased47f1532011-12-16 11:18:52 -080014358 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014359 @Override
14360 public void setValue(View object, float value) {
14361 object.setScaleY(value);
14362 }
14363
14364 @Override
14365 public Float get(View object) {
14366 return object.getScaleY();
14367 }
14368 };
14369
Jeff Brown33bbfd22011-02-24 20:55:35 -080014370 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014371 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
14372 * Each MeasureSpec represents a requirement for either the width or the height.
14373 * A MeasureSpec is comprised of a size and a mode. There are three possible
14374 * modes:
14375 * <dl>
14376 * <dt>UNSPECIFIED</dt>
14377 * <dd>
14378 * The parent has not imposed any constraint on the child. It can be whatever size
14379 * it wants.
14380 * </dd>
14381 *
14382 * <dt>EXACTLY</dt>
14383 * <dd>
14384 * The parent has determined an exact size for the child. The child is going to be
14385 * given those bounds regardless of how big it wants to be.
14386 * </dd>
14387 *
14388 * <dt>AT_MOST</dt>
14389 * <dd>
14390 * The child can be as large as it wants up to the specified size.
14391 * </dd>
14392 * </dl>
14393 *
14394 * MeasureSpecs are implemented as ints to reduce object allocation. This class
14395 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
14396 */
14397 public static class MeasureSpec {
14398 private static final int MODE_SHIFT = 30;
14399 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
14400
14401 /**
14402 * Measure specification mode: The parent has not imposed any constraint
14403 * on the child. It can be whatever size it wants.
14404 */
14405 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
14406
14407 /**
14408 * Measure specification mode: The parent has determined an exact size
14409 * for the child. The child is going to be given those bounds regardless
14410 * of how big it wants to be.
14411 */
14412 public static final int EXACTLY = 1 << MODE_SHIFT;
14413
14414 /**
14415 * Measure specification mode: The child can be as large as it wants up
14416 * to the specified size.
14417 */
14418 public static final int AT_MOST = 2 << MODE_SHIFT;
14419
14420 /**
14421 * Creates a measure specification based on the supplied size and mode.
14422 *
14423 * The mode must always be one of the following:
14424 * <ul>
14425 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
14426 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
14427 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
14428 * </ul>
14429 *
14430 * @param size the size of the measure specification
14431 * @param mode the mode of the measure specification
14432 * @return the measure specification based on size and mode
14433 */
14434 public static int makeMeasureSpec(int size, int mode) {
14435 return size + mode;
14436 }
14437
14438 /**
14439 * Extracts the mode from the supplied measure specification.
14440 *
14441 * @param measureSpec the measure specification to extract the mode from
14442 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
14443 * {@link android.view.View.MeasureSpec#AT_MOST} or
14444 * {@link android.view.View.MeasureSpec#EXACTLY}
14445 */
14446 public static int getMode(int measureSpec) {
14447 return (measureSpec & MODE_MASK);
14448 }
14449
14450 /**
14451 * Extracts the size from the supplied measure specification.
14452 *
14453 * @param measureSpec the measure specification to extract the size from
14454 * @return the size in pixels defined in the supplied measure specification
14455 */
14456 public static int getSize(int measureSpec) {
14457 return (measureSpec & ~MODE_MASK);
14458 }
14459
14460 /**
14461 * Returns a String representation of the specified measure
14462 * specification.
14463 *
14464 * @param measureSpec the measure specification to convert to a String
14465 * @return a String with the following format: "MeasureSpec: MODE SIZE"
14466 */
14467 public static String toString(int measureSpec) {
14468 int mode = getMode(measureSpec);
14469 int size = getSize(measureSpec);
14470
14471 StringBuilder sb = new StringBuilder("MeasureSpec: ");
14472
14473 if (mode == UNSPECIFIED)
14474 sb.append("UNSPECIFIED ");
14475 else if (mode == EXACTLY)
14476 sb.append("EXACTLY ");
14477 else if (mode == AT_MOST)
14478 sb.append("AT_MOST ");
14479 else
14480 sb.append(mode).append(" ");
14481
14482 sb.append(size);
14483 return sb.toString();
14484 }
14485 }
14486
14487 class CheckForLongPress implements Runnable {
14488
14489 private int mOriginalWindowAttachCount;
14490
14491 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070014492 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014493 && mOriginalWindowAttachCount == mWindowAttachCount) {
14494 if (performLongClick()) {
14495 mHasPerformedLongPress = true;
14496 }
14497 }
14498 }
14499
14500 public void rememberWindowAttachCount() {
14501 mOriginalWindowAttachCount = mWindowAttachCount;
14502 }
14503 }
Joe Malin32736f02011-01-19 16:14:20 -080014504
Adam Powelle14579b2009-12-16 18:39:52 -080014505 private final class CheckForTap implements Runnable {
14506 public void run() {
14507 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080014508 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014509 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080014510 }
14511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014512
Adam Powella35d7682010-03-12 14:48:13 -080014513 private final class PerformClick implements Runnable {
14514 public void run() {
14515 performClick();
14516 }
14517 }
14518
Dianne Hackborn63042d62011-01-26 18:56:29 -080014519 /** @hide */
14520 public void hackTurnOffWindowResizeAnim(boolean off) {
14521 mAttachInfo.mTurnOffWindowResizeAnim = off;
14522 }
Joe Malin32736f02011-01-19 16:14:20 -080014523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014524 /**
Chet Haasea00f3862011-02-22 06:34:40 -080014525 * This method returns a ViewPropertyAnimator object, which can be used to animate
14526 * specific properties on this View.
14527 *
14528 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
14529 */
14530 public ViewPropertyAnimator animate() {
14531 if (mAnimator == null) {
14532 mAnimator = new ViewPropertyAnimator(this);
14533 }
14534 return mAnimator;
14535 }
14536
14537 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014538 * Interface definition for a callback to be invoked when a key event is
14539 * dispatched to this view. The callback will be invoked before the key
14540 * event is given to the view.
14541 */
14542 public interface OnKeyListener {
14543 /**
14544 * Called when a key is dispatched to a view. This allows listeners to
14545 * get a chance to respond before the target view.
14546 *
14547 * @param v The view the key has been dispatched to.
14548 * @param keyCode The code for the physical key that was pressed
14549 * @param event The KeyEvent object containing full information about
14550 * the event.
14551 * @return True if the listener has consumed the event, false otherwise.
14552 */
14553 boolean onKey(View v, int keyCode, KeyEvent event);
14554 }
14555
14556 /**
14557 * Interface definition for a callback to be invoked when a touch event is
14558 * dispatched to this view. The callback will be invoked before the touch
14559 * event is given to the view.
14560 */
14561 public interface OnTouchListener {
14562 /**
14563 * Called when a touch event is dispatched to a view. This allows listeners to
14564 * get a chance to respond before the target view.
14565 *
14566 * @param v The view the touch event has been dispatched to.
14567 * @param event The MotionEvent object containing full information about
14568 * the event.
14569 * @return True if the listener has consumed the event, false otherwise.
14570 */
14571 boolean onTouch(View v, MotionEvent event);
14572 }
14573
14574 /**
Jeff Brown10b62902011-06-20 16:40:37 -070014575 * Interface definition for a callback to be invoked when a hover event is
14576 * dispatched to this view. The callback will be invoked before the hover
14577 * event is given to the view.
14578 */
14579 public interface OnHoverListener {
14580 /**
14581 * Called when a hover event is dispatched to a view. This allows listeners to
14582 * get a chance to respond before the target view.
14583 *
14584 * @param v The view the hover event has been dispatched to.
14585 * @param event The MotionEvent object containing full information about
14586 * the event.
14587 * @return True if the listener has consumed the event, false otherwise.
14588 */
14589 boolean onHover(View v, MotionEvent event);
14590 }
14591
14592 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014593 * Interface definition for a callback to be invoked when a generic motion event is
14594 * dispatched to this view. The callback will be invoked before the generic motion
14595 * event is given to the view.
14596 */
14597 public interface OnGenericMotionListener {
14598 /**
14599 * Called when a generic motion event is dispatched to a view. This allows listeners to
14600 * get a chance to respond before the target view.
14601 *
14602 * @param v The view the generic motion event has been dispatched to.
14603 * @param event The MotionEvent object containing full information about
14604 * the event.
14605 * @return True if the listener has consumed the event, false otherwise.
14606 */
14607 boolean onGenericMotion(View v, MotionEvent event);
14608 }
14609
14610 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014611 * Interface definition for a callback to be invoked when a view has been clicked and held.
14612 */
14613 public interface OnLongClickListener {
14614 /**
14615 * Called when a view has been clicked and held.
14616 *
14617 * @param v The view that was clicked and held.
14618 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080014619 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014620 */
14621 boolean onLongClick(View v);
14622 }
14623
14624 /**
Chris Tate32affef2010-10-18 15:29:21 -070014625 * Interface definition for a callback to be invoked when a drag is being dispatched
14626 * to this view. The callback will be invoked before the hosting view's own
14627 * onDrag(event) method. If the listener wants to fall back to the hosting view's
14628 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070014629 *
14630 * <div class="special reference">
14631 * <h3>Developer Guides</h3>
14632 * <p>For a guide to implementing drag and drop features, read the
14633 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
14634 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070014635 */
14636 public interface OnDragListener {
14637 /**
14638 * Called when a drag event is dispatched to a view. This allows listeners
14639 * to get a chance to override base View behavior.
14640 *
Joe Malin32736f02011-01-19 16:14:20 -080014641 * @param v The View that received the drag event.
14642 * @param event The {@link android.view.DragEvent} object for the drag event.
14643 * @return {@code true} if the drag event was handled successfully, or {@code false}
14644 * if the drag event was not handled. Note that {@code false} will trigger the View
14645 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070014646 */
14647 boolean onDrag(View v, DragEvent event);
14648 }
14649
14650 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014651 * Interface definition for a callback to be invoked when the focus state of
14652 * a view changed.
14653 */
14654 public interface OnFocusChangeListener {
14655 /**
14656 * Called when the focus state of a view has changed.
14657 *
14658 * @param v The view whose state has changed.
14659 * @param hasFocus The new focus state of v.
14660 */
14661 void onFocusChange(View v, boolean hasFocus);
14662 }
14663
14664 /**
14665 * Interface definition for a callback to be invoked when a view is clicked.
14666 */
14667 public interface OnClickListener {
14668 /**
14669 * Called when a view has been clicked.
14670 *
14671 * @param v The view that was clicked.
14672 */
14673 void onClick(View v);
14674 }
14675
14676 /**
14677 * Interface definition for a callback to be invoked when the context menu
14678 * for this view is being built.
14679 */
14680 public interface OnCreateContextMenuListener {
14681 /**
14682 * Called when the context menu for this view is being built. It is not
14683 * safe to hold onto the menu after this method returns.
14684 *
14685 * @param menu The context menu that is being built
14686 * @param v The view for which the context menu is being built
14687 * @param menuInfo Extra information about the item for which the
14688 * context menu should be shown. This information will vary
14689 * depending on the class of v.
14690 */
14691 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
14692 }
14693
Joe Onorato664644d2011-01-23 17:53:23 -080014694 /**
14695 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014696 * visibility. This reports <strong>global</strong> changes to the system UI
14697 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080014698 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070014699 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080014700 */
14701 public interface OnSystemUiVisibilityChangeListener {
14702 /**
14703 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070014704 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080014705 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040014706 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014707 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
14708 * <strong>global</strong> state of the UI visibility flags, not what your
14709 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080014710 */
14711 public void onSystemUiVisibilityChange(int visibility);
14712 }
14713
Adam Powell4afd62b2011-02-18 15:02:18 -080014714 /**
14715 * Interface definition for a callback to be invoked when this view is attached
14716 * or detached from its window.
14717 */
14718 public interface OnAttachStateChangeListener {
14719 /**
14720 * Called when the view is attached to a window.
14721 * @param v The view that was attached
14722 */
14723 public void onViewAttachedToWindow(View v);
14724 /**
14725 * Called when the view is detached from a window.
14726 * @param v The view that was detached
14727 */
14728 public void onViewDetachedFromWindow(View v);
14729 }
14730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014731 private final class UnsetPressedState implements Runnable {
14732 public void run() {
14733 setPressed(false);
14734 }
14735 }
14736
14737 /**
14738 * Base class for derived classes that want to save and restore their own
14739 * state in {@link android.view.View#onSaveInstanceState()}.
14740 */
14741 public static class BaseSavedState extends AbsSavedState {
14742 /**
14743 * Constructor used when reading from a parcel. Reads the state of the superclass.
14744 *
14745 * @param source
14746 */
14747 public BaseSavedState(Parcel source) {
14748 super(source);
14749 }
14750
14751 /**
14752 * Constructor called by derived classes when creating their SavedState objects
14753 *
14754 * @param superState The state of the superclass of this view
14755 */
14756 public BaseSavedState(Parcelable superState) {
14757 super(superState);
14758 }
14759
14760 public static final Parcelable.Creator<BaseSavedState> CREATOR =
14761 new Parcelable.Creator<BaseSavedState>() {
14762 public BaseSavedState createFromParcel(Parcel in) {
14763 return new BaseSavedState(in);
14764 }
14765
14766 public BaseSavedState[] newArray(int size) {
14767 return new BaseSavedState[size];
14768 }
14769 };
14770 }
14771
14772 /**
14773 * A set of information given to a view when it is attached to its parent
14774 * window.
14775 */
14776 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014777 interface Callbacks {
14778 void playSoundEffect(int effectId);
14779 boolean performHapticFeedback(int effectId, boolean always);
14780 }
14781
14782 /**
14783 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
14784 * to a Handler. This class contains the target (View) to invalidate and
14785 * the coordinates of the dirty rectangle.
14786 *
14787 * For performance purposes, this class also implements a pool of up to
14788 * POOL_LIMIT objects that get reused. This reduces memory allocations
14789 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014790 */
Romain Guyd928d682009-03-31 17:52:16 -070014791 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014792 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070014793 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
14794 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070014795 public InvalidateInfo newInstance() {
14796 return new InvalidateInfo();
14797 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014798
Romain Guyd928d682009-03-31 17:52:16 -070014799 public void onAcquired(InvalidateInfo element) {
14800 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014801
Romain Guyd928d682009-03-31 17:52:16 -070014802 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070014803 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070014804 }
14805 }, POOL_LIMIT)
14806 );
14807
14808 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014809 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014810
14811 View target;
14812
14813 int left;
14814 int top;
14815 int right;
14816 int bottom;
14817
Romain Guyd928d682009-03-31 17:52:16 -070014818 public void setNextPoolable(InvalidateInfo element) {
14819 mNext = element;
14820 }
14821
14822 public InvalidateInfo getNextPoolable() {
14823 return mNext;
14824 }
14825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014826 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070014827 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014828 }
14829
14830 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070014831 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014832 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014833
14834 public boolean isPooled() {
14835 return mIsPooled;
14836 }
14837
14838 public void setPooled(boolean isPooled) {
14839 mIsPooled = isPooled;
14840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014841 }
14842
14843 final IWindowSession mSession;
14844
14845 final IWindow mWindow;
14846
14847 final IBinder mWindowToken;
14848
14849 final Callbacks mRootCallbacks;
14850
Romain Guy59a12ca2011-06-09 17:48:21 -070014851 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080014852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014853 /**
14854 * The top view of the hierarchy.
14855 */
14856 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070014857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014858 IBinder mPanelParentWindowToken;
14859 Surface mSurface;
14860
Romain Guyb051e892010-09-28 19:09:36 -070014861 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014862 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070014863 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080014864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014865 /**
Romain Guy8506ab42009-06-11 17:35:47 -070014866 * Scale factor used by the compatibility mode
14867 */
14868 float mApplicationScale;
14869
14870 /**
14871 * Indicates whether the application is in compatibility mode
14872 */
14873 boolean mScalingRequired;
14874
14875 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014876 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080014877 */
14878 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080014879
Dianne Hackborn63042d62011-01-26 18:56:29 -080014880 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014881 * Left position of this view's window
14882 */
14883 int mWindowLeft;
14884
14885 /**
14886 * Top position of this view's window
14887 */
14888 int mWindowTop;
14889
14890 /**
Adam Powell26153a32010-11-08 15:22:27 -080014891 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070014892 */
Adam Powell26153a32010-11-08 15:22:27 -080014893 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070014894
14895 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014896 * For windows that are full-screen but using insets to layout inside
14897 * of the screen decorations, these are the current insets for the
14898 * content of the window.
14899 */
14900 final Rect mContentInsets = new Rect();
14901
14902 /**
14903 * For windows that are full-screen but using insets to layout inside
14904 * of the screen decorations, these are the current insets for the
14905 * actual visible parts of the window.
14906 */
14907 final Rect mVisibleInsets = new Rect();
14908
14909 /**
14910 * The internal insets given by this window. This value is
14911 * supplied by the client (through
14912 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
14913 * be given to the window manager when changed to be used in laying
14914 * out windows behind it.
14915 */
14916 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
14917 = new ViewTreeObserver.InternalInsetsInfo();
14918
14919 /**
14920 * All views in the window's hierarchy that serve as scroll containers,
14921 * used to determine if the window can be resized or must be panned
14922 * to adjust for a soft input area.
14923 */
14924 final ArrayList<View> mScrollContainers = new ArrayList<View>();
14925
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070014926 final KeyEvent.DispatcherState mKeyDispatchState
14927 = new KeyEvent.DispatcherState();
14928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014929 /**
14930 * Indicates whether the view's window currently has the focus.
14931 */
14932 boolean mHasWindowFocus;
14933
14934 /**
14935 * The current visibility of the window.
14936 */
14937 int mWindowVisibility;
14938
14939 /**
14940 * Indicates the time at which drawing started to occur.
14941 */
14942 long mDrawingTime;
14943
14944 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070014945 * Indicates whether or not ignoring the DIRTY_MASK flags.
14946 */
14947 boolean mIgnoreDirtyState;
14948
14949 /**
Romain Guy02ccac62011-06-24 13:20:23 -070014950 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
14951 * to avoid clearing that flag prematurely.
14952 */
14953 boolean mSetIgnoreDirtyState = false;
14954
14955 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014956 * Indicates whether the view's window is currently in touch mode.
14957 */
14958 boolean mInTouchMode;
14959
14960 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014961 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014962 * the next time it performs a traversal
14963 */
14964 boolean mRecomputeGlobalAttributes;
14965
14966 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014967 * Always report new attributes at next traversal.
14968 */
14969 boolean mForceReportNewAttributes;
14970
14971 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014972 * Set during a traveral if any views want to keep the screen on.
14973 */
14974 boolean mKeepScreenOn;
14975
14976 /**
Joe Onorato664644d2011-01-23 17:53:23 -080014977 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
14978 */
14979 int mSystemUiVisibility;
14980
14981 /**
14982 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
14983 * attached.
14984 */
14985 boolean mHasSystemUiListeners;
14986
14987 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014988 * Set if the visibility of any views has changed.
14989 */
14990 boolean mViewVisibilityChanged;
14991
14992 /**
14993 * Set to true if a view has been scrolled.
14994 */
14995 boolean mViewScrollChanged;
14996
14997 /**
14998 * Global to the view hierarchy used as a temporary for dealing with
14999 * x/y points in the transparent region computations.
15000 */
15001 final int[] mTransparentLocation = new int[2];
15002
15003 /**
15004 * Global to the view hierarchy used as a temporary for dealing with
15005 * x/y points in the ViewGroup.invalidateChild implementation.
15006 */
15007 final int[] mInvalidateChildLocation = new int[2];
15008
Chet Haasec3aa3612010-06-17 08:50:37 -070015009
15010 /**
15011 * Global to the view hierarchy used as a temporary for dealing with
15012 * x/y location when view is transformed.
15013 */
15014 final float[] mTmpTransformLocation = new float[2];
15015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015016 /**
15017 * The view tree observer used to dispatch global events like
15018 * layout, pre-draw, touch mode change, etc.
15019 */
15020 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
15021
15022 /**
15023 * A Canvas used by the view hierarchy to perform bitmap caching.
15024 */
15025 Canvas mCanvas;
15026
15027 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080015028 * The view root impl.
15029 */
15030 final ViewRootImpl mViewRootImpl;
15031
15032 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015033 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015034 * handler can be used to pump events in the UI events queue.
15035 */
15036 final Handler mHandler;
15037
15038 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015039 * Temporary for use in computing invalidate rectangles while
15040 * calling up the hierarchy.
15041 */
15042 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070015043
15044 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070015045 * Temporary for use in computing hit areas with transformed views
15046 */
15047 final RectF mTmpTransformRect = new RectF();
15048
15049 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070015050 * Temporary list for use in collecting focusable descendents of a view.
15051 */
15052 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
15053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015054 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070015055 * The id of the window for accessibility purposes.
15056 */
15057 int mAccessibilityWindowId = View.NO_ID;
15058
15059 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015060 * Creates a new set of attachment information with the specified
15061 * events handler and thread.
15062 *
15063 * @param handler the events handler the view must use
15064 */
15065 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080015066 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015067 mSession = session;
15068 mWindow = window;
15069 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080015070 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015071 mHandler = handler;
15072 mRootCallbacks = effectPlayer;
15073 }
15074 }
15075
15076 /**
15077 * <p>ScrollabilityCache holds various fields used by a View when scrolling
15078 * is supported. This avoids keeping too many unused fields in most
15079 * instances of View.</p>
15080 */
Mike Cleronf116bf82009-09-27 19:14:12 -070015081 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080015082
Mike Cleronf116bf82009-09-27 19:14:12 -070015083 /**
15084 * Scrollbars are not visible
15085 */
15086 public static final int OFF = 0;
15087
15088 /**
15089 * Scrollbars are visible
15090 */
15091 public static final int ON = 1;
15092
15093 /**
15094 * Scrollbars are fading away
15095 */
15096 public static final int FADING = 2;
15097
15098 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080015099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015100 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070015101 public int scrollBarDefaultDelayBeforeFade;
15102 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015103
15104 public int scrollBarSize;
15105 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070015106 public float[] interpolatorValues;
15107 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015108
15109 public final Paint paint;
15110 public final Matrix matrix;
15111 public Shader shader;
15112
Mike Cleronf116bf82009-09-27 19:14:12 -070015113 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
15114
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015115 private static final float[] OPAQUE = { 255 };
15116 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080015117
Mike Cleronf116bf82009-09-27 19:14:12 -070015118 /**
15119 * When fading should start. This time moves into the future every time
15120 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
15121 */
15122 public long fadeStartTime;
15123
15124
15125 /**
15126 * The current state of the scrollbars: ON, OFF, or FADING
15127 */
15128 public int state = OFF;
15129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015130 private int mLastColor;
15131
Mike Cleronf116bf82009-09-27 19:14:12 -070015132 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015133 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
15134 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070015135 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
15136 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015137
15138 paint = new Paint();
15139 matrix = new Matrix();
15140 // use use a height of 1, and then wack the matrix each time we
15141 // actually use it.
15142 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070015143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015144 paint.setShader(shader);
15145 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070015146 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015147 }
Romain Guy8506ab42009-06-11 17:35:47 -070015148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015149 public void setFadeColor(int color) {
15150 if (color != 0 && color != mLastColor) {
15151 mLastColor = color;
15152 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070015153
Romain Guye55e1a72009-08-27 10:42:26 -070015154 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
15155 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070015156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015157 paint.setShader(shader);
15158 // Restore the default transfer mode (src_over)
15159 paint.setXfermode(null);
15160 }
15161 }
Joe Malin32736f02011-01-19 16:14:20 -080015162
Mike Cleronf116bf82009-09-27 19:14:12 -070015163 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070015164 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070015165 if (now >= fadeStartTime) {
15166
15167 // the animation fades the scrollbars out by changing
15168 // the opacity (alpha) from fully opaque to fully
15169 // transparent
15170 int nextFrame = (int) now;
15171 int framesCount = 0;
15172
15173 Interpolator interpolator = scrollBarInterpolator;
15174
15175 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015176 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070015177
15178 // End transparent
15179 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015180 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070015181
15182 state = FADING;
15183
15184 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080015185 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070015186 }
15187 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070015188 }
Mike Cleronf116bf82009-09-27 19:14:12 -070015189
Svetoslav Ganova0156172011-06-26 17:55:44 -070015190 /**
15191 * Resuable callback for sending
15192 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
15193 */
15194 private class SendViewScrolledAccessibilityEvent implements Runnable {
15195 public volatile boolean mIsPending;
15196
15197 public void run() {
15198 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
15199 mIsPending = false;
15200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015201 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015202
15203 /**
15204 * <p>
15205 * This class represents a delegate that can be registered in a {@link View}
15206 * to enhance accessibility support via composition rather via inheritance.
15207 * It is specifically targeted to widget developers that extend basic View
15208 * classes i.e. classes in package android.view, that would like their
15209 * applications to be backwards compatible.
15210 * </p>
15211 * <p>
15212 * A scenario in which a developer would like to use an accessibility delegate
15213 * is overriding a method introduced in a later API version then the minimal API
15214 * version supported by the application. For example, the method
15215 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
15216 * in API version 4 when the accessibility APIs were first introduced. If a
15217 * developer would like his application to run on API version 4 devices (assuming
15218 * all other APIs used by the application are version 4 or lower) and take advantage
15219 * of this method, instead of overriding the method which would break the application's
15220 * backwards compatibility, he can override the corresponding method in this
15221 * delegate and register the delegate in the target View if the API version of
15222 * the system is high enough i.e. the API version is same or higher to the API
15223 * version that introduced
15224 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
15225 * </p>
15226 * <p>
15227 * Here is an example implementation:
15228 * </p>
15229 * <code><pre><p>
15230 * if (Build.VERSION.SDK_INT >= 14) {
15231 * // If the API version is equal of higher than the version in
15232 * // which onInitializeAccessibilityNodeInfo was introduced we
15233 * // register a delegate with a customized implementation.
15234 * View view = findViewById(R.id.view_id);
15235 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
15236 * public void onInitializeAccessibilityNodeInfo(View host,
15237 * AccessibilityNodeInfo info) {
15238 * // Let the default implementation populate the info.
15239 * super.onInitializeAccessibilityNodeInfo(host, info);
15240 * // Set some other information.
15241 * info.setEnabled(host.isEnabled());
15242 * }
15243 * });
15244 * }
15245 * </code></pre></p>
15246 * <p>
15247 * This delegate contains methods that correspond to the accessibility methods
15248 * in View. If a delegate has been specified the implementation in View hands
15249 * off handling to the corresponding method in this delegate. The default
15250 * implementation the delegate methods behaves exactly as the corresponding
15251 * method in View for the case of no accessibility delegate been set. Hence,
15252 * to customize the behavior of a View method, clients can override only the
15253 * corresponding delegate method without altering the behavior of the rest
15254 * accessibility related methods of the host view.
15255 * </p>
15256 */
15257 public static class AccessibilityDelegate {
15258
15259 /**
15260 * Sends an accessibility event of the given type. If accessibility is not
15261 * enabled this method has no effect.
15262 * <p>
15263 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
15264 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
15265 * been set.
15266 * </p>
15267 *
15268 * @param host The View hosting the delegate.
15269 * @param eventType The type of the event to send.
15270 *
15271 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
15272 */
15273 public void sendAccessibilityEvent(View host, int eventType) {
15274 host.sendAccessibilityEventInternal(eventType);
15275 }
15276
15277 /**
15278 * Sends an accessibility event. This method behaves exactly as
15279 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
15280 * empty {@link AccessibilityEvent} and does not perform a check whether
15281 * accessibility is enabled.
15282 * <p>
15283 * The default implementation behaves as
15284 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15285 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
15286 * the case of no accessibility delegate been set.
15287 * </p>
15288 *
15289 * @param host The View hosting the delegate.
15290 * @param event The event to send.
15291 *
15292 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15293 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15294 */
15295 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
15296 host.sendAccessibilityEventUncheckedInternal(event);
15297 }
15298
15299 /**
15300 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
15301 * to its children for adding their text content to the event.
15302 * <p>
15303 * The default implementation behaves as
15304 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15305 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
15306 * the case of no accessibility delegate been set.
15307 * </p>
15308 *
15309 * @param host The View hosting the delegate.
15310 * @param event The event.
15311 * @return True if the event population was completed.
15312 *
15313 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15314 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15315 */
15316 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15317 return host.dispatchPopulateAccessibilityEventInternal(event);
15318 }
15319
15320 /**
15321 * Gives a chance to the host View to populate the accessibility event with its
15322 * text content.
15323 * <p>
15324 * The default implementation behaves as
15325 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
15326 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
15327 * the case of no accessibility delegate been set.
15328 * </p>
15329 *
15330 * @param host The View hosting the delegate.
15331 * @param event The accessibility event which to populate.
15332 *
15333 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
15334 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
15335 */
15336 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15337 host.onPopulateAccessibilityEventInternal(event);
15338 }
15339
15340 /**
15341 * Initializes an {@link AccessibilityEvent} with information about the
15342 * the host View which is the event source.
15343 * <p>
15344 * The default implementation behaves as
15345 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
15346 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
15347 * the case of no accessibility delegate been set.
15348 * </p>
15349 *
15350 * @param host The View hosting the delegate.
15351 * @param event The event to initialize.
15352 *
15353 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
15354 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
15355 */
15356 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
15357 host.onInitializeAccessibilityEventInternal(event);
15358 }
15359
15360 /**
15361 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
15362 * <p>
15363 * The default implementation behaves as
15364 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15365 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
15366 * the case of no accessibility delegate been set.
15367 * </p>
15368 *
15369 * @param host The View hosting the delegate.
15370 * @param info The instance to initialize.
15371 *
15372 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15373 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15374 */
15375 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
15376 host.onInitializeAccessibilityNodeInfoInternal(info);
15377 }
15378
15379 /**
15380 * Called when a child of the host View has requested sending an
15381 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
15382 * to augment the event.
15383 * <p>
15384 * The default implementation behaves as
15385 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15386 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
15387 * the case of no accessibility delegate been set.
15388 * </p>
15389 *
15390 * @param host The View hosting the delegate.
15391 * @param child The child which requests sending the event.
15392 * @param event The event to be sent.
15393 * @return True if the event should be sent
15394 *
15395 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15396 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15397 */
15398 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
15399 AccessibilityEvent event) {
15400 return host.onRequestSendAccessibilityEventInternal(child, event);
15401 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070015402
15403 /**
15404 * Gets the provider for managing a virtual view hierarchy rooted at this View
15405 * and reported to {@link android.accessibilityservice.AccessibilityService}s
15406 * that explore the window content.
15407 * <p>
15408 * The default implementation behaves as
15409 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
15410 * the case of no accessibility delegate been set.
15411 * </p>
15412 *
15413 * @return The provider.
15414 *
15415 * @see AccessibilityNodeProvider
15416 */
15417 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
15418 return null;
15419 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015421}