blob: d193d6e41504d3ed0c5c0a479e9289a676666b1f [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070066import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070067import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.inputmethod.InputConnection;
69import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.widget.ScrollBarDrawable;
71
Romain Guy1ef3fdb2011-09-09 15:30:30 -070072import static android.os.Build.VERSION_CODES.*;
73
Doug Feltcb3791202011-07-07 11:57:48 -070074import com.android.internal.R;
75import com.android.internal.util.Predicate;
76import com.android.internal.view.menu.MenuBuilder;
77
Christopher Tatea0374192010-10-05 13:06:41 -070078import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070079import java.lang.reflect.InvocationTargetException;
80import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import java.util.ArrayList;
82import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070083import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080084import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085
86/**
87 * <p>
88 * This class represents the basic building block for user interface components. A View
89 * occupies a rectangular area on the screen and is responsible for drawing and
90 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070091 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
93 * are invisible containers that hold other Views (or other ViewGroups) and define
94 * their layout properties.
95 * </p>
96 *
97 * <div class="special">
Romain Guy8506ab42009-06-11 17:35:47 -070098 * <p>For an introduction to using this class to develop your
99 * application's user interface, read the Developer Guide documentation on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics
Romain Guy8506ab42009-06-11 17:35:47 -0700101 * include:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>
103 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>
104 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a>
105 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a>
106 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a>
107 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>
108 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>
109 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>.
110 * </p>
111 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700112 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 * <a name="Using"></a>
114 * <h3>Using Views</h3>
115 * <p>
116 * All of the views in a window are arranged in a single tree. You can add views
117 * either from code or by specifying a tree of views in one or more XML layout
118 * files. There are many specialized subclasses of views that act as controls or
119 * are capable of displaying text, images, or other content.
120 * </p>
121 * <p>
122 * Once you have created a tree of views, there are typically a few types of
123 * common operations you may wish to perform:
124 * <ul>
125 * <li><strong>Set properties:</strong> for example setting the text of a
126 * {@link android.widget.TextView}. The available properties and the methods
127 * that set them will vary among the different subclasses of views. Note that
128 * properties that are known at build time can be set in the XML layout
129 * files.</li>
130 * <li><strong>Set focus:</strong> The framework will handled moving focus in
131 * response to user input. To force focus to a specific view, call
132 * {@link #requestFocus}.</li>
133 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
134 * that will be notified when something interesting happens to the view. For
135 * example, all views will let you set a listener to be notified when the view
136 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700137 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
138 * Other view subclasses offer more specialized listeners. For example, a Button
139 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700141 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 * </ul>
143 * </p>
144 * <p><em>
145 * Note: The Android framework is responsible for measuring, laying out and
146 * drawing views. You should not call methods that perform these actions on
147 * views yourself unless you are actually implementing a
148 * {@link android.view.ViewGroup}.
149 * </em></p>
150 *
151 * <a name="Lifecycle"></a>
152 * <h3>Implementing a Custom View</h3>
153 *
154 * <p>
155 * To implement a custom view, you will usually begin by providing overrides for
156 * some of the standard methods that the framework calls on all views. You do
157 * not need to override all of these methods. In fact, you can start by just
158 * overriding {@link #onDraw(android.graphics.Canvas)}.
159 * <table border="2" width="85%" align="center" cellpadding="5">
160 * <thead>
161 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
162 * </thead>
163 *
164 * <tbody>
165 * <tr>
166 * <td rowspan="2">Creation</td>
167 * <td>Constructors</td>
168 * <td>There is a form of the constructor that are called when the view
169 * is created from code and a form that is called when the view is
170 * inflated from a layout file. The second form should parse and apply
171 * any attributes defined in the layout file.
172 * </td>
173 * </tr>
174 * <tr>
175 * <td><code>{@link #onFinishInflate()}</code></td>
176 * <td>Called after a view and all of its children has been inflated
177 * from XML.</td>
178 * </tr>
179 *
180 * <tr>
181 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700182 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 * <td>Called to determine the size requirements for this view and all
184 * of its children.
185 * </td>
186 * </tr>
187 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700188 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 * <td>Called when this view should assign a size and position to all
190 * of its children.
191 * </td>
192 * </tr>
193 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700194 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 * <td>Called when the size of this view has changed.
196 * </td>
197 * </tr>
198 *
199 * <tr>
200 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700201 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 * <td>Called when the view should render its content.
203 * </td>
204 * </tr>
205 *
206 * <tr>
207 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700208 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 * <td>Called when a new key event occurs.
210 * </td>
211 * </tr>
212 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700213 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 * <td>Called when a key up event occurs.
215 * </td>
216 * </tr>
217 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700218 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 * <td>Called when a trackball motion event occurs.
220 * </td>
221 * </tr>
222 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700223 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 * <td>Called when a touch screen motion event occurs.
225 * </td>
226 * </tr>
227 *
228 * <tr>
229 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700230 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 * <td>Called when the view gains or loses focus.
232 * </td>
233 * </tr>
234 *
235 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700236 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 * <td>Called when the window containing the view gains or loses focus.
238 * </td>
239 * </tr>
240 *
241 * <tr>
242 * <td rowspan="3">Attaching</td>
243 * <td><code>{@link #onAttachedToWindow()}</code></td>
244 * <td>Called when the view is attached to a window.
245 * </td>
246 * </tr>
247 *
248 * <tr>
249 * <td><code>{@link #onDetachedFromWindow}</code></td>
250 * <td>Called when the view is detached from its window.
251 * </td>
252 * </tr>
253 *
254 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700255 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 * <td>Called when the visibility of the window containing the view
257 * has changed.
258 * </td>
259 * </tr>
260 * </tbody>
261 *
262 * </table>
263 * </p>
264 *
265 * <a name="IDs"></a>
266 * <h3>IDs</h3>
267 * Views may have an integer id associated with them. These ids are typically
268 * assigned in the layout XML files, and are used to find specific views within
269 * the view tree. A common pattern is to:
270 * <ul>
271 * <li>Define a Button in the layout file and assign it a unique ID.
272 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700273 * &lt;Button
274 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 * android:layout_width="wrap_content"
276 * android:layout_height="wrap_content"
277 * android:text="@string/my_button_text"/&gt;
278 * </pre></li>
279 * <li>From the onCreate method of an Activity, find the Button
280 * <pre class="prettyprint">
281 * Button myButton = (Button) findViewById(R.id.my_button);
282 * </pre></li>
283 * </ul>
284 * <p>
285 * View IDs need not be unique throughout the tree, but it is good practice to
286 * ensure that they are at least unique within the part of the tree you are
287 * searching.
288 * </p>
289 *
290 * <a name="Position"></a>
291 * <h3>Position</h3>
292 * <p>
293 * The geometry of a view is that of a rectangle. A view has a location,
294 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
295 * two dimensions, expressed as a width and a height. The unit for location
296 * and dimensions is the pixel.
297 * </p>
298 *
299 * <p>
300 * It is possible to retrieve the location of a view by invoking the methods
301 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
302 * coordinate of the rectangle representing the view. The latter returns the
303 * top, or Y, coordinate of the rectangle representing the view. These methods
304 * both return the location of the view relative to its parent. For instance,
305 * when getLeft() returns 20, that means the view is located 20 pixels to the
306 * right of the left edge of its direct parent.
307 * </p>
308 *
309 * <p>
310 * In addition, several convenience methods are offered to avoid unnecessary
311 * computations, namely {@link #getRight()} and {@link #getBottom()}.
312 * These methods return the coordinates of the right and bottom edges of the
313 * rectangle representing the view. For instance, calling {@link #getRight()}
314 * is similar to the following computation: <code>getLeft() + getWidth()</code>
315 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
316 * </p>
317 *
318 * <a name="SizePaddingMargins"></a>
319 * <h3>Size, padding and margins</h3>
320 * <p>
321 * The size of a view is expressed with a width and a height. A view actually
322 * possess two pairs of width and height values.
323 * </p>
324 *
325 * <p>
326 * The first pair is known as <em>measured width</em> and
327 * <em>measured height</em>. These dimensions define how big a view wants to be
328 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
329 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
330 * and {@link #getMeasuredHeight()}.
331 * </p>
332 *
333 * <p>
334 * The second pair is simply known as <em>width</em> and <em>height</em>, or
335 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
336 * dimensions define the actual size of the view on screen, at drawing time and
337 * after layout. These values may, but do not have to, be different from the
338 * measured width and height. The width and height can be obtained by calling
339 * {@link #getWidth()} and {@link #getHeight()}.
340 * </p>
341 *
342 * <p>
343 * To measure its dimensions, a view takes into account its padding. The padding
344 * is expressed in pixels for the left, top, right and bottom parts of the view.
345 * Padding can be used to offset the content of the view by a specific amount of
346 * pixels. For instance, a left padding of 2 will push the view's content by
347 * 2 pixels to the right of the left edge. Padding can be set using the
348 * {@link #setPadding(int, int, int, int)} method and queried by calling
349 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
Fabrice Di Megliod8703a92011-06-16 18:54:08 -0700350 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 * </p>
352 *
353 * <p>
354 * Even though a view can define a padding, it does not provide any support for
355 * margins. However, view groups provide such a support. Refer to
356 * {@link android.view.ViewGroup} and
357 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
358 * </p>
359 *
360 * <a name="Layout"></a>
361 * <h3>Layout</h3>
362 * <p>
363 * Layout is a two pass process: a measure pass and a layout pass. The measuring
364 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
365 * of the view tree. Each view pushes dimension specifications down the tree
366 * during the recursion. At the end of the measure pass, every view has stored
367 * its measurements. The second pass happens in
368 * {@link #layout(int,int,int,int)} and is also top-down. During
369 * this pass each parent is responsible for positioning all of its children
370 * using the sizes computed in the measure pass.
371 * </p>
372 *
373 * <p>
374 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
375 * {@link #getMeasuredHeight()} values must be set, along with those for all of
376 * that view's descendants. A view's measured width and measured height values
377 * must respect the constraints imposed by the view's parents. This guarantees
378 * that at the end of the measure pass, all parents accept all of their
379 * children's measurements. A parent view may call measure() more than once on
380 * its children. For example, the parent may measure each child once with
381 * unspecified dimensions to find out how big they want to be, then call
382 * measure() on them again with actual numbers if the sum of all the children's
383 * unconstrained sizes is too big or too small.
384 * </p>
385 *
386 * <p>
387 * The measure pass uses two classes to communicate dimensions. The
388 * {@link MeasureSpec} class is used by views to tell their parents how they
389 * want to be measured and positioned. The base LayoutParams class just
390 * describes how big the view wants to be for both width and height. For each
391 * dimension, it can specify one of:
392 * <ul>
393 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800394 * <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 -0800395 * (minus padding)
396 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
397 * enclose its content (plus padding).
398 * </ul>
399 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
400 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
401 * an X and Y value.
402 * </p>
403 *
404 * <p>
405 * MeasureSpecs are used to push requirements down the tree from parent to
406 * child. A MeasureSpec can be in one of three modes:
407 * <ul>
408 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
409 * of a child view. For example, a LinearLayout may call measure() on its child
410 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
411 * tall the child view wants to be given a width of 240 pixels.
412 * <li>EXACTLY: This is used by the parent to impose an exact size on the
413 * child. The child must use this size, and guarantee that all of its
414 * descendants will fit within this size.
415 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
416 * child. The child must gurantee that it and all of its descendants will fit
417 * within this size.
418 * </ul>
419 * </p>
420 *
421 * <p>
422 * To intiate a layout, call {@link #requestLayout}. This method is typically
423 * called by a view on itself when it believes that is can no longer fit within
424 * its current bounds.
425 * </p>
426 *
427 * <a name="Drawing"></a>
428 * <h3>Drawing</h3>
429 * <p>
430 * Drawing is handled by walking the tree and rendering each view that
431 * intersects the the invalid region. Because the tree is traversed in-order,
432 * this means that parents will draw before (i.e., behind) their children, with
433 * siblings drawn in the order they appear in the tree.
434 * If you set a background drawable for a View, then the View will draw it for you
435 * before calling back to its <code>onDraw()</code> method.
436 * </p>
437 *
438 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700439 * 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 -0800440 * </p>
441 *
442 * <p>
443 * To force a view to draw, call {@link #invalidate()}.
444 * </p>
445 *
446 * <a name="EventHandlingThreading"></a>
447 * <h3>Event Handling and Threading</h3>
448 * <p>
449 * The basic cycle of a view is as follows:
450 * <ol>
451 * <li>An event comes in and is dispatched to the appropriate view. The view
452 * handles the event and notifies any listeners.</li>
453 * <li>If in the course of processing the event, the view's bounds may need
454 * to be changed, the view will call {@link #requestLayout()}.</li>
455 * <li>Similarly, if in the course of processing the event the view's appearance
456 * may need to be changed, the view will call {@link #invalidate()}.</li>
457 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
458 * the framework will take care of measuring, laying out, and drawing the tree
459 * as appropriate.</li>
460 * </ol>
461 * </p>
462 *
463 * <p><em>Note: The entire view tree is single threaded. You must always be on
464 * the UI thread when calling any method on any view.</em>
465 * If you are doing work on other threads and want to update the state of a view
466 * from that thread, you should use a {@link Handler}.
467 * </p>
468 *
469 * <a name="FocusHandling"></a>
470 * <h3>Focus Handling</h3>
471 * <p>
472 * The framework will handle routine focus movement in response to user input.
473 * This includes changing the focus as views are removed or hidden, or as new
474 * views become available. Views indicate their willingness to take focus
475 * through the {@link #isFocusable} method. To change whether a view can take
476 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
477 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
478 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
479 * </p>
480 * <p>
481 * Focus movement is based on an algorithm which finds the nearest neighbor in a
482 * given direction. In rare cases, the default algorithm may not match the
483 * intended behavior of the developer. In these situations, you can provide
484 * explicit overrides by using these XML attributes in the layout file:
485 * <pre>
486 * nextFocusDown
487 * nextFocusLeft
488 * nextFocusRight
489 * nextFocusUp
490 * </pre>
491 * </p>
492 *
493 *
494 * <p>
495 * To get a particular view to take focus, call {@link #requestFocus()}.
496 * </p>
497 *
498 * <a name="TouchMode"></a>
499 * <h3>Touch Mode</h3>
500 * <p>
501 * When a user is navigating a user interface via directional keys such as a D-pad, it is
502 * necessary to give focus to actionable items such as buttons so the user can see
503 * what will take input. If the device has touch capabilities, however, and the user
504 * begins interacting with the interface by touching it, it is no longer necessary to
505 * always highlight, or give focus to, a particular view. This motivates a mode
506 * for interaction named 'touch mode'.
507 * </p>
508 * <p>
509 * For a touch capable device, once the user touches the screen, the device
510 * will enter touch mode. From this point onward, only views for which
511 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
512 * Other views that are touchable, like buttons, will not take focus when touched; they will
513 * only fire the on click listeners.
514 * </p>
515 * <p>
516 * Any time a user hits a directional key, such as a D-pad direction, the view device will
517 * exit touch mode, and find a view to take focus, so that the user may resume interacting
518 * with the user interface without touching the screen again.
519 * </p>
520 * <p>
521 * The touch mode state is maintained across {@link android.app.Activity}s. Call
522 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
523 * </p>
524 *
525 * <a name="Scrolling"></a>
526 * <h3>Scrolling</h3>
527 * <p>
528 * The framework provides basic support for views that wish to internally
529 * scroll their content. This includes keeping track of the X and Y scroll
530 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800531 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700532 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 * </p>
534 *
535 * <a name="Tags"></a>
536 * <h3>Tags</h3>
537 * <p>
538 * Unlike IDs, tags are not used to identify views. Tags are essentially an
539 * extra piece of information that can be associated with a view. They are most
540 * often used as a convenience to store data related to views in the views
541 * themselves rather than by putting them in a separate structure.
542 * </p>
543 *
544 * <a name="Animation"></a>
545 * <h3>Animation</h3>
546 * <p>
547 * You can attach an {@link Animation} object to a view using
548 * {@link #setAnimation(Animation)} or
549 * {@link #startAnimation(Animation)}. The animation can alter the scale,
550 * rotation, translation and alpha of a view over time. If the animation is
551 * attached to a view that has children, the animation will affect the entire
552 * subtree rooted by that node. When an animation is started, the framework will
553 * take care of redrawing the appropriate views until the animation completes.
554 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800555 * <p>
556 * Starting with Android 3.0, the preferred way of animating views is to use the
557 * {@link android.animation} package APIs.
558 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 *
Jeff Brown85a31762010-09-01 17:01:00 -0700560 * <a name="Security"></a>
561 * <h3>Security</h3>
562 * <p>
563 * Sometimes it is essential that an application be able to verify that an action
564 * is being performed with the full knowledge and consent of the user, such as
565 * granting a permission request, making a purchase or clicking on an advertisement.
566 * Unfortunately, a malicious application could try to spoof the user into
567 * performing these actions, unaware, by concealing the intended purpose of the view.
568 * As a remedy, the framework offers a touch filtering mechanism that can be used to
569 * improve the security of views that provide access to sensitive functionality.
570 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700571 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800572 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700573 * will discard touches that are received whenever the view's window is obscured by
574 * another visible window. As a result, the view will not receive touches whenever a
575 * toast, dialog or other window appears above the view's window.
576 * </p><p>
577 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700578 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
579 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700580 * </p>
581 *
Romain Guy171c5922011-01-06 10:04:23 -0800582 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700583 * @attr ref android.R.styleable#View_background
584 * @attr ref android.R.styleable#View_clickable
585 * @attr ref android.R.styleable#View_contentDescription
586 * @attr ref android.R.styleable#View_drawingCacheQuality
587 * @attr ref android.R.styleable#View_duplicateParentState
588 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700589 * @attr ref android.R.styleable#View_requiresFadingEdge
Romain Guyd6a463a2009-05-21 23:10:10 -0700590 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700591 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700593 * @attr ref android.R.styleable#View_isScrollContainer
594 * @attr ref android.R.styleable#View_focusable
595 * @attr ref android.R.styleable#View_focusableInTouchMode
596 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
597 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800598 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700599 * @attr ref android.R.styleable#View_longClickable
600 * @attr ref android.R.styleable#View_minHeight
601 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 * @attr ref android.R.styleable#View_nextFocusDown
603 * @attr ref android.R.styleable#View_nextFocusLeft
604 * @attr ref android.R.styleable#View_nextFocusRight
605 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700606 * @attr ref android.R.styleable#View_onClick
607 * @attr ref android.R.styleable#View_padding
608 * @attr ref android.R.styleable#View_paddingBottom
609 * @attr ref android.R.styleable#View_paddingLeft
610 * @attr ref android.R.styleable#View_paddingRight
611 * @attr ref android.R.styleable#View_paddingTop
612 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800613 * @attr ref android.R.styleable#View_rotation
614 * @attr ref android.R.styleable#View_rotationX
615 * @attr ref android.R.styleable#View_rotationY
616 * @attr ref android.R.styleable#View_scaleX
617 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 * @attr ref android.R.styleable#View_scrollX
619 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700620 * @attr ref android.R.styleable#View_scrollbarSize
621 * @attr ref android.R.styleable#View_scrollbarStyle
622 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700623 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
624 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
626 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 * @attr ref android.R.styleable#View_scrollbarThumbVertical
628 * @attr ref android.R.styleable#View_scrollbarTrackVertical
629 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
630 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700631 * @attr ref android.R.styleable#View_soundEffectsEnabled
632 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800633 * @attr ref android.R.styleable#View_transformPivotX
634 * @attr ref android.R.styleable#View_transformPivotY
635 * @attr ref android.R.styleable#View_translationX
636 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700637 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 *
639 * @see android.view.ViewGroup
640 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700641public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
642 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 private static final boolean DBG = false;
644
645 /**
646 * The logging tag used by this class with android.util.Log.
647 */
648 protected static final String VIEW_LOG_TAG = "View";
649
650 /**
651 * Used to mark a View that has no ID.
652 */
653 public static final int NO_ID = -1;
654
655 /**
656 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
657 * calling setFlags.
658 */
659 private static final int NOT_FOCUSABLE = 0x00000000;
660
661 /**
662 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
663 * setFlags.
664 */
665 private static final int FOCUSABLE = 0x00000001;
666
667 /**
668 * Mask for use with setFlags indicating bits used for focus.
669 */
670 private static final int FOCUSABLE_MASK = 0x00000001;
671
672 /**
673 * This view will adjust its padding to fit sytem windows (e.g. status bar)
674 */
675 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
676
677 /**
Scott Main812634c22011-07-27 13:22:35 -0700678 * This view is visible.
679 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
680 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 */
682 public static final int VISIBLE = 0x00000000;
683
684 /**
685 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700686 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
687 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 */
689 public static final int INVISIBLE = 0x00000004;
690
691 /**
692 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700693 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
694 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 */
696 public static final int GONE = 0x00000008;
697
698 /**
699 * Mask for use with setFlags indicating bits used for visibility.
700 * {@hide}
701 */
702 static final int VISIBILITY_MASK = 0x0000000C;
703
704 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
705
706 /**
707 * This view is enabled. Intrepretation varies by subclass.
708 * Use with ENABLED_MASK when calling setFlags.
709 * {@hide}
710 */
711 static final int ENABLED = 0x00000000;
712
713 /**
714 * This view is disabled. Intrepretation varies by subclass.
715 * Use with ENABLED_MASK when calling setFlags.
716 * {@hide}
717 */
718 static final int DISABLED = 0x00000020;
719
720 /**
721 * Mask for use with setFlags indicating bits used for indicating whether
722 * this view is enabled
723 * {@hide}
724 */
725 static final int ENABLED_MASK = 0x00000020;
726
727 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700728 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
729 * called and further optimizations will be performed. It is okay to have
730 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 * {@hide}
732 */
733 static final int WILL_NOT_DRAW = 0x00000080;
734
735 /**
736 * Mask for use with setFlags indicating bits used for indicating whether
737 * this view is will draw
738 * {@hide}
739 */
740 static final int DRAW_MASK = 0x00000080;
741
742 /**
743 * <p>This view doesn't show scrollbars.</p>
744 * {@hide}
745 */
746 static final int SCROLLBARS_NONE = 0x00000000;
747
748 /**
749 * <p>This view shows horizontal scrollbars.</p>
750 * {@hide}
751 */
752 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
753
754 /**
755 * <p>This view shows vertical scrollbars.</p>
756 * {@hide}
757 */
758 static final int SCROLLBARS_VERTICAL = 0x00000200;
759
760 /**
761 * <p>Mask for use with setFlags indicating bits used for indicating which
762 * scrollbars are enabled.</p>
763 * {@hide}
764 */
765 static final int SCROLLBARS_MASK = 0x00000300;
766
Jeff Brown85a31762010-09-01 17:01:00 -0700767 /**
768 * Indicates that the view should filter touches when its window is obscured.
769 * Refer to the class comments for more information about this security feature.
770 * {@hide}
771 */
772 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
773
774 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775
776 /**
777 * <p>This view doesn't show fading edges.</p>
778 * {@hide}
779 */
780 static final int FADING_EDGE_NONE = 0x00000000;
781
782 /**
783 * <p>This view shows horizontal fading edges.</p>
784 * {@hide}
785 */
786 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
787
788 /**
789 * <p>This view shows vertical fading edges.</p>
790 * {@hide}
791 */
792 static final int FADING_EDGE_VERTICAL = 0x00002000;
793
794 /**
795 * <p>Mask for use with setFlags indicating bits used for indicating which
796 * fading edges are enabled.</p>
797 * {@hide}
798 */
799 static final int FADING_EDGE_MASK = 0x00003000;
800
801 /**
802 * <p>Indicates this view can be clicked. When clickable, a View reacts
803 * to clicks by notifying the OnClickListener.<p>
804 * {@hide}
805 */
806 static final int CLICKABLE = 0x00004000;
807
808 /**
809 * <p>Indicates this view is caching its drawing into a bitmap.</p>
810 * {@hide}
811 */
812 static final int DRAWING_CACHE_ENABLED = 0x00008000;
813
814 /**
815 * <p>Indicates that no icicle should be saved for this view.<p>
816 * {@hide}
817 */
818 static final int SAVE_DISABLED = 0x000010000;
819
820 /**
821 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
822 * property.</p>
823 * {@hide}
824 */
825 static final int SAVE_DISABLED_MASK = 0x000010000;
826
827 /**
828 * <p>Indicates that no drawing cache should ever be created for this view.<p>
829 * {@hide}
830 */
831 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
832
833 /**
834 * <p>Indicates this view can take / keep focus when int touch mode.</p>
835 * {@hide}
836 */
837 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
838
839 /**
840 * <p>Enables low quality mode for the drawing cache.</p>
841 */
842 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
843
844 /**
845 * <p>Enables high quality mode for the drawing cache.</p>
846 */
847 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
848
849 /**
850 * <p>Enables automatic quality mode for the drawing cache.</p>
851 */
852 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
853
854 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
855 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
856 };
857
858 /**
859 * <p>Mask for use with setFlags indicating bits used for the cache
860 * quality property.</p>
861 * {@hide}
862 */
863 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
864
865 /**
866 * <p>
867 * Indicates this view can be long clicked. When long clickable, a View
868 * reacts to long clicks by notifying the OnLongClickListener or showing a
869 * context menu.
870 * </p>
871 * {@hide}
872 */
873 static final int LONG_CLICKABLE = 0x00200000;
874
875 /**
876 * <p>Indicates that this view gets its drawable states from its direct parent
877 * and ignores its original internal states.</p>
878 *
879 * @hide
880 */
881 static final int DUPLICATE_PARENT_STATE = 0x00400000;
882
883 /**
884 * The scrollbar style to display the scrollbars inside the content area,
885 * without increasing the padding. The scrollbars will be overlaid with
886 * translucency on the view's content.
887 */
888 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
889
890 /**
891 * The scrollbar style to display the scrollbars inside the padded area,
892 * increasing the padding of the view. The scrollbars will not overlap the
893 * content area of the view.
894 */
895 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
896
897 /**
898 * The scrollbar style to display the scrollbars at the edge of the view,
899 * without increasing the padding. The scrollbars will be overlaid with
900 * translucency.
901 */
902 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
903
904 /**
905 * The scrollbar style to display the scrollbars at the edge of the view,
906 * increasing the padding of the view. The scrollbars will only overlap the
907 * background, if any.
908 */
909 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
910
911 /**
912 * Mask to check if the scrollbar style is overlay or inset.
913 * {@hide}
914 */
915 static final int SCROLLBARS_INSET_MASK = 0x01000000;
916
917 /**
918 * Mask to check if the scrollbar style is inside or outside.
919 * {@hide}
920 */
921 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
922
923 /**
924 * Mask for scrollbar style.
925 * {@hide}
926 */
927 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
928
929 /**
930 * View flag indicating that the screen should remain on while the
931 * window containing this view is visible to the user. This effectively
932 * takes care of automatically setting the WindowManager's
933 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
934 */
935 public static final int KEEP_SCREEN_ON = 0x04000000;
936
937 /**
938 * View flag indicating whether this view should have sound effects enabled
939 * for events such as clicking and touching.
940 */
941 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
942
943 /**
944 * View flag indicating whether this view should have haptic feedback
945 * enabled for events such as long presses.
946 */
947 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
948
949 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700950 * <p>Indicates that the view hierarchy should stop saving state when
951 * it reaches this view. If state saving is initiated immediately at
952 * the view, it will be allowed.
953 * {@hide}
954 */
955 static final int PARENT_SAVE_DISABLED = 0x20000000;
956
957 /**
958 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
959 * {@hide}
960 */
961 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
962
963 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800964 * Horizontal direction of this view is from Left to Right.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700965 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800966 * {@hide}
967 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700968 public static final int LAYOUT_DIRECTION_LTR = 0x00000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800969
970 /**
971 * Horizontal direction of this view is from Right to Left.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700972 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800973 * {@hide}
974 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700975 public static final int LAYOUT_DIRECTION_RTL = 0x40000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800976
977 /**
978 * Horizontal direction of this view is inherited from its parent.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700979 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800980 * {@hide}
981 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700982 public static final int LAYOUT_DIRECTION_INHERIT = 0x80000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800983
984 /**
985 * Horizontal direction of this view is from deduced from the default language
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700986 * script for the locale. Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800987 * {@hide}
988 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700989 public static final int LAYOUT_DIRECTION_LOCALE = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800990
991 /**
992 * Mask for use with setFlags indicating bits used for horizontalDirection.
993 * {@hide}
994 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700995 static final int LAYOUT_DIRECTION_MASK = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800996
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700997 /*
998 * Array of horizontal direction flags for mapping attribute "horizontalDirection" to correct
999 * flag value.
1000 * {@hide}
1001 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07001002 private static final int[] LAYOUT_DIRECTION_FLAGS = {LAYOUT_DIRECTION_LTR,
1003 LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE};
Cibu Johny7632cb92010-02-22 13:01:02 -08001004
1005 /**
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001006 * Default horizontalDirection.
1007 * {@hide}
1008 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07001009 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001010
1011 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001012 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1013 * should add all focusable Views regardless if they are focusable in touch mode.
1014 */
1015 public static final int FOCUSABLES_ALL = 0x00000000;
1016
1017 /**
1018 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1019 * should add only Views focusable in touch mode.
1020 */
1021 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1022
1023 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001024 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 * item.
1026 */
1027 public static final int FOCUS_BACKWARD = 0x00000001;
1028
1029 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001030 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 * item.
1032 */
1033 public static final int FOCUS_FORWARD = 0x00000002;
1034
1035 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001036 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 */
1038 public static final int FOCUS_LEFT = 0x00000011;
1039
1040 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001041 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 */
1043 public static final int FOCUS_UP = 0x00000021;
1044
1045 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001046 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 */
1048 public static final int FOCUS_RIGHT = 0x00000042;
1049
1050 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001051 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 */
1053 public static final int FOCUS_DOWN = 0x00000082;
1054
1055 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001056 * Bits of {@link #getMeasuredWidthAndState()} and
1057 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1058 */
1059 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1060
1061 /**
1062 * Bits of {@link #getMeasuredWidthAndState()} and
1063 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1064 */
1065 public static final int MEASURED_STATE_MASK = 0xff000000;
1066
1067 /**
1068 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1069 * for functions that combine both width and height into a single int,
1070 * such as {@link #getMeasuredState()} and the childState argument of
1071 * {@link #resolveSizeAndState(int, int, int)}.
1072 */
1073 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1074
1075 /**
1076 * Bit of {@link #getMeasuredWidthAndState()} and
1077 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1078 * is smaller that the space the view would like to have.
1079 */
1080 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1081
1082 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 * Base View state sets
1084 */
1085 // Singles
1086 /**
1087 * Indicates the view has no states set. States are used with
1088 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1089 * view depending on its state.
1090 *
1091 * @see android.graphics.drawable.Drawable
1092 * @see #getDrawableState()
1093 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001094 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 /**
1096 * Indicates the view is enabled. States are used with
1097 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1098 * view depending on its state.
1099 *
1100 * @see android.graphics.drawable.Drawable
1101 * @see #getDrawableState()
1102 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001103 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 /**
1105 * Indicates the view is focused. States are used with
1106 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1107 * view depending on its state.
1108 *
1109 * @see android.graphics.drawable.Drawable
1110 * @see #getDrawableState()
1111 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001112 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 /**
1114 * Indicates the view is selected. States are used with
1115 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1116 * view depending on its state.
1117 *
1118 * @see android.graphics.drawable.Drawable
1119 * @see #getDrawableState()
1120 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001121 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 /**
1123 * Indicates the view is pressed. States are used with
1124 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1125 * view depending on its state.
1126 *
1127 * @see android.graphics.drawable.Drawable
1128 * @see #getDrawableState()
1129 * @hide
1130 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001131 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 /**
1133 * Indicates the view's window has focus. States are used with
1134 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1135 * view depending on its state.
1136 *
1137 * @see android.graphics.drawable.Drawable
1138 * @see #getDrawableState()
1139 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001140 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 // Doubles
1142 /**
1143 * Indicates the view is enabled and has the focus.
1144 *
1145 * @see #ENABLED_STATE_SET
1146 * @see #FOCUSED_STATE_SET
1147 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001148 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 /**
1150 * Indicates the view is enabled and selected.
1151 *
1152 * @see #ENABLED_STATE_SET
1153 * @see #SELECTED_STATE_SET
1154 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001155 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 /**
1157 * Indicates the view is enabled and that its window has focus.
1158 *
1159 * @see #ENABLED_STATE_SET
1160 * @see #WINDOW_FOCUSED_STATE_SET
1161 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001162 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 /**
1164 * Indicates the view is focused and selected.
1165 *
1166 * @see #FOCUSED_STATE_SET
1167 * @see #SELECTED_STATE_SET
1168 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001169 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 /**
1171 * Indicates the view has the focus and that its window has the focus.
1172 *
1173 * @see #FOCUSED_STATE_SET
1174 * @see #WINDOW_FOCUSED_STATE_SET
1175 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001176 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 /**
1178 * Indicates the view is selected and that its window has the focus.
1179 *
1180 * @see #SELECTED_STATE_SET
1181 * @see #WINDOW_FOCUSED_STATE_SET
1182 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001183 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 // Triples
1185 /**
1186 * Indicates the view is enabled, focused and selected.
1187 *
1188 * @see #ENABLED_STATE_SET
1189 * @see #FOCUSED_STATE_SET
1190 * @see #SELECTED_STATE_SET
1191 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001192 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 /**
1194 * Indicates the view is enabled, focused and its window has the focus.
1195 *
1196 * @see #ENABLED_STATE_SET
1197 * @see #FOCUSED_STATE_SET
1198 * @see #WINDOW_FOCUSED_STATE_SET
1199 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001200 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 /**
1202 * Indicates the view is enabled, selected and its window has the focus.
1203 *
1204 * @see #ENABLED_STATE_SET
1205 * @see #SELECTED_STATE_SET
1206 * @see #WINDOW_FOCUSED_STATE_SET
1207 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001208 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 /**
1210 * Indicates the view is focused, selected and its window has the focus.
1211 *
1212 * @see #FOCUSED_STATE_SET
1213 * @see #SELECTED_STATE_SET
1214 * @see #WINDOW_FOCUSED_STATE_SET
1215 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001216 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 /**
1218 * Indicates the view is enabled, focused, selected and its window
1219 * has the focus.
1220 *
1221 * @see #ENABLED_STATE_SET
1222 * @see #FOCUSED_STATE_SET
1223 * @see #SELECTED_STATE_SET
1224 * @see #WINDOW_FOCUSED_STATE_SET
1225 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001226 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 /**
1228 * Indicates the view is pressed and its window has the focus.
1229 *
1230 * @see #PRESSED_STATE_SET
1231 * @see #WINDOW_FOCUSED_STATE_SET
1232 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001233 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 /**
1235 * Indicates the view is pressed and selected.
1236 *
1237 * @see #PRESSED_STATE_SET
1238 * @see #SELECTED_STATE_SET
1239 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001240 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 /**
1242 * Indicates the view is pressed, selected and its window has the focus.
1243 *
1244 * @see #PRESSED_STATE_SET
1245 * @see #SELECTED_STATE_SET
1246 * @see #WINDOW_FOCUSED_STATE_SET
1247 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001248 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 /**
1250 * Indicates the view is pressed and focused.
1251 *
1252 * @see #PRESSED_STATE_SET
1253 * @see #FOCUSED_STATE_SET
1254 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001255 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 /**
1257 * Indicates the view is pressed, focused and its window has the focus.
1258 *
1259 * @see #PRESSED_STATE_SET
1260 * @see #FOCUSED_STATE_SET
1261 * @see #WINDOW_FOCUSED_STATE_SET
1262 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001263 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 /**
1265 * Indicates the view is pressed, focused and selected.
1266 *
1267 * @see #PRESSED_STATE_SET
1268 * @see #SELECTED_STATE_SET
1269 * @see #FOCUSED_STATE_SET
1270 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001271 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 /**
1273 * Indicates the view is pressed, focused, selected and its window has the focus.
1274 *
1275 * @see #PRESSED_STATE_SET
1276 * @see #FOCUSED_STATE_SET
1277 * @see #SELECTED_STATE_SET
1278 * @see #WINDOW_FOCUSED_STATE_SET
1279 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001280 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 /**
1282 * Indicates the view is pressed and enabled.
1283 *
1284 * @see #PRESSED_STATE_SET
1285 * @see #ENABLED_STATE_SET
1286 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001287 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 /**
1289 * Indicates the view is pressed, enabled and its window has the focus.
1290 *
1291 * @see #PRESSED_STATE_SET
1292 * @see #ENABLED_STATE_SET
1293 * @see #WINDOW_FOCUSED_STATE_SET
1294 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001295 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 /**
1297 * Indicates the view is pressed, enabled and selected.
1298 *
1299 * @see #PRESSED_STATE_SET
1300 * @see #ENABLED_STATE_SET
1301 * @see #SELECTED_STATE_SET
1302 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001303 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 /**
1305 * Indicates the view is pressed, enabled, selected and its window has the
1306 * focus.
1307 *
1308 * @see #PRESSED_STATE_SET
1309 * @see #ENABLED_STATE_SET
1310 * @see #SELECTED_STATE_SET
1311 * @see #WINDOW_FOCUSED_STATE_SET
1312 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001313 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 /**
1315 * Indicates the view is pressed, enabled and focused.
1316 *
1317 * @see #PRESSED_STATE_SET
1318 * @see #ENABLED_STATE_SET
1319 * @see #FOCUSED_STATE_SET
1320 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001321 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 /**
1323 * Indicates the view is pressed, enabled, focused and its window has the
1324 * focus.
1325 *
1326 * @see #PRESSED_STATE_SET
1327 * @see #ENABLED_STATE_SET
1328 * @see #FOCUSED_STATE_SET
1329 * @see #WINDOW_FOCUSED_STATE_SET
1330 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001331 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 /**
1333 * Indicates the view is pressed, enabled, focused and selected.
1334 *
1335 * @see #PRESSED_STATE_SET
1336 * @see #ENABLED_STATE_SET
1337 * @see #SELECTED_STATE_SET
1338 * @see #FOCUSED_STATE_SET
1339 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001340 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 /**
1342 * Indicates the view is pressed, enabled, focused, selected and its window
1343 * has the focus.
1344 *
1345 * @see #PRESSED_STATE_SET
1346 * @see #ENABLED_STATE_SET
1347 * @see #SELECTED_STATE_SET
1348 * @see #FOCUSED_STATE_SET
1349 * @see #WINDOW_FOCUSED_STATE_SET
1350 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001351 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352
1353 /**
1354 * The order here is very important to {@link #getDrawableState()}
1355 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001356 private static final int[][] VIEW_STATE_SETS;
1357
Romain Guyb051e892010-09-28 19:09:36 -07001358 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1359 static final int VIEW_STATE_SELECTED = 1 << 1;
1360 static final int VIEW_STATE_FOCUSED = 1 << 2;
1361 static final int VIEW_STATE_ENABLED = 1 << 3;
1362 static final int VIEW_STATE_PRESSED = 1 << 4;
1363 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001364 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001365 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001366 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1367 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001368
1369 static final int[] VIEW_STATE_IDS = new int[] {
1370 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1371 R.attr.state_selected, VIEW_STATE_SELECTED,
1372 R.attr.state_focused, VIEW_STATE_FOCUSED,
1373 R.attr.state_enabled, VIEW_STATE_ENABLED,
1374 R.attr.state_pressed, VIEW_STATE_PRESSED,
1375 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001376 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001377 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001378 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1379 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 };
1381
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001382 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001383 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1384 throw new IllegalStateException(
1385 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1386 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001387 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001388 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001389 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001390 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001391 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001392 orderedIds[i * 2] = viewState;
1393 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001394 }
1395 }
1396 }
Romain Guyb051e892010-09-28 19:09:36 -07001397 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1398 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1399 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001400 int numBits = Integer.bitCount(i);
1401 int[] set = new int[numBits];
1402 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001403 for (int j = 0; j < orderedIds.length; j += 2) {
1404 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001405 set[pos++] = orderedIds[j];
1406 }
1407 }
1408 VIEW_STATE_SETS[i] = set;
1409 }
1410
1411 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1412 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1413 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1414 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1416 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1417 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1418 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1419 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1421 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1422 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1423 | VIEW_STATE_FOCUSED];
1424 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1425 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1426 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1427 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1428 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1429 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1430 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1431 | VIEW_STATE_ENABLED];
1432 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1433 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1434 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1435 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1436 | VIEW_STATE_ENABLED];
1437 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1438 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1439 | VIEW_STATE_ENABLED];
1440 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1441 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1442 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1443
1444 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1445 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1447 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1449 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1451 | VIEW_STATE_PRESSED];
1452 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1453 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1454 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1455 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1456 | VIEW_STATE_PRESSED];
1457 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1459 | VIEW_STATE_PRESSED];
1460 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1462 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1463 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1464 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1465 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1467 | VIEW_STATE_PRESSED];
1468 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1470 | VIEW_STATE_PRESSED];
1471 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1472 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1473 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1474 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1475 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1476 | VIEW_STATE_PRESSED];
1477 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1478 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1479 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1480 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1481 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1482 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1483 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1484 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1485 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1486 | VIEW_STATE_PRESSED];
1487 }
1488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 * Temporary Rect currently for use in setBackground(). This will probably
1491 * be extended in the future to hold our own class with more than just
1492 * a Rect. :)
1493 */
1494 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001495
1496 /**
1497 * Map used to store views' tags.
1498 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001499 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001502 * The next available accessiiblity id.
1503 */
1504 private static int sNextAccessibilityViewId;
1505
1506 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 * The animation currently associated with this view.
1508 * @hide
1509 */
1510 protected Animation mCurrentAnimation = null;
1511
1512 /**
1513 * Width as measured during measure pass.
1514 * {@hide}
1515 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001516 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001517 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518
1519 /**
1520 * Height as measured during measure pass.
1521 * {@hide}
1522 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001523 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001524 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525
1526 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001527 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1528 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1529 * its display list. This flag, used only when hw accelerated, allows us to clear the
1530 * flag while retaining this information until it's needed (at getDisplayList() time and
1531 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1532 *
1533 * {@hide}
1534 */
1535 boolean mRecreateDisplayList = false;
1536
1537 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 * The view's identifier.
1539 * {@hide}
1540 *
1541 * @see #setId(int)
1542 * @see #getId()
1543 */
1544 @ViewDebug.ExportedProperty(resolveId = true)
1545 int mID = NO_ID;
1546
1547 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001548 * The stable ID of this view for accessibility porposes.
1549 */
1550 int mAccessibilityViewId = NO_ID;
1551
1552 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 * The view's tag.
1554 * {@hide}
1555 *
1556 * @see #setTag(Object)
1557 * @see #getTag()
1558 */
1559 protected Object mTag;
1560
1561 // for mPrivateFlags:
1562 /** {@hide} */
1563 static final int WANTS_FOCUS = 0x00000001;
1564 /** {@hide} */
1565 static final int FOCUSED = 0x00000002;
1566 /** {@hide} */
1567 static final int SELECTED = 0x00000004;
1568 /** {@hide} */
1569 static final int IS_ROOT_NAMESPACE = 0x00000008;
1570 /** {@hide} */
1571 static final int HAS_BOUNDS = 0x00000010;
1572 /** {@hide} */
1573 static final int DRAWN = 0x00000020;
1574 /**
1575 * When this flag is set, this view is running an animation on behalf of its
1576 * children and should therefore not cancel invalidate requests, even if they
1577 * lie outside of this view's bounds.
1578 *
1579 * {@hide}
1580 */
1581 static final int DRAW_ANIMATION = 0x00000040;
1582 /** {@hide} */
1583 static final int SKIP_DRAW = 0x00000080;
1584 /** {@hide} */
1585 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1586 /** {@hide} */
1587 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1588 /** {@hide} */
1589 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1590 /** {@hide} */
1591 static final int MEASURED_DIMENSION_SET = 0x00000800;
1592 /** {@hide} */
1593 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001594 /** {@hide} */
1595 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596
1597 private static final int PRESSED = 0x00004000;
1598
1599 /** {@hide} */
1600 static final int DRAWING_CACHE_VALID = 0x00008000;
1601 /**
1602 * Flag used to indicate that this view should be drawn once more (and only once
1603 * more) after its animation has completed.
1604 * {@hide}
1605 */
1606 static final int ANIMATION_STARTED = 0x00010000;
1607
1608 private static final int SAVE_STATE_CALLED = 0x00020000;
1609
1610 /**
1611 * Indicates that the View returned true when onSetAlpha() was called and that
1612 * the alpha must be restored.
1613 * {@hide}
1614 */
1615 static final int ALPHA_SET = 0x00040000;
1616
1617 /**
1618 * Set by {@link #setScrollContainer(boolean)}.
1619 */
1620 static final int SCROLL_CONTAINER = 0x00080000;
1621
1622 /**
1623 * Set by {@link #setScrollContainer(boolean)}.
1624 */
1625 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1626
1627 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001628 * View flag indicating whether this view was invalidated (fully or partially.)
1629 *
1630 * @hide
1631 */
1632 static final int DIRTY = 0x00200000;
1633
1634 /**
1635 * View flag indicating whether this view was invalidated by an opaque
1636 * invalidate request.
1637 *
1638 * @hide
1639 */
1640 static final int DIRTY_OPAQUE = 0x00400000;
1641
1642 /**
1643 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1644 *
1645 * @hide
1646 */
1647 static final int DIRTY_MASK = 0x00600000;
1648
1649 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001650 * Indicates whether the background is opaque.
1651 *
1652 * @hide
1653 */
1654 static final int OPAQUE_BACKGROUND = 0x00800000;
1655
1656 /**
1657 * Indicates whether the scrollbars are opaque.
1658 *
1659 * @hide
1660 */
1661 static final int OPAQUE_SCROLLBARS = 0x01000000;
1662
1663 /**
1664 * Indicates whether the view is opaque.
1665 *
1666 * @hide
1667 */
1668 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001669
Adam Powelle14579b2009-12-16 18:39:52 -08001670 /**
1671 * Indicates a prepressed state;
1672 * the short time between ACTION_DOWN and recognizing
1673 * a 'real' press. Prepressed is used to recognize quick taps
1674 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001675 *
Adam Powelle14579b2009-12-16 18:39:52 -08001676 * @hide
1677 */
1678 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001679
Adam Powellc9fbaab2010-02-16 17:16:19 -08001680 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001681 * Indicates whether the view is temporarily detached.
1682 *
1683 * @hide
1684 */
1685 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001686
Adam Powell8568c3a2010-04-19 14:26:11 -07001687 /**
1688 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001689 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001690 * @hide
1691 */
1692 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001693
1694 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001695 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1696 * @hide
1697 */
1698 private static final int HOVERED = 0x10000000;
1699
1700 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001701 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1702 * for transform operations
1703 *
1704 * @hide
1705 */
Adam Powellf37df072010-09-17 16:22:49 -07001706 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001707
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001708 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001709 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001710
Chet Haasefd2b0022010-08-06 13:08:56 -07001711 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001712 * Indicates that this view was specifically invalidated, not just dirtied because some
1713 * child view was invalidated. The flag is used to determine when we need to recreate
1714 * a view's display list (as opposed to just returning a reference to its existing
1715 * display list).
1716 *
1717 * @hide
1718 */
1719 static final int INVALIDATED = 0x80000000;
1720
Christopher Tate3d4bf172011-03-28 16:16:46 -07001721 /* Masks for mPrivateFlags2 */
1722
1723 /**
1724 * Indicates that this view has reported that it can accept the current drag's content.
1725 * Cleared when the drag operation concludes.
1726 * @hide
1727 */
1728 static final int DRAG_CAN_ACCEPT = 0x00000001;
1729
1730 /**
1731 * Indicates that this view is currently directly under the drag location in a
1732 * drag-and-drop operation involving content that it can accept. Cleared when
1733 * the drag exits the view, or when the drag operation concludes.
1734 * @hide
1735 */
1736 static final int DRAG_HOVERED = 0x00000002;
1737
Cibu Johny86666632010-02-22 13:01:02 -08001738 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001739 * Indicates whether the view layout direction has been resolved and drawn to the
1740 * right-to-left direction.
Cibu Johny86666632010-02-22 13:01:02 -08001741 *
1742 * @hide
1743 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001744 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004;
1745
1746 /**
1747 * Indicates whether the view layout direction has been resolved.
1748 *
1749 * @hide
1750 */
1751 static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008;
1752
Cibu Johny86666632010-02-22 13:01:02 -08001753
Christopher Tate3d4bf172011-03-28 16:16:46 -07001754 /* End of masks for mPrivateFlags2 */
1755
1756 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1757
Chet Haasedaf98e92011-01-10 14:10:36 -08001758 /**
Adam Powell637d3372010-08-25 14:37:03 -07001759 * Always allow a user to over-scroll this view, provided it is a
1760 * view that can scroll.
1761 *
1762 * @see #getOverScrollMode()
1763 * @see #setOverScrollMode(int)
1764 */
1765 public static final int OVER_SCROLL_ALWAYS = 0;
1766
1767 /**
1768 * Allow a user to over-scroll this view only if the content is large
1769 * enough to meaningfully scroll, provided it is a view that can scroll.
1770 *
1771 * @see #getOverScrollMode()
1772 * @see #setOverScrollMode(int)
1773 */
1774 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1775
1776 /**
1777 * Never allow a user to over-scroll this view.
1778 *
1779 * @see #getOverScrollMode()
1780 * @see #setOverScrollMode(int)
1781 */
1782 public static final int OVER_SCROLL_NEVER = 2;
1783
1784 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001785 * View has requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08001786 *
Joe Malin32736f02011-01-19 16:14:20 -08001787 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001788 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001789 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08001790
1791 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001792 * View has requested the system UI to enter an unobtrusive "low profile" mode.
1793 *
1794 * This is for use in games, book readers, video players, or any other "immersive" application
1795 * where the usual system chrome is deemed too distracting.
1796 *
1797 * In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08001798 *
Joe Malin32736f02011-01-19 16:14:20 -08001799 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001800 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001801 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
1802
1803 /**
1804 * View has requested that the system navigation be temporarily hidden.
1805 *
1806 * This is an even less obtrusive state than that called for by
1807 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
1808 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
1809 * those to disappear. This is useful (in conjunction with the
1810 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
1811 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
1812 * window flags) for displaying content using every last pixel on the display.
1813 *
1814 * There is a limitation: because navigation controls are so important, the least user
1815 * interaction will cause them to reappear immediately.
1816 *
1817 * @see #setSystemUiVisibility(int)
1818 */
1819 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
1820
1821 /**
1822 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
1823 */
1824 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
1825
1826 /**
1827 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
1828 */
1829 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08001830
1831 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001832 * @hide
1833 *
1834 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1835 * out of the public fields to keep the undefined bits out of the developer's way.
1836 *
1837 * Flag to make the status bar not expandable. Unless you also
1838 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1839 */
1840 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1841
1842 /**
1843 * @hide
1844 *
1845 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1846 * out of the public fields to keep the undefined bits out of the developer's way.
1847 *
1848 * Flag to hide notification icons and scrolling ticker text.
1849 */
1850 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1851
1852 /**
1853 * @hide
1854 *
1855 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1856 * out of the public fields to keep the undefined bits out of the developer's way.
1857 *
1858 * Flag to disable incoming notification alerts. This will not block
1859 * icons, but it will block sound, vibrating and other visual or aural notifications.
1860 */
1861 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1862
1863 /**
1864 * @hide
1865 *
1866 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1867 * out of the public fields to keep the undefined bits out of the developer's way.
1868 *
1869 * Flag to hide only the scrolling ticker. Note that
1870 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1871 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1872 */
1873 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1874
1875 /**
1876 * @hide
1877 *
1878 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1879 * out of the public fields to keep the undefined bits out of the developer's way.
1880 *
1881 * Flag to hide the center system info area.
1882 */
1883 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1884
1885 /**
1886 * @hide
1887 *
1888 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1889 * out of the public fields to keep the undefined bits out of the developer's way.
1890 *
1891 * Flag to hide only the navigation buttons. Don't use this
1892 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001893 *
1894 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001895 */
1896 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1897
1898 /**
1899 * @hide
1900 *
1901 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1902 * out of the public fields to keep the undefined bits out of the developer's way.
1903 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001904 * Flag to hide only the back button. Don't use this
1905 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1906 */
1907 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1908
1909 /**
1910 * @hide
1911 *
1912 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1913 * out of the public fields to keep the undefined bits out of the developer's way.
1914 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001915 * Flag to hide only the clock. You might use this if your activity has
1916 * its own clock making the status bar's clock redundant.
1917 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001918 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1919
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001920 /**
1921 * @hide
1922 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001923 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001924
1925 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001926 * Find views that render the specified text.
1927 *
1928 * @see #findViewsWithText(ArrayList, CharSequence, int)
1929 */
1930 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
1931
1932 /**
1933 * Find find views that contain the specified content description.
1934 *
1935 * @see #findViewsWithText(ArrayList, CharSequence, int)
1936 */
1937 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
1938
1939 /**
Adam Powell637d3372010-08-25 14:37:03 -07001940 * Controls the over-scroll mode for this view.
1941 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1942 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1943 * and {@link #OVER_SCROLL_NEVER}.
1944 */
1945 private int mOverScrollMode;
1946
1947 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 * The parent this view is attached to.
1949 * {@hide}
1950 *
1951 * @see #getParent()
1952 */
1953 protected ViewParent mParent;
1954
1955 /**
1956 * {@hide}
1957 */
1958 AttachInfo mAttachInfo;
1959
1960 /**
1961 * {@hide}
1962 */
Romain Guy809a7f62009-05-14 15:44:42 -07001963 @ViewDebug.ExportedProperty(flagMapping = {
1964 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1965 name = "FORCE_LAYOUT"),
1966 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1967 name = "LAYOUT_REQUIRED"),
1968 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001969 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001970 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1971 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1972 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1973 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1974 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001976 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977
1978 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001979 * This view's request for the visibility of the status bar.
1980 * @hide
1981 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001982 @ViewDebug.ExportedProperty(flagMapping = {
1983 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
1984 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
1985 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
1986 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
1987 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
1988 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
1989 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
1990 equals = SYSTEM_UI_FLAG_VISIBLE,
1991 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
1992 })
Joe Onorato664644d2011-01-23 17:53:23 -08001993 int mSystemUiVisibility;
1994
1995 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 * Count of how many windows this view has been attached to.
1997 */
1998 int mWindowAttachCount;
1999
2000 /**
2001 * The layout parameters associated with this view and used by the parent
2002 * {@link android.view.ViewGroup} to determine how this view should be
2003 * laid out.
2004 * {@hide}
2005 */
2006 protected ViewGroup.LayoutParams mLayoutParams;
2007
2008 /**
2009 * The view flags hold various views states.
2010 * {@hide}
2011 */
2012 @ViewDebug.ExportedProperty
2013 int mViewFlags;
2014
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002015 static class TransformationInfo {
2016 /**
2017 * The transform matrix for the View. This transform is calculated internally
2018 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2019 * is used by default. Do *not* use this variable directly; instead call
2020 * getMatrix(), which will automatically recalculate the matrix if necessary
2021 * to get the correct matrix based on the latest rotation and scale properties.
2022 */
2023 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002024
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002025 /**
2026 * The transform matrix for the View. This transform is calculated internally
2027 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2028 * is used by default. Do *not* use this variable directly; instead call
2029 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2030 * to get the correct matrix based on the latest rotation and scale properties.
2031 */
2032 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002033
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002034 /**
2035 * An internal variable that tracks whether we need to recalculate the
2036 * transform matrix, based on whether the rotation or scaleX/Y properties
2037 * have changed since the matrix was last calculated.
2038 */
2039 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002040
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002041 /**
2042 * An internal variable that tracks whether we need to recalculate the
2043 * transform matrix, based on whether the rotation or scaleX/Y properties
2044 * have changed since the matrix was last calculated.
2045 */
2046 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002047
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002048 /**
2049 * A variable that tracks whether we need to recalculate the
2050 * transform matrix, based on whether the rotation or scaleX/Y properties
2051 * have changed since the matrix was last calculated. This variable
2052 * is only valid after a call to updateMatrix() or to a function that
2053 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2054 */
2055 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002056
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002057 /**
2058 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2059 */
2060 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002061
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002062 /**
2063 * This matrix is used when computing the matrix for 3D rotations.
2064 */
2065 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002066
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002067 /**
2068 * These prev values are used to recalculate a centered pivot point when necessary. The
2069 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2070 * set), so thes values are only used then as well.
2071 */
2072 private int mPrevWidth = -1;
2073 private int mPrevHeight = -1;
2074
2075 /**
2076 * The degrees rotation around the vertical axis through the pivot point.
2077 */
2078 @ViewDebug.ExportedProperty
2079 float mRotationY = 0f;
2080
2081 /**
2082 * The degrees rotation around the horizontal axis through the pivot point.
2083 */
2084 @ViewDebug.ExportedProperty
2085 float mRotationX = 0f;
2086
2087 /**
2088 * The degrees rotation around the pivot point.
2089 */
2090 @ViewDebug.ExportedProperty
2091 float mRotation = 0f;
2092
2093 /**
2094 * The amount of translation of the object away from its left property (post-layout).
2095 */
2096 @ViewDebug.ExportedProperty
2097 float mTranslationX = 0f;
2098
2099 /**
2100 * The amount of translation of the object away from its top property (post-layout).
2101 */
2102 @ViewDebug.ExportedProperty
2103 float mTranslationY = 0f;
2104
2105 /**
2106 * The amount of scale in the x direction around the pivot point. A
2107 * value of 1 means no scaling is applied.
2108 */
2109 @ViewDebug.ExportedProperty
2110 float mScaleX = 1f;
2111
2112 /**
2113 * The amount of scale in the y direction around the pivot point. A
2114 * value of 1 means no scaling is applied.
2115 */
2116 @ViewDebug.ExportedProperty
2117 float mScaleY = 1f;
2118
2119 /**
2120 * The amount of scale in the x direction around the pivot point. A
2121 * value of 1 means no scaling is applied.
2122 */
2123 @ViewDebug.ExportedProperty
2124 float mPivotX = 0f;
2125
2126 /**
2127 * The amount of scale in the y direction around the pivot point. A
2128 * value of 1 means no scaling is applied.
2129 */
2130 @ViewDebug.ExportedProperty
2131 float mPivotY = 0f;
2132
2133 /**
2134 * The opacity of the View. This is a value from 0 to 1, where 0 means
2135 * completely transparent and 1 means completely opaque.
2136 */
2137 @ViewDebug.ExportedProperty
2138 float mAlpha = 1f;
2139 }
2140
2141 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002142
Joe Malin32736f02011-01-19 16:14:20 -08002143 private boolean mLastIsOpaque;
2144
Chet Haasefd2b0022010-08-06 13:08:56 -07002145 /**
2146 * Convenience value to check for float values that are close enough to zero to be considered
2147 * zero.
2148 */
Romain Guy2542d192010-08-18 11:47:12 -07002149 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002150
2151 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 * The distance in pixels from the left edge of this view's parent
2153 * to the left edge of this view.
2154 * {@hide}
2155 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002156 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 protected int mLeft;
2158 /**
2159 * The distance in pixels from the left edge of this view's parent
2160 * to the right edge of this view.
2161 * {@hide}
2162 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002163 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 protected int mRight;
2165 /**
2166 * The distance in pixels from the top edge of this view's parent
2167 * to the top edge of this view.
2168 * {@hide}
2169 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002170 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 protected int mTop;
2172 /**
2173 * The distance in pixels from the top edge of this view's parent
2174 * to the bottom edge of this view.
2175 * {@hide}
2176 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002177 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 protected int mBottom;
2179
2180 /**
2181 * The offset, in pixels, by which the content of this view is scrolled
2182 * horizontally.
2183 * {@hide}
2184 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002185 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 protected int mScrollX;
2187 /**
2188 * The offset, in pixels, by which the content of this view is scrolled
2189 * vertically.
2190 * {@hide}
2191 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002192 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 protected int mScrollY;
2194
2195 /**
2196 * The left padding in pixels, that is the distance in pixels between the
2197 * left edge of this view and the left edge of its content.
2198 * {@hide}
2199 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002200 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 protected int mPaddingLeft;
2202 /**
2203 * The right padding in pixels, that is the distance in pixels between the
2204 * right edge of this view and the right edge of its content.
2205 * {@hide}
2206 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002207 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 protected int mPaddingRight;
2209 /**
2210 * The top padding in pixels, that is the distance in pixels between the
2211 * top edge of this view and the top edge of its content.
2212 * {@hide}
2213 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002214 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 protected int mPaddingTop;
2216 /**
2217 * The bottom padding in pixels, that is the distance in pixels between the
2218 * bottom edge of this view and the bottom edge of its content.
2219 * {@hide}
2220 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002221 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 protected int mPaddingBottom;
2223
2224 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002225 * Briefly describes the view and is primarily used for accessibility support.
2226 */
2227 private CharSequence mContentDescription;
2228
2229 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002231 *
2232 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002234 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002235 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236
2237 /**
2238 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002239 *
2240 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002242 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002243 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002245 /**
Adam Powell20232d02010-12-08 21:08:53 -08002246 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002247 *
2248 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002249 */
2250 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002251 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002252
2253 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002254 * Cache if the user padding is relative.
2255 *
2256 */
2257 @ViewDebug.ExportedProperty(category = "padding")
2258 boolean mUserPaddingRelative;
2259
2260 /**
2261 * Cache the paddingStart set by the user to append to the scrollbar's size.
2262 *
2263 */
2264 @ViewDebug.ExportedProperty(category = "padding")
2265 int mUserPaddingStart;
2266
2267 /**
2268 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2269 *
2270 */
2271 @ViewDebug.ExportedProperty(category = "padding")
2272 int mUserPaddingEnd;
2273
2274 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002275 * @hide
2276 */
2277 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2278 /**
2279 * @hide
2280 */
2281 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 private Drawable mBGDrawable;
2284
2285 private int mBackgroundResource;
2286 private boolean mBackgroundSizeChanged;
2287
2288 /**
2289 * Listener used to dispatch focus change events.
2290 * This field should be made private, so it is hidden from the SDK.
2291 * {@hide}
2292 */
2293 protected OnFocusChangeListener mOnFocusChangeListener;
2294
2295 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002296 * Listeners for layout change events.
2297 */
2298 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2299
2300 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002301 * Listeners for attach events.
2302 */
2303 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2304
2305 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306 * Listener used to dispatch click events.
2307 * This field should be made private, so it is hidden from the SDK.
2308 * {@hide}
2309 */
2310 protected OnClickListener mOnClickListener;
2311
2312 /**
2313 * Listener used to dispatch long click events.
2314 * This field should be made private, so it is hidden from the SDK.
2315 * {@hide}
2316 */
2317 protected OnLongClickListener mOnLongClickListener;
2318
2319 /**
2320 * Listener used to build the context menu.
2321 * This field should be made private, so it is hidden from the SDK.
2322 * {@hide}
2323 */
2324 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2325
2326 private OnKeyListener mOnKeyListener;
2327
2328 private OnTouchListener mOnTouchListener;
2329
Jeff Brown10b62902011-06-20 16:40:37 -07002330 private OnHoverListener mOnHoverListener;
2331
Jeff Brown33bbfd22011-02-24 20:55:35 -08002332 private OnGenericMotionListener mOnGenericMotionListener;
2333
Chris Tate32affef2010-10-18 15:29:21 -07002334 private OnDragListener mOnDragListener;
2335
Joe Onorato664644d2011-01-23 17:53:23 -08002336 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 /**
2339 * The application environment this view lives in.
2340 * This field should be made private, so it is hidden from the SDK.
2341 * {@hide}
2342 */
2343 protected Context mContext;
2344
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002345 private final Resources mResources;
2346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 private ScrollabilityCache mScrollCache;
2348
2349 private int[] mDrawableState = null;
2350
Romain Guy0211a0a2011-02-14 16:34:59 -08002351 /**
2352 * Set to true when drawing cache is enabled and cannot be created.
2353 *
2354 * @hide
2355 */
2356 public boolean mCachingFailed;
2357
Romain Guy02890fd2010-08-06 17:58:44 -07002358 private Bitmap mDrawingCache;
2359 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002360 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002361 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362
2363 /**
2364 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2365 * the user may specify which view to go to next.
2366 */
2367 private int mNextFocusLeftId = View.NO_ID;
2368
2369 /**
2370 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2371 * the user may specify which view to go to next.
2372 */
2373 private int mNextFocusRightId = View.NO_ID;
2374
2375 /**
2376 * When this view has focus and the next focus is {@link #FOCUS_UP},
2377 * the user may specify which view to go to next.
2378 */
2379 private int mNextFocusUpId = View.NO_ID;
2380
2381 /**
2382 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2383 * the user may specify which view to go to next.
2384 */
2385 private int mNextFocusDownId = View.NO_ID;
2386
Jeff Brown4e6319b2010-12-13 10:36:51 -08002387 /**
2388 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2389 * the user may specify which view to go to next.
2390 */
2391 int mNextFocusForwardId = View.NO_ID;
2392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002394 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002395 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002396 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 private UnsetPressedState mUnsetPressedState;
2399
2400 /**
2401 * Whether the long press's action has been invoked. The tap's action is invoked on the
2402 * up event while a long press is invoked as soon as the long press duration is reached, so
2403 * a long press could be performed before the tap is checked, in which case the tap's action
2404 * should not be invoked.
2405 */
2406 private boolean mHasPerformedLongPress;
2407
2408 /**
2409 * The minimum height of the view. We'll try our best to have the height
2410 * of this view to at least this amount.
2411 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002412 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 private int mMinHeight;
2414
2415 /**
2416 * The minimum width of the view. We'll try our best to have the width
2417 * of this view to at least this amount.
2418 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002419 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 private int mMinWidth;
2421
2422 /**
2423 * The delegate to handle touch events that are physically in this view
2424 * but should be handled by another view.
2425 */
2426 private TouchDelegate mTouchDelegate = null;
2427
2428 /**
2429 * Solid color to use as a background when creating the drawing cache. Enables
2430 * the cache to use 16 bit bitmaps instead of 32 bit.
2431 */
2432 private int mDrawingCacheBackgroundColor = 0;
2433
2434 /**
2435 * Special tree observer used when mAttachInfo is null.
2436 */
2437 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002438
Adam Powelle14579b2009-12-16 18:39:52 -08002439 /**
2440 * Cache the touch slop from the context that created the view.
2441 */
2442 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002445 * Object that handles automatic animation of view properties.
2446 */
2447 private ViewPropertyAnimator mAnimator = null;
2448
2449 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002450 * Flag indicating that a drag can cross window boundaries. When
2451 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2452 * with this flag set, all visible applications will be able to participate
2453 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002454 *
2455 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002456 */
2457 public static final int DRAG_FLAG_GLOBAL = 1;
2458
2459 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002460 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2461 */
2462 private float mVerticalScrollFactor;
2463
2464 /**
Adam Powell20232d02010-12-08 21:08:53 -08002465 * Position of the vertical scroll bar.
2466 */
2467 private int mVerticalScrollbarPosition;
2468
2469 /**
2470 * Position the scroll bar at the default position as determined by the system.
2471 */
2472 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2473
2474 /**
2475 * Position the scroll bar along the left edge.
2476 */
2477 public static final int SCROLLBAR_POSITION_LEFT = 1;
2478
2479 /**
2480 * Position the scroll bar along the right edge.
2481 */
2482 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2483
2484 /**
Romain Guy171c5922011-01-06 10:04:23 -08002485 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002486 *
2487 * @see #getLayerType()
2488 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002489 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002490 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002491 */
2492 public static final int LAYER_TYPE_NONE = 0;
2493
2494 /**
2495 * <p>Indicates that the view has a software layer. A software layer is backed
2496 * by a bitmap and causes the view to be rendered using Android's software
2497 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002498 *
Romain Guy171c5922011-01-06 10:04:23 -08002499 * <p>Software layers have various usages:</p>
2500 * <p>When the application is not using hardware acceleration, a software layer
2501 * is useful to apply a specific color filter and/or blending mode and/or
2502 * translucency to a view and all its children.</p>
2503 * <p>When the application is using hardware acceleration, a software layer
2504 * is useful to render drawing primitives not supported by the hardware
2505 * accelerated pipeline. It can also be used to cache a complex view tree
2506 * into a texture and reduce the complexity of drawing operations. For instance,
2507 * when animating a complex view tree with a translation, a software layer can
2508 * be used to render the view tree only once.</p>
2509 * <p>Software layers should be avoided when the affected view tree updates
2510 * often. Every update will require to re-render the software layer, which can
2511 * potentially be slow (particularly when hardware acceleration is turned on
2512 * since the layer will have to be uploaded into a hardware texture after every
2513 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002514 *
2515 * @see #getLayerType()
2516 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002517 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002518 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002519 */
2520 public static final int LAYER_TYPE_SOFTWARE = 1;
2521
2522 /**
2523 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2524 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2525 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2526 * rendering pipeline, but only if hardware acceleration is turned on for the
2527 * view hierarchy. When hardware acceleration is turned off, hardware layers
2528 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002529 *
Romain Guy171c5922011-01-06 10:04:23 -08002530 * <p>A hardware layer is useful to apply a specific color filter and/or
2531 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002532 * <p>A hardware layer can be used to cache a complex view tree into a
2533 * texture and reduce the complexity of drawing operations. For instance,
2534 * when animating a complex view tree with a translation, a hardware layer can
2535 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002536 * <p>A hardware layer can also be used to increase the rendering quality when
2537 * rotation transformations are applied on a view. It can also be used to
2538 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002539 *
2540 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002541 * @see #setLayerType(int, android.graphics.Paint)
2542 * @see #LAYER_TYPE_NONE
2543 * @see #LAYER_TYPE_SOFTWARE
2544 */
2545 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002546
Romain Guy3aaff3a2011-01-12 14:18:47 -08002547 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2548 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2549 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2550 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2551 })
Romain Guy171c5922011-01-06 10:04:23 -08002552 int mLayerType = LAYER_TYPE_NONE;
2553 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002554 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002555
2556 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002557 * Set to true when the view is sending hover accessibility events because it
2558 * is the innermost hovered view.
2559 */
2560 private boolean mSendingHoverAccessibilityEvents;
2561
2562 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002563 * Delegate for injecting accessiblity functionality.
2564 */
2565 AccessibilityDelegate mAccessibilityDelegate;
2566
2567 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002568 * Text direction is inherited thru {@link ViewGroup}
2569 * @hide
2570 */
2571 public static final int TEXT_DIRECTION_INHERIT = 0;
2572
2573 /**
2574 * Text direction is using "first strong algorithm". The first strong directional character
2575 * determines the paragraph direction. If there is no strong directional character, the
Doug Feltcb3791202011-07-07 11:57:48 -07002576 * paragraph direction is the view's resolved ayout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002577 *
2578 * @hide
2579 */
2580 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2581
2582 /**
2583 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2584 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002585 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002586 *
2587 * @hide
2588 */
2589 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2590
2591 /**
2592 * Text direction is forced to LTR.
2593 *
2594 * @hide
2595 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002596 public static final int TEXT_DIRECTION_LTR = 3;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002597
2598 /**
2599 * Text direction is forced to RTL.
2600 *
2601 * @hide
2602 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002603 public static final int TEXT_DIRECTION_RTL = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002604
2605 /**
2606 * Default text direction is inherited
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07002607 *
2608 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002609 */
2610 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2611
2612 /**
2613 * The text direction that has been defined by {@link #setTextDirection(int)}.
2614 *
2615 * {@hide}
2616 */
2617 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002618 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2619 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2620 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2621 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2622 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2623 })
Doug Feltcb3791202011-07-07 11:57:48 -07002624 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002625
2626 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002627 * The resolved text direction. This needs resolution if the value is
2628 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if that is
2629 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2630 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002631 *
2632 * {@hide}
2633 */
2634 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002635 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2636 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2637 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002638 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2639 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2640 })
Doug Feltcb3791202011-07-07 11:57:48 -07002641 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002642
2643 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002644 * Consistency verifier for debugging purposes.
2645 * @hide
2646 */
2647 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2648 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2649 new InputEventConsistencyVerifier(this, 0) : null;
2650
2651 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 * Simple constructor to use when creating a view from code.
2653 *
2654 * @param context The Context the view is running in, through which it can
2655 * access the current theme, resources, etc.
2656 */
2657 public View(Context context) {
2658 mContext = context;
2659 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002660 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002661 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002662 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002663 mUserPaddingStart = -1;
2664 mUserPaddingEnd = -1;
2665 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 }
2667
2668 /**
2669 * Constructor that is called when inflating a view from XML. This is called
2670 * when a view is being constructed from an XML file, supplying attributes
2671 * that were specified in the XML file. This version uses a default style of
2672 * 0, so the only attribute values applied are those in the Context's Theme
2673 * and the given AttributeSet.
2674 *
2675 * <p>
2676 * The method onFinishInflate() will be called after all children have been
2677 * added.
2678 *
2679 * @param context The Context the view is running in, through which it can
2680 * access the current theme, resources, etc.
2681 * @param attrs The attributes of the XML tag that is inflating the view.
2682 * @see #View(Context, AttributeSet, int)
2683 */
2684 public View(Context context, AttributeSet attrs) {
2685 this(context, attrs, 0);
2686 }
2687
2688 /**
2689 * Perform inflation from XML and apply a class-specific base style. This
2690 * constructor of View allows subclasses to use their own base style when
2691 * they are inflating. For example, a Button class's constructor would call
2692 * this version of the super class constructor and supply
2693 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2694 * the theme's button style to modify all of the base view attributes (in
2695 * particular its background) as well as the Button class's attributes.
2696 *
2697 * @param context The Context the view is running in, through which it can
2698 * access the current theme, resources, etc.
2699 * @param attrs The attributes of the XML tag that is inflating the view.
2700 * @param defStyle The default style to apply to this view. If 0, no style
2701 * will be applied (beyond what is included in the theme). This may
2702 * either be an attribute resource, whose value will be retrieved
2703 * from the current theme, or an explicit style resource.
2704 * @see #View(Context, AttributeSet)
2705 */
2706 public View(Context context, AttributeSet attrs, int defStyle) {
2707 this(context);
2708
2709 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2710 defStyle, 0);
2711
2712 Drawable background = null;
2713
2714 int leftPadding = -1;
2715 int topPadding = -1;
2716 int rightPadding = -1;
2717 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002718 int startPadding = -1;
2719 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720
2721 int padding = -1;
2722
2723 int viewFlagValues = 0;
2724 int viewFlagMasks = 0;
2725
2726 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 int x = 0;
2729 int y = 0;
2730
Chet Haase73066682010-11-29 15:55:32 -08002731 float tx = 0;
2732 float ty = 0;
2733 float rotation = 0;
2734 float rotationX = 0;
2735 float rotationY = 0;
2736 float sx = 1f;
2737 float sy = 1f;
2738 boolean transformSet = false;
2739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2741
Adam Powell637d3372010-08-25 14:37:03 -07002742 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 final int N = a.getIndexCount();
2744 for (int i = 0; i < N; i++) {
2745 int attr = a.getIndex(i);
2746 switch (attr) {
2747 case com.android.internal.R.styleable.View_background:
2748 background = a.getDrawable(attr);
2749 break;
2750 case com.android.internal.R.styleable.View_padding:
2751 padding = a.getDimensionPixelSize(attr, -1);
2752 break;
2753 case com.android.internal.R.styleable.View_paddingLeft:
2754 leftPadding = a.getDimensionPixelSize(attr, -1);
2755 break;
2756 case com.android.internal.R.styleable.View_paddingTop:
2757 topPadding = a.getDimensionPixelSize(attr, -1);
2758 break;
2759 case com.android.internal.R.styleable.View_paddingRight:
2760 rightPadding = a.getDimensionPixelSize(attr, -1);
2761 break;
2762 case com.android.internal.R.styleable.View_paddingBottom:
2763 bottomPadding = a.getDimensionPixelSize(attr, -1);
2764 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002765 case com.android.internal.R.styleable.View_paddingStart:
2766 startPadding = a.getDimensionPixelSize(attr, -1);
2767 break;
2768 case com.android.internal.R.styleable.View_paddingEnd:
2769 endPadding = a.getDimensionPixelSize(attr, -1);
2770 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 case com.android.internal.R.styleable.View_scrollX:
2772 x = a.getDimensionPixelOffset(attr, 0);
2773 break;
2774 case com.android.internal.R.styleable.View_scrollY:
2775 y = a.getDimensionPixelOffset(attr, 0);
2776 break;
Chet Haase73066682010-11-29 15:55:32 -08002777 case com.android.internal.R.styleable.View_alpha:
2778 setAlpha(a.getFloat(attr, 1f));
2779 break;
2780 case com.android.internal.R.styleable.View_transformPivotX:
2781 setPivotX(a.getDimensionPixelOffset(attr, 0));
2782 break;
2783 case com.android.internal.R.styleable.View_transformPivotY:
2784 setPivotY(a.getDimensionPixelOffset(attr, 0));
2785 break;
2786 case com.android.internal.R.styleable.View_translationX:
2787 tx = a.getDimensionPixelOffset(attr, 0);
2788 transformSet = true;
2789 break;
2790 case com.android.internal.R.styleable.View_translationY:
2791 ty = a.getDimensionPixelOffset(attr, 0);
2792 transformSet = true;
2793 break;
2794 case com.android.internal.R.styleable.View_rotation:
2795 rotation = a.getFloat(attr, 0);
2796 transformSet = true;
2797 break;
2798 case com.android.internal.R.styleable.View_rotationX:
2799 rotationX = a.getFloat(attr, 0);
2800 transformSet = true;
2801 break;
2802 case com.android.internal.R.styleable.View_rotationY:
2803 rotationY = a.getFloat(attr, 0);
2804 transformSet = true;
2805 break;
2806 case com.android.internal.R.styleable.View_scaleX:
2807 sx = a.getFloat(attr, 1f);
2808 transformSet = true;
2809 break;
2810 case com.android.internal.R.styleable.View_scaleY:
2811 sy = a.getFloat(attr, 1f);
2812 transformSet = true;
2813 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 case com.android.internal.R.styleable.View_id:
2815 mID = a.getResourceId(attr, NO_ID);
2816 break;
2817 case com.android.internal.R.styleable.View_tag:
2818 mTag = a.getText(attr);
2819 break;
2820 case com.android.internal.R.styleable.View_fitsSystemWindows:
2821 if (a.getBoolean(attr, false)) {
2822 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2823 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2824 }
2825 break;
2826 case com.android.internal.R.styleable.View_focusable:
2827 if (a.getBoolean(attr, false)) {
2828 viewFlagValues |= FOCUSABLE;
2829 viewFlagMasks |= FOCUSABLE_MASK;
2830 }
2831 break;
2832 case com.android.internal.R.styleable.View_focusableInTouchMode:
2833 if (a.getBoolean(attr, false)) {
2834 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2835 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2836 }
2837 break;
2838 case com.android.internal.R.styleable.View_clickable:
2839 if (a.getBoolean(attr, false)) {
2840 viewFlagValues |= CLICKABLE;
2841 viewFlagMasks |= CLICKABLE;
2842 }
2843 break;
2844 case com.android.internal.R.styleable.View_longClickable:
2845 if (a.getBoolean(attr, false)) {
2846 viewFlagValues |= LONG_CLICKABLE;
2847 viewFlagMasks |= LONG_CLICKABLE;
2848 }
2849 break;
2850 case com.android.internal.R.styleable.View_saveEnabled:
2851 if (!a.getBoolean(attr, true)) {
2852 viewFlagValues |= SAVE_DISABLED;
2853 viewFlagMasks |= SAVE_DISABLED_MASK;
2854 }
2855 break;
2856 case com.android.internal.R.styleable.View_duplicateParentState:
2857 if (a.getBoolean(attr, false)) {
2858 viewFlagValues |= DUPLICATE_PARENT_STATE;
2859 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2860 }
2861 break;
2862 case com.android.internal.R.styleable.View_visibility:
2863 final int visibility = a.getInt(attr, 0);
2864 if (visibility != 0) {
2865 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2866 viewFlagMasks |= VISIBILITY_MASK;
2867 }
2868 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002869 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002870 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002871 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002872 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002873 final int layoutDirection = a.getInt(attr, -1);
2874 if (layoutDirection != -1) {
2875 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002876 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002877 // Set to default (LAYOUT_DIRECTION_INHERIT)
2878 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002879 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002880 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002881 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 case com.android.internal.R.styleable.View_drawingCacheQuality:
2883 final int cacheQuality = a.getInt(attr, 0);
2884 if (cacheQuality != 0) {
2885 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2886 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2887 }
2888 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002889 case com.android.internal.R.styleable.View_contentDescription:
2890 mContentDescription = a.getString(attr);
2891 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2893 if (!a.getBoolean(attr, true)) {
2894 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2895 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2896 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002897 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2899 if (!a.getBoolean(attr, true)) {
2900 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2901 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2902 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002903 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 case R.styleable.View_scrollbars:
2905 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2906 if (scrollbars != SCROLLBARS_NONE) {
2907 viewFlagValues |= scrollbars;
2908 viewFlagMasks |= SCROLLBARS_MASK;
2909 initializeScrollbars(a);
2910 }
2911 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07002912 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07002914 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
2915 // Ignore the attribute starting with ICS
2916 break;
2917 }
2918 // With builds < ICS, fall through and apply fading edges
2919 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2921 if (fadingEdge != FADING_EDGE_NONE) {
2922 viewFlagValues |= fadingEdge;
2923 viewFlagMasks |= FADING_EDGE_MASK;
2924 initializeFadingEdge(a);
2925 }
2926 break;
2927 case R.styleable.View_scrollbarStyle:
2928 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2929 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2930 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2931 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2932 }
2933 break;
2934 case R.styleable.View_isScrollContainer:
2935 setScrollContainer = true;
2936 if (a.getBoolean(attr, false)) {
2937 setScrollContainer(true);
2938 }
2939 break;
2940 case com.android.internal.R.styleable.View_keepScreenOn:
2941 if (a.getBoolean(attr, false)) {
2942 viewFlagValues |= KEEP_SCREEN_ON;
2943 viewFlagMasks |= KEEP_SCREEN_ON;
2944 }
2945 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002946 case R.styleable.View_filterTouchesWhenObscured:
2947 if (a.getBoolean(attr, false)) {
2948 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2949 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2950 }
2951 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 case R.styleable.View_nextFocusLeft:
2953 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2954 break;
2955 case R.styleable.View_nextFocusRight:
2956 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2957 break;
2958 case R.styleable.View_nextFocusUp:
2959 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2960 break;
2961 case R.styleable.View_nextFocusDown:
2962 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2963 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002964 case R.styleable.View_nextFocusForward:
2965 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2966 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 case R.styleable.View_minWidth:
2968 mMinWidth = a.getDimensionPixelSize(attr, 0);
2969 break;
2970 case R.styleable.View_minHeight:
2971 mMinHeight = a.getDimensionPixelSize(attr, 0);
2972 break;
Romain Guy9a817362009-05-01 10:57:14 -07002973 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002974 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002975 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002976 + "be used within a restricted context");
2977 }
2978
Romain Guy9a817362009-05-01 10:57:14 -07002979 final String handlerName = a.getString(attr);
2980 if (handlerName != null) {
2981 setOnClickListener(new OnClickListener() {
2982 private Method mHandler;
2983
2984 public void onClick(View v) {
2985 if (mHandler == null) {
2986 try {
2987 mHandler = getContext().getClass().getMethod(handlerName,
2988 View.class);
2989 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002990 int id = getId();
2991 String idText = id == NO_ID ? "" : " with id '"
2992 + getContext().getResources().getResourceEntryName(
2993 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002994 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002995 handlerName + "(View) in the activity "
2996 + getContext().getClass() + " for onClick handler"
2997 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002998 }
2999 }
3000
3001 try {
3002 mHandler.invoke(getContext(), View.this);
3003 } catch (IllegalAccessException e) {
3004 throw new IllegalStateException("Could not execute non "
3005 + "public method of the activity", e);
3006 } catch (InvocationTargetException e) {
3007 throw new IllegalStateException("Could not execute "
3008 + "method of the activity", e);
3009 }
3010 }
3011 });
3012 }
3013 break;
Adam Powell637d3372010-08-25 14:37:03 -07003014 case R.styleable.View_overScrollMode:
3015 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3016 break;
Adam Powell20232d02010-12-08 21:08:53 -08003017 case R.styleable.View_verticalScrollbarPosition:
3018 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3019 break;
Romain Guy171c5922011-01-06 10:04:23 -08003020 case R.styleable.View_layerType:
3021 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3022 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003023 case R.styleable.View_textDirection:
3024 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
3025 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003026 }
3027 }
3028
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003029 a.recycle();
3030
Adam Powell637d3372010-08-25 14:37:03 -07003031 setOverScrollMode(overScrollMode);
3032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 if (background != null) {
3034 setBackgroundDrawable(background);
3035 }
3036
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003037 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
3038
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003039 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3040 // layout direction). Those cached values will be used later during padding resolution.
3041 mUserPaddingStart = startPadding;
3042 mUserPaddingEnd = endPadding;
3043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003044 if (padding >= 0) {
3045 leftPadding = padding;
3046 topPadding = padding;
3047 rightPadding = padding;
3048 bottomPadding = padding;
3049 }
3050
3051 // If the user specified the padding (either with android:padding or
3052 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3053 // use the default padding or the padding from the background drawable
3054 // (stored at this point in mPadding*)
3055 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3056 topPadding >= 0 ? topPadding : mPaddingTop,
3057 rightPadding >= 0 ? rightPadding : mPaddingRight,
3058 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3059
3060 if (viewFlagMasks != 0) {
3061 setFlags(viewFlagValues, viewFlagMasks);
3062 }
3063
3064 // Needs to be called after mViewFlags is set
3065 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3066 recomputePadding();
3067 }
3068
3069 if (x != 0 || y != 0) {
3070 scrollTo(x, y);
3071 }
3072
Chet Haase73066682010-11-29 15:55:32 -08003073 if (transformSet) {
3074 setTranslationX(tx);
3075 setTranslationY(ty);
3076 setRotation(rotation);
3077 setRotationX(rotationX);
3078 setRotationY(rotationY);
3079 setScaleX(sx);
3080 setScaleY(sy);
3081 }
3082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3084 setScrollContainer(true);
3085 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003086
3087 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 }
3089
3090 /**
3091 * Non-public constructor for use in testing
3092 */
3093 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003094 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003095 }
3096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 /**
3098 * <p>
3099 * Initializes the fading edges from a given set of styled attributes. This
3100 * method should be called by subclasses that need fading edges and when an
3101 * instance of these subclasses is created programmatically rather than
3102 * being inflated from XML. This method is automatically called when the XML
3103 * is inflated.
3104 * </p>
3105 *
3106 * @param a the styled attributes set to initialize the fading edges from
3107 */
3108 protected void initializeFadingEdge(TypedArray a) {
3109 initScrollCache();
3110
3111 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3112 R.styleable.View_fadingEdgeLength,
3113 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3114 }
3115
3116 /**
3117 * Returns the size of the vertical faded edges used to indicate that more
3118 * content in this view is visible.
3119 *
3120 * @return The size in pixels of the vertical faded edge or 0 if vertical
3121 * faded edges are not enabled for this view.
3122 * @attr ref android.R.styleable#View_fadingEdgeLength
3123 */
3124 public int getVerticalFadingEdgeLength() {
3125 if (isVerticalFadingEdgeEnabled()) {
3126 ScrollabilityCache cache = mScrollCache;
3127 if (cache != null) {
3128 return cache.fadingEdgeLength;
3129 }
3130 }
3131 return 0;
3132 }
3133
3134 /**
3135 * Set the size of the faded edge used to indicate that more content in this
3136 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003137 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3138 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3139 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003140 *
3141 * @param length The size in pixels of the faded edge used to indicate that more
3142 * content in this view is visible.
3143 */
3144 public void setFadingEdgeLength(int length) {
3145 initScrollCache();
3146 mScrollCache.fadingEdgeLength = length;
3147 }
3148
3149 /**
3150 * Returns the size of the horizontal faded edges used to indicate that more
3151 * content in this view is visible.
3152 *
3153 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3154 * faded edges are not enabled for this view.
3155 * @attr ref android.R.styleable#View_fadingEdgeLength
3156 */
3157 public int getHorizontalFadingEdgeLength() {
3158 if (isHorizontalFadingEdgeEnabled()) {
3159 ScrollabilityCache cache = mScrollCache;
3160 if (cache != null) {
3161 return cache.fadingEdgeLength;
3162 }
3163 }
3164 return 0;
3165 }
3166
3167 /**
3168 * Returns the width of the vertical scrollbar.
3169 *
3170 * @return The width in pixels of the vertical scrollbar or 0 if there
3171 * is no vertical scrollbar.
3172 */
3173 public int getVerticalScrollbarWidth() {
3174 ScrollabilityCache cache = mScrollCache;
3175 if (cache != null) {
3176 ScrollBarDrawable scrollBar = cache.scrollBar;
3177 if (scrollBar != null) {
3178 int size = scrollBar.getSize(true);
3179 if (size <= 0) {
3180 size = cache.scrollBarSize;
3181 }
3182 return size;
3183 }
3184 return 0;
3185 }
3186 return 0;
3187 }
3188
3189 /**
3190 * Returns the height of the horizontal scrollbar.
3191 *
3192 * @return The height in pixels of the horizontal scrollbar or 0 if
3193 * there is no horizontal scrollbar.
3194 */
3195 protected int getHorizontalScrollbarHeight() {
3196 ScrollabilityCache cache = mScrollCache;
3197 if (cache != null) {
3198 ScrollBarDrawable scrollBar = cache.scrollBar;
3199 if (scrollBar != null) {
3200 int size = scrollBar.getSize(false);
3201 if (size <= 0) {
3202 size = cache.scrollBarSize;
3203 }
3204 return size;
3205 }
3206 return 0;
3207 }
3208 return 0;
3209 }
3210
3211 /**
3212 * <p>
3213 * Initializes the scrollbars from a given set of styled attributes. This
3214 * method should be called by subclasses that need scrollbars and when an
3215 * instance of these subclasses is created programmatically rather than
3216 * being inflated from XML. This method is automatically called when the XML
3217 * is inflated.
3218 * </p>
3219 *
3220 * @param a the styled attributes set to initialize the scrollbars from
3221 */
3222 protected void initializeScrollbars(TypedArray a) {
3223 initScrollCache();
3224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003226
Mike Cleronf116bf82009-09-27 19:14:12 -07003227 if (scrollabilityCache.scrollBar == null) {
3228 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3229 }
Joe Malin32736f02011-01-19 16:14:20 -08003230
Romain Guy8bda2482010-03-02 11:42:11 -08003231 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232
Mike Cleronf116bf82009-09-27 19:14:12 -07003233 if (!fadeScrollbars) {
3234 scrollabilityCache.state = ScrollabilityCache.ON;
3235 }
3236 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003237
3238
Mike Cleronf116bf82009-09-27 19:14:12 -07003239 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3240 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3241 .getScrollBarFadeDuration());
3242 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3243 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3244 ViewConfiguration.getScrollDefaultDelay());
3245
Joe Malin32736f02011-01-19 16:14:20 -08003246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3248 com.android.internal.R.styleable.View_scrollbarSize,
3249 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3250
3251 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3252 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3253
3254 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3255 if (thumb != null) {
3256 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3257 }
3258
3259 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3260 false);
3261 if (alwaysDraw) {
3262 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3263 }
3264
3265 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3266 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3267
3268 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3269 if (thumb != null) {
3270 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3271 }
3272
3273 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3274 false);
3275 if (alwaysDraw) {
3276 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3277 }
3278
3279 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003280 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 }
3282
3283 /**
3284 * <p>
3285 * Initalizes the scrollability cache if necessary.
3286 * </p>
3287 */
3288 private void initScrollCache() {
3289 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003290 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291 }
3292 }
3293
3294 /**
Adam Powell20232d02010-12-08 21:08:53 -08003295 * Set the position of the vertical scroll bar. Should be one of
3296 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3297 * {@link #SCROLLBAR_POSITION_RIGHT}.
3298 *
3299 * @param position Where the vertical scroll bar should be positioned.
3300 */
3301 public void setVerticalScrollbarPosition(int position) {
3302 if (mVerticalScrollbarPosition != position) {
3303 mVerticalScrollbarPosition = position;
3304 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003305 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003306 }
3307 }
3308
3309 /**
3310 * @return The position where the vertical scroll bar will show, if applicable.
3311 * @see #setVerticalScrollbarPosition(int)
3312 */
3313 public int getVerticalScrollbarPosition() {
3314 return mVerticalScrollbarPosition;
3315 }
3316
3317 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318 * Register a callback to be invoked when focus of this view changed.
3319 *
3320 * @param l The callback that will run.
3321 */
3322 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3323 mOnFocusChangeListener = l;
3324 }
3325
3326 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003327 * Add a listener that will be called when the bounds of the view change due to
3328 * layout processing.
3329 *
3330 * @param listener The listener that will be called when layout bounds change.
3331 */
3332 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3333 if (mOnLayoutChangeListeners == null) {
3334 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3335 }
3336 mOnLayoutChangeListeners.add(listener);
3337 }
3338
3339 /**
3340 * Remove a listener for layout changes.
3341 *
3342 * @param listener The listener for layout bounds change.
3343 */
3344 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3345 if (mOnLayoutChangeListeners == null) {
3346 return;
3347 }
3348 mOnLayoutChangeListeners.remove(listener);
3349 }
3350
3351 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003352 * Add a listener for attach state changes.
3353 *
3354 * This listener will be called whenever this view is attached or detached
3355 * from a window. Remove the listener using
3356 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3357 *
3358 * @param listener Listener to attach
3359 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3360 */
3361 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3362 if (mOnAttachStateChangeListeners == null) {
3363 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3364 }
3365 mOnAttachStateChangeListeners.add(listener);
3366 }
3367
3368 /**
3369 * Remove a listener for attach state changes. The listener will receive no further
3370 * notification of window attach/detach events.
3371 *
3372 * @param listener Listener to remove
3373 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3374 */
3375 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3376 if (mOnAttachStateChangeListeners == null) {
3377 return;
3378 }
3379 mOnAttachStateChangeListeners.remove(listener);
3380 }
3381
3382 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003383 * Returns the focus-change callback registered for this view.
3384 *
3385 * @return The callback, or null if one is not registered.
3386 */
3387 public OnFocusChangeListener getOnFocusChangeListener() {
3388 return mOnFocusChangeListener;
3389 }
3390
3391 /**
3392 * Register a callback to be invoked when this view is clicked. If this view is not
3393 * clickable, it becomes clickable.
3394 *
3395 * @param l The callback that will run
3396 *
3397 * @see #setClickable(boolean)
3398 */
3399 public void setOnClickListener(OnClickListener l) {
3400 if (!isClickable()) {
3401 setClickable(true);
3402 }
3403 mOnClickListener = l;
3404 }
3405
3406 /**
3407 * Register a callback to be invoked when this view is clicked and held. If this view is not
3408 * long clickable, it becomes long clickable.
3409 *
3410 * @param l The callback that will run
3411 *
3412 * @see #setLongClickable(boolean)
3413 */
3414 public void setOnLongClickListener(OnLongClickListener l) {
3415 if (!isLongClickable()) {
3416 setLongClickable(true);
3417 }
3418 mOnLongClickListener = l;
3419 }
3420
3421 /**
3422 * Register a callback to be invoked when the context menu for this view is
3423 * being built. If this view is not long clickable, it becomes long clickable.
3424 *
3425 * @param l The callback that will run
3426 *
3427 */
3428 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3429 if (!isLongClickable()) {
3430 setLongClickable(true);
3431 }
3432 mOnCreateContextMenuListener = l;
3433 }
3434
3435 /**
3436 * Call this view's OnClickListener, if it is defined.
3437 *
3438 * @return True there was an assigned OnClickListener that was called, false
3439 * otherwise is returned.
3440 */
3441 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003442 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003444 if (mOnClickListener != null) {
3445 playSoundEffect(SoundEffectConstants.CLICK);
3446 mOnClickListener.onClick(this);
3447 return true;
3448 }
3449
3450 return false;
3451 }
3452
3453 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003454 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3455 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003457 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 */
3459 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003460 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003462 boolean handled = false;
3463 if (mOnLongClickListener != null) {
3464 handled = mOnLongClickListener.onLongClick(View.this);
3465 }
3466 if (!handled) {
3467 handled = showContextMenu();
3468 }
3469 if (handled) {
3470 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3471 }
3472 return handled;
3473 }
3474
3475 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003476 * Performs button-related actions during a touch down event.
3477 *
3478 * @param event The event.
3479 * @return True if the down was consumed.
3480 *
3481 * @hide
3482 */
3483 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3484 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3485 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3486 return true;
3487 }
3488 }
3489 return false;
3490 }
3491
3492 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 * Bring up the context menu for this view.
3494 *
3495 * @return Whether a context menu was displayed.
3496 */
3497 public boolean showContextMenu() {
3498 return getParent().showContextMenuForChild(this);
3499 }
3500
3501 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003502 * Bring up the context menu for this view, referring to the item under the specified point.
3503 *
3504 * @param x The referenced x coordinate.
3505 * @param y The referenced y coordinate.
3506 * @param metaState The keyboard modifiers that were pressed.
3507 * @return Whether a context menu was displayed.
3508 *
3509 * @hide
3510 */
3511 public boolean showContextMenu(float x, float y, int metaState) {
3512 return showContextMenu();
3513 }
3514
3515 /**
Adam Powell6e346362010-07-23 10:18:23 -07003516 * Start an action mode.
3517 *
3518 * @param callback Callback that will control the lifecycle of the action mode
3519 * @return The new action mode if it is started, null otherwise
3520 *
3521 * @see ActionMode
3522 */
3523 public ActionMode startActionMode(ActionMode.Callback callback) {
3524 return getParent().startActionModeForChild(this, callback);
3525 }
3526
3527 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003528 * Register a callback to be invoked when a key is pressed in this view.
3529 * @param l the key listener to attach to this view
3530 */
3531 public void setOnKeyListener(OnKeyListener l) {
3532 mOnKeyListener = l;
3533 }
3534
3535 /**
3536 * Register a callback to be invoked when a touch event is sent to this view.
3537 * @param l the touch listener to attach to this view
3538 */
3539 public void setOnTouchListener(OnTouchListener l) {
3540 mOnTouchListener = l;
3541 }
3542
3543 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003544 * Register a callback to be invoked when a generic motion event is sent to this view.
3545 * @param l the generic motion listener to attach to this view
3546 */
3547 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3548 mOnGenericMotionListener = l;
3549 }
3550
3551 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003552 * Register a callback to be invoked when a hover event is sent to this view.
3553 * @param l the hover listener to attach to this view
3554 */
3555 public void setOnHoverListener(OnHoverListener l) {
3556 mOnHoverListener = l;
3557 }
3558
3559 /**
Joe Malin32736f02011-01-19 16:14:20 -08003560 * Register a drag event listener callback object for this View. The parameter is
3561 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3562 * View, the system calls the
3563 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3564 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003565 */
3566 public void setOnDragListener(OnDragListener l) {
3567 mOnDragListener = l;
3568 }
3569
3570 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003571 * Give this view focus. This will cause
3572 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003573 *
3574 * Note: this does not check whether this {@link View} should get focus, it just
3575 * gives it focus no matter what. It should only be called internally by framework
3576 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3577 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003578 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3579 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003580 * focus moved when requestFocus() is called. It may not always
3581 * apply, in which case use the default View.FOCUS_DOWN.
3582 * @param previouslyFocusedRect The rectangle of the view that had focus
3583 * prior in this View's coordinate system.
3584 */
3585 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3586 if (DBG) {
3587 System.out.println(this + " requestFocus()");
3588 }
3589
3590 if ((mPrivateFlags & FOCUSED) == 0) {
3591 mPrivateFlags |= FOCUSED;
3592
3593 if (mParent != null) {
3594 mParent.requestChildFocus(this, this);
3595 }
3596
3597 onFocusChanged(true, direction, previouslyFocusedRect);
3598 refreshDrawableState();
3599 }
3600 }
3601
3602 /**
3603 * Request that a rectangle of this view be visible on the screen,
3604 * scrolling if necessary just enough.
3605 *
3606 * <p>A View should call this if it maintains some notion of which part
3607 * of its content is interesting. For example, a text editing view
3608 * should call this when its cursor moves.
3609 *
3610 * @param rectangle The rectangle.
3611 * @return Whether any parent scrolled.
3612 */
3613 public boolean requestRectangleOnScreen(Rect rectangle) {
3614 return requestRectangleOnScreen(rectangle, false);
3615 }
3616
3617 /**
3618 * Request that a rectangle of this view be visible on the screen,
3619 * scrolling if necessary just enough.
3620 *
3621 * <p>A View should call this if it maintains some notion of which part
3622 * of its content is interesting. For example, a text editing view
3623 * should call this when its cursor moves.
3624 *
3625 * <p>When <code>immediate</code> is set to true, scrolling will not be
3626 * animated.
3627 *
3628 * @param rectangle The rectangle.
3629 * @param immediate True to forbid animated scrolling, false otherwise
3630 * @return Whether any parent scrolled.
3631 */
3632 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3633 View child = this;
3634 ViewParent parent = mParent;
3635 boolean scrolled = false;
3636 while (parent != null) {
3637 scrolled |= parent.requestChildRectangleOnScreen(child,
3638 rectangle, immediate);
3639
3640 // offset rect so next call has the rectangle in the
3641 // coordinate system of its direct child.
3642 rectangle.offset(child.getLeft(), child.getTop());
3643 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3644
3645 if (!(parent instanceof View)) {
3646 break;
3647 }
Romain Guy8506ab42009-06-11 17:35:47 -07003648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003649 child = (View) parent;
3650 parent = child.getParent();
3651 }
3652 return scrolled;
3653 }
3654
3655 /**
3656 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003657 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003658 */
3659 public void clearFocus() {
3660 if (DBG) {
3661 System.out.println(this + " clearFocus()");
3662 }
3663
3664 if ((mPrivateFlags & FOCUSED) != 0) {
3665 mPrivateFlags &= ~FOCUSED;
3666
3667 if (mParent != null) {
3668 mParent.clearChildFocus(this);
3669 }
3670
3671 onFocusChanged(false, 0, null);
3672 refreshDrawableState();
3673 }
3674 }
3675
3676 /**
3677 * Called to clear the focus of a view that is about to be removed.
3678 * Doesn't call clearChildFocus, which prevents this view from taking
3679 * focus again before it has been removed from the parent
3680 */
3681 void clearFocusForRemoval() {
3682 if ((mPrivateFlags & FOCUSED) != 0) {
3683 mPrivateFlags &= ~FOCUSED;
3684
3685 onFocusChanged(false, 0, null);
3686 refreshDrawableState();
3687 }
3688 }
3689
3690 /**
3691 * Called internally by the view system when a new view is getting focus.
3692 * This is what clears the old focus.
3693 */
3694 void unFocus() {
3695 if (DBG) {
3696 System.out.println(this + " unFocus()");
3697 }
3698
3699 if ((mPrivateFlags & FOCUSED) != 0) {
3700 mPrivateFlags &= ~FOCUSED;
3701
3702 onFocusChanged(false, 0, null);
3703 refreshDrawableState();
3704 }
3705 }
3706
3707 /**
3708 * Returns true if this view has focus iteself, or is the ancestor of the
3709 * view that has focus.
3710 *
3711 * @return True if this view has or contains focus, false otherwise.
3712 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003713 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003714 public boolean hasFocus() {
3715 return (mPrivateFlags & FOCUSED) != 0;
3716 }
3717
3718 /**
3719 * Returns true if this view is focusable or if it contains a reachable View
3720 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3721 * is a View whose parents do not block descendants focus.
3722 *
3723 * Only {@link #VISIBLE} views are considered focusable.
3724 *
3725 * @return True if the view is focusable or if the view contains a focusable
3726 * View, false otherwise.
3727 *
3728 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3729 */
3730 public boolean hasFocusable() {
3731 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3732 }
3733
3734 /**
3735 * Called by the view system when the focus state of this view changes.
3736 * When the focus change event is caused by directional navigation, direction
3737 * and previouslyFocusedRect provide insight into where the focus is coming from.
3738 * When overriding, be sure to call up through to the super class so that
3739 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003740 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003741 * @param gainFocus True if the View has focus; false otherwise.
3742 * @param direction The direction focus has moved when requestFocus()
3743 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003744 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3745 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3746 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3748 * system, of the previously focused view. If applicable, this will be
3749 * passed in as finer grained information about where the focus is coming
3750 * from (in addition to direction). Will be <code>null</code> otherwise.
3751 */
3752 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003753 if (gainFocus) {
3754 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3755 }
3756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 InputMethodManager imm = InputMethodManager.peekInstance();
3758 if (!gainFocus) {
3759 if (isPressed()) {
3760 setPressed(false);
3761 }
3762 if (imm != null && mAttachInfo != null
3763 && mAttachInfo.mHasWindowFocus) {
3764 imm.focusOut(this);
3765 }
Romain Guya2431d02009-04-30 16:30:00 -07003766 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003767 } else if (imm != null && mAttachInfo != null
3768 && mAttachInfo.mHasWindowFocus) {
3769 imm.focusIn(this);
3770 }
Romain Guy8506ab42009-06-11 17:35:47 -07003771
Romain Guy0fd89bf2011-01-26 15:41:30 -08003772 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003773 if (mOnFocusChangeListener != null) {
3774 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3775 }
Joe Malin32736f02011-01-19 16:14:20 -08003776
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003777 if (mAttachInfo != null) {
3778 mAttachInfo.mKeyDispatchState.reset(this);
3779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003780 }
3781
3782 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003783 * Sends an accessibility event of the given type. If accessiiblity is
3784 * not enabled this method has no effect. The default implementation calls
3785 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3786 * to populate information about the event source (this View), then calls
3787 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3788 * populate the text content of the event source including its descendants,
3789 * and last calls
3790 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3791 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003792 * <p>
3793 * If an {@link AccessibilityDelegate} has been specified via calling
3794 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3795 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
3796 * responsible for handling this call.
3797 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003798 *
3799 * @param eventType The type of the event to send.
3800 *
3801 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3802 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3803 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003804 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07003805 */
3806 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003807 if (mAccessibilityDelegate != null) {
3808 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
3809 } else {
3810 sendAccessibilityEventInternal(eventType);
3811 }
3812 }
3813
3814 /**
3815 * @see #sendAccessibilityEvent(int)
3816 *
3817 * Note: Called from the default {@link AccessibilityDelegate}.
3818 */
3819 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003820 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3821 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3822 }
3823 }
3824
3825 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003826 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3827 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003828 * perform a check whether accessibility is enabled.
3829 * <p>
3830 * If an {@link AccessibilityDelegate} has been specified via calling
3831 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3832 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
3833 * is responsible for handling this call.
3834 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003835 *
3836 * @param event The event to send.
3837 *
3838 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003839 */
3840 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003841 if (mAccessibilityDelegate != null) {
3842 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
3843 } else {
3844 sendAccessibilityEventUncheckedInternal(event);
3845 }
3846 }
3847
3848 /**
3849 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
3850 *
3851 * Note: Called from the default {@link AccessibilityDelegate}.
3852 */
3853 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003854 if (!isShown()) {
3855 return;
3856 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003857 onInitializeAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003858 dispatchPopulateAccessibilityEvent(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003859 // In the beginning we called #isShown(), so we know that getParent() is not null.
3860 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003861 }
3862
3863 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003864 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3865 * to its children for adding their text content to the event. Note that the
3866 * event text is populated in a separate dispatch path since we add to the
3867 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003868 * A typical implementation will call
3869 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3870 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3871 * on each child. Override this method if custom population of the event text
3872 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003873 * <p>
3874 * If an {@link AccessibilityDelegate} has been specified via calling
3875 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3876 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
3877 * is responsible for handling this call.
3878 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07003879 *
3880 * @param event The event.
3881 *
3882 * @return True if the event population was completed.
3883 */
3884 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003885 if (mAccessibilityDelegate != null) {
3886 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
3887 } else {
3888 return dispatchPopulateAccessibilityEventInternal(event);
3889 }
3890 }
3891
3892 /**
3893 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3894 *
3895 * Note: Called from the default {@link AccessibilityDelegate}.
3896 */
3897 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganovb84b94e2011-09-22 19:26:56 -07003898 // Do not populate text to scroll events. They describe position change
3899 // and usually come from container with a lot of text which is not very
3900 // informative for accessibility purposes. Also they are fired frequently.
3901 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
3902 return true;
3903 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003904 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003905 return false;
3906 }
3907
3908 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003909 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003910 * giving a chance to this View to populate the accessibility event with its
3911 * text content. While the implementation is free to modify other event
3912 * attributes this should be performed in
3913 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
3914 * <p>
3915 * Example: Adding formatted date string to an accessibility event in addition
3916 * to the text added by the super implementation.
3917 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003918 * public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3919 * super.onPopulateAccessibilityEvent(event);
3920 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
3921 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
3922 * mCurrentDate.getTimeInMillis(), flags);
3923 * event.getText().add(selectedDateUtterance);
3924 * }
3925 * </code></pre></p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003926 * <p>
3927 * If an {@link AccessibilityDelegate} has been specified via calling
3928 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3929 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
3930 * is responsible for handling this call.
3931 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003932 *
3933 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003934 *
3935 * @see #sendAccessibilityEvent(int)
3936 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003937 */
3938 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003939 if (mAccessibilityDelegate != null) {
3940 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
3941 } else {
3942 onPopulateAccessibilityEventInternal(event);
3943 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003944 }
3945
3946 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003947 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
3948 *
3949 * Note: Called from the default {@link AccessibilityDelegate}.
3950 */
3951 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
3952
3953 }
3954
3955 /**
3956 * Initializes an {@link AccessibilityEvent} with information about
3957 * this View which is the event source. In other words, the source of
3958 * an accessibility event is the view whose state change triggered firing
3959 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003960 * <p>
3961 * Example: Setting the password property of an event in addition
3962 * to properties set by the super implementation.
3963 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003964 * public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
3965 * super.onInitializeAccessibilityEvent(event);
3966 * event.setPassword(true);
3967 * }
3968 * </code></pre></p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003969 * <p>
3970 * If an {@link AccessibilityDelegate} has been specified via calling
3971 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3972 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
3973 * is responsible for handling this call.
3974 * </p>
3975 *
3976 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003977 *
3978 * @see #sendAccessibilityEvent(int)
3979 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3980 */
3981 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003982 if (mAccessibilityDelegate != null) {
3983 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
3984 } else {
3985 onInitializeAccessibilityEventInternal(event);
3986 }
3987 }
3988
3989 /**
3990 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3991 *
3992 * Note: Called from the default {@link AccessibilityDelegate}.
3993 */
3994 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003995 event.setSource(this);
Svetoslav Ganov30401322011-05-12 18:53:45 -07003996 event.setClassName(getClass().getName());
3997 event.setPackageName(getContext().getPackageName());
3998 event.setEnabled(isEnabled());
3999 event.setContentDescription(mContentDescription);
4000
Svetoslav Ganova0156172011-06-26 17:55:44 -07004001 final int eventType = event.getEventType();
4002 switch (eventType) {
4003 case AccessibilityEvent.TYPE_VIEW_FOCUSED: {
4004 if (mAttachInfo != null) {
4005 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4006 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4007 FOCUSABLES_ALL);
4008 event.setItemCount(focusablesTempList.size());
4009 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4010 focusablesTempList.clear();
4011 }
4012 } break;
4013 case AccessibilityEvent.TYPE_VIEW_SCROLLED: {
4014 event.setScrollX(mScrollX);
4015 event.setScrollY(mScrollY);
4016 event.setItemCount(getHeight());
4017 } break;
Svetoslav Ganov30401322011-05-12 18:53:45 -07004018 }
4019 }
4020
4021 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004022 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4023 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4024 * This method is responsible for obtaining an accessibility node info from a
4025 * pool of reusable instances and calling
4026 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4027 * initialize the former.
4028 * <p>
4029 * Note: The client is responsible for recycling the obtained instance by calling
4030 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4031 * </p>
4032 * @return A populated {@link AccessibilityNodeInfo}.
4033 *
4034 * @see AccessibilityNodeInfo
4035 */
4036 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
4037 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4038 onInitializeAccessibilityNodeInfo(info);
4039 return info;
4040 }
4041
4042 /**
4043 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4044 * The base implementation sets:
4045 * <ul>
4046 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004047 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4048 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004049 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4050 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4051 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4052 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4053 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4054 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4055 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4056 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4057 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4058 * </ul>
4059 * <p>
4060 * Subclasses should override this method, call the super implementation,
4061 * and set additional attributes.
4062 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004063 * <p>
4064 * If an {@link AccessibilityDelegate} has been specified via calling
4065 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4066 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4067 * is responsible for handling this call.
4068 * </p>
4069 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004070 * @param info The instance to initialize.
4071 */
4072 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004073 if (mAccessibilityDelegate != null) {
4074 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4075 } else {
4076 onInitializeAccessibilityNodeInfoInternal(info);
4077 }
4078 }
4079
4080 /**
4081 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4082 *
4083 * Note: Called from the default {@link AccessibilityDelegate}.
4084 */
4085 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004086 Rect bounds = mAttachInfo.mTmpInvalRect;
4087 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004088 info.setBoundsInParent(bounds);
4089
4090 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4091 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004092 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004093 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4094 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004095
4096 ViewParent parent = getParent();
4097 if (parent instanceof View) {
4098 View parentView = (View) parent;
4099 info.setParent(parentView);
4100 }
4101
4102 info.setPackageName(mContext.getPackageName());
4103 info.setClassName(getClass().getName());
4104 info.setContentDescription(getContentDescription());
4105
4106 info.setEnabled(isEnabled());
4107 info.setClickable(isClickable());
4108 info.setFocusable(isFocusable());
4109 info.setFocused(isFocused());
4110 info.setSelected(isSelected());
4111 info.setLongClickable(isLongClickable());
4112
4113 // TODO: These make sense only if we are in an AdapterView but all
4114 // views can be selected. Maybe from accessiiblity perspective
4115 // we should report as selectable view in an AdapterView.
4116 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4117 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4118
4119 if (isFocusable()) {
4120 if (isFocused()) {
4121 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4122 } else {
4123 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4124 }
4125 }
4126 }
4127
4128 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004129 * Sets a delegate for implementing accessibility support via compositon as
4130 * opposed to inheritance. The delegate's primary use is for implementing
4131 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4132 *
4133 * @param delegate The delegate instance.
4134 *
4135 * @see AccessibilityDelegate
4136 */
4137 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4138 mAccessibilityDelegate = delegate;
4139 }
4140
4141 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004142 * Gets the unique identifier of this view on the screen for accessibility purposes.
4143 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4144 *
4145 * @return The view accessibility id.
4146 *
4147 * @hide
4148 */
4149 public int getAccessibilityViewId() {
4150 if (mAccessibilityViewId == NO_ID) {
4151 mAccessibilityViewId = sNextAccessibilityViewId++;
4152 }
4153 return mAccessibilityViewId;
4154 }
4155
4156 /**
4157 * Gets the unique identifier of the window in which this View reseides.
4158 *
4159 * @return The window accessibility id.
4160 *
4161 * @hide
4162 */
4163 public int getAccessibilityWindowId() {
4164 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4165 }
4166
4167 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004168 * Gets the {@link View} description. It briefly describes the view and is
4169 * primarily used for accessibility support. Set this property to enable
4170 * better accessibility support for your application. This is especially
4171 * true for views that do not have textual representation (For example,
4172 * ImageButton).
4173 *
4174 * @return The content descriptiopn.
4175 *
4176 * @attr ref android.R.styleable#View_contentDescription
4177 */
4178 public CharSequence getContentDescription() {
4179 return mContentDescription;
4180 }
4181
4182 /**
4183 * Sets the {@link View} description. It briefly describes the view and is
4184 * primarily used for accessibility support. Set this property to enable
4185 * better accessibility support for your application. This is especially
4186 * true for views that do not have textual representation (For example,
4187 * ImageButton).
4188 *
4189 * @param contentDescription The content description.
4190 *
4191 * @attr ref android.R.styleable#View_contentDescription
4192 */
4193 public void setContentDescription(CharSequence contentDescription) {
4194 mContentDescription = contentDescription;
4195 }
4196
4197 /**
Romain Guya2431d02009-04-30 16:30:00 -07004198 * Invoked whenever this view loses focus, either by losing window focus or by losing
4199 * focus within its window. This method can be used to clear any state tied to the
4200 * focus. For instance, if a button is held pressed with the trackball and the window
4201 * loses focus, this method can be used to cancel the press.
4202 *
4203 * Subclasses of View overriding this method should always call super.onFocusLost().
4204 *
4205 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004206 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004207 *
4208 * @hide pending API council approval
4209 */
4210 protected void onFocusLost() {
4211 resetPressedState();
4212 }
4213
4214 private void resetPressedState() {
4215 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4216 return;
4217 }
4218
4219 if (isPressed()) {
4220 setPressed(false);
4221
4222 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004223 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004224 }
4225 }
4226 }
4227
4228 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004229 * Returns true if this view has focus
4230 *
4231 * @return True if this view has focus, false otherwise.
4232 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004233 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004234 public boolean isFocused() {
4235 return (mPrivateFlags & FOCUSED) != 0;
4236 }
4237
4238 /**
4239 * Find the view in the hierarchy rooted at this view that currently has
4240 * focus.
4241 *
4242 * @return The view that currently has focus, or null if no focused view can
4243 * be found.
4244 */
4245 public View findFocus() {
4246 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4247 }
4248
4249 /**
4250 * Change whether this view is one of the set of scrollable containers in
4251 * its window. This will be used to determine whether the window can
4252 * resize or must pan when a soft input area is open -- scrollable
4253 * containers allow the window to use resize mode since the container
4254 * will appropriately shrink.
4255 */
4256 public void setScrollContainer(boolean isScrollContainer) {
4257 if (isScrollContainer) {
4258 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4259 mAttachInfo.mScrollContainers.add(this);
4260 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4261 }
4262 mPrivateFlags |= SCROLL_CONTAINER;
4263 } else {
4264 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4265 mAttachInfo.mScrollContainers.remove(this);
4266 }
4267 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4268 }
4269 }
4270
4271 /**
4272 * Returns the quality of the drawing cache.
4273 *
4274 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4275 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4276 *
4277 * @see #setDrawingCacheQuality(int)
4278 * @see #setDrawingCacheEnabled(boolean)
4279 * @see #isDrawingCacheEnabled()
4280 *
4281 * @attr ref android.R.styleable#View_drawingCacheQuality
4282 */
4283 public int getDrawingCacheQuality() {
4284 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4285 }
4286
4287 /**
4288 * Set the drawing cache quality of this view. This value is used only when the
4289 * drawing cache is enabled
4290 *
4291 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4292 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4293 *
4294 * @see #getDrawingCacheQuality()
4295 * @see #setDrawingCacheEnabled(boolean)
4296 * @see #isDrawingCacheEnabled()
4297 *
4298 * @attr ref android.R.styleable#View_drawingCacheQuality
4299 */
4300 public void setDrawingCacheQuality(int quality) {
4301 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4302 }
4303
4304 /**
4305 * Returns whether the screen should remain on, corresponding to the current
4306 * value of {@link #KEEP_SCREEN_ON}.
4307 *
4308 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4309 *
4310 * @see #setKeepScreenOn(boolean)
4311 *
4312 * @attr ref android.R.styleable#View_keepScreenOn
4313 */
4314 public boolean getKeepScreenOn() {
4315 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4316 }
4317
4318 /**
4319 * Controls whether the screen should remain on, modifying the
4320 * value of {@link #KEEP_SCREEN_ON}.
4321 *
4322 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4323 *
4324 * @see #getKeepScreenOn()
4325 *
4326 * @attr ref android.R.styleable#View_keepScreenOn
4327 */
4328 public void setKeepScreenOn(boolean keepScreenOn) {
4329 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4330 }
4331
4332 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004333 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4334 * @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 -08004335 *
4336 * @attr ref android.R.styleable#View_nextFocusLeft
4337 */
4338 public int getNextFocusLeftId() {
4339 return mNextFocusLeftId;
4340 }
4341
4342 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004343 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4344 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4345 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004346 *
4347 * @attr ref android.R.styleable#View_nextFocusLeft
4348 */
4349 public void setNextFocusLeftId(int nextFocusLeftId) {
4350 mNextFocusLeftId = nextFocusLeftId;
4351 }
4352
4353 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004354 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4355 * @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 -08004356 *
4357 * @attr ref android.R.styleable#View_nextFocusRight
4358 */
4359 public int getNextFocusRightId() {
4360 return mNextFocusRightId;
4361 }
4362
4363 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004364 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4365 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4366 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004367 *
4368 * @attr ref android.R.styleable#View_nextFocusRight
4369 */
4370 public void setNextFocusRightId(int nextFocusRightId) {
4371 mNextFocusRightId = nextFocusRightId;
4372 }
4373
4374 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004375 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4376 * @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 -08004377 *
4378 * @attr ref android.R.styleable#View_nextFocusUp
4379 */
4380 public int getNextFocusUpId() {
4381 return mNextFocusUpId;
4382 }
4383
4384 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004385 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4386 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4387 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004388 *
4389 * @attr ref android.R.styleable#View_nextFocusUp
4390 */
4391 public void setNextFocusUpId(int nextFocusUpId) {
4392 mNextFocusUpId = nextFocusUpId;
4393 }
4394
4395 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004396 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4397 * @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 -08004398 *
4399 * @attr ref android.R.styleable#View_nextFocusDown
4400 */
4401 public int getNextFocusDownId() {
4402 return mNextFocusDownId;
4403 }
4404
4405 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004406 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4407 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4408 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004409 *
4410 * @attr ref android.R.styleable#View_nextFocusDown
4411 */
4412 public void setNextFocusDownId(int nextFocusDownId) {
4413 mNextFocusDownId = nextFocusDownId;
4414 }
4415
4416 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004417 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4418 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4419 *
4420 * @attr ref android.R.styleable#View_nextFocusForward
4421 */
4422 public int getNextFocusForwardId() {
4423 return mNextFocusForwardId;
4424 }
4425
4426 /**
4427 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4428 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4429 * decide automatically.
4430 *
4431 * @attr ref android.R.styleable#View_nextFocusForward
4432 */
4433 public void setNextFocusForwardId(int nextFocusForwardId) {
4434 mNextFocusForwardId = nextFocusForwardId;
4435 }
4436
4437 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 * Returns the visibility of this view and all of its ancestors
4439 *
4440 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4441 */
4442 public boolean isShown() {
4443 View current = this;
4444 //noinspection ConstantConditions
4445 do {
4446 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4447 return false;
4448 }
4449 ViewParent parent = current.mParent;
4450 if (parent == null) {
4451 return false; // We are not attached to the view root
4452 }
4453 if (!(parent instanceof View)) {
4454 return true;
4455 }
4456 current = (View) parent;
4457 } while (current != null);
4458
4459 return false;
4460 }
4461
4462 /**
4463 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4464 * is set
4465 *
4466 * @param insets Insets for system windows
4467 *
4468 * @return True if this view applied the insets, false otherwise
4469 */
4470 protected boolean fitSystemWindows(Rect insets) {
4471 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4472 mPaddingLeft = insets.left;
4473 mPaddingTop = insets.top;
4474 mPaddingRight = insets.right;
4475 mPaddingBottom = insets.bottom;
4476 requestLayout();
4477 return true;
4478 }
4479 return false;
4480 }
4481
4482 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004483 * Set whether or not this view should account for system screen decorations
4484 * such as the status bar and inset its content. This allows this view to be
4485 * positioned in absolute screen coordinates and remain visible to the user.
4486 *
4487 * <p>This should only be used by top-level window decor views.
4488 *
4489 * @param fitSystemWindows true to inset content for system screen decorations, false for
4490 * default behavior.
4491 *
4492 * @attr ref android.R.styleable#View_fitsSystemWindows
4493 */
4494 public void setFitsSystemWindows(boolean fitSystemWindows) {
4495 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4496 }
4497
4498 /**
4499 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4500 * will account for system screen decorations such as the status bar and inset its
4501 * content. This allows the view to be positioned in absolute screen coordinates
4502 * and remain visible to the user.
4503 *
4504 * @return true if this view will adjust its content bounds for system screen decorations.
4505 *
4506 * @attr ref android.R.styleable#View_fitsSystemWindows
4507 */
4508 public boolean fitsSystemWindows() {
4509 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4510 }
4511
4512 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004513 * Returns the visibility status for this view.
4514 *
4515 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4516 * @attr ref android.R.styleable#View_visibility
4517 */
4518 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004519 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4520 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4521 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004522 })
4523 public int getVisibility() {
4524 return mViewFlags & VISIBILITY_MASK;
4525 }
4526
4527 /**
4528 * Set the enabled state of this view.
4529 *
4530 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4531 * @attr ref android.R.styleable#View_visibility
4532 */
4533 @RemotableViewMethod
4534 public void setVisibility(int visibility) {
4535 setFlags(visibility, VISIBILITY_MASK);
4536 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4537 }
4538
4539 /**
4540 * Returns the enabled status for this view. The interpretation of the
4541 * enabled state varies by subclass.
4542 *
4543 * @return True if this view is enabled, false otherwise.
4544 */
4545 @ViewDebug.ExportedProperty
4546 public boolean isEnabled() {
4547 return (mViewFlags & ENABLED_MASK) == ENABLED;
4548 }
4549
4550 /**
4551 * Set the enabled state of this view. The interpretation of the enabled
4552 * state varies by subclass.
4553 *
4554 * @param enabled True if this view is enabled, false otherwise.
4555 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004556 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004557 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004558 if (enabled == isEnabled()) return;
4559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004560 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4561
4562 /*
4563 * The View most likely has to change its appearance, so refresh
4564 * the drawable state.
4565 */
4566 refreshDrawableState();
4567
4568 // Invalidate too, since the default behavior for views is to be
4569 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004570 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004571 }
4572
4573 /**
4574 * Set whether this view can receive the focus.
4575 *
4576 * Setting this to false will also ensure that this view is not focusable
4577 * in touch mode.
4578 *
4579 * @param focusable If true, this view can receive the focus.
4580 *
4581 * @see #setFocusableInTouchMode(boolean)
4582 * @attr ref android.R.styleable#View_focusable
4583 */
4584 public void setFocusable(boolean focusable) {
4585 if (!focusable) {
4586 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4587 }
4588 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4589 }
4590
4591 /**
4592 * Set whether this view can receive focus while in touch mode.
4593 *
4594 * Setting this to true will also ensure that this view is focusable.
4595 *
4596 * @param focusableInTouchMode If true, this view can receive the focus while
4597 * in touch mode.
4598 *
4599 * @see #setFocusable(boolean)
4600 * @attr ref android.R.styleable#View_focusableInTouchMode
4601 */
4602 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4603 // Focusable in touch mode should always be set before the focusable flag
4604 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4605 // which, in touch mode, will not successfully request focus on this view
4606 // because the focusable in touch mode flag is not set
4607 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4608 if (focusableInTouchMode) {
4609 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4610 }
4611 }
4612
4613 /**
4614 * Set whether this view should have sound effects enabled for events such as
4615 * clicking and touching.
4616 *
4617 * <p>You may wish to disable sound effects for a view if you already play sounds,
4618 * for instance, a dial key that plays dtmf tones.
4619 *
4620 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4621 * @see #isSoundEffectsEnabled()
4622 * @see #playSoundEffect(int)
4623 * @attr ref android.R.styleable#View_soundEffectsEnabled
4624 */
4625 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4626 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4627 }
4628
4629 /**
4630 * @return whether this view should have sound effects enabled for events such as
4631 * clicking and touching.
4632 *
4633 * @see #setSoundEffectsEnabled(boolean)
4634 * @see #playSoundEffect(int)
4635 * @attr ref android.R.styleable#View_soundEffectsEnabled
4636 */
4637 @ViewDebug.ExportedProperty
4638 public boolean isSoundEffectsEnabled() {
4639 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4640 }
4641
4642 /**
4643 * Set whether this view should have haptic feedback for events such as
4644 * long presses.
4645 *
4646 * <p>You may wish to disable haptic feedback if your view already controls
4647 * its own haptic feedback.
4648 *
4649 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4650 * @see #isHapticFeedbackEnabled()
4651 * @see #performHapticFeedback(int)
4652 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4653 */
4654 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4655 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4656 }
4657
4658 /**
4659 * @return whether this view should have haptic feedback enabled for events
4660 * long presses.
4661 *
4662 * @see #setHapticFeedbackEnabled(boolean)
4663 * @see #performHapticFeedback(int)
4664 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4665 */
4666 @ViewDebug.ExportedProperty
4667 public boolean isHapticFeedbackEnabled() {
4668 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4669 }
4670
4671 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004672 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004673 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004674 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4675 * {@link #LAYOUT_DIRECTION_RTL},
4676 * {@link #LAYOUT_DIRECTION_INHERIT} or
4677 * {@link #LAYOUT_DIRECTION_LOCALE}.
4678 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004679 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004680 * @hide
4681 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004682 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004683 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4684 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4685 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4686 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004687 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004688 public int getLayoutDirection() {
4689 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004690 }
4691
4692 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004693 * Set the layout direction for this view. This will propagate a reset of layout direction
4694 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004695 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004696 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4697 * {@link #LAYOUT_DIRECTION_RTL},
4698 * {@link #LAYOUT_DIRECTION_INHERIT} or
4699 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004700 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004701 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004702 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004703 * @hide
4704 */
4705 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004706 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004707 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004708 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004709 // Setting the flag will also request a layout.
4710 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4711 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004712 }
4713
4714 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004715 * Returns the resolved layout direction for this view.
4716 *
4717 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4718 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4719 *
4720 * @hide
4721 */
4722 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4723 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4724 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4725 })
4726 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004727 resolveLayoutDirectionIfNeeded();
4728 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004729 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4730 }
4731
4732 /**
4733 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4734 * layout attribute and/or the inherited value from the parent.</p>
4735 *
4736 * @return true if the layout is right-to-left.
4737 *
4738 * @hide
4739 */
4740 @ViewDebug.ExportedProperty(category = "layout")
4741 public boolean isLayoutRtl() {
4742 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4743 }
4744
4745 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004746 * If this view doesn't do any drawing on its own, set this flag to
4747 * allow further optimizations. By default, this flag is not set on
4748 * View, but could be set on some View subclasses such as ViewGroup.
4749 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004750 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4751 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004752 *
4753 * @param willNotDraw whether or not this View draw on its own
4754 */
4755 public void setWillNotDraw(boolean willNotDraw) {
4756 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4757 }
4758
4759 /**
4760 * Returns whether or not this View draws on its own.
4761 *
4762 * @return true if this view has nothing to draw, false otherwise
4763 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004764 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004765 public boolean willNotDraw() {
4766 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4767 }
4768
4769 /**
4770 * When a View's drawing cache is enabled, drawing is redirected to an
4771 * offscreen bitmap. Some views, like an ImageView, must be able to
4772 * bypass this mechanism if they already draw a single bitmap, to avoid
4773 * unnecessary usage of the memory.
4774 *
4775 * @param willNotCacheDrawing true if this view does not cache its
4776 * drawing, false otherwise
4777 */
4778 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4779 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4780 }
4781
4782 /**
4783 * Returns whether or not this View can cache its drawing or not.
4784 *
4785 * @return true if this view does not cache its drawing, false otherwise
4786 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004787 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004788 public boolean willNotCacheDrawing() {
4789 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4790 }
4791
4792 /**
4793 * Indicates whether this view reacts to click events or not.
4794 *
4795 * @return true if the view is clickable, false otherwise
4796 *
4797 * @see #setClickable(boolean)
4798 * @attr ref android.R.styleable#View_clickable
4799 */
4800 @ViewDebug.ExportedProperty
4801 public boolean isClickable() {
4802 return (mViewFlags & CLICKABLE) == CLICKABLE;
4803 }
4804
4805 /**
4806 * Enables or disables click events for this view. When a view
4807 * is clickable it will change its state to "pressed" on every click.
4808 * Subclasses should set the view clickable to visually react to
4809 * user's clicks.
4810 *
4811 * @param clickable true to make the view clickable, false otherwise
4812 *
4813 * @see #isClickable()
4814 * @attr ref android.R.styleable#View_clickable
4815 */
4816 public void setClickable(boolean clickable) {
4817 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4818 }
4819
4820 /**
4821 * Indicates whether this view reacts to long click events or not.
4822 *
4823 * @return true if the view is long clickable, false otherwise
4824 *
4825 * @see #setLongClickable(boolean)
4826 * @attr ref android.R.styleable#View_longClickable
4827 */
4828 public boolean isLongClickable() {
4829 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4830 }
4831
4832 /**
4833 * Enables or disables long click events for this view. When a view is long
4834 * clickable it reacts to the user holding down the button for a longer
4835 * duration than a tap. This event can either launch the listener or a
4836 * context menu.
4837 *
4838 * @param longClickable true to make the view long clickable, false otherwise
4839 * @see #isLongClickable()
4840 * @attr ref android.R.styleable#View_longClickable
4841 */
4842 public void setLongClickable(boolean longClickable) {
4843 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4844 }
4845
4846 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004847 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004848 *
4849 * @see #isClickable()
4850 * @see #setClickable(boolean)
4851 *
4852 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4853 * the View's internal state from a previously set "pressed" state.
4854 */
4855 public void setPressed(boolean pressed) {
4856 if (pressed) {
4857 mPrivateFlags |= PRESSED;
4858 } else {
4859 mPrivateFlags &= ~PRESSED;
4860 }
4861 refreshDrawableState();
4862 dispatchSetPressed(pressed);
4863 }
4864
4865 /**
4866 * Dispatch setPressed to all of this View's children.
4867 *
4868 * @see #setPressed(boolean)
4869 *
4870 * @param pressed The new pressed state
4871 */
4872 protected void dispatchSetPressed(boolean pressed) {
4873 }
4874
4875 /**
4876 * Indicates whether the view is currently in pressed state. Unless
4877 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4878 * the pressed state.
4879 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004880 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004881 * @see #isClickable()
4882 * @see #setClickable(boolean)
4883 *
4884 * @return true if the view is currently pressed, false otherwise
4885 */
4886 public boolean isPressed() {
4887 return (mPrivateFlags & PRESSED) == PRESSED;
4888 }
4889
4890 /**
4891 * Indicates whether this view will save its state (that is,
4892 * whether its {@link #onSaveInstanceState} method will be called).
4893 *
4894 * @return Returns true if the view state saving is enabled, else false.
4895 *
4896 * @see #setSaveEnabled(boolean)
4897 * @attr ref android.R.styleable#View_saveEnabled
4898 */
4899 public boolean isSaveEnabled() {
4900 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4901 }
4902
4903 /**
4904 * Controls whether the saving of this view's state is
4905 * enabled (that is, whether its {@link #onSaveInstanceState} method
4906 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004907 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004908 * for its state to be saved. This flag can only disable the
4909 * saving of this view; any child views may still have their state saved.
4910 *
4911 * @param enabled Set to false to <em>disable</em> state saving, or true
4912 * (the default) to allow it.
4913 *
4914 * @see #isSaveEnabled()
4915 * @see #setId(int)
4916 * @see #onSaveInstanceState()
4917 * @attr ref android.R.styleable#View_saveEnabled
4918 */
4919 public void setSaveEnabled(boolean enabled) {
4920 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4921 }
4922
Jeff Brown85a31762010-09-01 17:01:00 -07004923 /**
4924 * Gets whether the framework should discard touches when the view's
4925 * window is obscured by another visible window.
4926 * Refer to the {@link View} security documentation for more details.
4927 *
4928 * @return True if touch filtering is enabled.
4929 *
4930 * @see #setFilterTouchesWhenObscured(boolean)
4931 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4932 */
4933 @ViewDebug.ExportedProperty
4934 public boolean getFilterTouchesWhenObscured() {
4935 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4936 }
4937
4938 /**
4939 * Sets whether the framework should discard touches when the view's
4940 * window is obscured by another visible window.
4941 * Refer to the {@link View} security documentation for more details.
4942 *
4943 * @param enabled True if touch filtering should be enabled.
4944 *
4945 * @see #getFilterTouchesWhenObscured
4946 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4947 */
4948 public void setFilterTouchesWhenObscured(boolean enabled) {
4949 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4950 FILTER_TOUCHES_WHEN_OBSCURED);
4951 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004952
4953 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004954 * Indicates whether the entire hierarchy under this view will save its
4955 * state when a state saving traversal occurs from its parent. The default
4956 * is true; if false, these views will not be saved unless
4957 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4958 *
4959 * @return Returns true if the view state saving from parent is enabled, else false.
4960 *
4961 * @see #setSaveFromParentEnabled(boolean)
4962 */
4963 public boolean isSaveFromParentEnabled() {
4964 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4965 }
4966
4967 /**
4968 * Controls whether the entire hierarchy under this view will save its
4969 * state when a state saving traversal occurs from its parent. The default
4970 * is true; if false, these views will not be saved unless
4971 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4972 *
4973 * @param enabled Set to false to <em>disable</em> state saving, or true
4974 * (the default) to allow it.
4975 *
4976 * @see #isSaveFromParentEnabled()
4977 * @see #setId(int)
4978 * @see #onSaveInstanceState()
4979 */
4980 public void setSaveFromParentEnabled(boolean enabled) {
4981 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4982 }
4983
4984
4985 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004986 * Returns whether this View is able to take focus.
4987 *
4988 * @return True if this view can take focus, or false otherwise.
4989 * @attr ref android.R.styleable#View_focusable
4990 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004991 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004992 public final boolean isFocusable() {
4993 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4994 }
4995
4996 /**
4997 * When a view is focusable, it may not want to take focus when in touch mode.
4998 * For example, a button would like focus when the user is navigating via a D-pad
4999 * so that the user can click on it, but once the user starts touching the screen,
5000 * the button shouldn't take focus
5001 * @return Whether the view is focusable in touch mode.
5002 * @attr ref android.R.styleable#View_focusableInTouchMode
5003 */
5004 @ViewDebug.ExportedProperty
5005 public final boolean isFocusableInTouchMode() {
5006 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5007 }
5008
5009 /**
5010 * Find the nearest view in the specified direction that can take focus.
5011 * This does not actually give focus to that view.
5012 *
5013 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5014 *
5015 * @return The nearest focusable in the specified direction, or null if none
5016 * can be found.
5017 */
5018 public View focusSearch(int direction) {
5019 if (mParent != null) {
5020 return mParent.focusSearch(this, direction);
5021 } else {
5022 return null;
5023 }
5024 }
5025
5026 /**
5027 * This method is the last chance for the focused view and its ancestors to
5028 * respond to an arrow key. This is called when the focused view did not
5029 * consume the key internally, nor could the view system find a new view in
5030 * the requested direction to give focus to.
5031 *
5032 * @param focused The currently focused view.
5033 * @param direction The direction focus wants to move. One of FOCUS_UP,
5034 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5035 * @return True if the this view consumed this unhandled move.
5036 */
5037 public boolean dispatchUnhandledMove(View focused, int direction) {
5038 return false;
5039 }
5040
5041 /**
5042 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005043 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005044 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005045 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5046 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005047 * @return The user specified next view, or null if there is none.
5048 */
5049 View findUserSetNextFocus(View root, int direction) {
5050 switch (direction) {
5051 case FOCUS_LEFT:
5052 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005053 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005054 case FOCUS_RIGHT:
5055 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005056 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005057 case FOCUS_UP:
5058 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005059 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005060 case FOCUS_DOWN:
5061 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005062 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005063 case FOCUS_FORWARD:
5064 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005065 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005066 case FOCUS_BACKWARD: {
5067 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005068 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005069 @Override
5070 public boolean apply(View t) {
5071 return t.mNextFocusForwardId == id;
5072 }
5073 });
5074 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005075 }
5076 return null;
5077 }
5078
Jeff Brown4dfbec22011-08-15 14:55:37 -07005079 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5080 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5081 @Override
5082 public boolean apply(View t) {
5083 return t.mID == childViewId;
5084 }
5085 });
5086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005087 if (result == null) {
5088 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5089 + "by user for id " + childViewId);
5090 }
5091 return result;
5092 }
5093
5094 /**
5095 * Find and return all focusable views that are descendants of this view,
5096 * possibly including this view if it is focusable itself.
5097 *
5098 * @param direction The direction of the focus
5099 * @return A list of focusable views
5100 */
5101 public ArrayList<View> getFocusables(int direction) {
5102 ArrayList<View> result = new ArrayList<View>(24);
5103 addFocusables(result, direction);
5104 return result;
5105 }
5106
5107 /**
5108 * Add any focusable views that are descendants of this view (possibly
5109 * including this view if it is focusable itself) to views. If we are in touch mode,
5110 * only add views that are also focusable in touch mode.
5111 *
5112 * @param views Focusable views found so far
5113 * @param direction The direction of the focus
5114 */
5115 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005116 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5117 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005118
svetoslavganov75986cf2009-05-14 22:28:01 -07005119 /**
5120 * Adds any focusable views that are descendants of this view (possibly
5121 * including this view if it is focusable itself) to views. This method
5122 * adds all focusable views regardless if we are in touch mode or
5123 * only views focusable in touch mode if we are in touch mode depending on
5124 * the focusable mode paramater.
5125 *
5126 * @param views Focusable views found so far or null if all we are interested is
5127 * the number of focusables.
5128 * @param direction The direction of the focus.
5129 * @param focusableMode The type of focusables to be added.
5130 *
5131 * @see #FOCUSABLES_ALL
5132 * @see #FOCUSABLES_TOUCH_MODE
5133 */
5134 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5135 if (!isFocusable()) {
5136 return;
5137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005138
svetoslavganov75986cf2009-05-14 22:28:01 -07005139 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5140 isInTouchMode() && !isFocusableInTouchMode()) {
5141 return;
5142 }
5143
5144 if (views != null) {
5145 views.add(this);
5146 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005147 }
5148
5149 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005150 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005151 * The search is performed by either the text that the View renders or the content
5152 * description that describes the view for accessibility purposes and the view does
5153 * not render or both. Clients can specify how the search is to be performed via
5154 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5155 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005156 *
5157 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005158 * @param searched The text to match against.
5159 *
5160 * @see #FIND_VIEWS_WITH_TEXT
5161 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5162 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005163 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005164 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
5165 if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0 && !TextUtils.isEmpty(searched)
5166 && !TextUtils.isEmpty(mContentDescription)) {
5167 String searchedLowerCase = searched.toString().toLowerCase();
5168 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5169 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5170 outViews.add(this);
5171 }
5172 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005173 }
5174
5175 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005176 * Find and return all touchable views that are descendants of this view,
5177 * possibly including this view if it is touchable itself.
5178 *
5179 * @return A list of touchable views
5180 */
5181 public ArrayList<View> getTouchables() {
5182 ArrayList<View> result = new ArrayList<View>();
5183 addTouchables(result);
5184 return result;
5185 }
5186
5187 /**
5188 * Add any touchable views that are descendants of this view (possibly
5189 * including this view if it is touchable itself) to views.
5190 *
5191 * @param views Touchable views found so far
5192 */
5193 public void addTouchables(ArrayList<View> views) {
5194 final int viewFlags = mViewFlags;
5195
5196 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5197 && (viewFlags & ENABLED_MASK) == ENABLED) {
5198 views.add(this);
5199 }
5200 }
5201
5202 /**
5203 * Call this to try to give focus to a specific view or to one of its
5204 * descendants.
5205 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005206 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5207 * false), or if it is focusable and it is not focusable in touch mode
5208 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005209 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005210 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005211 * have focus, and you want your parent to look for the next one.
5212 *
5213 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5214 * {@link #FOCUS_DOWN} and <code>null</code>.
5215 *
5216 * @return Whether this view or one of its descendants actually took focus.
5217 */
5218 public final boolean requestFocus() {
5219 return requestFocus(View.FOCUS_DOWN);
5220 }
5221
5222
5223 /**
5224 * Call this to try to give focus to a specific view or to one of its
5225 * descendants and give it a hint about what direction focus is heading.
5226 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005227 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5228 * false), or if it is focusable and it is not focusable in touch mode
5229 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005230 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005231 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005232 * have focus, and you want your parent to look for the next one.
5233 *
5234 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5235 * <code>null</code> set for the previously focused rectangle.
5236 *
5237 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5238 * @return Whether this view or one of its descendants actually took focus.
5239 */
5240 public final boolean requestFocus(int direction) {
5241 return requestFocus(direction, null);
5242 }
5243
5244 /**
5245 * Call this to try to give focus to a specific view or to one of its descendants
5246 * and give it hints about the direction and a specific rectangle that the focus
5247 * is coming from. The rectangle can help give larger views a finer grained hint
5248 * about where focus is coming from, and therefore, where to show selection, or
5249 * forward focus change internally.
5250 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005251 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5252 * false), or if it is focusable and it is not focusable in touch mode
5253 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005254 *
5255 * A View will not take focus if it is not visible.
5256 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005257 * A View will not take focus if one of its parents has
5258 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5259 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005260 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005261 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005262 * have focus, and you want your parent to look for the next one.
5263 *
5264 * You may wish to override this method if your custom {@link View} has an internal
5265 * {@link View} that it wishes to forward the request to.
5266 *
5267 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5268 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5269 * to give a finer grained hint about where focus is coming from. May be null
5270 * if there is no hint.
5271 * @return Whether this view or one of its descendants actually took focus.
5272 */
5273 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5274 // need to be focusable
5275 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5276 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5277 return false;
5278 }
5279
5280 // need to be focusable in touch mode if in touch mode
5281 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005282 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5283 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005284 }
5285
5286 // need to not have any parents blocking us
5287 if (hasAncestorThatBlocksDescendantFocus()) {
5288 return false;
5289 }
5290
5291 handleFocusGainInternal(direction, previouslyFocusedRect);
5292 return true;
5293 }
5294
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005295 /** Gets the ViewAncestor, or null if not attached. */
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005296 /*package*/ ViewRootImpl getViewRootImpl() {
Christopher Tate2c095f32010-10-04 14:13:40 -07005297 View root = getRootView();
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005298 return root != null ? (ViewRootImpl)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07005299 }
5300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005301 /**
5302 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5303 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5304 * touch mode to request focus when they are touched.
5305 *
5306 * @return Whether this view or one of its descendants actually took focus.
5307 *
5308 * @see #isInTouchMode()
5309 *
5310 */
5311 public final boolean requestFocusFromTouch() {
5312 // Leave touch mode if we need to
5313 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005314 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005315 if (viewRoot != null) {
5316 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005317 }
5318 }
5319 return requestFocus(View.FOCUS_DOWN);
5320 }
5321
5322 /**
5323 * @return Whether any ancestor of this view blocks descendant focus.
5324 */
5325 private boolean hasAncestorThatBlocksDescendantFocus() {
5326 ViewParent ancestor = mParent;
5327 while (ancestor instanceof ViewGroup) {
5328 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5329 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5330 return true;
5331 } else {
5332 ancestor = vgAncestor.getParent();
5333 }
5334 }
5335 return false;
5336 }
5337
5338 /**
Romain Guya440b002010-02-24 15:57:54 -08005339 * @hide
5340 */
5341 public void dispatchStartTemporaryDetach() {
5342 onStartTemporaryDetach();
5343 }
5344
5345 /**
5346 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005347 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5348 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005349 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005350 */
5351 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005352 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005353 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005354 }
5355
5356 /**
5357 * @hide
5358 */
5359 public void dispatchFinishTemporaryDetach() {
5360 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005361 }
Romain Guy8506ab42009-06-11 17:35:47 -07005362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 /**
5364 * Called after {@link #onStartTemporaryDetach} when the container is done
5365 * changing the view.
5366 */
5367 public void onFinishTemporaryDetach() {
5368 }
Romain Guy8506ab42009-06-11 17:35:47 -07005369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005370 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005371 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5372 * for this view's window. Returns null if the view is not currently attached
5373 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005374 * just use the standard high-level event callbacks like
5375 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005376 */
5377 public KeyEvent.DispatcherState getKeyDispatcherState() {
5378 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5379 }
Joe Malin32736f02011-01-19 16:14:20 -08005380
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005381 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005382 * Dispatch a key event before it is processed by any input method
5383 * associated with the view hierarchy. This can be used to intercept
5384 * key events in special situations before the IME consumes them; a
5385 * typical example would be handling the BACK key to update the application's
5386 * UI instead of allowing the IME to see it and close itself.
5387 *
5388 * @param event The key event to be dispatched.
5389 * @return True if the event was handled, false otherwise.
5390 */
5391 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5392 return onKeyPreIme(event.getKeyCode(), event);
5393 }
5394
5395 /**
5396 * Dispatch a key event to the next view on the focus path. This path runs
5397 * from the top of the view tree down to the currently focused view. If this
5398 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5399 * the next node down the focus path. This method also fires any key
5400 * listeners.
5401 *
5402 * @param event The key event to be dispatched.
5403 * @return True if the event was handled, false otherwise.
5404 */
5405 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005406 if (mInputEventConsistencyVerifier != null) {
5407 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005409
Jeff Brown21bc5c92011-02-28 18:27:14 -08005410 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005411 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005412 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5413 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
5414 return true;
5415 }
5416
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005417 if (event.dispatch(this, mAttachInfo != null
5418 ? mAttachInfo.mKeyDispatchState : null, this)) {
5419 return true;
5420 }
5421
5422 if (mInputEventConsistencyVerifier != null) {
5423 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5424 }
5425 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005426 }
5427
5428 /**
5429 * Dispatches a key shortcut event.
5430 *
5431 * @param event The key event to be dispatched.
5432 * @return True if the event was handled by the view, false otherwise.
5433 */
5434 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5435 return onKeyShortcut(event.getKeyCode(), event);
5436 }
5437
5438 /**
5439 * Pass the touch screen motion event down to the target view, or this
5440 * view if it is the target.
5441 *
5442 * @param event The motion event to be dispatched.
5443 * @return True if the event was handled by the view, false otherwise.
5444 */
5445 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005446 if (mInputEventConsistencyVerifier != null) {
5447 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5448 }
5449
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005450 if (onFilterTouchEventForSecurity(event)) {
5451 //noinspection SimplifiableIfStatement
5452 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
5453 mOnTouchListener.onTouch(this, event)) {
5454 return true;
5455 }
5456
5457 if (onTouchEvent(event)) {
5458 return true;
5459 }
Jeff Brown85a31762010-09-01 17:01:00 -07005460 }
5461
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005462 if (mInputEventConsistencyVerifier != null) {
5463 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005464 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005465 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005466 }
5467
5468 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005469 * Filter the touch event to apply security policies.
5470 *
5471 * @param event The motion event to be filtered.
5472 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005473 *
Jeff Brown85a31762010-09-01 17:01:00 -07005474 * @see #getFilterTouchesWhenObscured
5475 */
5476 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005477 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005478 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5479 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5480 // Window is obscured, drop this touch.
5481 return false;
5482 }
5483 return true;
5484 }
5485
5486 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005487 * Pass a trackball motion event down to the focused view.
5488 *
5489 * @param event The motion event to be dispatched.
5490 * @return True if the event was handled by the view, false otherwise.
5491 */
5492 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005493 if (mInputEventConsistencyVerifier != null) {
5494 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5495 }
5496
Romain Guy02ccac62011-06-24 13:20:23 -07005497 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005498 }
5499
5500 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005501 * Dispatch a generic motion event.
5502 * <p>
5503 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5504 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005505 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005506 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005507 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005508 *
5509 * @param event The motion event to be dispatched.
5510 * @return True if the event was handled by the view, false otherwise.
5511 */
5512 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005513 if (mInputEventConsistencyVerifier != null) {
5514 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5515 }
5516
Jeff Browna032cc02011-03-07 16:56:21 -08005517 final int source = event.getSource();
5518 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5519 final int action = event.getAction();
5520 if (action == MotionEvent.ACTION_HOVER_ENTER
5521 || action == MotionEvent.ACTION_HOVER_MOVE
5522 || action == MotionEvent.ACTION_HOVER_EXIT) {
5523 if (dispatchHoverEvent(event)) {
5524 return true;
5525 }
5526 } else if (dispatchGenericPointerEvent(event)) {
5527 return true;
5528 }
5529 } else if (dispatchGenericFocusedEvent(event)) {
5530 return true;
5531 }
5532
Jeff Brown10b62902011-06-20 16:40:37 -07005533 if (dispatchGenericMotionEventInternal(event)) {
5534 return true;
5535 }
5536
5537 if (mInputEventConsistencyVerifier != null) {
5538 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5539 }
5540 return false;
5541 }
5542
5543 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005544 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08005545 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5546 && mOnGenericMotionListener.onGenericMotion(this, event)) {
5547 return true;
5548 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005549
5550 if (onGenericMotionEvent(event)) {
5551 return true;
5552 }
5553
5554 if (mInputEventConsistencyVerifier != null) {
5555 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5556 }
5557 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005558 }
5559
5560 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005561 * Dispatch a hover event.
5562 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005563 * Do not call this method directly.
5564 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005565 * </p>
5566 *
5567 * @param event The motion event to be dispatched.
5568 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005569 */
5570 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005571 //noinspection SimplifiableIfStatement
Jeff Brown10b62902011-06-20 16:40:37 -07005572 if (mOnHoverListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5573 && mOnHoverListener.onHover(this, event)) {
5574 return true;
5575 }
5576
Jeff Browna032cc02011-03-07 16:56:21 -08005577 return onHoverEvent(event);
5578 }
5579
5580 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005581 * Returns true if the view has a child to which it has recently sent
5582 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5583 * it does not have a hovered child, then it must be the innermost hovered view.
5584 * @hide
5585 */
5586 protected boolean hasHoveredChild() {
5587 return false;
5588 }
5589
5590 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005591 * Dispatch a generic motion event to the view under the first pointer.
5592 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005593 * Do not call this method directly.
5594 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005595 * </p>
5596 *
5597 * @param event The motion event to be dispatched.
5598 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005599 */
5600 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5601 return false;
5602 }
5603
5604 /**
5605 * Dispatch a generic motion event to the currently focused view.
5606 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005607 * Do not call this method directly.
5608 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005609 * </p>
5610 *
5611 * @param event The motion event to be dispatched.
5612 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005613 */
5614 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5615 return false;
5616 }
5617
5618 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005619 * Dispatch a pointer event.
5620 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005621 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5622 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5623 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005624 * and should not be expected to handle other pointing device features.
5625 * </p>
5626 *
5627 * @param event The motion event to be dispatched.
5628 * @return True if the event was handled by the view, false otherwise.
5629 * @hide
5630 */
5631 public final boolean dispatchPointerEvent(MotionEvent event) {
5632 if (event.isTouchEvent()) {
5633 return dispatchTouchEvent(event);
5634 } else {
5635 return dispatchGenericMotionEvent(event);
5636 }
5637 }
5638
5639 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005640 * Called when the window containing this view gains or loses window focus.
5641 * ViewGroups should override to route to their children.
5642 *
5643 * @param hasFocus True if the window containing this view now has focus,
5644 * false otherwise.
5645 */
5646 public void dispatchWindowFocusChanged(boolean hasFocus) {
5647 onWindowFocusChanged(hasFocus);
5648 }
5649
5650 /**
5651 * Called when the window containing this view gains or loses focus. Note
5652 * that this is separate from view focus: to receive key events, both
5653 * your view and its window must have focus. If a window is displayed
5654 * on top of yours that takes input focus, then your own window will lose
5655 * focus but the view focus will remain unchanged.
5656 *
5657 * @param hasWindowFocus True if the window containing this view now has
5658 * focus, false otherwise.
5659 */
5660 public void onWindowFocusChanged(boolean hasWindowFocus) {
5661 InputMethodManager imm = InputMethodManager.peekInstance();
5662 if (!hasWindowFocus) {
5663 if (isPressed()) {
5664 setPressed(false);
5665 }
5666 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5667 imm.focusOut(this);
5668 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005669 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005670 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005671 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005672 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5673 imm.focusIn(this);
5674 }
5675 refreshDrawableState();
5676 }
5677
5678 /**
5679 * Returns true if this view is in a window that currently has window focus.
5680 * Note that this is not the same as the view itself having focus.
5681 *
5682 * @return True if this view is in a window that currently has window focus.
5683 */
5684 public boolean hasWindowFocus() {
5685 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5686 }
5687
5688 /**
Adam Powell326d8082009-12-09 15:10:07 -08005689 * Dispatch a view visibility change down the view hierarchy.
5690 * ViewGroups should override to route to their children.
5691 * @param changedView The view whose visibility changed. Could be 'this' or
5692 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005693 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5694 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005695 */
5696 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5697 onVisibilityChanged(changedView, visibility);
5698 }
5699
5700 /**
5701 * Called when the visibility of the view or an ancestor of the view is changed.
5702 * @param changedView The view whose visibility changed. Could be 'this' or
5703 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005704 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5705 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005706 */
5707 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005708 if (visibility == VISIBLE) {
5709 if (mAttachInfo != null) {
5710 initialAwakenScrollBars();
5711 } else {
5712 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5713 }
5714 }
Adam Powell326d8082009-12-09 15:10:07 -08005715 }
5716
5717 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005718 * Dispatch a hint about whether this view is displayed. For instance, when
5719 * a View moves out of the screen, it might receives a display hint indicating
5720 * the view is not displayed. Applications should not <em>rely</em> on this hint
5721 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005722 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005723 * @param hint A hint about whether or not this view is displayed:
5724 * {@link #VISIBLE} or {@link #INVISIBLE}.
5725 */
5726 public void dispatchDisplayHint(int hint) {
5727 onDisplayHint(hint);
5728 }
5729
5730 /**
5731 * Gives this view a hint about whether is displayed or not. For instance, when
5732 * a View moves out of the screen, it might receives a display hint indicating
5733 * the view is not displayed. Applications should not <em>rely</em> on this hint
5734 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005735 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005736 * @param hint A hint about whether or not this view is displayed:
5737 * {@link #VISIBLE} or {@link #INVISIBLE}.
5738 */
5739 protected void onDisplayHint(int hint) {
5740 }
5741
5742 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005743 * Dispatch a window visibility change down the view hierarchy.
5744 * ViewGroups should override to route to their children.
5745 *
5746 * @param visibility The new visibility of the window.
5747 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005748 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005749 */
5750 public void dispatchWindowVisibilityChanged(int visibility) {
5751 onWindowVisibilityChanged(visibility);
5752 }
5753
5754 /**
5755 * Called when the window containing has change its visibility
5756 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5757 * that this tells you whether or not your window is being made visible
5758 * to the window manager; this does <em>not</em> tell you whether or not
5759 * your window is obscured by other windows on the screen, even if it
5760 * is itself visible.
5761 *
5762 * @param visibility The new visibility of the window.
5763 */
5764 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005765 if (visibility == VISIBLE) {
5766 initialAwakenScrollBars();
5767 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005768 }
5769
5770 /**
5771 * Returns the current visibility of the window this view is attached to
5772 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5773 *
5774 * @return Returns the current visibility of the view's window.
5775 */
5776 public int getWindowVisibility() {
5777 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5778 }
5779
5780 /**
5781 * Retrieve the overall visible display size in which the window this view is
5782 * attached to has been positioned in. This takes into account screen
5783 * decorations above the window, for both cases where the window itself
5784 * is being position inside of them or the window is being placed under
5785 * then and covered insets are used for the window to position its content
5786 * inside. In effect, this tells you the available area where content can
5787 * be placed and remain visible to users.
5788 *
5789 * <p>This function requires an IPC back to the window manager to retrieve
5790 * the requested information, so should not be used in performance critical
5791 * code like drawing.
5792 *
5793 * @param outRect Filled in with the visible display frame. If the view
5794 * is not attached to a window, this is simply the raw display size.
5795 */
5796 public void getWindowVisibleDisplayFrame(Rect outRect) {
5797 if (mAttachInfo != null) {
5798 try {
5799 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5800 } catch (RemoteException e) {
5801 return;
5802 }
5803 // XXX This is really broken, and probably all needs to be done
5804 // in the window manager, and we need to know more about whether
5805 // we want the area behind or in front of the IME.
5806 final Rect insets = mAttachInfo.mVisibleInsets;
5807 outRect.left += insets.left;
5808 outRect.top += insets.top;
5809 outRect.right -= insets.right;
5810 outRect.bottom -= insets.bottom;
5811 return;
5812 }
5813 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005814 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005815 }
5816
5817 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005818 * Dispatch a notification about a resource configuration change down
5819 * the view hierarchy.
5820 * ViewGroups should override to route to their children.
5821 *
5822 * @param newConfig The new resource configuration.
5823 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005824 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005825 */
5826 public void dispatchConfigurationChanged(Configuration newConfig) {
5827 onConfigurationChanged(newConfig);
5828 }
5829
5830 /**
5831 * Called when the current configuration of the resources being used
5832 * by the application have changed. You can use this to decide when
5833 * to reload resources that can changed based on orientation and other
5834 * configuration characterstics. You only need to use this if you are
5835 * not relying on the normal {@link android.app.Activity} mechanism of
5836 * recreating the activity instance upon a configuration change.
5837 *
5838 * @param newConfig The new resource configuration.
5839 */
5840 protected void onConfigurationChanged(Configuration newConfig) {
5841 }
5842
5843 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005844 * Private function to aggregate all per-view attributes in to the view
5845 * root.
5846 */
5847 void dispatchCollectViewAttributes(int visibility) {
5848 performCollectViewAttributes(visibility);
5849 }
5850
5851 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005852 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005853 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5854 mAttachInfo.mKeepScreenOn = true;
5855 }
5856 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5857 if (mOnSystemUiVisibilityChangeListener != null) {
5858 mAttachInfo.mHasSystemUiListeners = true;
5859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005860 }
5861 }
5862
5863 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005864 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005865 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005866 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5867 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005868 ai.mRecomputeGlobalAttributes = true;
5869 }
5870 }
5871 }
5872
5873 /**
5874 * Returns whether the device is currently in touch mode. Touch mode is entered
5875 * once the user begins interacting with the device by touch, and affects various
5876 * things like whether focus is always visible to the user.
5877 *
5878 * @return Whether the device is in touch mode.
5879 */
5880 @ViewDebug.ExportedProperty
5881 public boolean isInTouchMode() {
5882 if (mAttachInfo != null) {
5883 return mAttachInfo.mInTouchMode;
5884 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005885 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005886 }
5887 }
5888
5889 /**
5890 * Returns the context the view is running in, through which it can
5891 * access the current theme, resources, etc.
5892 *
5893 * @return The view's Context.
5894 */
5895 @ViewDebug.CapturedViewProperty
5896 public final Context getContext() {
5897 return mContext;
5898 }
5899
5900 /**
5901 * Handle a key event before it is processed by any input method
5902 * associated with the view hierarchy. This can be used to intercept
5903 * key events in special situations before the IME consumes them; a
5904 * typical example would be handling the BACK key to update the application's
5905 * UI instead of allowing the IME to see it and close itself.
5906 *
5907 * @param keyCode The value in event.getKeyCode().
5908 * @param event Description of the key event.
5909 * @return If you handled the event, return true. If you want to allow the
5910 * event to be handled by the next receiver, return false.
5911 */
5912 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5913 return false;
5914 }
5915
5916 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005917 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5918 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005919 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5920 * is released, if the view is enabled and clickable.
5921 *
5922 * @param keyCode A key code that represents the button pressed, from
5923 * {@link android.view.KeyEvent}.
5924 * @param event The KeyEvent object that defines the button action.
5925 */
5926 public boolean onKeyDown(int keyCode, KeyEvent event) {
5927 boolean result = false;
5928
5929 switch (keyCode) {
5930 case KeyEvent.KEYCODE_DPAD_CENTER:
5931 case KeyEvent.KEYCODE_ENTER: {
5932 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5933 return true;
5934 }
5935 // Long clickable items don't necessarily have to be clickable
5936 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5937 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5938 (event.getRepeatCount() == 0)) {
5939 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005940 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005941 return true;
5942 }
5943 break;
5944 }
5945 }
5946 return result;
5947 }
5948
5949 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005950 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5951 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5952 * the event).
5953 */
5954 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5955 return false;
5956 }
5957
5958 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005959 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5960 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005961 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5962 * {@link KeyEvent#KEYCODE_ENTER} is released.
5963 *
5964 * @param keyCode A key code that represents the button pressed, from
5965 * {@link android.view.KeyEvent}.
5966 * @param event The KeyEvent object that defines the button action.
5967 */
5968 public boolean onKeyUp(int keyCode, KeyEvent event) {
5969 boolean result = false;
5970
5971 switch (keyCode) {
5972 case KeyEvent.KEYCODE_DPAD_CENTER:
5973 case KeyEvent.KEYCODE_ENTER: {
5974 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5975 return true;
5976 }
5977 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5978 setPressed(false);
5979
5980 if (!mHasPerformedLongPress) {
5981 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005982 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005983
5984 result = performClick();
5985 }
5986 }
5987 break;
5988 }
5989 }
5990 return result;
5991 }
5992
5993 /**
5994 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5995 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5996 * the event).
5997 *
5998 * @param keyCode A key code that represents the button pressed, from
5999 * {@link android.view.KeyEvent}.
6000 * @param repeatCount The number of times the action was made.
6001 * @param event The KeyEvent object that defines the button action.
6002 */
6003 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6004 return false;
6005 }
6006
6007 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006008 * Called on the focused view when a key shortcut event is not handled.
6009 * Override this method to implement local key shortcuts for the View.
6010 * Key shortcuts can also be implemented by setting the
6011 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006012 *
6013 * @param keyCode The value in event.getKeyCode().
6014 * @param event Description of the key event.
6015 * @return If you handled the event, return true. If you want to allow the
6016 * event to be handled by the next receiver, return false.
6017 */
6018 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6019 return false;
6020 }
6021
6022 /**
6023 * Check whether the called view is a text editor, in which case it
6024 * would make sense to automatically display a soft input window for
6025 * it. Subclasses should override this if they implement
6026 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006027 * a call on that method would return a non-null InputConnection, and
6028 * they are really a first-class editor that the user would normally
6029 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006030 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006031 * <p>The default implementation always returns false. This does
6032 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6033 * will not be called or the user can not otherwise perform edits on your
6034 * view; it is just a hint to the system that this is not the primary
6035 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006036 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006037 * @return Returns true if this view is a text editor, else false.
6038 */
6039 public boolean onCheckIsTextEditor() {
6040 return false;
6041 }
Romain Guy8506ab42009-06-11 17:35:47 -07006042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006043 /**
6044 * Create a new InputConnection for an InputMethod to interact
6045 * with the view. The default implementation returns null, since it doesn't
6046 * support input methods. You can override this to implement such support.
6047 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006048 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006049 * <p>When implementing this, you probably also want to implement
6050 * {@link #onCheckIsTextEditor()} to indicate you will return a
6051 * non-null InputConnection.
6052 *
6053 * @param outAttrs Fill in with attribute information about the connection.
6054 */
6055 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6056 return null;
6057 }
6058
6059 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006060 * Called by the {@link android.view.inputmethod.InputMethodManager}
6061 * when a view who is not the current
6062 * input connection target is trying to make a call on the manager. The
6063 * default implementation returns false; you can override this to return
6064 * true for certain views if you are performing InputConnection proxying
6065 * to them.
6066 * @param view The View that is making the InputMethodManager call.
6067 * @return Return true to allow the call, false to reject.
6068 */
6069 public boolean checkInputConnectionProxy(View view) {
6070 return false;
6071 }
Romain Guy8506ab42009-06-11 17:35:47 -07006072
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006073 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006074 * Show the context menu for this view. It is not safe to hold on to the
6075 * menu after returning from this method.
6076 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006077 * You should normally not overload this method. Overload
6078 * {@link #onCreateContextMenu(ContextMenu)} or define an
6079 * {@link OnCreateContextMenuListener} to add items to the context menu.
6080 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006081 * @param menu The context menu to populate
6082 */
6083 public void createContextMenu(ContextMenu menu) {
6084 ContextMenuInfo menuInfo = getContextMenuInfo();
6085
6086 // Sets the current menu info so all items added to menu will have
6087 // my extra info set.
6088 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6089
6090 onCreateContextMenu(menu);
6091 if (mOnCreateContextMenuListener != null) {
6092 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
6093 }
6094
6095 // Clear the extra information so subsequent items that aren't mine don't
6096 // have my extra info.
6097 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6098
6099 if (mParent != null) {
6100 mParent.createContextMenu(menu);
6101 }
6102 }
6103
6104 /**
6105 * Views should implement this if they have extra information to associate
6106 * with the context menu. The return result is supplied as a parameter to
6107 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6108 * callback.
6109 *
6110 * @return Extra information about the item for which the context menu
6111 * should be shown. This information will vary across different
6112 * subclasses of View.
6113 */
6114 protected ContextMenuInfo getContextMenuInfo() {
6115 return null;
6116 }
6117
6118 /**
6119 * Views should implement this if the view itself is going to add items to
6120 * the context menu.
6121 *
6122 * @param menu the context menu to populate
6123 */
6124 protected void onCreateContextMenu(ContextMenu menu) {
6125 }
6126
6127 /**
6128 * Implement this method to handle trackball motion events. The
6129 * <em>relative</em> movement of the trackball since the last event
6130 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6131 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6132 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6133 * they will often be fractional values, representing the more fine-grained
6134 * movement information available from a trackball).
6135 *
6136 * @param event The motion event.
6137 * @return True if the event was handled, false otherwise.
6138 */
6139 public boolean onTrackballEvent(MotionEvent event) {
6140 return false;
6141 }
6142
6143 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006144 * Implement this method to handle generic motion events.
6145 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006146 * Generic motion events describe joystick movements, mouse hovers, track pad
6147 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006148 * {@link MotionEvent#getSource() source} of the motion event specifies
6149 * the class of input that was received. Implementations of this method
6150 * must examine the bits in the source before processing the event.
6151 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006152 * </p><p>
6153 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6154 * are delivered to the view under the pointer. All other generic motion events are
6155 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006156 * </p>
6157 * <code>
6158 * public boolean onGenericMotionEvent(MotionEvent event) {
6159 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006160 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6161 * // process the joystick movement...
6162 * return true;
6163 * }
6164 * }
6165 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6166 * switch (event.getAction()) {
6167 * case MotionEvent.ACTION_HOVER_MOVE:
6168 * // process the mouse hover movement...
6169 * return true;
6170 * case MotionEvent.ACTION_SCROLL:
6171 * // process the scroll wheel movement...
6172 * return true;
6173 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006174 * }
6175 * return super.onGenericMotionEvent(event);
6176 * }
6177 * </code>
6178 *
6179 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006180 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006181 */
6182 public boolean onGenericMotionEvent(MotionEvent event) {
6183 return false;
6184 }
6185
6186 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006187 * Implement this method to handle hover events.
6188 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006189 * This method is called whenever a pointer is hovering into, over, or out of the
6190 * bounds of a view and the view is not currently being touched.
6191 * Hover events are represented as pointer events with action
6192 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6193 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6194 * </p>
6195 * <ul>
6196 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6197 * when the pointer enters the bounds of the view.</li>
6198 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6199 * when the pointer has already entered the bounds of the view and has moved.</li>
6200 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6201 * when the pointer has exited the bounds of the view or when the pointer is
6202 * about to go down due to a button click, tap, or similar user action that
6203 * causes the view to be touched.</li>
6204 * </ul>
6205 * <p>
6206 * The view should implement this method to return true to indicate that it is
6207 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006208 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006209 * The default implementation calls {@link #setHovered} to update the hovered state
6210 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006211 * is enabled and is clickable. The default implementation also sends hover
6212 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006213 * </p>
6214 *
6215 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006216 * @return True if the view handled the hover event.
6217 *
6218 * @see #isHovered
6219 * @see #setHovered
6220 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006221 */
6222 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006223 // The root view may receive hover (or touch) events that are outside the bounds of
6224 // the window. This code ensures that we only send accessibility events for
6225 // hovers that are actually within the bounds of the root view.
6226 final int action = event.getAction();
6227 if (!mSendingHoverAccessibilityEvents) {
6228 if ((action == MotionEvent.ACTION_HOVER_ENTER
6229 || action == MotionEvent.ACTION_HOVER_MOVE)
6230 && !hasHoveredChild()
6231 && pointInView(event.getX(), event.getY())) {
6232 mSendingHoverAccessibilityEvents = true;
6233 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6234 }
6235 } else {
6236 if (action == MotionEvent.ACTION_HOVER_EXIT
6237 || (action == MotionEvent.ACTION_HOVER_MOVE
6238 && !pointInView(event.getX(), event.getY()))) {
6239 mSendingHoverAccessibilityEvents = false;
6240 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6241 }
Jeff Browna1b24182011-07-28 13:38:24 -07006242 }
6243
Jeff Brown87b7f802011-06-21 18:35:45 -07006244 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006245 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006246 case MotionEvent.ACTION_HOVER_ENTER:
6247 setHovered(true);
6248 break;
6249 case MotionEvent.ACTION_HOVER_EXIT:
6250 setHovered(false);
6251 break;
6252 }
Jeff Browna1b24182011-07-28 13:38:24 -07006253
6254 // Dispatch the event to onGenericMotionEvent before returning true.
6255 // This is to provide compatibility with existing applications that
6256 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6257 // break because of the new default handling for hoverable views
6258 // in onHoverEvent.
6259 // Note that onGenericMotionEvent will be called by default when
6260 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6261 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006262 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006263 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006264 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006265 }
6266
6267 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006268 * Returns true if the view should handle {@link #onHoverEvent}
6269 * by calling {@link #setHovered} to change its hovered state.
6270 *
6271 * @return True if the view is hoverable.
6272 */
6273 private boolean isHoverable() {
6274 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006275 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006276 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6277 return false;
6278 }
6279
6280 return (viewFlags & CLICKABLE) == CLICKABLE
6281 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6282 }
6283
6284 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006285 * Returns true if the view is currently hovered.
6286 *
6287 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006288 *
6289 * @see #setHovered
6290 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006291 */
Jeff Brown10b62902011-06-20 16:40:37 -07006292 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006293 public boolean isHovered() {
6294 return (mPrivateFlags & HOVERED) != 0;
6295 }
6296
6297 /**
6298 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006299 * <p>
6300 * Calling this method also changes the drawable state of the view. This
6301 * enables the view to react to hover by using different drawable resources
6302 * to change its appearance.
6303 * </p><p>
6304 * The {@link #onHoverChanged} method is called when the hovered state changes.
6305 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006306 *
6307 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006308 *
6309 * @see #isHovered
6310 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006311 */
6312 public void setHovered(boolean hovered) {
6313 if (hovered) {
6314 if ((mPrivateFlags & HOVERED) == 0) {
6315 mPrivateFlags |= HOVERED;
6316 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006317 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006318 }
6319 } else {
6320 if ((mPrivateFlags & HOVERED) != 0) {
6321 mPrivateFlags &= ~HOVERED;
6322 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006323 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006324 }
6325 }
6326 }
6327
6328 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006329 * Implement this method to handle hover state changes.
6330 * <p>
6331 * This method is called whenever the hover state changes as a result of a
6332 * call to {@link #setHovered}.
6333 * </p>
6334 *
6335 * @param hovered The current hover state, as returned by {@link #isHovered}.
6336 *
6337 * @see #isHovered
6338 * @see #setHovered
6339 */
6340 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006341 }
6342
6343 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006344 * Implement this method to handle touch screen motion events.
6345 *
6346 * @param event The motion event.
6347 * @return True if the event was handled, false otherwise.
6348 */
6349 public boolean onTouchEvent(MotionEvent event) {
6350 final int viewFlags = mViewFlags;
6351
6352 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006353 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
6354 mPrivateFlags &= ~PRESSED;
6355 refreshDrawableState();
6356 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006357 // A disabled view that is clickable still consumes the touch
6358 // events, it just doesn't respond to them.
6359 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6360 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6361 }
6362
6363 if (mTouchDelegate != null) {
6364 if (mTouchDelegate.onTouchEvent(event)) {
6365 return true;
6366 }
6367 }
6368
6369 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6370 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6371 switch (event.getAction()) {
6372 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006373 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6374 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006375 // take focus if we don't have it already and we should in
6376 // touch mode.
6377 boolean focusTaken = false;
6378 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6379 focusTaken = requestFocus();
6380 }
6381
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006382 if (prepressed) {
6383 // The button is being released before we actually
6384 // showed it as pressed. Make it show the pressed
6385 // state now (before scheduling the click) to ensure
6386 // the user sees it.
6387 mPrivateFlags |= PRESSED;
6388 refreshDrawableState();
6389 }
Joe Malin32736f02011-01-19 16:14:20 -08006390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006391 if (!mHasPerformedLongPress) {
6392 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006393 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006394
6395 // Only perform take click actions if we were in the pressed state
6396 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006397 // Use a Runnable and post this rather than calling
6398 // performClick directly. This lets other visual state
6399 // of the view update before click actions start.
6400 if (mPerformClick == null) {
6401 mPerformClick = new PerformClick();
6402 }
6403 if (!post(mPerformClick)) {
6404 performClick();
6405 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006406 }
6407 }
6408
6409 if (mUnsetPressedState == null) {
6410 mUnsetPressedState = new UnsetPressedState();
6411 }
6412
Adam Powelle14579b2009-12-16 18:39:52 -08006413 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006414 postDelayed(mUnsetPressedState,
6415 ViewConfiguration.getPressedStateDuration());
6416 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006417 // If the post failed, unpress right now
6418 mUnsetPressedState.run();
6419 }
Adam Powelle14579b2009-12-16 18:39:52 -08006420 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006421 }
6422 break;
6423
6424 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006425 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006426
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006427 if (performButtonActionOnTouchDown(event)) {
6428 break;
6429 }
6430
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006431 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006432 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006433
6434 // For views inside a scrolling container, delay the pressed feedback for
6435 // a short period in case this is a scroll.
6436 if (isInScrollingContainer) {
6437 mPrivateFlags |= PREPRESSED;
6438 if (mPendingCheckForTap == null) {
6439 mPendingCheckForTap = new CheckForTap();
6440 }
6441 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6442 } else {
6443 // Not inside a scrolling container, so show the feedback right away
6444 mPrivateFlags |= PRESSED;
6445 refreshDrawableState();
6446 checkForLongClick(0);
6447 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006448 break;
6449
6450 case MotionEvent.ACTION_CANCEL:
6451 mPrivateFlags &= ~PRESSED;
6452 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08006453 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006454 break;
6455
6456 case MotionEvent.ACTION_MOVE:
6457 final int x = (int) event.getX();
6458 final int y = (int) event.getY();
6459
6460 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006461 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006462 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006463 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006464 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006465 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006466 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006467
6468 // Need to switch from pressed to not pressed
6469 mPrivateFlags &= ~PRESSED;
6470 refreshDrawableState();
6471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006472 }
6473 break;
6474 }
6475 return true;
6476 }
6477
6478 return false;
6479 }
6480
6481 /**
Adam Powell10298662011-08-14 18:26:30 -07006482 * @hide
6483 */
6484 public boolean isInScrollingContainer() {
6485 ViewParent p = getParent();
6486 while (p != null && p instanceof ViewGroup) {
6487 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6488 return true;
6489 }
6490 p = p.getParent();
6491 }
6492 return false;
6493 }
6494
6495 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006496 * Remove the longpress detection timer.
6497 */
6498 private void removeLongPressCallback() {
6499 if (mPendingCheckForLongPress != null) {
6500 removeCallbacks(mPendingCheckForLongPress);
6501 }
6502 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006503
6504 /**
6505 * Remove the pending click action
6506 */
6507 private void removePerformClickCallback() {
6508 if (mPerformClick != null) {
6509 removeCallbacks(mPerformClick);
6510 }
6511 }
6512
Adam Powelle14579b2009-12-16 18:39:52 -08006513 /**
Romain Guya440b002010-02-24 15:57:54 -08006514 * Remove the prepress detection timer.
6515 */
6516 private void removeUnsetPressCallback() {
6517 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6518 setPressed(false);
6519 removeCallbacks(mUnsetPressedState);
6520 }
6521 }
6522
6523 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006524 * Remove the tap detection timer.
6525 */
6526 private void removeTapCallback() {
6527 if (mPendingCheckForTap != null) {
6528 mPrivateFlags &= ~PREPRESSED;
6529 removeCallbacks(mPendingCheckForTap);
6530 }
6531 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006532
6533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006534 * Cancels a pending long press. Your subclass can use this if you
6535 * want the context menu to come up if the user presses and holds
6536 * at the same place, but you don't want it to come up if they press
6537 * and then move around enough to cause scrolling.
6538 */
6539 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006540 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006541
6542 /*
6543 * The prepressed state handled by the tap callback is a display
6544 * construct, but the tap callback will post a long press callback
6545 * less its own timeout. Remove it here.
6546 */
6547 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006548 }
6549
6550 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006551 * Remove the pending callback for sending a
6552 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6553 */
6554 private void removeSendViewScrolledAccessibilityEventCallback() {
6555 if (mSendViewScrolledAccessibilityEvent != null) {
6556 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6557 }
6558 }
6559
6560 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006561 * Sets the TouchDelegate for this View.
6562 */
6563 public void setTouchDelegate(TouchDelegate delegate) {
6564 mTouchDelegate = delegate;
6565 }
6566
6567 /**
6568 * Gets the TouchDelegate for this View.
6569 */
6570 public TouchDelegate getTouchDelegate() {
6571 return mTouchDelegate;
6572 }
6573
6574 /**
6575 * Set flags controlling behavior of this view.
6576 *
6577 * @param flags Constant indicating the value which should be set
6578 * @param mask Constant indicating the bit range that should be changed
6579 */
6580 void setFlags(int flags, int mask) {
6581 int old = mViewFlags;
6582 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6583
6584 int changed = mViewFlags ^ old;
6585 if (changed == 0) {
6586 return;
6587 }
6588 int privateFlags = mPrivateFlags;
6589
6590 /* Check if the FOCUSABLE bit has changed */
6591 if (((changed & FOCUSABLE_MASK) != 0) &&
6592 ((privateFlags & HAS_BOUNDS) !=0)) {
6593 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6594 && ((privateFlags & FOCUSED) != 0)) {
6595 /* Give up focus if we are no longer focusable */
6596 clearFocus();
6597 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6598 && ((privateFlags & FOCUSED) == 0)) {
6599 /*
6600 * Tell the view system that we are now available to take focus
6601 * if no one else already has it.
6602 */
6603 if (mParent != null) mParent.focusableViewAvailable(this);
6604 }
6605 }
6606
6607 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6608 if ((changed & VISIBILITY_MASK) != 0) {
6609 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006610 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006611 * it was not visible. Marking it drawn ensures that the invalidation will
6612 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006613 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006614 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006615 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006616
6617 needGlobalAttributesUpdate(true);
6618
6619 // a view becoming visible is worth notifying the parent
6620 // about in case nothing has focus. even if this specific view
6621 // isn't focusable, it may contain something that is, so let
6622 // the root view try to give this focus if nothing else does.
6623 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6624 mParent.focusableViewAvailable(this);
6625 }
6626 }
6627 }
6628
6629 /* Check if the GONE bit has changed */
6630 if ((changed & GONE) != 0) {
6631 needGlobalAttributesUpdate(false);
6632 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006633
Romain Guyecd80ee2009-12-03 17:13:02 -08006634 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6635 if (hasFocus()) clearFocus();
6636 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006637 if (mParent instanceof View) {
6638 // GONE views noop invalidation, so invalidate the parent
6639 ((View) mParent).invalidate(true);
6640 }
6641 // Mark the view drawn to ensure that it gets invalidated properly the next
6642 // time it is visible and gets invalidated
6643 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006644 }
6645 if (mAttachInfo != null) {
6646 mAttachInfo.mViewVisibilityChanged = true;
6647 }
6648 }
6649
6650 /* Check if the VISIBLE bit has changed */
6651 if ((changed & INVISIBLE) != 0) {
6652 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006653 /*
6654 * If this view is becoming invisible, set the DRAWN flag so that
6655 * the next invalidate() will not be skipped.
6656 */
6657 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006658
6659 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6660 // root view becoming invisible shouldn't clear focus
6661 if (getRootView() != this) {
6662 clearFocus();
6663 }
6664 }
6665 if (mAttachInfo != null) {
6666 mAttachInfo.mViewVisibilityChanged = true;
6667 }
6668 }
6669
Adam Powell326d8082009-12-09 15:10:07 -08006670 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006671 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006672 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6673 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006674 } else if (mParent != null) {
6675 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006676 }
Adam Powell326d8082009-12-09 15:10:07 -08006677 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6678 }
6679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006680 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6681 destroyDrawingCache();
6682 }
6683
6684 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6685 destroyDrawingCache();
6686 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006687 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006688 }
6689
6690 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6691 destroyDrawingCache();
6692 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6693 }
6694
6695 if ((changed & DRAW_MASK) != 0) {
6696 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6697 if (mBGDrawable != null) {
6698 mPrivateFlags &= ~SKIP_DRAW;
6699 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6700 } else {
6701 mPrivateFlags |= SKIP_DRAW;
6702 }
6703 } else {
6704 mPrivateFlags &= ~SKIP_DRAW;
6705 }
6706 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006707 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006708 }
6709
6710 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006711 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006712 mParent.recomputeViewAttributes(this);
6713 }
6714 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006715
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006716 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006717 requestLayout();
6718 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006719 }
6720
6721 /**
6722 * Change the view's z order in the tree, so it's on top of other sibling
6723 * views
6724 */
6725 public void bringToFront() {
6726 if (mParent != null) {
6727 mParent.bringChildToFront(this);
6728 }
6729 }
6730
6731 /**
6732 * This is called in response to an internal scroll in this view (i.e., the
6733 * view scrolled its own contents). This is typically as a result of
6734 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6735 * called.
6736 *
6737 * @param l Current horizontal scroll origin.
6738 * @param t Current vertical scroll origin.
6739 * @param oldl Previous horizontal scroll origin.
6740 * @param oldt Previous vertical scroll origin.
6741 */
6742 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006743 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6744 postSendViewScrolledAccessibilityEventCallback();
6745 }
6746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006747 mBackgroundSizeChanged = true;
6748
6749 final AttachInfo ai = mAttachInfo;
6750 if (ai != null) {
6751 ai.mViewScrollChanged = true;
6752 }
6753 }
6754
6755 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006756 * Interface definition for a callback to be invoked when the layout bounds of a view
6757 * changes due to layout processing.
6758 */
6759 public interface OnLayoutChangeListener {
6760 /**
6761 * Called when the focus state of a view has changed.
6762 *
6763 * @param v The view whose state has changed.
6764 * @param left The new value of the view's left property.
6765 * @param top The new value of the view's top property.
6766 * @param right The new value of the view's right property.
6767 * @param bottom The new value of the view's bottom property.
6768 * @param oldLeft The previous value of the view's left property.
6769 * @param oldTop The previous value of the view's top property.
6770 * @param oldRight The previous value of the view's right property.
6771 * @param oldBottom The previous value of the view's bottom property.
6772 */
6773 void onLayoutChange(View v, int left, int top, int right, int bottom,
6774 int oldLeft, int oldTop, int oldRight, int oldBottom);
6775 }
6776
6777 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006778 * This is called during layout when the size of this view has changed. If
6779 * you were just added to the view hierarchy, you're called with the old
6780 * values of 0.
6781 *
6782 * @param w Current width of this view.
6783 * @param h Current height of this view.
6784 * @param oldw Old width of this view.
6785 * @param oldh Old height of this view.
6786 */
6787 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6788 }
6789
6790 /**
6791 * Called by draw to draw the child views. This may be overridden
6792 * by derived classes to gain control just before its children are drawn
6793 * (but after its own view has been drawn).
6794 * @param canvas the canvas on which to draw the view
6795 */
6796 protected void dispatchDraw(Canvas canvas) {
6797 }
6798
6799 /**
6800 * Gets the parent of this view. Note that the parent is a
6801 * ViewParent and not necessarily a View.
6802 *
6803 * @return Parent of this view.
6804 */
6805 public final ViewParent getParent() {
6806 return mParent;
6807 }
6808
6809 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006810 * Set the horizontal scrolled position of your view. This will cause a call to
6811 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6812 * invalidated.
6813 * @param value the x position to scroll to
6814 */
6815 public void setScrollX(int value) {
6816 scrollTo(value, mScrollY);
6817 }
6818
6819 /**
6820 * Set the vertical scrolled position of your view. This will cause a call to
6821 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6822 * invalidated.
6823 * @param value the y position to scroll to
6824 */
6825 public void setScrollY(int value) {
6826 scrollTo(mScrollX, value);
6827 }
6828
6829 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006830 * Return the scrolled left position of this view. This is the left edge of
6831 * the displayed part of your view. You do not need to draw any pixels
6832 * farther left, since those are outside of the frame of your view on
6833 * screen.
6834 *
6835 * @return The left edge of the displayed part of your view, in pixels.
6836 */
6837 public final int getScrollX() {
6838 return mScrollX;
6839 }
6840
6841 /**
6842 * Return the scrolled top position of this view. This is the top edge of
6843 * the displayed part of your view. You do not need to draw any pixels above
6844 * it, since those are outside of the frame of your view on screen.
6845 *
6846 * @return The top edge of the displayed part of your view, in pixels.
6847 */
6848 public final int getScrollY() {
6849 return mScrollY;
6850 }
6851
6852 /**
6853 * Return the width of the your view.
6854 *
6855 * @return The width of your view, in pixels.
6856 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006857 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006858 public final int getWidth() {
6859 return mRight - mLeft;
6860 }
6861
6862 /**
6863 * Return the height of your view.
6864 *
6865 * @return The height of your view, in pixels.
6866 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006867 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006868 public final int getHeight() {
6869 return mBottom - mTop;
6870 }
6871
6872 /**
6873 * Return the visible drawing bounds of your view. Fills in the output
6874 * rectangle with the values from getScrollX(), getScrollY(),
6875 * getWidth(), and getHeight().
6876 *
6877 * @param outRect The (scrolled) drawing bounds of the view.
6878 */
6879 public void getDrawingRect(Rect outRect) {
6880 outRect.left = mScrollX;
6881 outRect.top = mScrollY;
6882 outRect.right = mScrollX + (mRight - mLeft);
6883 outRect.bottom = mScrollY + (mBottom - mTop);
6884 }
6885
6886 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006887 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6888 * raw width component (that is the result is masked by
6889 * {@link #MEASURED_SIZE_MASK}).
6890 *
6891 * @return The raw measured width of this view.
6892 */
6893 public final int getMeasuredWidth() {
6894 return mMeasuredWidth & MEASURED_SIZE_MASK;
6895 }
6896
6897 /**
6898 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006899 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006900 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006901 * This should be used during measurement and layout calculations only. Use
6902 * {@link #getWidth()} to see how wide a view is after layout.
6903 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006904 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006905 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006906 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006907 return mMeasuredWidth;
6908 }
6909
6910 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006911 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6912 * raw width component (that is the result is masked by
6913 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006914 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006915 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006916 */
6917 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006918 return mMeasuredHeight & MEASURED_SIZE_MASK;
6919 }
6920
6921 /**
6922 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006923 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006924 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6925 * This should be used during measurement and layout calculations only. Use
6926 * {@link #getHeight()} to see how wide a view is after layout.
6927 *
6928 * @return The measured width of this view as a bit mask.
6929 */
6930 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006931 return mMeasuredHeight;
6932 }
6933
6934 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006935 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6936 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6937 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6938 * and the height component is at the shifted bits
6939 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6940 */
6941 public final int getMeasuredState() {
6942 return (mMeasuredWidth&MEASURED_STATE_MASK)
6943 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6944 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6945 }
6946
6947 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006948 * The transform matrix of this view, which is calculated based on the current
6949 * roation, scale, and pivot properties.
6950 *
6951 * @see #getRotation()
6952 * @see #getScaleX()
6953 * @see #getScaleY()
6954 * @see #getPivotX()
6955 * @see #getPivotY()
6956 * @return The current transform matrix for the view
6957 */
6958 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07006959 if (mTransformationInfo != null) {
6960 updateMatrix();
6961 return mTransformationInfo.mMatrix;
6962 }
6963 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07006964 }
6965
6966 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006967 * Utility function to determine if the value is far enough away from zero to be
6968 * considered non-zero.
6969 * @param value A floating point value to check for zero-ness
6970 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6971 */
6972 private static boolean nonzero(float value) {
6973 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6974 }
6975
6976 /**
Jeff Brown86671742010-09-30 20:00:15 -07006977 * Returns true if the transform matrix is the identity matrix.
6978 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006979 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006980 * @return True if the transform matrix is the identity matrix, false otherwise.
6981 */
Jeff Brown86671742010-09-30 20:00:15 -07006982 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07006983 if (mTransformationInfo != null) {
6984 updateMatrix();
6985 return mTransformationInfo.mMatrixIsIdentity;
6986 }
6987 return true;
6988 }
6989
6990 void ensureTransformationInfo() {
6991 if (mTransformationInfo == null) {
6992 mTransformationInfo = new TransformationInfo();
6993 }
Jeff Brown86671742010-09-30 20:00:15 -07006994 }
6995
6996 /**
6997 * Recomputes the transform matrix if necessary.
6998 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006999 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007000 final TransformationInfo info = mTransformationInfo;
7001 if (info == null) {
7002 return;
7003 }
7004 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007005 // transform-related properties have changed since the last time someone
7006 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007007
7008 // Figure out if we need to update the pivot point
7009 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007010 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7011 info.mPrevWidth = mRight - mLeft;
7012 info.mPrevHeight = mBottom - mTop;
7013 info.mPivotX = info.mPrevWidth / 2f;
7014 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007015 }
7016 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007017 info.mMatrix.reset();
7018 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7019 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7020 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7021 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007022 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007023 if (info.mCamera == null) {
7024 info.mCamera = new Camera();
7025 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007026 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007027 info.mCamera.save();
7028 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7029 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7030 info.mCamera.getMatrix(info.matrix3D);
7031 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7032 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7033 info.mPivotY + info.mTranslationY);
7034 info.mMatrix.postConcat(info.matrix3D);
7035 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007036 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007037 info.mMatrixDirty = false;
7038 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7039 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007040 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007041 }
7042
7043 /**
7044 * Utility method to retrieve the inverse of the current mMatrix property.
7045 * We cache the matrix to avoid recalculating it when transform properties
7046 * have not changed.
7047 *
7048 * @return The inverse of the current matrix of this view.
7049 */
Jeff Brown86671742010-09-30 20:00:15 -07007050 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007051 final TransformationInfo info = mTransformationInfo;
7052 if (info != null) {
7053 updateMatrix();
7054 if (info.mInverseMatrixDirty) {
7055 if (info.mInverseMatrix == null) {
7056 info.mInverseMatrix = new Matrix();
7057 }
7058 info.mMatrix.invert(info.mInverseMatrix);
7059 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007060 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007061 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007062 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007063 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007064 }
7065
7066 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007067 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7068 * views are drawn) from the camera to this view. The camera's distance
7069 * affects 3D transformations, for instance rotations around the X and Y
7070 * axis. If the rotationX or rotationY properties are changed and this view is
7071 * large (more than half the size of the screen), it is recommended to always
7072 * use a camera distance that's greater than the height (X axis rotation) or
7073 * the width (Y axis rotation) of this view.</p>
7074 *
7075 * <p>The distance of the camera from the view plane can have an affect on the
7076 * perspective distortion of the view when it is rotated around the x or y axis.
7077 * For example, a large distance will result in a large viewing angle, and there
7078 * will not be much perspective distortion of the view as it rotates. A short
7079 * distance may cause much more perspective distortion upon rotation, and can
7080 * also result in some drawing artifacts if the rotated view ends up partially
7081 * behind the camera (which is why the recommendation is to use a distance at
7082 * least as far as the size of the view, if the view is to be rotated.)</p>
7083 *
7084 * <p>The distance is expressed in "depth pixels." The default distance depends
7085 * on the screen density. For instance, on a medium density display, the
7086 * default distance is 1280. On a high density display, the default distance
7087 * is 1920.</p>
7088 *
7089 * <p>If you want to specify a distance that leads to visually consistent
7090 * results across various densities, use the following formula:</p>
7091 * <pre>
7092 * float scale = context.getResources().getDisplayMetrics().density;
7093 * view.setCameraDistance(distance * scale);
7094 * </pre>
7095 *
7096 * <p>The density scale factor of a high density display is 1.5,
7097 * and 1920 = 1280 * 1.5.</p>
7098 *
7099 * @param distance The distance in "depth pixels", if negative the opposite
7100 * value is used
7101 *
7102 * @see #setRotationX(float)
7103 * @see #setRotationY(float)
7104 */
7105 public void setCameraDistance(float distance) {
7106 invalidateParentCaches();
7107 invalidate(false);
7108
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007109 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007110 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007111 final TransformationInfo info = mTransformationInfo;
7112 if (info.mCamera == null) {
7113 info.mCamera = new Camera();
7114 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007115 }
7116
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007117 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7118 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007119
7120 invalidate(false);
7121 }
7122
7123 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007124 * The degrees that the view is rotated around the pivot point.
7125 *
Romain Guya5364ee2011-02-24 14:46:04 -08007126 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007127 * @see #getPivotX()
7128 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007129 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007130 * @return The degrees of rotation.
7131 */
7132 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007133 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007134 }
7135
7136 /**
Chet Haase897247b2010-09-09 14:54:47 -07007137 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7138 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007139 *
7140 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007141 *
7142 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007143 * @see #getPivotX()
7144 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007145 * @see #setRotationX(float)
7146 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007147 *
7148 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007149 */
7150 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007151 ensureTransformationInfo();
7152 final TransformationInfo info = mTransformationInfo;
7153 if (info.mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007154 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007155 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007156 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007157 info.mRotation = rotation;
7158 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007159 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007160 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007161 }
7162 }
7163
7164 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007165 * The degrees that the view is rotated around the vertical axis through the pivot point.
7166 *
7167 * @see #getPivotX()
7168 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007169 * @see #setRotationY(float)
7170 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007171 * @return The degrees of Y rotation.
7172 */
7173 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007174 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007175 }
7176
7177 /**
Chet Haase897247b2010-09-09 14:54:47 -07007178 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7179 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7180 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007181 *
7182 * When rotating large views, it is recommended to adjust the camera distance
7183 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007184 *
7185 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007186 *
7187 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007188 * @see #getPivotX()
7189 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007190 * @see #setRotation(float)
7191 * @see #setRotationX(float)
7192 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007193 *
7194 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007195 */
7196 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007197 ensureTransformationInfo();
7198 final TransformationInfo info = mTransformationInfo;
7199 if (info.mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007200 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007201 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007202 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007203 info.mRotationY = rotationY;
7204 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007205 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007206 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007207 }
7208 }
7209
7210 /**
7211 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7212 *
7213 * @see #getPivotX()
7214 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007215 * @see #setRotationX(float)
7216 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007217 * @return The degrees of X rotation.
7218 */
7219 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007220 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007221 }
7222
7223 /**
Chet Haase897247b2010-09-09 14:54:47 -07007224 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7225 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7226 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007227 *
7228 * When rotating large views, it is recommended to adjust the camera distance
7229 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007230 *
7231 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007232 *
7233 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007234 * @see #getPivotX()
7235 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007236 * @see #setRotation(float)
7237 * @see #setRotationY(float)
7238 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007239 *
7240 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007241 */
7242 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007243 ensureTransformationInfo();
7244 final TransformationInfo info = mTransformationInfo;
7245 if (info.mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007246 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007247 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007248 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007249 info.mRotationX = rotationX;
7250 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007251 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007252 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007253 }
7254 }
7255
7256 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007257 * The amount that the view is scaled in x around the pivot point, as a proportion of
7258 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7259 *
Joe Onorato93162322010-09-16 15:42:01 -04007260 * <p>By default, this is 1.0f.
7261 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007262 * @see #getPivotX()
7263 * @see #getPivotY()
7264 * @return The scaling factor.
7265 */
7266 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007267 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007268 }
7269
7270 /**
7271 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7272 * the view's unscaled width. A value of 1 means that no scaling is applied.
7273 *
7274 * @param scaleX The scaling factor.
7275 * @see #getPivotX()
7276 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007277 *
7278 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007279 */
7280 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007281 ensureTransformationInfo();
7282 final TransformationInfo info = mTransformationInfo;
7283 if (info.mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007284 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007285 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007286 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007287 info.mScaleX = scaleX;
7288 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007289 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007290 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007291 }
7292 }
7293
7294 /**
7295 * The amount that the view is scaled in y around the pivot point, as a proportion of
7296 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7297 *
Joe Onorato93162322010-09-16 15:42:01 -04007298 * <p>By default, this is 1.0f.
7299 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007300 * @see #getPivotX()
7301 * @see #getPivotY()
7302 * @return The scaling factor.
7303 */
7304 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007305 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007306 }
7307
7308 /**
7309 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7310 * the view's unscaled width. A value of 1 means that no scaling is applied.
7311 *
7312 * @param scaleY The scaling factor.
7313 * @see #getPivotX()
7314 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007315 *
7316 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007317 */
7318 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007319 ensureTransformationInfo();
7320 final TransformationInfo info = mTransformationInfo;
7321 if (info.mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007322 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007323 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007324 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007325 info.mScaleY = scaleY;
7326 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007327 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007328 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007329 }
7330 }
7331
7332 /**
7333 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7334 * and {@link #setScaleX(float) scaled}.
7335 *
7336 * @see #getRotation()
7337 * @see #getScaleX()
7338 * @see #getScaleY()
7339 * @see #getPivotY()
7340 * @return The x location of the pivot point.
7341 */
7342 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007343 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007344 }
7345
7346 /**
7347 * Sets the x location of the point around which the view is
7348 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007349 * By default, the pivot point is centered on the object.
7350 * Setting this property disables this behavior and causes the view to use only the
7351 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007352 *
7353 * @param pivotX The x location of the pivot point.
7354 * @see #getRotation()
7355 * @see #getScaleX()
7356 * @see #getScaleY()
7357 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007358 *
7359 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007360 */
7361 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007362 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007363 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007364 final TransformationInfo info = mTransformationInfo;
7365 if (info.mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007366 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007367 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007368 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007369 info.mPivotX = pivotX;
7370 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007371 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007372 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007373 }
7374 }
7375
7376 /**
7377 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7378 * and {@link #setScaleY(float) scaled}.
7379 *
7380 * @see #getRotation()
7381 * @see #getScaleX()
7382 * @see #getScaleY()
7383 * @see #getPivotY()
7384 * @return The y location of the pivot point.
7385 */
7386 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007387 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007388 }
7389
7390 /**
7391 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007392 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7393 * Setting this property disables this behavior and causes the view to use only the
7394 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007395 *
7396 * @param pivotY The y location of the pivot point.
7397 * @see #getRotation()
7398 * @see #getScaleX()
7399 * @see #getScaleY()
7400 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007401 *
7402 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007403 */
7404 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007405 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007406 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007407 final TransformationInfo info = mTransformationInfo;
7408 if (info.mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007409 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007410 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007411 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007412 info.mPivotY = pivotY;
7413 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007414 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007415 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007416 }
7417 }
7418
7419 /**
7420 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7421 * completely transparent and 1 means the view is completely opaque.
7422 *
Joe Onorato93162322010-09-16 15:42:01 -04007423 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007424 * @return The opacity of the view.
7425 */
7426 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007427 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007428 }
7429
7430 /**
Romain Guy171c5922011-01-06 10:04:23 -08007431 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7432 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007433 *
Romain Guy171c5922011-01-06 10:04:23 -08007434 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7435 * responsible for applying the opacity itself. Otherwise, calling this method is
7436 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007437 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007438 *
7439 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007440 *
Joe Malin32736f02011-01-19 16:14:20 -08007441 * @see #setLayerType(int, android.graphics.Paint)
7442 *
Chet Haase73066682010-11-29 15:55:32 -08007443 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007444 */
7445 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007446 ensureTransformationInfo();
7447 mTransformationInfo.mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007448 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07007449 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07007450 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007451 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007452 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07007453 } else {
Romain Guya3496a92010-10-12 11:53:24 -07007454 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007455 invalidate(false);
7456 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007457 }
7458
7459 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007460 * Faster version of setAlpha() which performs the same steps except there are
7461 * no calls to invalidate(). The caller of this function should perform proper invalidation
7462 * on the parent and this object. The return value indicates whether the subclass handles
7463 * alpha (the return value for onSetAlpha()).
7464 *
7465 * @param alpha The new value for the alpha property
7466 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
7467 */
7468 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007469 ensureTransformationInfo();
7470 mTransformationInfo.mAlpha = alpha;
Chet Haasea00f3862011-02-22 06:34:40 -08007471 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7472 if (subclassHandlesAlpha) {
7473 mPrivateFlags |= ALPHA_SET;
7474 } else {
7475 mPrivateFlags &= ~ALPHA_SET;
7476 }
7477 return subclassHandlesAlpha;
7478 }
7479
7480 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007481 * Top position of this view relative to its parent.
7482 *
7483 * @return The top of this view, in pixels.
7484 */
7485 @ViewDebug.CapturedViewProperty
7486 public final int getTop() {
7487 return mTop;
7488 }
7489
7490 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007491 * Sets the top position of this view relative to its parent. This method is meant to be called
7492 * by the layout system and should not generally be called otherwise, because the property
7493 * may be changed at any time by the layout.
7494 *
7495 * @param top The top of this view, in pixels.
7496 */
7497 public final void setTop(int top) {
7498 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007499 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007500 final boolean matrixIsIdentity = mTransformationInfo == null
7501 || mTransformationInfo.mMatrixIsIdentity;
7502 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007503 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007504 int minTop;
7505 int yLoc;
7506 if (top < mTop) {
7507 minTop = top;
7508 yLoc = top - mTop;
7509 } else {
7510 minTop = mTop;
7511 yLoc = 0;
7512 }
Chet Haasee9140a72011-02-16 16:23:29 -08007513 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007514 }
7515 } else {
7516 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007517 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007518 }
7519
Chet Haaseed032702010-10-01 14:05:54 -07007520 int width = mRight - mLeft;
7521 int oldHeight = mBottom - mTop;
7522
Chet Haase21cd1382010-09-01 17:42:29 -07007523 mTop = top;
7524
Chet Haaseed032702010-10-01 14:05:54 -07007525 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7526
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007527 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007528 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7529 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007530 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007531 }
Chet Haase21cd1382010-09-01 17:42:29 -07007532 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007533 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007534 }
Chet Haase55dbb652010-12-21 20:15:08 -08007535 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007536 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007537 }
7538 }
7539
7540 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007541 * Bottom position of this view relative to its parent.
7542 *
7543 * @return The bottom of this view, in pixels.
7544 */
7545 @ViewDebug.CapturedViewProperty
7546 public final int getBottom() {
7547 return mBottom;
7548 }
7549
7550 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007551 * True if this view has changed since the last time being drawn.
7552 *
7553 * @return The dirty state of this view.
7554 */
7555 public boolean isDirty() {
7556 return (mPrivateFlags & DIRTY_MASK) != 0;
7557 }
7558
7559 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007560 * Sets the bottom position of this view relative to its parent. This method is meant to be
7561 * called by the layout system and should not generally be called otherwise, because the
7562 * property may be changed at any time by the layout.
7563 *
7564 * @param bottom The bottom of this view, in pixels.
7565 */
7566 public final void setBottom(int bottom) {
7567 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007568 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007569 final boolean matrixIsIdentity = mTransformationInfo == null
7570 || mTransformationInfo.mMatrixIsIdentity;
7571 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007572 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007573 int maxBottom;
7574 if (bottom < mBottom) {
7575 maxBottom = mBottom;
7576 } else {
7577 maxBottom = bottom;
7578 }
Chet Haasee9140a72011-02-16 16:23:29 -08007579 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007580 }
7581 } else {
7582 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007583 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007584 }
7585
Chet Haaseed032702010-10-01 14:05:54 -07007586 int width = mRight - mLeft;
7587 int oldHeight = mBottom - mTop;
7588
Chet Haase21cd1382010-09-01 17:42:29 -07007589 mBottom = bottom;
7590
Chet Haaseed032702010-10-01 14:05:54 -07007591 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7592
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007593 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007594 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7595 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007596 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007597 }
Chet Haase21cd1382010-09-01 17:42:29 -07007598 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007599 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007600 }
Chet Haase55dbb652010-12-21 20:15:08 -08007601 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007602 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007603 }
7604 }
7605
7606 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007607 * Left position of this view relative to its parent.
7608 *
7609 * @return The left edge of this view, in pixels.
7610 */
7611 @ViewDebug.CapturedViewProperty
7612 public final int getLeft() {
7613 return mLeft;
7614 }
7615
7616 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007617 * Sets the left position of this view relative to its parent. This method is meant to be called
7618 * by the layout system and should not generally be called otherwise, because the property
7619 * may be changed at any time by the layout.
7620 *
7621 * @param left The bottom of this view, in pixels.
7622 */
7623 public final void setLeft(int left) {
7624 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007625 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007626 final boolean matrixIsIdentity = mTransformationInfo == null
7627 || mTransformationInfo.mMatrixIsIdentity;
7628 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007629 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007630 int minLeft;
7631 int xLoc;
7632 if (left < mLeft) {
7633 minLeft = left;
7634 xLoc = left - mLeft;
7635 } else {
7636 minLeft = mLeft;
7637 xLoc = 0;
7638 }
Chet Haasee9140a72011-02-16 16:23:29 -08007639 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007640 }
7641 } else {
7642 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007643 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007644 }
7645
Chet Haaseed032702010-10-01 14:05:54 -07007646 int oldWidth = mRight - mLeft;
7647 int height = mBottom - mTop;
7648
Chet Haase21cd1382010-09-01 17:42:29 -07007649 mLeft = left;
7650
Chet Haaseed032702010-10-01 14:05:54 -07007651 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7652
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007653 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007654 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7655 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007656 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007657 }
Chet Haase21cd1382010-09-01 17:42:29 -07007658 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007659 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007660 }
Chet Haase55dbb652010-12-21 20:15:08 -08007661 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007662 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007663 }
7664 }
7665
7666 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007667 * Right position of this view relative to its parent.
7668 *
7669 * @return The right edge of this view, in pixels.
7670 */
7671 @ViewDebug.CapturedViewProperty
7672 public final int getRight() {
7673 return mRight;
7674 }
7675
7676 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007677 * Sets the right position of this view relative to its parent. This method is meant to be called
7678 * by the layout system and should not generally be called otherwise, because the property
7679 * may be changed at any time by the layout.
7680 *
7681 * @param right The bottom of this view, in pixels.
7682 */
7683 public final void setRight(int right) {
7684 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007685 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007686 final boolean matrixIsIdentity = mTransformationInfo == null
7687 || mTransformationInfo.mMatrixIsIdentity;
7688 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007689 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007690 int maxRight;
7691 if (right < mRight) {
7692 maxRight = mRight;
7693 } else {
7694 maxRight = right;
7695 }
Chet Haasee9140a72011-02-16 16:23:29 -08007696 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007697 }
7698 } else {
7699 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007700 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007701 }
7702
Chet Haaseed032702010-10-01 14:05:54 -07007703 int oldWidth = mRight - mLeft;
7704 int height = mBottom - mTop;
7705
Chet Haase21cd1382010-09-01 17:42:29 -07007706 mRight = right;
7707
Chet Haaseed032702010-10-01 14:05:54 -07007708 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7709
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007710 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007711 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7712 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007713 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007714 }
Chet Haase21cd1382010-09-01 17:42:29 -07007715 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007716 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007717 }
Chet Haase55dbb652010-12-21 20:15:08 -08007718 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007719 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007720 }
7721 }
7722
7723 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007724 * The visual x position of this view, in pixels. This is equivalent to the
7725 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007726 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007727 *
Chet Haasedf030d22010-07-30 17:22:38 -07007728 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007729 */
Chet Haasedf030d22010-07-30 17:22:38 -07007730 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007731 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007732 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007733
Chet Haasedf030d22010-07-30 17:22:38 -07007734 /**
7735 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7736 * {@link #setTranslationX(float) translationX} property to be the difference between
7737 * the x value passed in and the current {@link #getLeft() left} property.
7738 *
7739 * @param x The visual x position of this view, in pixels.
7740 */
7741 public void setX(float x) {
7742 setTranslationX(x - mLeft);
7743 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007744
Chet Haasedf030d22010-07-30 17:22:38 -07007745 /**
7746 * The visual y position of this view, in pixels. This is equivalent to the
7747 * {@link #setTranslationY(float) translationY} property plus the current
7748 * {@link #getTop() top} property.
7749 *
7750 * @return The visual y position of this view, in pixels.
7751 */
7752 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007753 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007754 }
7755
7756 /**
7757 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7758 * {@link #setTranslationY(float) translationY} property to be the difference between
7759 * the y value passed in and the current {@link #getTop() top} property.
7760 *
7761 * @param y The visual y position of this view, in pixels.
7762 */
7763 public void setY(float y) {
7764 setTranslationY(y - mTop);
7765 }
7766
7767
7768 /**
7769 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7770 * This position is post-layout, in addition to wherever the object's
7771 * layout placed it.
7772 *
7773 * @return The horizontal position of this view relative to its left position, in pixels.
7774 */
7775 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007776 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07007777 }
7778
7779 /**
7780 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7781 * This effectively positions the object post-layout, in addition to wherever the object's
7782 * layout placed it.
7783 *
7784 * @param translationX The horizontal position of this view relative to its left position,
7785 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007786 *
7787 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007788 */
7789 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007790 ensureTransformationInfo();
7791 final TransformationInfo info = mTransformationInfo;
7792 if (info.mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007793 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007794 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007795 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007796 info.mTranslationX = translationX;
7797 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07007798 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007799 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007800 }
7801 }
7802
7803 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007804 * The horizontal location of this view relative to its {@link #getTop() top} position.
7805 * This position is post-layout, in addition to wherever the object's
7806 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007807 *
Chet Haasedf030d22010-07-30 17:22:38 -07007808 * @return The vertical position of this view relative to its top position,
7809 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007810 */
Chet Haasedf030d22010-07-30 17:22:38 -07007811 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007812 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007813 }
7814
7815 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007816 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7817 * This effectively positions the object post-layout, in addition to wherever the object's
7818 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007819 *
Chet Haasedf030d22010-07-30 17:22:38 -07007820 * @param translationY The vertical position of this view relative to its top position,
7821 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007822 *
7823 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007824 */
Chet Haasedf030d22010-07-30 17:22:38 -07007825 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007826 ensureTransformationInfo();
7827 final TransformationInfo info = mTransformationInfo;
7828 if (info.mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007829 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007830 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007831 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007832 info.mTranslationY = translationY;
7833 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07007834 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007835 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007836 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007837 }
7838
7839 /**
Romain Guyda489792011-02-03 01:05:15 -08007840 * @hide
7841 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007842 public void setFastTranslationX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007843 ensureTransformationInfo();
7844 final TransformationInfo info = mTransformationInfo;
7845 info.mTranslationX = x;
7846 info.mMatrixDirty = true;
Michael Jurkadece29f2011-02-03 01:41:49 -08007847 }
7848
7849 /**
7850 * @hide
7851 */
7852 public void setFastTranslationY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007853 ensureTransformationInfo();
7854 final TransformationInfo info = mTransformationInfo;
7855 info.mTranslationY = y;
7856 info.mMatrixDirty = true;
Michael Jurkadece29f2011-02-03 01:41:49 -08007857 }
7858
7859 /**
7860 * @hide
7861 */
Romain Guyda489792011-02-03 01:05:15 -08007862 public void setFastX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007863 ensureTransformationInfo();
7864 final TransformationInfo info = mTransformationInfo;
7865 info.mTranslationX = x - mLeft;
7866 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007867 }
7868
7869 /**
7870 * @hide
7871 */
7872 public void setFastY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007873 ensureTransformationInfo();
7874 final TransformationInfo info = mTransformationInfo;
7875 info.mTranslationY = y - mTop;
7876 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007877 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007878
Romain Guyda489792011-02-03 01:05:15 -08007879 /**
7880 * @hide
7881 */
7882 public void setFastScaleX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007883 ensureTransformationInfo();
7884 final TransformationInfo info = mTransformationInfo;
7885 info.mScaleX = x;
7886 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007887 }
7888
7889 /**
7890 * @hide
7891 */
7892 public void setFastScaleY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007893 ensureTransformationInfo();
7894 final TransformationInfo info = mTransformationInfo;
7895 info.mScaleY = y;
7896 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007897 }
7898
7899 /**
7900 * @hide
7901 */
7902 public void setFastAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007903 ensureTransformationInfo();
7904 mTransformationInfo.mAlpha = alpha;
Romain Guyda489792011-02-03 01:05:15 -08007905 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007906
Romain Guyda489792011-02-03 01:05:15 -08007907 /**
7908 * @hide
7909 */
7910 public void setFastRotationY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007911 ensureTransformationInfo();
7912 final TransformationInfo info = mTransformationInfo;
7913 info.mRotationY = y;
7914 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007915 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007916
Romain Guyda489792011-02-03 01:05:15 -08007917 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007918 * Hit rectangle in parent's coordinates
7919 *
7920 * @param outRect The hit rectangle of the view.
7921 */
7922 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07007923 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007924 final TransformationInfo info = mTransformationInfo;
7925 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007926 outRect.set(mLeft, mTop, mRight, mBottom);
7927 } else {
7928 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007929 tmpRect.set(-info.mPivotX, -info.mPivotY,
7930 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
7931 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07007932 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
7933 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07007934 }
7935 }
7936
7937 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007938 * Determines whether the given point, in local coordinates is inside the view.
7939 */
7940 /*package*/ final boolean pointInView(float localX, float localY) {
7941 return localX >= 0 && localX < (mRight - mLeft)
7942 && localY >= 0 && localY < (mBottom - mTop);
7943 }
7944
7945 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007946 * Utility method to determine whether the given point, in local coordinates,
7947 * is inside the view, where the area of the view is expanded by the slop factor.
7948 * This method is called while processing touch-move events to determine if the event
7949 * is still within the view.
7950 */
7951 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007952 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007953 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007954 }
7955
7956 /**
7957 * When a view has focus and the user navigates away from it, the next view is searched for
7958 * starting from the rectangle filled in by this method.
7959 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07007960 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
7961 * of the view. However, if your view maintains some idea of internal selection,
7962 * such as a cursor, or a selected row or column, you should override this method and
7963 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007964 *
7965 * @param r The rectangle to fill in, in this view's coordinates.
7966 */
7967 public void getFocusedRect(Rect r) {
7968 getDrawingRect(r);
7969 }
7970
7971 /**
7972 * If some part of this view is not clipped by any of its parents, then
7973 * return that area in r in global (root) coordinates. To convert r to local
7974 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
7975 * -globalOffset.y)) If the view is completely clipped or translated out,
7976 * return false.
7977 *
7978 * @param r If true is returned, r holds the global coordinates of the
7979 * visible portion of this view.
7980 * @param globalOffset If true is returned, globalOffset holds the dx,dy
7981 * between this view and its root. globalOffet may be null.
7982 * @return true if r is non-empty (i.e. part of the view is visible at the
7983 * root level.
7984 */
7985 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7986 int width = mRight - mLeft;
7987 int height = mBottom - mTop;
7988 if (width > 0 && height > 0) {
7989 r.set(0, 0, width, height);
7990 if (globalOffset != null) {
7991 globalOffset.set(-mScrollX, -mScrollY);
7992 }
7993 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7994 }
7995 return false;
7996 }
7997
7998 public final boolean getGlobalVisibleRect(Rect r) {
7999 return getGlobalVisibleRect(r, null);
8000 }
8001
8002 public final boolean getLocalVisibleRect(Rect r) {
8003 Point offset = new Point();
8004 if (getGlobalVisibleRect(r, offset)) {
8005 r.offset(-offset.x, -offset.y); // make r local
8006 return true;
8007 }
8008 return false;
8009 }
8010
8011 /**
8012 * Offset this view's vertical location by the specified number of pixels.
8013 *
8014 * @param offset the number of pixels to offset the view by
8015 */
8016 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008017 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008018 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008019 final boolean matrixIsIdentity = mTransformationInfo == null
8020 || mTransformationInfo.mMatrixIsIdentity;
8021 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008022 final ViewParent p = mParent;
8023 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008024 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008025 int minTop;
8026 int maxBottom;
8027 int yLoc;
8028 if (offset < 0) {
8029 minTop = mTop + offset;
8030 maxBottom = mBottom;
8031 yLoc = offset;
8032 } else {
8033 minTop = mTop;
8034 maxBottom = mBottom + offset;
8035 yLoc = 0;
8036 }
8037 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8038 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008039 }
8040 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008041 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008042 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008043
Chet Haasec3aa3612010-06-17 08:50:37 -07008044 mTop += offset;
8045 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008046
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008047 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008048 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008049 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008050 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008051 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008052 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008053 }
8054
8055 /**
8056 * Offset this view's horizontal location by the specified amount of pixels.
8057 *
8058 * @param offset the numer of pixels to offset the view by
8059 */
8060 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008061 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008062 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008063 final boolean matrixIsIdentity = mTransformationInfo == null
8064 || mTransformationInfo.mMatrixIsIdentity;
8065 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008066 final ViewParent p = mParent;
8067 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008068 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008069 int minLeft;
8070 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008071 if (offset < 0) {
8072 minLeft = mLeft + offset;
8073 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008074 } else {
8075 minLeft = mLeft;
8076 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008077 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008078 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008079 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008080 }
8081 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008082 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008083 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008084
Chet Haasec3aa3612010-06-17 08:50:37 -07008085 mLeft += offset;
8086 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008087
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008088 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008089 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008090 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008091 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008092 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008094 }
8095
8096 /**
8097 * Get the LayoutParams associated with this view. All views should have
8098 * layout parameters. These supply parameters to the <i>parent</i> of this
8099 * view specifying how it should be arranged. There are many subclasses of
8100 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8101 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008102 *
8103 * This method may return null if this View is not attached to a parent
8104 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8105 * was not invoked successfully. When a View is attached to a parent
8106 * ViewGroup, this method must not return null.
8107 *
8108 * @return The LayoutParams associated with this view, or null if no
8109 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008110 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008111 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008112 public ViewGroup.LayoutParams getLayoutParams() {
8113 return mLayoutParams;
8114 }
8115
8116 /**
8117 * Set the layout parameters associated with this view. These supply
8118 * parameters to the <i>parent</i> of this view specifying how it should be
8119 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8120 * correspond to the different subclasses of ViewGroup that are responsible
8121 * for arranging their children.
8122 *
Romain Guy01c174b2011-02-22 11:51:06 -08008123 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008124 */
8125 public void setLayoutParams(ViewGroup.LayoutParams params) {
8126 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008127 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008128 }
8129 mLayoutParams = params;
8130 requestLayout();
8131 }
8132
8133 /**
8134 * Set the scrolled position of your view. This will cause a call to
8135 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8136 * invalidated.
8137 * @param x the x position to scroll to
8138 * @param y the y position to scroll to
8139 */
8140 public void scrollTo(int x, int y) {
8141 if (mScrollX != x || mScrollY != y) {
8142 int oldX = mScrollX;
8143 int oldY = mScrollY;
8144 mScrollX = x;
8145 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008146 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008147 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008148 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008149 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008151 }
8152 }
8153
8154 /**
8155 * Move the scrolled position of your view. This will cause a call to
8156 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8157 * invalidated.
8158 * @param x the amount of pixels to scroll by horizontally
8159 * @param y the amount of pixels to scroll by vertically
8160 */
8161 public void scrollBy(int x, int y) {
8162 scrollTo(mScrollX + x, mScrollY + y);
8163 }
8164
8165 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008166 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8167 * animation to fade the scrollbars out after a default delay. If a subclass
8168 * provides animated scrolling, the start delay should equal the duration
8169 * of the scrolling animation.</p>
8170 *
8171 * <p>The animation starts only if at least one of the scrollbars is
8172 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8173 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8174 * this method returns true, and false otherwise. If the animation is
8175 * started, this method calls {@link #invalidate()}; in that case the
8176 * caller should not call {@link #invalidate()}.</p>
8177 *
8178 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008179 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008180 *
8181 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8182 * and {@link #scrollTo(int, int)}.</p>
8183 *
8184 * @return true if the animation is played, false otherwise
8185 *
8186 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008187 * @see #scrollBy(int, int)
8188 * @see #scrollTo(int, int)
8189 * @see #isHorizontalScrollBarEnabled()
8190 * @see #isVerticalScrollBarEnabled()
8191 * @see #setHorizontalScrollBarEnabled(boolean)
8192 * @see #setVerticalScrollBarEnabled(boolean)
8193 */
8194 protected boolean awakenScrollBars() {
8195 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008196 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008197 }
8198
8199 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008200 * Trigger the scrollbars to draw.
8201 * This method differs from awakenScrollBars() only in its default duration.
8202 * initialAwakenScrollBars() will show the scroll bars for longer than
8203 * usual to give the user more of a chance to notice them.
8204 *
8205 * @return true if the animation is played, false otherwise.
8206 */
8207 private boolean initialAwakenScrollBars() {
8208 return mScrollCache != null &&
8209 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8210 }
8211
8212 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008213 * <p>
8214 * Trigger the scrollbars to draw. When invoked this method starts an
8215 * animation to fade the scrollbars out after a fixed delay. If a subclass
8216 * provides animated scrolling, the start delay should equal the duration of
8217 * the scrolling animation.
8218 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008219 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008220 * <p>
8221 * The animation starts only if at least one of the scrollbars is enabled,
8222 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8223 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8224 * this method returns true, and false otherwise. If the animation is
8225 * started, this method calls {@link #invalidate()}; in that case the caller
8226 * should not call {@link #invalidate()}.
8227 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008228 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008229 * <p>
8230 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008231 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008232 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008233 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008234 * @param startDelay the delay, in milliseconds, after which the animation
8235 * should start; when the delay is 0, the animation starts
8236 * immediately
8237 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008238 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008239 * @see #scrollBy(int, int)
8240 * @see #scrollTo(int, int)
8241 * @see #isHorizontalScrollBarEnabled()
8242 * @see #isVerticalScrollBarEnabled()
8243 * @see #setHorizontalScrollBarEnabled(boolean)
8244 * @see #setVerticalScrollBarEnabled(boolean)
8245 */
8246 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008247 return awakenScrollBars(startDelay, true);
8248 }
Joe Malin32736f02011-01-19 16:14:20 -08008249
Mike Cleron290947b2009-09-29 18:34:32 -07008250 /**
8251 * <p>
8252 * Trigger the scrollbars to draw. When invoked this method starts an
8253 * animation to fade the scrollbars out after a fixed delay. If a subclass
8254 * provides animated scrolling, the start delay should equal the duration of
8255 * the scrolling animation.
8256 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008257 *
Mike Cleron290947b2009-09-29 18:34:32 -07008258 * <p>
8259 * The animation starts only if at least one of the scrollbars is enabled,
8260 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8261 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8262 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008263 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008264 * is set to true; in that case the caller
8265 * should not call {@link #invalidate()}.
8266 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008267 *
Mike Cleron290947b2009-09-29 18:34:32 -07008268 * <p>
8269 * This method should be invoked everytime a subclass directly updates the
8270 * scroll parameters.
8271 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008272 *
Mike Cleron290947b2009-09-29 18:34:32 -07008273 * @param startDelay the delay, in milliseconds, after which the animation
8274 * should start; when the delay is 0, the animation starts
8275 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008276 *
Mike Cleron290947b2009-09-29 18:34:32 -07008277 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008278 *
Mike Cleron290947b2009-09-29 18:34:32 -07008279 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008280 *
Mike Cleron290947b2009-09-29 18:34:32 -07008281 * @see #scrollBy(int, int)
8282 * @see #scrollTo(int, int)
8283 * @see #isHorizontalScrollBarEnabled()
8284 * @see #isVerticalScrollBarEnabled()
8285 * @see #setHorizontalScrollBarEnabled(boolean)
8286 * @see #setVerticalScrollBarEnabled(boolean)
8287 */
8288 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008289 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008290
Mike Cleronf116bf82009-09-27 19:14:12 -07008291 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8292 return false;
8293 }
8294
8295 if (scrollCache.scrollBar == null) {
8296 scrollCache.scrollBar = new ScrollBarDrawable();
8297 }
8298
8299 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8300
Mike Cleron290947b2009-09-29 18:34:32 -07008301 if (invalidate) {
8302 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008303 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008304 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008305
8306 if (scrollCache.state == ScrollabilityCache.OFF) {
8307 // FIXME: this is copied from WindowManagerService.
8308 // We should get this value from the system when it
8309 // is possible to do so.
8310 final int KEY_REPEAT_FIRST_DELAY = 750;
8311 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8312 }
8313
8314 // Tell mScrollCache when we should start fading. This may
8315 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008316 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008317 scrollCache.fadeStartTime = fadeStartTime;
8318 scrollCache.state = ScrollabilityCache.ON;
8319
8320 // Schedule our fader to run, unscheduling any old ones first
8321 if (mAttachInfo != null) {
8322 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8323 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8324 }
8325
8326 return true;
8327 }
8328
8329 return false;
8330 }
8331
8332 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008333 * Do not invalidate views which are not visible and which are not running an animation. They
8334 * will not get drawn and they should not set dirty flags as if they will be drawn
8335 */
8336 private boolean skipInvalidate() {
8337 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8338 (!(mParent instanceof ViewGroup) ||
8339 !((ViewGroup) mParent).isViewTransitioning(this));
8340 }
8341 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008342 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008343 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8344 * in the future. This must be called from a UI thread. To call from a non-UI
8345 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008346 *
8347 * WARNING: This method is destructive to dirty.
8348 * @param dirty the rectangle representing the bounds of the dirty region
8349 */
8350 public void invalidate(Rect dirty) {
8351 if (ViewDebug.TRACE_HIERARCHY) {
8352 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8353 }
8354
Chet Haaseaceafe62011-08-26 15:44:33 -07008355 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008356 return;
8357 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008358 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008359 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8360 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008361 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008362 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008363 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008364 final ViewParent p = mParent;
8365 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008366 //noinspection PointlessBooleanExpression,ConstantConditions
8367 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8368 if (p != null && ai != null && ai.mHardwareAccelerated) {
8369 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008370 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008371 p.invalidateChild(this, null);
8372 return;
8373 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008375 if (p != null && ai != null) {
8376 final int scrollX = mScrollX;
8377 final int scrollY = mScrollY;
8378 final Rect r = ai.mTmpInvalRect;
8379 r.set(dirty.left - scrollX, dirty.top - scrollY,
8380 dirty.right - scrollX, dirty.bottom - scrollY);
8381 mParent.invalidateChild(this, r);
8382 }
8383 }
8384 }
8385
8386 /**
8387 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
8388 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008389 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8390 * will be called at some point in the future. This must be called from
8391 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008392 * @param l the left position of the dirty region
8393 * @param t the top position of the dirty region
8394 * @param r the right position of the dirty region
8395 * @param b the bottom position of the dirty region
8396 */
8397 public void invalidate(int l, int t, int r, int b) {
8398 if (ViewDebug.TRACE_HIERARCHY) {
8399 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8400 }
8401
Chet Haaseaceafe62011-08-26 15:44:33 -07008402 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008403 return;
8404 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008405 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008406 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8407 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008408 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008409 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008410 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008411 final ViewParent p = mParent;
8412 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008413 //noinspection PointlessBooleanExpression,ConstantConditions
8414 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8415 if (p != null && ai != null && ai.mHardwareAccelerated) {
8416 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008417 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008418 p.invalidateChild(this, null);
8419 return;
8420 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008422 if (p != null && ai != null && l < r && t < b) {
8423 final int scrollX = mScrollX;
8424 final int scrollY = mScrollY;
8425 final Rect tmpr = ai.mTmpInvalRect;
8426 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8427 p.invalidateChild(this, tmpr);
8428 }
8429 }
8430 }
8431
8432 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008433 * Invalidate the whole view. If the view is visible,
8434 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8435 * the future. This must be called from a UI thread. To call from a non-UI thread,
8436 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008437 */
8438 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008439 invalidate(true);
8440 }
Joe Malin32736f02011-01-19 16:14:20 -08008441
Chet Haaseed032702010-10-01 14:05:54 -07008442 /**
8443 * This is where the invalidate() work actually happens. A full invalidate()
8444 * causes the drawing cache to be invalidated, but this function can be called with
8445 * invalidateCache set to false to skip that invalidation step for cases that do not
8446 * need it (for example, a component that remains at the same dimensions with the same
8447 * content).
8448 *
8449 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8450 * well. This is usually true for a full invalidate, but may be set to false if the
8451 * View's contents or dimensions have not changed.
8452 */
Romain Guy849d0a32011-02-01 17:20:48 -08008453 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008454 if (ViewDebug.TRACE_HIERARCHY) {
8455 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8456 }
8457
Chet Haaseaceafe62011-08-26 15:44:33 -07008458 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008459 return;
8460 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008461 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008462 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008463 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8464 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008465 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008466 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008467 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008468 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008469 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008471 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008472 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008473 //noinspection PointlessBooleanExpression,ConstantConditions
8474 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8475 if (p != null && ai != null && ai.mHardwareAccelerated) {
8476 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008477 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008478 p.invalidateChild(this, null);
8479 return;
8480 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008481 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008483 if (p != null && ai != null) {
8484 final Rect r = ai.mTmpInvalRect;
8485 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8486 // Don't call invalidate -- we don't want to internally scroll
8487 // our own bounds
8488 p.invalidateChild(this, r);
8489 }
8490 }
8491 }
8492
8493 /**
Romain Guyda489792011-02-03 01:05:15 -08008494 * @hide
8495 */
8496 public void fastInvalidate() {
Chet Haaseaceafe62011-08-26 15:44:33 -07008497 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008498 return;
8499 }
Romain Guyda489792011-02-03 01:05:15 -08008500 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
8501 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8502 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
8503 if (mParent instanceof View) {
8504 ((View) mParent).mPrivateFlags |= INVALIDATED;
8505 }
8506 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008507 mPrivateFlags |= DIRTY;
Romain Guyda489792011-02-03 01:05:15 -08008508 mPrivateFlags |= INVALIDATED;
8509 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07008510 if (mParent != null && mAttachInfo != null) {
8511 if (mAttachInfo.mHardwareAccelerated) {
8512 mParent.invalidateChild(this, null);
8513 } else {
8514 final Rect r = mAttachInfo.mTmpInvalRect;
8515 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8516 // Don't call invalidate -- we don't want to internally scroll
8517 // our own bounds
8518 mParent.invalidateChild(this, r);
8519 }
Romain Guyda489792011-02-03 01:05:15 -08008520 }
8521 }
8522 }
8523
8524 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008525 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008526 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8527 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008528 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8529 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008530 *
8531 * @hide
8532 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008533 protected void invalidateParentCaches() {
8534 if (mParent instanceof View) {
8535 ((View) mParent).mPrivateFlags |= INVALIDATED;
8536 }
8537 }
Joe Malin32736f02011-01-19 16:14:20 -08008538
Romain Guy0fd89bf2011-01-26 15:41:30 -08008539 /**
8540 * Used to indicate that the parent of this view should be invalidated. This functionality
8541 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8542 * which is necessary when various parent-managed properties of the view change, such as
8543 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8544 * an invalidation event to the parent.
8545 *
8546 * @hide
8547 */
8548 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008549 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008550 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008551 }
8552 }
8553
8554 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008555 * Indicates whether this View is opaque. An opaque View guarantees that it will
8556 * draw all the pixels overlapping its bounds using a fully opaque color.
8557 *
8558 * Subclasses of View should override this method whenever possible to indicate
8559 * whether an instance is opaque. Opaque Views are treated in a special way by
8560 * the View hierarchy, possibly allowing it to perform optimizations during
8561 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008562 *
Romain Guy24443ea2009-05-11 11:56:30 -07008563 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008564 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008565 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008566 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008567 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008568 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8569 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008570 }
8571
Adam Powell20232d02010-12-08 21:08:53 -08008572 /**
8573 * @hide
8574 */
8575 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008576 // Opaque if:
8577 // - Has a background
8578 // - Background is opaque
8579 // - Doesn't have scrollbars or scrollbars are inside overlay
8580
8581 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8582 mPrivateFlags |= OPAQUE_BACKGROUND;
8583 } else {
8584 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8585 }
8586
8587 final int flags = mViewFlags;
8588 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8589 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8590 mPrivateFlags |= OPAQUE_SCROLLBARS;
8591 } else {
8592 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8593 }
8594 }
8595
8596 /**
8597 * @hide
8598 */
8599 protected boolean hasOpaqueScrollbars() {
8600 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008601 }
8602
8603 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008604 * @return A handler associated with the thread running the View. This
8605 * handler can be used to pump events in the UI events queue.
8606 */
8607 public Handler getHandler() {
8608 if (mAttachInfo != null) {
8609 return mAttachInfo.mHandler;
8610 }
8611 return null;
8612 }
8613
8614 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008615 * <p>Causes the Runnable to be added to the message queue.
8616 * The runnable will be run on the user interface thread.</p>
8617 *
8618 * <p>This method can be invoked from outside of the UI thread
8619 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008620 *
8621 * @param action The Runnable that will be executed.
8622 *
8623 * @return Returns true if the Runnable was successfully placed in to the
8624 * message queue. Returns false on failure, usually because the
8625 * looper processing the message queue is exiting.
8626 */
8627 public boolean post(Runnable action) {
8628 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008629 AttachInfo attachInfo = mAttachInfo;
8630 if (attachInfo != null) {
8631 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008632 } else {
8633 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008634 ViewRootImpl.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008635 return true;
8636 }
8637
8638 return handler.post(action);
8639 }
8640
8641 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008642 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008643 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008644 * The runnable will be run on the user interface thread.</p>
8645 *
8646 * <p>This method can be invoked from outside of the UI thread
8647 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008648 *
8649 * @param action The Runnable that will be executed.
8650 * @param delayMillis The delay (in milliseconds) until the Runnable
8651 * will be executed.
8652 *
8653 * @return true if the Runnable was successfully placed in to the
8654 * message queue. Returns false on failure, usually because the
8655 * looper processing the message queue is exiting. Note that a
8656 * result of true does not mean the Runnable will be processed --
8657 * if the looper is quit before the delivery time of the message
8658 * occurs then the message will be dropped.
8659 */
8660 public boolean postDelayed(Runnable action, long delayMillis) {
8661 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008662 AttachInfo attachInfo = mAttachInfo;
8663 if (attachInfo != null) {
8664 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008665 } else {
8666 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008667 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008668 return true;
8669 }
8670
8671 return handler.postDelayed(action, delayMillis);
8672 }
8673
8674 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008675 * <p>Removes the specified Runnable from the message queue.</p>
8676 *
8677 * <p>This method can be invoked from outside of the UI thread
8678 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008679 *
8680 * @param action The Runnable to remove from the message handling queue
8681 *
8682 * @return true if this view could ask the Handler to remove the Runnable,
8683 * false otherwise. When the returned value is true, the Runnable
8684 * may or may not have been actually removed from the message queue
8685 * (for instance, if the Runnable was not in the queue already.)
8686 */
8687 public boolean removeCallbacks(Runnable action) {
8688 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008689 AttachInfo attachInfo = mAttachInfo;
8690 if (attachInfo != null) {
8691 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008692 } else {
8693 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008694 ViewRootImpl.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008695 return true;
8696 }
8697
8698 handler.removeCallbacks(action);
8699 return true;
8700 }
8701
8702 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008703 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
8704 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008705 *
Romain Guye63a4f32011-08-11 11:33:31 -07008706 * <p>This method can be invoked from outside of the UI thread
8707 * only when this View is attached to a window.</p>
8708 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008709 * @see #invalidate()
8710 */
8711 public void postInvalidate() {
8712 postInvalidateDelayed(0);
8713 }
8714
8715 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008716 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8717 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
8718 *
8719 * <p>This method can be invoked from outside of the UI thread
8720 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008721 *
8722 * @param left The left coordinate of the rectangle to invalidate.
8723 * @param top The top coordinate of the rectangle to invalidate.
8724 * @param right The right coordinate of the rectangle to invalidate.
8725 * @param bottom The bottom coordinate of the rectangle to invalidate.
8726 *
8727 * @see #invalidate(int, int, int, int)
8728 * @see #invalidate(Rect)
8729 */
8730 public void postInvalidate(int left, int top, int right, int bottom) {
8731 postInvalidateDelayed(0, left, top, right, bottom);
8732 }
8733
8734 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008735 * <p>Cause an invalidate to happen on a subsequent cycle through the event
8736 * loop. Waits for the specified amount of time.</p>
8737 *
8738 * <p>This method can be invoked from outside of the UI thread
8739 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008740 *
8741 * @param delayMilliseconds the duration in milliseconds to delay the
8742 * invalidation by
8743 */
8744 public void postInvalidateDelayed(long delayMilliseconds) {
8745 // We try only with the AttachInfo because there's no point in invalidating
8746 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008747 AttachInfo attachInfo = mAttachInfo;
8748 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008749 Message msg = Message.obtain();
8750 msg.what = AttachInfo.INVALIDATE_MSG;
8751 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008752 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008753 }
8754 }
8755
8756 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008757 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8758 * through the event loop. Waits for the specified amount of time.</p>
8759 *
8760 * <p>This method can be invoked from outside of the UI thread
8761 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008762 *
8763 * @param delayMilliseconds the duration in milliseconds to delay the
8764 * invalidation by
8765 * @param left The left coordinate of the rectangle to invalidate.
8766 * @param top The top coordinate of the rectangle to invalidate.
8767 * @param right The right coordinate of the rectangle to invalidate.
8768 * @param bottom The bottom coordinate of the rectangle to invalidate.
8769 */
8770 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8771 int right, int bottom) {
8772
8773 // We try only with the AttachInfo because there's no point in invalidating
8774 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008775 AttachInfo attachInfo = mAttachInfo;
8776 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008777 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8778 info.target = this;
8779 info.left = left;
8780 info.top = top;
8781 info.right = right;
8782 info.bottom = bottom;
8783
8784 final Message msg = Message.obtain();
8785 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8786 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008787 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008788 }
8789 }
8790
8791 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008792 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8793 * This event is sent at most once every
8794 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8795 */
8796 private void postSendViewScrolledAccessibilityEventCallback() {
8797 if (mSendViewScrolledAccessibilityEvent == null) {
8798 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8799 }
8800 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8801 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8802 postDelayed(mSendViewScrolledAccessibilityEvent,
8803 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8804 }
8805 }
8806
8807 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008808 * Called by a parent to request that a child update its values for mScrollX
8809 * and mScrollY if necessary. This will typically be done if the child is
8810 * animating a scroll using a {@link android.widget.Scroller Scroller}
8811 * object.
8812 */
8813 public void computeScroll() {
8814 }
8815
8816 /**
8817 * <p>Indicate whether the horizontal edges are faded when the view is
8818 * scrolled horizontally.</p>
8819 *
8820 * @return true if the horizontal edges should are faded on scroll, false
8821 * otherwise
8822 *
8823 * @see #setHorizontalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008824 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008825 */
8826 public boolean isHorizontalFadingEdgeEnabled() {
8827 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8828 }
8829
8830 /**
8831 * <p>Define whether the horizontal edges should be faded when this view
8832 * is scrolled horizontally.</p>
8833 *
8834 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8835 * be faded when the view is scrolled
8836 * horizontally
8837 *
8838 * @see #isHorizontalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008839 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008840 */
8841 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8842 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8843 if (horizontalFadingEdgeEnabled) {
8844 initScrollCache();
8845 }
8846
8847 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8848 }
8849 }
8850
8851 /**
8852 * <p>Indicate whether the vertical edges are faded when the view is
8853 * scrolled horizontally.</p>
8854 *
8855 * @return true if the vertical edges should are faded on scroll, false
8856 * otherwise
8857 *
8858 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008859 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008860 */
8861 public boolean isVerticalFadingEdgeEnabled() {
8862 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8863 }
8864
8865 /**
8866 * <p>Define whether the vertical edges should be faded when this view
8867 * is scrolled vertically.</p>
8868 *
8869 * @param verticalFadingEdgeEnabled true if the vertical edges should
8870 * be faded when the view is scrolled
8871 * vertically
8872 *
8873 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008874 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008875 */
8876 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8877 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8878 if (verticalFadingEdgeEnabled) {
8879 initScrollCache();
8880 }
8881
8882 mViewFlags ^= FADING_EDGE_VERTICAL;
8883 }
8884 }
8885
8886 /**
8887 * Returns the strength, or intensity, of the top faded edge. The strength is
8888 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8889 * returns 0.0 or 1.0 but no value in between.
8890 *
8891 * Subclasses should override this method to provide a smoother fade transition
8892 * when scrolling occurs.
8893 *
8894 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8895 */
8896 protected float getTopFadingEdgeStrength() {
8897 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8898 }
8899
8900 /**
8901 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8902 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8903 * returns 0.0 or 1.0 but no value in between.
8904 *
8905 * Subclasses should override this method to provide a smoother fade transition
8906 * when scrolling occurs.
8907 *
8908 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8909 */
8910 protected float getBottomFadingEdgeStrength() {
8911 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8912 computeVerticalScrollRange() ? 1.0f : 0.0f;
8913 }
8914
8915 /**
8916 * Returns the strength, or intensity, of the left faded edge. The strength is
8917 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8918 * returns 0.0 or 1.0 but no value in between.
8919 *
8920 * Subclasses should override this method to provide a smoother fade transition
8921 * when scrolling occurs.
8922 *
8923 * @return the intensity of the left fade as a float between 0.0f and 1.0f
8924 */
8925 protected float getLeftFadingEdgeStrength() {
8926 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
8927 }
8928
8929 /**
8930 * Returns the strength, or intensity, of the right faded edge. The strength is
8931 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8932 * returns 0.0 or 1.0 but no value in between.
8933 *
8934 * Subclasses should override this method to provide a smoother fade transition
8935 * when scrolling occurs.
8936 *
8937 * @return the intensity of the right fade as a float between 0.0f and 1.0f
8938 */
8939 protected float getRightFadingEdgeStrength() {
8940 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
8941 computeHorizontalScrollRange() ? 1.0f : 0.0f;
8942 }
8943
8944 /**
8945 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
8946 * scrollbar is not drawn by default.</p>
8947 *
8948 * @return true if the horizontal scrollbar should be painted, false
8949 * otherwise
8950 *
8951 * @see #setHorizontalScrollBarEnabled(boolean)
8952 */
8953 public boolean isHorizontalScrollBarEnabled() {
8954 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8955 }
8956
8957 /**
8958 * <p>Define whether the horizontal scrollbar should be drawn or not. The
8959 * scrollbar is not drawn by default.</p>
8960 *
8961 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
8962 * be painted
8963 *
8964 * @see #isHorizontalScrollBarEnabled()
8965 */
8966 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
8967 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
8968 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008969 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008970 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008971 }
8972 }
8973
8974 /**
8975 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
8976 * scrollbar is not drawn by default.</p>
8977 *
8978 * @return true if the vertical scrollbar should be painted, false
8979 * otherwise
8980 *
8981 * @see #setVerticalScrollBarEnabled(boolean)
8982 */
8983 public boolean isVerticalScrollBarEnabled() {
8984 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
8985 }
8986
8987 /**
8988 * <p>Define whether the vertical scrollbar should be drawn or not. The
8989 * scrollbar is not drawn by default.</p>
8990 *
8991 * @param verticalScrollBarEnabled true if the vertical scrollbar should
8992 * be painted
8993 *
8994 * @see #isVerticalScrollBarEnabled()
8995 */
8996 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
8997 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
8998 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008999 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009000 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009001 }
9002 }
9003
Adam Powell20232d02010-12-08 21:08:53 -08009004 /**
9005 * @hide
9006 */
9007 protected void recomputePadding() {
9008 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009009 }
Joe Malin32736f02011-01-19 16:14:20 -08009010
Mike Cleronfe81d382009-09-28 14:22:16 -07009011 /**
9012 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009013 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009014 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009015 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009016 */
9017 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9018 initScrollCache();
9019 final ScrollabilityCache scrollabilityCache = mScrollCache;
9020 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009021 if (fadeScrollbars) {
9022 scrollabilityCache.state = ScrollabilityCache.OFF;
9023 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009024 scrollabilityCache.state = ScrollabilityCache.ON;
9025 }
9026 }
Joe Malin32736f02011-01-19 16:14:20 -08009027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009028 /**
Joe Malin32736f02011-01-19 16:14:20 -08009029 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009030 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009031 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009032 * @return true if scrollbar fading is enabled
9033 */
9034 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009035 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009036 }
Joe Malin32736f02011-01-19 16:14:20 -08009037
Mike Cleron52f0a642009-09-28 18:21:37 -07009038 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009039 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9040 * inset. When inset, they add to the padding of the view. And the scrollbars
9041 * can be drawn inside the padding area or on the edge of the view. For example,
9042 * if a view has a background drawable and you want to draw the scrollbars
9043 * inside the padding specified by the drawable, you can use
9044 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9045 * appear at the edge of the view, ignoring the padding, then you can use
9046 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9047 * @param style the style of the scrollbars. Should be one of
9048 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9049 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9050 * @see #SCROLLBARS_INSIDE_OVERLAY
9051 * @see #SCROLLBARS_INSIDE_INSET
9052 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9053 * @see #SCROLLBARS_OUTSIDE_INSET
9054 */
9055 public void setScrollBarStyle(int style) {
9056 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9057 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009058 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009059 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009060 }
9061 }
9062
9063 /**
9064 * <p>Returns the current scrollbar style.</p>
9065 * @return the current scrollbar style
9066 * @see #SCROLLBARS_INSIDE_OVERLAY
9067 * @see #SCROLLBARS_INSIDE_INSET
9068 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9069 * @see #SCROLLBARS_OUTSIDE_INSET
9070 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009071 @ViewDebug.ExportedProperty(mapping = {
9072 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9073 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9074 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9075 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9076 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009077 public int getScrollBarStyle() {
9078 return mViewFlags & SCROLLBARS_STYLE_MASK;
9079 }
9080
9081 /**
9082 * <p>Compute the horizontal range that the horizontal scrollbar
9083 * represents.</p>
9084 *
9085 * <p>The range is expressed in arbitrary units that must be the same as the
9086 * units used by {@link #computeHorizontalScrollExtent()} and
9087 * {@link #computeHorizontalScrollOffset()}.</p>
9088 *
9089 * <p>The default range is the drawing width of this view.</p>
9090 *
9091 * @return the total horizontal range represented by the horizontal
9092 * scrollbar
9093 *
9094 * @see #computeHorizontalScrollExtent()
9095 * @see #computeHorizontalScrollOffset()
9096 * @see android.widget.ScrollBarDrawable
9097 */
9098 protected int computeHorizontalScrollRange() {
9099 return getWidth();
9100 }
9101
9102 /**
9103 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9104 * within the horizontal range. This value is used to compute the position
9105 * of the thumb within the scrollbar's track.</p>
9106 *
9107 * <p>The range is expressed in arbitrary units that must be the same as the
9108 * units used by {@link #computeHorizontalScrollRange()} and
9109 * {@link #computeHorizontalScrollExtent()}.</p>
9110 *
9111 * <p>The default offset is the scroll offset of this view.</p>
9112 *
9113 * @return the horizontal offset of the scrollbar's thumb
9114 *
9115 * @see #computeHorizontalScrollRange()
9116 * @see #computeHorizontalScrollExtent()
9117 * @see android.widget.ScrollBarDrawable
9118 */
9119 protected int computeHorizontalScrollOffset() {
9120 return mScrollX;
9121 }
9122
9123 /**
9124 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9125 * within the horizontal range. This value is used to compute the length
9126 * of the thumb within the scrollbar's track.</p>
9127 *
9128 * <p>The range is expressed in arbitrary units that must be the same as the
9129 * units used by {@link #computeHorizontalScrollRange()} and
9130 * {@link #computeHorizontalScrollOffset()}.</p>
9131 *
9132 * <p>The default extent is the drawing width of this view.</p>
9133 *
9134 * @return the horizontal extent of the scrollbar's thumb
9135 *
9136 * @see #computeHorizontalScrollRange()
9137 * @see #computeHorizontalScrollOffset()
9138 * @see android.widget.ScrollBarDrawable
9139 */
9140 protected int computeHorizontalScrollExtent() {
9141 return getWidth();
9142 }
9143
9144 /**
9145 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9146 *
9147 * <p>The range is expressed in arbitrary units that must be the same as the
9148 * units used by {@link #computeVerticalScrollExtent()} and
9149 * {@link #computeVerticalScrollOffset()}.</p>
9150 *
9151 * @return the total vertical range represented by the vertical scrollbar
9152 *
9153 * <p>The default range is the drawing height of this view.</p>
9154 *
9155 * @see #computeVerticalScrollExtent()
9156 * @see #computeVerticalScrollOffset()
9157 * @see android.widget.ScrollBarDrawable
9158 */
9159 protected int computeVerticalScrollRange() {
9160 return getHeight();
9161 }
9162
9163 /**
9164 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9165 * within the horizontal range. This value is used to compute the position
9166 * of the thumb within the scrollbar's track.</p>
9167 *
9168 * <p>The range is expressed in arbitrary units that must be the same as the
9169 * units used by {@link #computeVerticalScrollRange()} and
9170 * {@link #computeVerticalScrollExtent()}.</p>
9171 *
9172 * <p>The default offset is the scroll offset of this view.</p>
9173 *
9174 * @return the vertical offset of the scrollbar's thumb
9175 *
9176 * @see #computeVerticalScrollRange()
9177 * @see #computeVerticalScrollExtent()
9178 * @see android.widget.ScrollBarDrawable
9179 */
9180 protected int computeVerticalScrollOffset() {
9181 return mScrollY;
9182 }
9183
9184 /**
9185 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9186 * within the vertical range. This value is used to compute the length
9187 * of the thumb within the scrollbar's track.</p>
9188 *
9189 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009190 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009191 * {@link #computeVerticalScrollOffset()}.</p>
9192 *
9193 * <p>The default extent is the drawing height of this view.</p>
9194 *
9195 * @return the vertical extent of the scrollbar's thumb
9196 *
9197 * @see #computeVerticalScrollRange()
9198 * @see #computeVerticalScrollOffset()
9199 * @see android.widget.ScrollBarDrawable
9200 */
9201 protected int computeVerticalScrollExtent() {
9202 return getHeight();
9203 }
9204
9205 /**
Adam Powell69159442011-06-13 17:53:06 -07009206 * Check if this view can be scrolled horizontally in a certain direction.
9207 *
9208 * @param direction Negative to check scrolling left, positive to check scrolling right.
9209 * @return true if this view can be scrolled in the specified direction, false otherwise.
9210 */
9211 public boolean canScrollHorizontally(int direction) {
9212 final int offset = computeHorizontalScrollOffset();
9213 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9214 if (range == 0) return false;
9215 if (direction < 0) {
9216 return offset > 0;
9217 } else {
9218 return offset < range - 1;
9219 }
9220 }
9221
9222 /**
9223 * Check if this view can be scrolled vertically in a certain direction.
9224 *
9225 * @param direction Negative to check scrolling up, positive to check scrolling down.
9226 * @return true if this view can be scrolled in the specified direction, false otherwise.
9227 */
9228 public boolean canScrollVertically(int direction) {
9229 final int offset = computeVerticalScrollOffset();
9230 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9231 if (range == 0) return false;
9232 if (direction < 0) {
9233 return offset > 0;
9234 } else {
9235 return offset < range - 1;
9236 }
9237 }
9238
9239 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009240 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9241 * scrollbars are painted only if they have been awakened first.</p>
9242 *
9243 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009244 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009245 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009246 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009247 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009248 // scrollbars are drawn only when the animation is running
9249 final ScrollabilityCache cache = mScrollCache;
9250 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009251
Mike Cleronf116bf82009-09-27 19:14:12 -07009252 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009253
Mike Cleronf116bf82009-09-27 19:14:12 -07009254 if (state == ScrollabilityCache.OFF) {
9255 return;
9256 }
Joe Malin32736f02011-01-19 16:14:20 -08009257
Mike Cleronf116bf82009-09-27 19:14:12 -07009258 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009259
Mike Cleronf116bf82009-09-27 19:14:12 -07009260 if (state == ScrollabilityCache.FADING) {
9261 // We're fading -- get our fade interpolation
9262 if (cache.interpolatorValues == null) {
9263 cache.interpolatorValues = new float[1];
9264 }
Joe Malin32736f02011-01-19 16:14:20 -08009265
Mike Cleronf116bf82009-09-27 19:14:12 -07009266 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009267
Mike Cleronf116bf82009-09-27 19:14:12 -07009268 // Stops the animation if we're done
9269 if (cache.scrollBarInterpolator.timeToValues(values) ==
9270 Interpolator.Result.FREEZE_END) {
9271 cache.state = ScrollabilityCache.OFF;
9272 } else {
9273 cache.scrollBar.setAlpha(Math.round(values[0]));
9274 }
Joe Malin32736f02011-01-19 16:14:20 -08009275
9276 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009277 // drawing. We only want this when we're fading so that
9278 // we prevent excessive redraws
9279 invalidate = true;
9280 } else {
9281 // We're just on -- but we may have been fading before so
9282 // reset alpha
9283 cache.scrollBar.setAlpha(255);
9284 }
9285
Joe Malin32736f02011-01-19 16:14:20 -08009286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009287 final int viewFlags = mViewFlags;
9288
9289 final boolean drawHorizontalScrollBar =
9290 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9291 final boolean drawVerticalScrollBar =
9292 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9293 && !isVerticalScrollBarHidden();
9294
9295 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9296 final int width = mRight - mLeft;
9297 final int height = mBottom - mTop;
9298
9299 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009300
Mike Reede8853fc2009-09-04 14:01:48 -04009301 final int scrollX = mScrollX;
9302 final int scrollY = mScrollY;
9303 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9304
Mike Cleronf116bf82009-09-27 19:14:12 -07009305 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009307 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009308 int size = scrollBar.getSize(false);
9309 if (size <= 0) {
9310 size = cache.scrollBarSize;
9311 }
9312
Mike Cleronf116bf82009-09-27 19:14:12 -07009313 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009314 computeHorizontalScrollOffset(),
9315 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009316 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009317 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009318 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009319 left = scrollX + (mPaddingLeft & inside);
9320 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9321 bottom = top + size;
9322 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9323 if (invalidate) {
9324 invalidate(left, top, right, bottom);
9325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009326 }
9327
9328 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009329 int size = scrollBar.getSize(true);
9330 if (size <= 0) {
9331 size = cache.scrollBarSize;
9332 }
9333
Mike Reede8853fc2009-09-04 14:01:48 -04009334 scrollBar.setParameters(computeVerticalScrollRange(),
9335 computeVerticalScrollOffset(),
9336 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009337 switch (mVerticalScrollbarPosition) {
9338 default:
9339 case SCROLLBAR_POSITION_DEFAULT:
9340 case SCROLLBAR_POSITION_RIGHT:
9341 left = scrollX + width - size - (mUserPaddingRight & inside);
9342 break;
9343 case SCROLLBAR_POSITION_LEFT:
9344 left = scrollX + (mUserPaddingLeft & inside);
9345 break;
9346 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009347 top = scrollY + (mPaddingTop & inside);
9348 right = left + size;
9349 bottom = scrollY + height - (mUserPaddingBottom & inside);
9350 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9351 if (invalidate) {
9352 invalidate(left, top, right, bottom);
9353 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009354 }
9355 }
9356 }
9357 }
Romain Guy8506ab42009-06-11 17:35:47 -07009358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009359 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009360 * 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 -08009361 * FastScroller is visible.
9362 * @return whether to temporarily hide the vertical scrollbar
9363 * @hide
9364 */
9365 protected boolean isVerticalScrollBarHidden() {
9366 return false;
9367 }
9368
9369 /**
9370 * <p>Draw the horizontal scrollbar if
9371 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9372 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009373 * @param canvas the canvas on which to draw the scrollbar
9374 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009375 *
9376 * @see #isHorizontalScrollBarEnabled()
9377 * @see #computeHorizontalScrollRange()
9378 * @see #computeHorizontalScrollExtent()
9379 * @see #computeHorizontalScrollOffset()
9380 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009381 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009382 */
Romain Guy8fb95422010-08-17 18:38:51 -07009383 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9384 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009385 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009386 scrollBar.draw(canvas);
9387 }
Mike Reede8853fc2009-09-04 14:01:48 -04009388
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009389 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009390 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9391 * returns true.</p>
9392 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009393 * @param canvas the canvas on which to draw the scrollbar
9394 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009395 *
9396 * @see #isVerticalScrollBarEnabled()
9397 * @see #computeVerticalScrollRange()
9398 * @see #computeVerticalScrollExtent()
9399 * @see #computeVerticalScrollOffset()
9400 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009401 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009402 */
Romain Guy8fb95422010-08-17 18:38:51 -07009403 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9404 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009405 scrollBar.setBounds(l, t, r, b);
9406 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009407 }
9408
9409 /**
9410 * Implement this to do your drawing.
9411 *
9412 * @param canvas the canvas on which the background will be drawn
9413 */
9414 protected void onDraw(Canvas canvas) {
9415 }
9416
9417 /*
9418 * Caller is responsible for calling requestLayout if necessary.
9419 * (This allows addViewInLayout to not request a new layout.)
9420 */
9421 void assignParent(ViewParent parent) {
9422 if (mParent == null) {
9423 mParent = parent;
9424 } else if (parent == null) {
9425 mParent = null;
9426 } else {
9427 throw new RuntimeException("view " + this + " being added, but"
9428 + " it already has a parent");
9429 }
9430 }
9431
9432 /**
9433 * This is called when the view is attached to a window. At this point it
9434 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009435 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9436 * however it may be called any time before the first onDraw -- including
9437 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009438 *
9439 * @see #onDetachedFromWindow()
9440 */
9441 protected void onAttachedToWindow() {
9442 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9443 mParent.requestTransparentRegion(this);
9444 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009445 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9446 initialAwakenScrollBars();
9447 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9448 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009449 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009450 // Order is important here: LayoutDirection MUST be resolved before Padding
9451 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009452 resolveLayoutDirectionIfNeeded();
9453 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009454 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009455 if (isFocused()) {
9456 InputMethodManager imm = InputMethodManager.peekInstance();
9457 imm.focusIn(this);
9458 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009459 }
Cibu Johny86666632010-02-22 13:01:02 -08009460
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009461 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009462 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9463 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009464 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009465 private void resolveLayoutDirectionIfNeeded() {
9466 // Do not resolve if it is not needed
9467 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9468
9469 // Clear any previous layout direction resolution
9470 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9471
Fabrice Di Meglio4b60c302011-08-17 16:56:55 -07009472 // Reset also TextDirection as a change into LayoutDirection may impact the selected
9473 // TextDirectionHeuristic
9474 resetResolvedTextDirection();
9475
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009476 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009477 switch (getLayoutDirection()) {
9478 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009479 // We cannot do the resolution if there is no parent
9480 if (mParent == null) return;
9481
Cibu Johny86666632010-02-22 13:01:02 -08009482 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009483 if (mParent instanceof ViewGroup) {
9484 ViewGroup viewGroup = ((ViewGroup) mParent);
9485
9486 // Check if the parent view group can resolve
9487 if (! viewGroup.canResolveLayoutDirection()) {
9488 return;
9489 }
9490 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9491 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9492 }
Cibu Johny86666632010-02-22 13:01:02 -08009493 }
9494 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009495 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009496 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009497 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009498 case LAYOUT_DIRECTION_LOCALE:
9499 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009500 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009501 }
9502 break;
9503 default:
9504 // Nothing to do, LTR by default
9505 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009506
9507 // Set to resolved
9508 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
9509 }
9510
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009511 /**
9512 * @hide
9513 */
9514 protected void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009515 // If the user specified the absolute padding (either with android:padding or
9516 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9517 // use the default padding or the padding from the background drawable
9518 // (stored at this point in mPadding*)
9519 switch (getResolvedLayoutDirection()) {
9520 case LAYOUT_DIRECTION_RTL:
9521 // Start user padding override Right user padding. Otherwise, if Right user
9522 // padding is not defined, use the default Right padding. If Right user padding
9523 // is defined, just use it.
9524 if (mUserPaddingStart >= 0) {
9525 mUserPaddingRight = mUserPaddingStart;
9526 } else if (mUserPaddingRight < 0) {
9527 mUserPaddingRight = mPaddingRight;
9528 }
9529 if (mUserPaddingEnd >= 0) {
9530 mUserPaddingLeft = mUserPaddingEnd;
9531 } else if (mUserPaddingLeft < 0) {
9532 mUserPaddingLeft = mPaddingLeft;
9533 }
9534 break;
9535 case LAYOUT_DIRECTION_LTR:
9536 default:
9537 // Start user padding override Left user padding. Otherwise, if Left user
9538 // padding is not defined, use the default left padding. If Left user padding
9539 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009540 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009541 mUserPaddingLeft = mUserPaddingStart;
9542 } else if (mUserPaddingLeft < 0) {
9543 mUserPaddingLeft = mPaddingLeft;
9544 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009545 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009546 mUserPaddingRight = mUserPaddingEnd;
9547 } else if (mUserPaddingRight < 0) {
9548 mUserPaddingRight = mPaddingRight;
9549 }
9550 }
9551
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009552 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9553
9554 recomputePadding();
9555 }
9556
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009557 /**
9558 * Return true if layout direction resolution can be done
9559 *
9560 * @hide
9561 */
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009562 protected boolean canResolveLayoutDirection() {
9563 switch (getLayoutDirection()) {
9564 case LAYOUT_DIRECTION_INHERIT:
9565 return (mParent != null);
9566 default:
9567 return true;
9568 }
9569 }
9570
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009571 /**
Doug Feltc0ccf0c2011-06-23 16:13:18 -07009572 * Reset the resolved layout direction.
9573 *
9574 * Subclasses need to override this method to clear cached information that depends on the
9575 * resolved layout direction, or to inform child views that inherit their layout direction.
9576 * Overrides must also call the superclass implementation at the start of their implementation.
9577 *
9578 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009579 */
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009580 protected void resetResolvedLayoutDirection() {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07009581 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009582 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009583 }
9584
9585 /**
9586 * Check if a Locale is corresponding to a RTL script.
9587 *
9588 * @param locale Locale to check
9589 * @return true if a Locale is corresponding to a RTL script.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009590 *
9591 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009592 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009593 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07009594 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
9595 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009596 }
9597
9598 /**
9599 * This is called when the view is detached from a window. At this point it
9600 * no longer has a surface for drawing.
9601 *
9602 * @see #onAttachedToWindow()
9603 */
9604 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009605 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009606
Romain Guya440b002010-02-24 15:57:54 -08009607 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009608 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009609 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009610 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009612 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009613
Romain Guy6d7475d2011-07-27 16:28:21 -07009614 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009615
Chet Haasedaf98e92011-01-10 14:10:36 -08009616 if (mDisplayList != null) {
9617 mDisplayList.invalidate();
9618 }
9619
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009620 if (mAttachInfo != null) {
9621 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009622 }
9623
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009624 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009625
9626 resetResolvedLayoutDirection();
9627 resetResolvedTextDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009628 }
9629
9630 /**
9631 * @return The number of times this view has been attached to a window
9632 */
9633 protected int getWindowAttachCount() {
9634 return mWindowAttachCount;
9635 }
9636
9637 /**
9638 * Retrieve a unique token identifying the window this view is attached to.
9639 * @return Return the window's token for use in
9640 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9641 */
9642 public IBinder getWindowToken() {
9643 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9644 }
9645
9646 /**
9647 * Retrieve a unique token identifying the top-level "real" window of
9648 * the window that this view is attached to. That is, this is like
9649 * {@link #getWindowToken}, except if the window this view in is a panel
9650 * window (attached to another containing window), then the token of
9651 * the containing window is returned instead.
9652 *
9653 * @return Returns the associated window token, either
9654 * {@link #getWindowToken()} or the containing window's token.
9655 */
9656 public IBinder getApplicationWindowToken() {
9657 AttachInfo ai = mAttachInfo;
9658 if (ai != null) {
9659 IBinder appWindowToken = ai.mPanelParentWindowToken;
9660 if (appWindowToken == null) {
9661 appWindowToken = ai.mWindowToken;
9662 }
9663 return appWindowToken;
9664 }
9665 return null;
9666 }
9667
9668 /**
9669 * Retrieve private session object this view hierarchy is using to
9670 * communicate with the window manager.
9671 * @return the session object to communicate with the window manager
9672 */
9673 /*package*/ IWindowSession getWindowSession() {
9674 return mAttachInfo != null ? mAttachInfo.mSession : null;
9675 }
9676
9677 /**
9678 * @param info the {@link android.view.View.AttachInfo} to associated with
9679 * this view
9680 */
9681 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9682 //System.out.println("Attached! " + this);
9683 mAttachInfo = info;
9684 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009685 // We will need to evaluate the drawable state at least once.
9686 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009687 if (mFloatingTreeObserver != null) {
9688 info.mTreeObserver.merge(mFloatingTreeObserver);
9689 mFloatingTreeObserver = null;
9690 }
9691 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9692 mAttachInfo.mScrollContainers.add(this);
9693 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9694 }
9695 performCollectViewAttributes(visibility);
9696 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009697
9698 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9699 mOnAttachStateChangeListeners;
9700 if (listeners != null && listeners.size() > 0) {
9701 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9702 // perform the dispatching. The iterator is a safe guard against listeners that
9703 // could mutate the list by calling the various add/remove methods. This prevents
9704 // the array from being modified while we iterate it.
9705 for (OnAttachStateChangeListener listener : listeners) {
9706 listener.onViewAttachedToWindow(this);
9707 }
9708 }
9709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009710 int vis = info.mWindowVisibility;
9711 if (vis != GONE) {
9712 onWindowVisibilityChanged(vis);
9713 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009714 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9715 // If nobody has evaluated the drawable state yet, then do it now.
9716 refreshDrawableState();
9717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009718 }
9719
9720 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009721 AttachInfo info = mAttachInfo;
9722 if (info != null) {
9723 int vis = info.mWindowVisibility;
9724 if (vis != GONE) {
9725 onWindowVisibilityChanged(GONE);
9726 }
9727 }
9728
9729 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009730
Adam Powell4afd62b2011-02-18 15:02:18 -08009731 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9732 mOnAttachStateChangeListeners;
9733 if (listeners != null && listeners.size() > 0) {
9734 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9735 // perform the dispatching. The iterator is a safe guard against listeners that
9736 // could mutate the list by calling the various add/remove methods. This prevents
9737 // the array from being modified while we iterate it.
9738 for (OnAttachStateChangeListener listener : listeners) {
9739 listener.onViewDetachedFromWindow(this);
9740 }
9741 }
9742
Romain Guy01d5edc2011-01-28 11:28:53 -08009743 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009744 mAttachInfo.mScrollContainers.remove(this);
9745 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9746 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009748 mAttachInfo = null;
9749 }
9750
9751 /**
9752 * Store this view hierarchy's frozen state into the given container.
9753 *
9754 * @param container The SparseArray in which to save the view's state.
9755 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009756 * @see #restoreHierarchyState(android.util.SparseArray)
9757 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9758 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009759 */
9760 public void saveHierarchyState(SparseArray<Parcelable> container) {
9761 dispatchSaveInstanceState(container);
9762 }
9763
9764 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009765 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9766 * this view and its children. May be overridden to modify how freezing happens to a
9767 * 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 -08009768 *
9769 * @param container The SparseArray in which to save the view's state.
9770 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009771 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9772 * @see #saveHierarchyState(android.util.SparseArray)
9773 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009774 */
9775 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9776 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9777 mPrivateFlags &= ~SAVE_STATE_CALLED;
9778 Parcelable state = onSaveInstanceState();
9779 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9780 throw new IllegalStateException(
9781 "Derived class did not call super.onSaveInstanceState()");
9782 }
9783 if (state != null) {
9784 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9785 // + ": " + state);
9786 container.put(mID, state);
9787 }
9788 }
9789 }
9790
9791 /**
9792 * Hook allowing a view to generate a representation of its internal state
9793 * that can later be used to create a new instance with that same state.
9794 * This state should only contain information that is not persistent or can
9795 * not be reconstructed later. For example, you will never store your
9796 * current position on screen because that will be computed again when a
9797 * new instance of the view is placed in its view hierarchy.
9798 * <p>
9799 * Some examples of things you may store here: the current cursor position
9800 * in a text view (but usually not the text itself since that is stored in a
9801 * content provider or other persistent storage), the currently selected
9802 * item in a list view.
9803 *
9804 * @return Returns a Parcelable object containing the view's current dynamic
9805 * state, or null if there is nothing interesting to save. The
9806 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009807 * @see #onRestoreInstanceState(android.os.Parcelable)
9808 * @see #saveHierarchyState(android.util.SparseArray)
9809 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009810 * @see #setSaveEnabled(boolean)
9811 */
9812 protected Parcelable onSaveInstanceState() {
9813 mPrivateFlags |= SAVE_STATE_CALLED;
9814 return BaseSavedState.EMPTY_STATE;
9815 }
9816
9817 /**
9818 * Restore this view hierarchy's frozen state from the given container.
9819 *
9820 * @param container The SparseArray which holds previously frozen states.
9821 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009822 * @see #saveHierarchyState(android.util.SparseArray)
9823 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9824 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009825 */
9826 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9827 dispatchRestoreInstanceState(container);
9828 }
9829
9830 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009831 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9832 * state for this view and its children. May be overridden to modify how restoring
9833 * happens to a view's children; for example, some views may want to not store state
9834 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009835 *
9836 * @param container The SparseArray which holds previously saved state.
9837 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009838 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9839 * @see #restoreHierarchyState(android.util.SparseArray)
9840 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009841 */
9842 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9843 if (mID != NO_ID) {
9844 Parcelable state = container.get(mID);
9845 if (state != null) {
9846 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9847 // + ": " + state);
9848 mPrivateFlags &= ~SAVE_STATE_CALLED;
9849 onRestoreInstanceState(state);
9850 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9851 throw new IllegalStateException(
9852 "Derived class did not call super.onRestoreInstanceState()");
9853 }
9854 }
9855 }
9856 }
9857
9858 /**
9859 * Hook allowing a view to re-apply a representation of its internal state that had previously
9860 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9861 * null state.
9862 *
9863 * @param state The frozen state that had previously been returned by
9864 * {@link #onSaveInstanceState}.
9865 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009866 * @see #onSaveInstanceState()
9867 * @see #restoreHierarchyState(android.util.SparseArray)
9868 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009869 */
9870 protected void onRestoreInstanceState(Parcelable state) {
9871 mPrivateFlags |= SAVE_STATE_CALLED;
9872 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009873 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9874 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009875 + "when two views of different type have the same id in the same hierarchy. "
9876 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009877 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009878 }
9879 }
9880
9881 /**
9882 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9883 *
9884 * @return the drawing start time in milliseconds
9885 */
9886 public long getDrawingTime() {
9887 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9888 }
9889
9890 /**
9891 * <p>Enables or disables the duplication of the parent's state into this view. When
9892 * duplication is enabled, this view gets its drawable state from its parent rather
9893 * than from its own internal properties.</p>
9894 *
9895 * <p>Note: in the current implementation, setting this property to true after the
9896 * view was added to a ViewGroup might have no effect at all. This property should
9897 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
9898 *
9899 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
9900 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009901 *
Gilles Debunnefb817032011-01-13 13:52:49 -08009902 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
9903 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009904 *
9905 * @param enabled True to enable duplication of the parent's drawable state, false
9906 * to disable it.
9907 *
9908 * @see #getDrawableState()
9909 * @see #isDuplicateParentStateEnabled()
9910 */
9911 public void setDuplicateParentStateEnabled(boolean enabled) {
9912 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
9913 }
9914
9915 /**
9916 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
9917 *
9918 * @return True if this view's drawable state is duplicated from the parent,
9919 * false otherwise
9920 *
9921 * @see #getDrawableState()
9922 * @see #setDuplicateParentStateEnabled(boolean)
9923 */
9924 public boolean isDuplicateParentStateEnabled() {
9925 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
9926 }
9927
9928 /**
Romain Guy171c5922011-01-06 10:04:23 -08009929 * <p>Specifies the type of layer backing this view. The layer can be
9930 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
9931 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009932 *
Romain Guy171c5922011-01-06 10:04:23 -08009933 * <p>A layer is associated with an optional {@link android.graphics.Paint}
9934 * instance that controls how the layer is composed on screen. The following
9935 * properties of the paint are taken into account when composing the layer:</p>
9936 * <ul>
9937 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
9938 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
9939 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
9940 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08009941 *
Romain Guy171c5922011-01-06 10:04:23 -08009942 * <p>If this view has an alpha value set to < 1.0 by calling
9943 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
9944 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
9945 * equivalent to setting a hardware layer on this view and providing a paint with
9946 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08009947 *
Romain Guy171c5922011-01-06 10:04:23 -08009948 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
9949 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
9950 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009951 *
Romain Guy171c5922011-01-06 10:04:23 -08009952 * @param layerType The ype of layer to use with this view, must be one of
9953 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9954 * {@link #LAYER_TYPE_HARDWARE}
9955 * @param paint The paint used to compose the layer. This argument is optional
9956 * and can be null. It is ignored when the layer type is
9957 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08009958 *
9959 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08009960 * @see #LAYER_TYPE_NONE
9961 * @see #LAYER_TYPE_SOFTWARE
9962 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08009963 * @see #setAlpha(float)
9964 *
Romain Guy171c5922011-01-06 10:04:23 -08009965 * @attr ref android.R.styleable#View_layerType
9966 */
9967 public void setLayerType(int layerType, Paint paint) {
9968 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08009969 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08009970 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
9971 }
Chet Haasedaf98e92011-01-10 14:10:36 -08009972
Romain Guyd6cd5722011-01-17 14:42:41 -08009973 if (layerType == mLayerType) {
9974 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
9975 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009976 invalidateParentCaches();
9977 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08009978 }
9979 return;
9980 }
Romain Guy171c5922011-01-06 10:04:23 -08009981
9982 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08009983 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -07009984 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -07009985 destroyLayer();
Chet Haase6f33e812011-05-17 12:42:19 -07009986 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -08009987 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -07009988 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009989 break;
Romain Guy6c319ca2011-01-11 14:29:25 -08009990 default:
9991 break;
Romain Guy171c5922011-01-06 10:04:23 -08009992 }
9993
9994 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08009995 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
9996 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
9997 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08009998
Romain Guy0fd89bf2011-01-26 15:41:30 -08009999 invalidateParentCaches();
10000 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010001 }
10002
10003 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010004 * Indicates whether this view has a static layer. A view with layer type
10005 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10006 * dynamic.
10007 */
10008 boolean hasStaticLayer() {
10009 return mLayerType == LAYER_TYPE_NONE;
10010 }
10011
10012 /**
Romain Guy171c5922011-01-06 10:04:23 -080010013 * Indicates what type of layer is currently associated with this view. By default
10014 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10015 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10016 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010017 *
Romain Guy171c5922011-01-06 10:04:23 -080010018 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10019 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010020 *
10021 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010022 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010023 * @see #LAYER_TYPE_NONE
10024 * @see #LAYER_TYPE_SOFTWARE
10025 * @see #LAYER_TYPE_HARDWARE
10026 */
10027 public int getLayerType() {
10028 return mLayerType;
10029 }
Joe Malin32736f02011-01-19 16:14:20 -080010030
Romain Guy6c319ca2011-01-11 14:29:25 -080010031 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010032 * Forces this view's layer to be created and this view to be rendered
10033 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10034 * invoking this method will have no effect.
10035 *
10036 * This method can for instance be used to render a view into its layer before
10037 * starting an animation. If this view is complex, rendering into the layer
10038 * before starting the animation will avoid skipping frames.
10039 *
10040 * @throws IllegalStateException If this view is not attached to a window
10041 *
10042 * @see #setLayerType(int, android.graphics.Paint)
10043 */
10044 public void buildLayer() {
10045 if (mLayerType == LAYER_TYPE_NONE) return;
10046
10047 if (mAttachInfo == null) {
10048 throw new IllegalStateException("This view must be attached to a window first");
10049 }
10050
10051 switch (mLayerType) {
10052 case LAYER_TYPE_HARDWARE:
10053 getHardwareLayer();
10054 break;
10055 case LAYER_TYPE_SOFTWARE:
10056 buildDrawingCache(true);
10057 break;
10058 }
10059 }
10060
10061 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010062 * <p>Returns a hardware layer that can be used to draw this view again
10063 * without executing its draw method.</p>
10064 *
10065 * @return A HardwareLayer ready to render, or null if an error occurred.
10066 */
Romain Guy5e7f7662011-01-24 22:35:56 -080010067 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010068 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10069 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010070 return null;
10071 }
10072
10073 final int width = mRight - mLeft;
10074 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010075
Romain Guy6c319ca2011-01-11 14:29:25 -080010076 if (width == 0 || height == 0) {
10077 return null;
10078 }
10079
10080 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10081 if (mHardwareLayer == null) {
10082 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10083 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -080010084 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010085 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10086 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -080010087 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010088 }
10089
Romain Guy59a12ca2011-06-09 17:48:21 -070010090 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -080010091 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
10092 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010093 try {
10094 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080010095 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -080010096 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010097
Chet Haase88172fe2011-03-07 17:36:33 -080010098 final int restoreCount = canvas.save();
10099
Romain Guy6c319ca2011-01-11 14:29:25 -080010100 computeScroll();
10101 canvas.translate(-mScrollX, -mScrollY);
10102
Romain Guy6c319ca2011-01-11 14:29:25 -080010103 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -080010104
Romain Guy6c319ca2011-01-11 14:29:25 -080010105 // Fast path for layouts with no backgrounds
10106 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10107 mPrivateFlags &= ~DIRTY_MASK;
10108 dispatchDraw(canvas);
10109 } else {
10110 draw(canvas);
10111 }
Joe Malin32736f02011-01-19 16:14:20 -080010112
Chet Haase88172fe2011-03-07 17:36:33 -080010113 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -080010114 } finally {
10115 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -080010116 mHardwareLayer.end(currentCanvas);
10117 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010118 }
10119 }
10120
10121 return mHardwareLayer;
10122 }
Romain Guy171c5922011-01-06 10:04:23 -080010123
Romain Guy65b345f2011-07-27 18:51:50 -070010124 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -070010125 if (mHardwareLayer != null) {
10126 mHardwareLayer.destroy();
10127 mHardwareLayer = null;
Romain Guy65b345f2011-07-27 18:51:50 -070010128 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010129 }
Romain Guy65b345f2011-07-27 18:51:50 -070010130 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010131 }
10132
Romain Guy171c5922011-01-06 10:04:23 -080010133 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010134 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10135 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10136 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10137 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10138 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10139 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010140 *
Romain Guy171c5922011-01-06 10:04:23 -080010141 * <p>Enabling the drawing cache is similar to
10142 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010143 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10144 * drawing cache has no effect on rendering because the system uses a different mechanism
10145 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10146 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10147 * for information on how to enable software and hardware layers.</p>
10148 *
10149 * <p>This API can be used to manually generate
10150 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10151 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010152 *
10153 * @param enabled true to enable the drawing cache, false otherwise
10154 *
10155 * @see #isDrawingCacheEnabled()
10156 * @see #getDrawingCache()
10157 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010158 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010159 */
10160 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010161 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010162 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10163 }
10164
10165 /**
10166 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10167 *
10168 * @return true if the drawing cache is enabled
10169 *
10170 * @see #setDrawingCacheEnabled(boolean)
10171 * @see #getDrawingCache()
10172 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010173 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010174 public boolean isDrawingCacheEnabled() {
10175 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10176 }
10177
10178 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010179 * Debugging utility which recursively outputs the dirty state of a view and its
10180 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010181 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010182 * @hide
10183 */
Romain Guy676b1732011-02-14 14:45:33 -080010184 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010185 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10186 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10187 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10188 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10189 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10190 if (clear) {
10191 mPrivateFlags &= clearMask;
10192 }
10193 if (this instanceof ViewGroup) {
10194 ViewGroup parent = (ViewGroup) this;
10195 final int count = parent.getChildCount();
10196 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010197 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010198 child.outputDirtyFlags(indent + " ", clear, clearMask);
10199 }
10200 }
10201 }
10202
10203 /**
10204 * This method is used by ViewGroup to cause its children to restore or recreate their
10205 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10206 * to recreate its own display list, which would happen if it went through the normal
10207 * draw/dispatchDraw mechanisms.
10208 *
10209 * @hide
10210 */
10211 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010212
10213 /**
10214 * A view that is not attached or hardware accelerated cannot create a display list.
10215 * This method checks these conditions and returns the appropriate result.
10216 *
10217 * @return true if view has the ability to create a display list, false otherwise.
10218 *
10219 * @hide
10220 */
10221 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010222 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010223 }
Joe Malin32736f02011-01-19 16:14:20 -080010224
Chet Haasedaf98e92011-01-10 14:10:36 -080010225 /**
Romain Guyb051e892010-09-28 19:09:36 -070010226 * <p>Returns a display list that can be used to draw this view again
10227 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010228 *
Romain Guyb051e892010-09-28 19:09:36 -070010229 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010230 *
10231 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010232 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010233 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -080010234 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -070010235 return null;
10236 }
10237
Chet Haasedaf98e92011-01-10 14:10:36 -080010238 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10239 mDisplayList == null || !mDisplayList.isValid() ||
10240 mRecreateDisplayList)) {
10241 // Don't need to recreate the display list, just need to tell our
10242 // children to restore/recreate theirs
10243 if (mDisplayList != null && mDisplayList.isValid() &&
10244 !mRecreateDisplayList) {
10245 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10246 mPrivateFlags &= ~DIRTY_MASK;
10247 dispatchGetDisplayList();
10248
10249 return mDisplayList;
10250 }
10251
10252 // If we got here, we're recreating it. Mark it as such to ensure that
10253 // we copy in child display lists into ours in drawChild()
10254 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -080010255 if (mDisplayList == null) {
Jeff Brown162a0212011-07-21 17:02:54 -070010256 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList();
Chet Haasedaf98e92011-01-10 14:10:36 -080010257 // If we're creating a new display list, make sure our parent gets invalidated
10258 // since they will need to recreate their display list to account for this
10259 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -080010260 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -080010261 }
Romain Guyb051e892010-09-28 19:09:36 -070010262
10263 final HardwareCanvas canvas = mDisplayList.start();
Romain Guye080af32011-09-08 15:03:39 -070010264 int restoreCount = 0;
Romain Guyb051e892010-09-28 19:09:36 -070010265 try {
10266 int width = mRight - mLeft;
10267 int height = mBottom - mTop;
10268
10269 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -080010270 // The dirty rect should always be null for a display list
10271 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -070010272
Chet Haasedaf98e92011-01-10 14:10:36 -080010273 computeScroll();
Romain Guye080af32011-09-08 15:03:39 -070010274
10275 restoreCount = canvas.save();
Chet Haasedaf98e92011-01-10 14:10:36 -080010276 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010277 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -070010278 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -080010279
Romain Guyb051e892010-09-28 19:09:36 -070010280 // Fast path for layouts with no backgrounds
10281 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -070010282 dispatchDraw(canvas);
10283 } else {
10284 draw(canvas);
10285 }
Romain Guyb051e892010-09-28 19:09:36 -070010286 } finally {
Romain Guye080af32011-09-08 15:03:39 -070010287 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -070010288 canvas.onPostDraw();
10289
10290 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -070010291 }
Chet Haase77785f92011-01-25 23:22:09 -080010292 } else {
10293 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10294 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -070010295 }
10296
10297 return mDisplayList;
10298 }
10299
10300 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010301 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010302 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010303 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010304 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010305 * @see #getDrawingCache(boolean)
10306 */
10307 public Bitmap getDrawingCache() {
10308 return getDrawingCache(false);
10309 }
10310
10311 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010312 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10313 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10314 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10315 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10316 * request the drawing cache by calling this method and draw it on screen if the
10317 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010318 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010319 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10320 * this method will create a bitmap of the same size as this view. Because this bitmap
10321 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10322 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10323 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10324 * size than the view. This implies that your application must be able to handle this
10325 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010326 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010327 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10328 * the current density of the screen when the application is in compatibility
10329 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010330 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010331 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010332 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010333 * @see #setDrawingCacheEnabled(boolean)
10334 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010335 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010336 * @see #destroyDrawingCache()
10337 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010338 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010339 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10340 return null;
10341 }
10342 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010343 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010344 }
Romain Guy02890fd2010-08-06 17:58:44 -070010345 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010346 }
10347
10348 /**
10349 * <p>Frees the resources used by the drawing cache. If you call
10350 * {@link #buildDrawingCache()} manually without calling
10351 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10352 * should cleanup the cache with this method afterwards.</p>
10353 *
10354 * @see #setDrawingCacheEnabled(boolean)
10355 * @see #buildDrawingCache()
10356 * @see #getDrawingCache()
10357 */
10358 public void destroyDrawingCache() {
10359 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010360 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010361 mDrawingCache = null;
10362 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010363 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010364 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010365 mUnscaledDrawingCache = null;
10366 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010367 }
10368
10369 /**
10370 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070010371 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010372 * view will always be drawn on top of a solid color.
10373 *
10374 * @param color The background color to use for the drawing cache's bitmap
10375 *
10376 * @see #setDrawingCacheEnabled(boolean)
10377 * @see #buildDrawingCache()
10378 * @see #getDrawingCache()
10379 */
10380 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010381 if (color != mDrawingCacheBackgroundColor) {
10382 mDrawingCacheBackgroundColor = color;
10383 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10384 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010385 }
10386
10387 /**
10388 * @see #setDrawingCacheBackgroundColor(int)
10389 *
10390 * @return The background color to used for the drawing cache's bitmap
10391 */
10392 public int getDrawingCacheBackgroundColor() {
10393 return mDrawingCacheBackgroundColor;
10394 }
10395
10396 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010397 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010398 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010399 * @see #buildDrawingCache(boolean)
10400 */
10401 public void buildDrawingCache() {
10402 buildDrawingCache(false);
10403 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010404
Romain Guyfbd8f692009-06-26 14:51:58 -070010405 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010406 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10407 *
10408 * <p>If you call {@link #buildDrawingCache()} manually without calling
10409 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10410 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010411 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010412 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10413 * this method will create a bitmap of the same size as this view. Because this bitmap
10414 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10415 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10416 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10417 * size than the view. This implies that your application must be able to handle this
10418 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010419 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010420 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10421 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010422 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010423 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010424 *
10425 * @see #getDrawingCache()
10426 * @see #destroyDrawingCache()
10427 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010428 public void buildDrawingCache(boolean autoScale) {
10429 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010430 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010431 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010432
10433 if (ViewDebug.TRACE_HIERARCHY) {
10434 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010436
Romain Guy8506ab42009-06-11 17:35:47 -070010437 int width = mRight - mLeft;
10438 int height = mBottom - mTop;
10439
10440 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010441 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010442
Romain Guye1123222009-06-29 14:24:56 -070010443 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010444 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10445 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010447
10448 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010449 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010450 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010451
10452 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010453 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010454 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010455 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10456 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010457 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010458 return;
10459 }
10460
10461 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010462 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010463
10464 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010465 Bitmap.Config quality;
10466 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010467 // Never pick ARGB_4444 because it looks awful
10468 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010469 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10470 case DRAWING_CACHE_QUALITY_AUTO:
10471 quality = Bitmap.Config.ARGB_8888;
10472 break;
10473 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010474 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010475 break;
10476 case DRAWING_CACHE_QUALITY_HIGH:
10477 quality = Bitmap.Config.ARGB_8888;
10478 break;
10479 default:
10480 quality = Bitmap.Config.ARGB_8888;
10481 break;
10482 }
10483 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010484 // Optimization for translucent windows
10485 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010486 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010487 }
10488
10489 // Try to cleanup memory
10490 if (bitmap != null) bitmap.recycle();
10491
10492 try {
10493 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010494 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010495 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010496 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010497 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010498 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010499 }
Adam Powell26153a32010-11-08 15:22:27 -080010500 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010501 } catch (OutOfMemoryError e) {
10502 // If there is not enough memory to create the bitmap cache, just
10503 // ignore the issue as bitmap caches are not required to draw the
10504 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010505 if (autoScale) {
10506 mDrawingCache = null;
10507 } else {
10508 mUnscaledDrawingCache = null;
10509 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010510 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010511 return;
10512 }
10513
10514 clear = drawingCacheBackgroundColor != 0;
10515 }
10516
10517 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010518 if (attachInfo != null) {
10519 canvas = attachInfo.mCanvas;
10520 if (canvas == null) {
10521 canvas = new Canvas();
10522 }
10523 canvas.setBitmap(bitmap);
10524 // Temporarily clobber the cached Canvas in case one of our children
10525 // is also using a drawing cache. Without this, the children would
10526 // steal the canvas by attaching their own bitmap to it and bad, bad
10527 // thing would happen (invisible views, corrupted drawings, etc.)
10528 attachInfo.mCanvas = null;
10529 } else {
10530 // This case should hopefully never or seldom happen
10531 canvas = new Canvas(bitmap);
10532 }
10533
10534 if (clear) {
10535 bitmap.eraseColor(drawingCacheBackgroundColor);
10536 }
10537
10538 computeScroll();
10539 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010540
Romain Guye1123222009-06-29 14:24:56 -070010541 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010542 final float scale = attachInfo.mApplicationScale;
10543 canvas.scale(scale, scale);
10544 }
Joe Malin32736f02011-01-19 16:14:20 -080010545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010546 canvas.translate(-mScrollX, -mScrollY);
10547
Romain Guy5bcdff42009-05-14 21:27:18 -070010548 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010549 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10550 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010551 mPrivateFlags |= DRAWING_CACHE_VALID;
10552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010553
10554 // Fast path for layouts with no backgrounds
10555 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10556 if (ViewDebug.TRACE_HIERARCHY) {
10557 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10558 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010559 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010560 dispatchDraw(canvas);
10561 } else {
10562 draw(canvas);
10563 }
10564
10565 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010566 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010567
10568 if (attachInfo != null) {
10569 // Restore the cached Canvas for our siblings
10570 attachInfo.mCanvas = canvas;
10571 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010572 }
10573 }
10574
10575 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010576 * Create a snapshot of the view into a bitmap. We should probably make
10577 * some form of this public, but should think about the API.
10578 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010579 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010580 int width = mRight - mLeft;
10581 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010582
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010583 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010584 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010585 width = (int) ((width * scale) + 0.5f);
10586 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010587
Romain Guy8c11e312009-09-14 15:15:30 -070010588 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010589 if (bitmap == null) {
10590 throw new OutOfMemoryError();
10591 }
10592
Romain Guyc529d8d2011-09-06 15:01:39 -070010593 Resources resources = getResources();
10594 if (resources != null) {
10595 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
10596 }
Joe Malin32736f02011-01-19 16:14:20 -080010597
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010598 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010599 if (attachInfo != null) {
10600 canvas = attachInfo.mCanvas;
10601 if (canvas == null) {
10602 canvas = new Canvas();
10603 }
10604 canvas.setBitmap(bitmap);
10605 // Temporarily clobber the cached Canvas in case one of our children
10606 // is also using a drawing cache. Without this, the children would
10607 // steal the canvas by attaching their own bitmap to it and bad, bad
10608 // things would happen (invisible views, corrupted drawings, etc.)
10609 attachInfo.mCanvas = null;
10610 } else {
10611 // This case should hopefully never or seldom happen
10612 canvas = new Canvas(bitmap);
10613 }
10614
Romain Guy5bcdff42009-05-14 21:27:18 -070010615 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010616 bitmap.eraseColor(backgroundColor);
10617 }
10618
10619 computeScroll();
10620 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010621 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010622 canvas.translate(-mScrollX, -mScrollY);
10623
Romain Guy5bcdff42009-05-14 21:27:18 -070010624 // Temporarily remove the dirty mask
10625 int flags = mPrivateFlags;
10626 mPrivateFlags &= ~DIRTY_MASK;
10627
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010628 // Fast path for layouts with no backgrounds
10629 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10630 dispatchDraw(canvas);
10631 } else {
10632 draw(canvas);
10633 }
10634
Romain Guy5bcdff42009-05-14 21:27:18 -070010635 mPrivateFlags = flags;
10636
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010637 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010638 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010639
10640 if (attachInfo != null) {
10641 // Restore the cached Canvas for our siblings
10642 attachInfo.mCanvas = canvas;
10643 }
Romain Guy8506ab42009-06-11 17:35:47 -070010644
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010645 return bitmap;
10646 }
10647
10648 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010649 * Indicates whether this View is currently in edit mode. A View is usually
10650 * in edit mode when displayed within a developer tool. For instance, if
10651 * this View is being drawn by a visual user interface builder, this method
10652 * should return true.
10653 *
10654 * Subclasses should check the return value of this method to provide
10655 * different behaviors if their normal behavior might interfere with the
10656 * host environment. For instance: the class spawns a thread in its
10657 * constructor, the drawing code relies on device-specific features, etc.
10658 *
10659 * This method is usually checked in the drawing code of custom widgets.
10660 *
10661 * @return True if this View is in edit mode, false otherwise.
10662 */
10663 public boolean isInEditMode() {
10664 return false;
10665 }
10666
10667 /**
10668 * If the View draws content inside its padding and enables fading edges,
10669 * it needs to support padding offsets. Padding offsets are added to the
10670 * fading edges to extend the length of the fade so that it covers pixels
10671 * drawn inside the padding.
10672 *
10673 * Subclasses of this class should override this method if they need
10674 * to draw content inside the padding.
10675 *
10676 * @return True if padding offset must be applied, false otherwise.
10677 *
10678 * @see #getLeftPaddingOffset()
10679 * @see #getRightPaddingOffset()
10680 * @see #getTopPaddingOffset()
10681 * @see #getBottomPaddingOffset()
10682 *
10683 * @since CURRENT
10684 */
10685 protected boolean isPaddingOffsetRequired() {
10686 return false;
10687 }
10688
10689 /**
10690 * Amount by which to extend the left fading region. Called only when
10691 * {@link #isPaddingOffsetRequired()} returns true.
10692 *
10693 * @return The left padding offset in pixels.
10694 *
10695 * @see #isPaddingOffsetRequired()
10696 *
10697 * @since CURRENT
10698 */
10699 protected int getLeftPaddingOffset() {
10700 return 0;
10701 }
10702
10703 /**
10704 * Amount by which to extend the right fading region. Called only when
10705 * {@link #isPaddingOffsetRequired()} returns true.
10706 *
10707 * @return The right padding offset in pixels.
10708 *
10709 * @see #isPaddingOffsetRequired()
10710 *
10711 * @since CURRENT
10712 */
10713 protected int getRightPaddingOffset() {
10714 return 0;
10715 }
10716
10717 /**
10718 * Amount by which to extend the top fading region. Called only when
10719 * {@link #isPaddingOffsetRequired()} returns true.
10720 *
10721 * @return The top padding offset in pixels.
10722 *
10723 * @see #isPaddingOffsetRequired()
10724 *
10725 * @since CURRENT
10726 */
10727 protected int getTopPaddingOffset() {
10728 return 0;
10729 }
10730
10731 /**
10732 * Amount by which to extend the bottom fading region. Called only when
10733 * {@link #isPaddingOffsetRequired()} returns true.
10734 *
10735 * @return The bottom padding offset in pixels.
10736 *
10737 * @see #isPaddingOffsetRequired()
10738 *
10739 * @since CURRENT
10740 */
10741 protected int getBottomPaddingOffset() {
10742 return 0;
10743 }
10744
10745 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070010746 * @hide
10747 * @param offsetRequired
10748 */
10749 protected int getFadeTop(boolean offsetRequired) {
10750 int top = mPaddingTop;
10751 if (offsetRequired) top += getTopPaddingOffset();
10752 return top;
10753 }
10754
10755 /**
10756 * @hide
10757 * @param offsetRequired
10758 */
10759 protected int getFadeHeight(boolean offsetRequired) {
10760 int padding = mPaddingTop;
10761 if (offsetRequired) padding += getTopPaddingOffset();
10762 return mBottom - mTop - mPaddingBottom - padding;
10763 }
10764
10765 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010766 * <p>Indicates whether this view is attached to an hardware accelerated
10767 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010768 *
Romain Guy2bffd262010-09-12 17:40:02 -070010769 * <p>Even if this method returns true, it does not mean that every call
10770 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10771 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10772 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10773 * window is hardware accelerated,
10774 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10775 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010776 *
Romain Guy2bffd262010-09-12 17:40:02 -070010777 * @return True if the view is attached to a window and the window is
10778 * hardware accelerated; false in any other case.
10779 */
10780 public boolean isHardwareAccelerated() {
10781 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10782 }
Joe Malin32736f02011-01-19 16:14:20 -080010783
Romain Guy2bffd262010-09-12 17:40:02 -070010784 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010785 * Manually render this view (and all of its children) to the given Canvas.
10786 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010787 * called. When implementing a view, implement
10788 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
10789 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010790 *
10791 * @param canvas The Canvas to which the View is rendered.
10792 */
10793 public void draw(Canvas canvas) {
10794 if (ViewDebug.TRACE_HIERARCHY) {
10795 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10796 }
10797
Romain Guy5bcdff42009-05-14 21:27:18 -070010798 final int privateFlags = mPrivateFlags;
10799 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
10800 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
10801 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070010802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010803 /*
10804 * Draw traversal performs several drawing steps which must be executed
10805 * in the appropriate order:
10806 *
10807 * 1. Draw the background
10808 * 2. If necessary, save the canvas' layers to prepare for fading
10809 * 3. Draw view's content
10810 * 4. Draw children
10811 * 5. If necessary, draw the fading edges and restore layers
10812 * 6. Draw decorations (scrollbars for instance)
10813 */
10814
10815 // Step 1, draw the background, if needed
10816 int saveCount;
10817
Romain Guy24443ea2009-05-11 11:56:30 -070010818 if (!dirtyOpaque) {
10819 final Drawable background = mBGDrawable;
10820 if (background != null) {
10821 final int scrollX = mScrollX;
10822 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010823
Romain Guy24443ea2009-05-11 11:56:30 -070010824 if (mBackgroundSizeChanged) {
10825 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
10826 mBackgroundSizeChanged = false;
10827 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010828
Romain Guy24443ea2009-05-11 11:56:30 -070010829 if ((scrollX | scrollY) == 0) {
10830 background.draw(canvas);
10831 } else {
10832 canvas.translate(scrollX, scrollY);
10833 background.draw(canvas);
10834 canvas.translate(-scrollX, -scrollY);
10835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010836 }
10837 }
10838
10839 // skip step 2 & 5 if possible (common case)
10840 final int viewFlags = mViewFlags;
10841 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
10842 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
10843 if (!verticalEdges && !horizontalEdges) {
10844 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010845 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010846
10847 // Step 4, draw the children
10848 dispatchDraw(canvas);
10849
10850 // Step 6, draw decorations (scrollbars)
10851 onDrawScrollBars(canvas);
10852
10853 // we're done...
10854 return;
10855 }
10856
10857 /*
10858 * Here we do the full fledged routine...
10859 * (this is an uncommon case where speed matters less,
10860 * this is why we repeat some of the tests that have been
10861 * done above)
10862 */
10863
10864 boolean drawTop = false;
10865 boolean drawBottom = false;
10866 boolean drawLeft = false;
10867 boolean drawRight = false;
10868
10869 float topFadeStrength = 0.0f;
10870 float bottomFadeStrength = 0.0f;
10871 float leftFadeStrength = 0.0f;
10872 float rightFadeStrength = 0.0f;
10873
10874 // Step 2, save the canvas' layers
10875 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010876
10877 final boolean offsetRequired = isPaddingOffsetRequired();
10878 if (offsetRequired) {
10879 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010880 }
10881
10882 int left = mScrollX + paddingLeft;
10883 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070010884 int top = mScrollY + getFadeTop(offsetRequired);
10885 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010886
10887 if (offsetRequired) {
10888 right += getRightPaddingOffset();
10889 bottom += getBottomPaddingOffset();
10890 }
10891
10892 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010893 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
10894 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010895
10896 // clip the fade length if top and bottom fades overlap
10897 // overlapping fades produce odd-looking artifacts
10898 if (verticalEdges && (top + length > bottom - length)) {
10899 length = (bottom - top) / 2;
10900 }
10901
10902 // also clip horizontal fades if necessary
10903 if (horizontalEdges && (left + length > right - length)) {
10904 length = (right - left) / 2;
10905 }
10906
10907 if (verticalEdges) {
10908 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010909 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010910 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010911 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010912 }
10913
10914 if (horizontalEdges) {
10915 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010916 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010917 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010918 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010919 }
10920
10921 saveCount = canvas.getSaveCount();
10922
10923 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070010924 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010925 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
10926
10927 if (drawTop) {
10928 canvas.saveLayer(left, top, right, top + length, null, flags);
10929 }
10930
10931 if (drawBottom) {
10932 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
10933 }
10934
10935 if (drawLeft) {
10936 canvas.saveLayer(left, top, left + length, bottom, null, flags);
10937 }
10938
10939 if (drawRight) {
10940 canvas.saveLayer(right - length, top, right, bottom, null, flags);
10941 }
10942 } else {
10943 scrollabilityCache.setFadeColor(solidColor);
10944 }
10945
10946 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010947 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010948
10949 // Step 4, draw the children
10950 dispatchDraw(canvas);
10951
10952 // Step 5, draw the fade effect and restore layers
10953 final Paint p = scrollabilityCache.paint;
10954 final Matrix matrix = scrollabilityCache.matrix;
10955 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010956
10957 if (drawTop) {
10958 matrix.setScale(1, fadeHeight * topFadeStrength);
10959 matrix.postTranslate(left, top);
10960 fade.setLocalMatrix(matrix);
10961 canvas.drawRect(left, top, right, top + length, p);
10962 }
10963
10964 if (drawBottom) {
10965 matrix.setScale(1, fadeHeight * bottomFadeStrength);
10966 matrix.postRotate(180);
10967 matrix.postTranslate(left, bottom);
10968 fade.setLocalMatrix(matrix);
10969 canvas.drawRect(left, bottom - length, right, bottom, p);
10970 }
10971
10972 if (drawLeft) {
10973 matrix.setScale(1, fadeHeight * leftFadeStrength);
10974 matrix.postRotate(-90);
10975 matrix.postTranslate(left, top);
10976 fade.setLocalMatrix(matrix);
10977 canvas.drawRect(left, top, left + length, bottom, p);
10978 }
10979
10980 if (drawRight) {
10981 matrix.setScale(1, fadeHeight * rightFadeStrength);
10982 matrix.postRotate(90);
10983 matrix.postTranslate(right, top);
10984 fade.setLocalMatrix(matrix);
10985 canvas.drawRect(right - length, top, right, bottom, p);
10986 }
10987
10988 canvas.restoreToCount(saveCount);
10989
10990 // Step 6, draw decorations (scrollbars)
10991 onDrawScrollBars(canvas);
10992 }
10993
10994 /**
10995 * Override this if your view is known to always be drawn on top of a solid color background,
10996 * and needs to draw fading edges. Returning a non-zero color enables the view system to
10997 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
10998 * should be set to 0xFF.
10999 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011000 * @see #setVerticalFadingEdgeEnabled(boolean)
11001 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011002 *
11003 * @return The known solid color background for this view, or 0 if the color may vary
11004 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011005 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011006 public int getSolidColor() {
11007 return 0;
11008 }
11009
11010 /**
11011 * Build a human readable string representation of the specified view flags.
11012 *
11013 * @param flags the view flags to convert to a string
11014 * @return a String representing the supplied flags
11015 */
11016 private static String printFlags(int flags) {
11017 String output = "";
11018 int numFlags = 0;
11019 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
11020 output += "TAKES_FOCUS";
11021 numFlags++;
11022 }
11023
11024 switch (flags & VISIBILITY_MASK) {
11025 case INVISIBLE:
11026 if (numFlags > 0) {
11027 output += " ";
11028 }
11029 output += "INVISIBLE";
11030 // USELESS HERE numFlags++;
11031 break;
11032 case GONE:
11033 if (numFlags > 0) {
11034 output += " ";
11035 }
11036 output += "GONE";
11037 // USELESS HERE numFlags++;
11038 break;
11039 default:
11040 break;
11041 }
11042 return output;
11043 }
11044
11045 /**
11046 * Build a human readable string representation of the specified private
11047 * view flags.
11048 *
11049 * @param privateFlags the private view flags to convert to a string
11050 * @return a String representing the supplied flags
11051 */
11052 private static String printPrivateFlags(int privateFlags) {
11053 String output = "";
11054 int numFlags = 0;
11055
11056 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
11057 output += "WANTS_FOCUS";
11058 numFlags++;
11059 }
11060
11061 if ((privateFlags & FOCUSED) == FOCUSED) {
11062 if (numFlags > 0) {
11063 output += " ";
11064 }
11065 output += "FOCUSED";
11066 numFlags++;
11067 }
11068
11069 if ((privateFlags & SELECTED) == SELECTED) {
11070 if (numFlags > 0) {
11071 output += " ";
11072 }
11073 output += "SELECTED";
11074 numFlags++;
11075 }
11076
11077 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
11078 if (numFlags > 0) {
11079 output += " ";
11080 }
11081 output += "IS_ROOT_NAMESPACE";
11082 numFlags++;
11083 }
11084
11085 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
11086 if (numFlags > 0) {
11087 output += " ";
11088 }
11089 output += "HAS_BOUNDS";
11090 numFlags++;
11091 }
11092
11093 if ((privateFlags & DRAWN) == DRAWN) {
11094 if (numFlags > 0) {
11095 output += " ";
11096 }
11097 output += "DRAWN";
11098 // USELESS HERE numFlags++;
11099 }
11100 return output;
11101 }
11102
11103 /**
11104 * <p>Indicates whether or not this view's layout will be requested during
11105 * the next hierarchy layout pass.</p>
11106 *
11107 * @return true if the layout will be forced during next layout pass
11108 */
11109 public boolean isLayoutRequested() {
11110 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
11111 }
11112
11113 /**
11114 * Assign a size and position to a view and all of its
11115 * descendants
11116 *
11117 * <p>This is the second phase of the layout mechanism.
11118 * (The first is measuring). In this phase, each parent calls
11119 * layout on all of its children to position them.
11120 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080011121 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011122 *
Chet Haase9c087442011-01-12 16:20:16 -080011123 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011124 * Derived classes with children should override
11125 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080011126 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011127 *
11128 * @param l Left position, relative to parent
11129 * @param t Top position, relative to parent
11130 * @param r Right position, relative to parent
11131 * @param b Bottom position, relative to parent
11132 */
Romain Guy5429e1d2010-09-07 12:38:00 -070011133 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080011134 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070011135 int oldL = mLeft;
11136 int oldT = mTop;
11137 int oldB = mBottom;
11138 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011139 boolean changed = setFrame(l, t, r, b);
11140 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
11141 if (ViewDebug.TRACE_HIERARCHY) {
11142 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
11143 }
11144
11145 onLayout(changed, l, t, r, b);
11146 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070011147
11148 if (mOnLayoutChangeListeners != null) {
11149 ArrayList<OnLayoutChangeListener> listenersCopy =
11150 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
11151 int numListeners = listenersCopy.size();
11152 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070011153 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070011154 }
11155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011156 }
11157 mPrivateFlags &= ~FORCE_LAYOUT;
11158 }
11159
11160 /**
11161 * Called from layout when this view should
11162 * assign a size and position to each of its children.
11163 *
11164 * Derived classes with children should override
11165 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070011166 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011167 * @param changed This is a new size or position for this view
11168 * @param left Left position, relative to parent
11169 * @param top Top position, relative to parent
11170 * @param right Right position, relative to parent
11171 * @param bottom Bottom position, relative to parent
11172 */
11173 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
11174 }
11175
11176 /**
11177 * Assign a size and position to this view.
11178 *
11179 * This is called from layout.
11180 *
11181 * @param left Left position, relative to parent
11182 * @param top Top position, relative to parent
11183 * @param right Right position, relative to parent
11184 * @param bottom Bottom position, relative to parent
11185 * @return true if the new size and position are different than the
11186 * previous ones
11187 * {@hide}
11188 */
11189 protected boolean setFrame(int left, int top, int right, int bottom) {
11190 boolean changed = false;
11191
11192 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070011193 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011194 + right + "," + bottom + ")");
11195 }
11196
11197 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
11198 changed = true;
11199
11200 // Remember our drawn bit
11201 int drawn = mPrivateFlags & DRAWN;
11202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011203 int oldWidth = mRight - mLeft;
11204 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070011205 int newWidth = right - left;
11206 int newHeight = bottom - top;
11207 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
11208
11209 // Invalidate our old position
11210 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011211
11212 mLeft = left;
11213 mTop = top;
11214 mRight = right;
11215 mBottom = bottom;
11216
11217 mPrivateFlags |= HAS_BOUNDS;
11218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011219
Chet Haase75755e22011-07-18 17:48:25 -070011220 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011221 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
11222 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070011223 if (mTransformationInfo != null) {
11224 mTransformationInfo.mMatrixDirty = true;
11225 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011227 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
11228 }
11229
11230 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
11231 // If we are visible, force the DRAWN bit to on so that
11232 // this invalidate will go through (at least to our parent).
11233 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011234 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011235 // the DRAWN bit.
11236 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070011237 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080011238 // parent display list may need to be recreated based on a change in the bounds
11239 // of any child
11240 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011241 }
11242
11243 // Reset drawn bit to original value (invalidate turns it off)
11244 mPrivateFlags |= drawn;
11245
11246 mBackgroundSizeChanged = true;
11247 }
11248 return changed;
11249 }
11250
11251 /**
11252 * Finalize inflating a view from XML. This is called as the last phase
11253 * of inflation, after all child views have been added.
11254 *
11255 * <p>Even if the subclass overrides onFinishInflate, they should always be
11256 * sure to call the super method, so that we get called.
11257 */
11258 protected void onFinishInflate() {
11259 }
11260
11261 /**
11262 * Returns the resources associated with this view.
11263 *
11264 * @return Resources object.
11265 */
11266 public Resources getResources() {
11267 return mResources;
11268 }
11269
11270 /**
11271 * Invalidates the specified Drawable.
11272 *
11273 * @param drawable the drawable to invalidate
11274 */
11275 public void invalidateDrawable(Drawable drawable) {
11276 if (verifyDrawable(drawable)) {
11277 final Rect dirty = drawable.getBounds();
11278 final int scrollX = mScrollX;
11279 final int scrollY = mScrollY;
11280
11281 invalidate(dirty.left + scrollX, dirty.top + scrollY,
11282 dirty.right + scrollX, dirty.bottom + scrollY);
11283 }
11284 }
11285
11286 /**
11287 * Schedules an action on a drawable to occur at a specified time.
11288 *
11289 * @param who the recipient of the action
11290 * @param what the action to run on the drawable
11291 * @param when the time at which the action must occur. Uses the
11292 * {@link SystemClock#uptimeMillis} timebase.
11293 */
11294 public void scheduleDrawable(Drawable who, Runnable what, long when) {
11295 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
11296 mAttachInfo.mHandler.postAtTime(what, who, when);
11297 }
11298 }
11299
11300 /**
11301 * Cancels a scheduled action on a drawable.
11302 *
11303 * @param who the recipient of the action
11304 * @param what the action to cancel
11305 */
11306 public void unscheduleDrawable(Drawable who, Runnable what) {
11307 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
11308 mAttachInfo.mHandler.removeCallbacks(what, who);
11309 }
11310 }
11311
11312 /**
11313 * Unschedule any events associated with the given Drawable. This can be
11314 * used when selecting a new Drawable into a view, so that the previous
11315 * one is completely unscheduled.
11316 *
11317 * @param who The Drawable to unschedule.
11318 *
11319 * @see #drawableStateChanged
11320 */
11321 public void unscheduleDrawable(Drawable who) {
11322 if (mAttachInfo != null) {
11323 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
11324 }
11325 }
11326
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011327 /**
11328 * Return the layout direction of a given Drawable.
11329 *
11330 * @param who the Drawable to query
11331 *
11332 * @hide
11333 */
11334 public int getResolvedLayoutDirection(Drawable who) {
11335 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070011336 }
11337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011338 /**
11339 * If your view subclass is displaying its own Drawable objects, it should
11340 * override this function and return true for any Drawable it is
11341 * displaying. This allows animations for those drawables to be
11342 * scheduled.
11343 *
11344 * <p>Be sure to call through to the super class when overriding this
11345 * function.
11346 *
11347 * @param who The Drawable to verify. Return true if it is one you are
11348 * displaying, else return the result of calling through to the
11349 * super class.
11350 *
11351 * @return boolean If true than the Drawable is being displayed in the
11352 * view; else false and it is not allowed to animate.
11353 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011354 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
11355 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011356 */
11357 protected boolean verifyDrawable(Drawable who) {
11358 return who == mBGDrawable;
11359 }
11360
11361 /**
11362 * This function is called whenever the state of the view changes in such
11363 * a way that it impacts the state of drawables being shown.
11364 *
11365 * <p>Be sure to call through to the superclass when overriding this
11366 * function.
11367 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011368 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011369 */
11370 protected void drawableStateChanged() {
11371 Drawable d = mBGDrawable;
11372 if (d != null && d.isStateful()) {
11373 d.setState(getDrawableState());
11374 }
11375 }
11376
11377 /**
11378 * Call this to force a view to update its drawable state. This will cause
11379 * drawableStateChanged to be called on this view. Views that are interested
11380 * in the new state should call getDrawableState.
11381 *
11382 * @see #drawableStateChanged
11383 * @see #getDrawableState
11384 */
11385 public void refreshDrawableState() {
11386 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
11387 drawableStateChanged();
11388
11389 ViewParent parent = mParent;
11390 if (parent != null) {
11391 parent.childDrawableStateChanged(this);
11392 }
11393 }
11394
11395 /**
11396 * Return an array of resource IDs of the drawable states representing the
11397 * current state of the view.
11398 *
11399 * @return The current drawable state
11400 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011401 * @see Drawable#setState(int[])
11402 * @see #drawableStateChanged()
11403 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011404 */
11405 public final int[] getDrawableState() {
11406 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
11407 return mDrawableState;
11408 } else {
11409 mDrawableState = onCreateDrawableState(0);
11410 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
11411 return mDrawableState;
11412 }
11413 }
11414
11415 /**
11416 * Generate the new {@link android.graphics.drawable.Drawable} state for
11417 * this view. This is called by the view
11418 * system when the cached Drawable state is determined to be invalid. To
11419 * retrieve the current state, you should use {@link #getDrawableState}.
11420 *
11421 * @param extraSpace if non-zero, this is the number of extra entries you
11422 * would like in the returned array in which you can place your own
11423 * states.
11424 *
11425 * @return Returns an array holding the current {@link Drawable} state of
11426 * the view.
11427 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011428 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011429 */
11430 protected int[] onCreateDrawableState(int extraSpace) {
11431 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
11432 mParent instanceof View) {
11433 return ((View) mParent).onCreateDrawableState(extraSpace);
11434 }
11435
11436 int[] drawableState;
11437
11438 int privateFlags = mPrivateFlags;
11439
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011440 int viewStateIndex = 0;
11441 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
11442 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
11443 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070011444 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011445 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
11446 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070011447 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
11448 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011449 // This is set if HW acceleration is requested, even if the current
11450 // process doesn't allow it. This is just to allow app preview
11451 // windows to better match their app.
11452 viewStateIndex |= VIEW_STATE_ACCELERATED;
11453 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070011454 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011455
Christopher Tate3d4bf172011-03-28 16:16:46 -070011456 final int privateFlags2 = mPrivateFlags2;
11457 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
11458 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
11459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011460 drawableState = VIEW_STATE_SETS[viewStateIndex];
11461
11462 //noinspection ConstantIfStatement
11463 if (false) {
11464 Log.i("View", "drawableStateIndex=" + viewStateIndex);
11465 Log.i("View", toString()
11466 + " pressed=" + ((privateFlags & PRESSED) != 0)
11467 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
11468 + " fo=" + hasFocus()
11469 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011470 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011471 + ": " + Arrays.toString(drawableState));
11472 }
11473
11474 if (extraSpace == 0) {
11475 return drawableState;
11476 }
11477
11478 final int[] fullState;
11479 if (drawableState != null) {
11480 fullState = new int[drawableState.length + extraSpace];
11481 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
11482 } else {
11483 fullState = new int[extraSpace];
11484 }
11485
11486 return fullState;
11487 }
11488
11489 /**
11490 * Merge your own state values in <var>additionalState</var> into the base
11491 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011492 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011493 *
11494 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011495 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011496 * own additional state values.
11497 *
11498 * @param additionalState The additional state values you would like
11499 * added to <var>baseState</var>; this array is not modified.
11500 *
11501 * @return As a convenience, the <var>baseState</var> array you originally
11502 * passed into the function is returned.
11503 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011504 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011505 */
11506 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
11507 final int N = baseState.length;
11508 int i = N - 1;
11509 while (i >= 0 && baseState[i] == 0) {
11510 i--;
11511 }
11512 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
11513 return baseState;
11514 }
11515
11516 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070011517 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
11518 * on all Drawable objects associated with this view.
11519 */
11520 public void jumpDrawablesToCurrentState() {
11521 if (mBGDrawable != null) {
11522 mBGDrawable.jumpToCurrentState();
11523 }
11524 }
11525
11526 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011527 * Sets the background color for this view.
11528 * @param color the color of the background
11529 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011530 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011531 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070011532 if (mBGDrawable instanceof ColorDrawable) {
11533 ((ColorDrawable) mBGDrawable).setColor(color);
11534 } else {
11535 setBackgroundDrawable(new ColorDrawable(color));
11536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011537 }
11538
11539 /**
11540 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070011541 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011542 * @param resid The identifier of the resource.
11543 * @attr ref android.R.styleable#View_background
11544 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011545 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011546 public void setBackgroundResource(int resid) {
11547 if (resid != 0 && resid == mBackgroundResource) {
11548 return;
11549 }
11550
11551 Drawable d= null;
11552 if (resid != 0) {
11553 d = mResources.getDrawable(resid);
11554 }
11555 setBackgroundDrawable(d);
11556
11557 mBackgroundResource = resid;
11558 }
11559
11560 /**
11561 * Set the background to a given Drawable, or remove the background. If the
11562 * background has padding, this View's padding is set to the background's
11563 * padding. However, when a background is removed, this View's padding isn't
11564 * touched. If setting the padding is desired, please use
11565 * {@link #setPadding(int, int, int, int)}.
11566 *
11567 * @param d The Drawable to use as the background, or null to remove the
11568 * background
11569 */
11570 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070011571 if (d == mBGDrawable) {
11572 return;
11573 }
11574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011575 boolean requestLayout = false;
11576
11577 mBackgroundResource = 0;
11578
11579 /*
11580 * Regardless of whether we're setting a new background or not, we want
11581 * to clear the previous drawable.
11582 */
11583 if (mBGDrawable != null) {
11584 mBGDrawable.setCallback(null);
11585 unscheduleDrawable(mBGDrawable);
11586 }
11587
11588 if (d != null) {
11589 Rect padding = sThreadLocal.get();
11590 if (padding == null) {
11591 padding = new Rect();
11592 sThreadLocal.set(padding);
11593 }
11594 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011595 switch (d.getResolvedLayoutDirectionSelf()) {
11596 case LAYOUT_DIRECTION_RTL:
11597 setPadding(padding.right, padding.top, padding.left, padding.bottom);
11598 break;
11599 case LAYOUT_DIRECTION_LTR:
11600 default:
11601 setPadding(padding.left, padding.top, padding.right, padding.bottom);
11602 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011603 }
11604
11605 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
11606 // if it has a different minimum size, we should layout again
11607 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
11608 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
11609 requestLayout = true;
11610 }
11611
11612 d.setCallback(this);
11613 if (d.isStateful()) {
11614 d.setState(getDrawableState());
11615 }
11616 d.setVisible(getVisibility() == VISIBLE, false);
11617 mBGDrawable = d;
11618
11619 if ((mPrivateFlags & SKIP_DRAW) != 0) {
11620 mPrivateFlags &= ~SKIP_DRAW;
11621 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
11622 requestLayout = true;
11623 }
11624 } else {
11625 /* Remove the background */
11626 mBGDrawable = null;
11627
11628 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
11629 /*
11630 * This view ONLY drew the background before and we're removing
11631 * the background, so now it won't draw anything
11632 * (hence we SKIP_DRAW)
11633 */
11634 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
11635 mPrivateFlags |= SKIP_DRAW;
11636 }
11637
11638 /*
11639 * When the background is set, we try to apply its padding to this
11640 * View. When the background is removed, we don't touch this View's
11641 * padding. This is noted in the Javadocs. Hence, we don't need to
11642 * requestLayout(), the invalidate() below is sufficient.
11643 */
11644
11645 // The old background's minimum size could have affected this
11646 // View's layout, so let's requestLayout
11647 requestLayout = true;
11648 }
11649
Romain Guy8f1344f52009-05-15 16:03:59 -070011650 computeOpaqueFlags();
11651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011652 if (requestLayout) {
11653 requestLayout();
11654 }
11655
11656 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011657 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011658 }
11659
11660 /**
11661 * Gets the background drawable
11662 * @return The drawable used as the background for this view, if any.
11663 */
11664 public Drawable getBackground() {
11665 return mBGDrawable;
11666 }
11667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011668 /**
11669 * Sets the padding. The view may add on the space required to display
11670 * the scrollbars, depending on the style and visibility of the scrollbars.
11671 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
11672 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
11673 * from the values set in this call.
11674 *
11675 * @attr ref android.R.styleable#View_padding
11676 * @attr ref android.R.styleable#View_paddingBottom
11677 * @attr ref android.R.styleable#View_paddingLeft
11678 * @attr ref android.R.styleable#View_paddingRight
11679 * @attr ref android.R.styleable#View_paddingTop
11680 * @param left the left padding in pixels
11681 * @param top the top padding in pixels
11682 * @param right the right padding in pixels
11683 * @param bottom the bottom padding in pixels
11684 */
11685 public void setPadding(int left, int top, int right, int bottom) {
11686 boolean changed = false;
11687
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011688 mUserPaddingRelative = false;
11689
Adam Powell20232d02010-12-08 21:08:53 -080011690 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011691 mUserPaddingRight = right;
11692 mUserPaddingBottom = bottom;
11693
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011694 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070011695
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011696 // Common case is there are no scroll bars.
11697 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011698 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080011699 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011700 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080011701 switch (mVerticalScrollbarPosition) {
11702 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011703 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11704 left += offset;
11705 } else {
11706 right += offset;
11707 }
11708 break;
Adam Powell20232d02010-12-08 21:08:53 -080011709 case SCROLLBAR_POSITION_RIGHT:
11710 right += offset;
11711 break;
11712 case SCROLLBAR_POSITION_LEFT:
11713 left += offset;
11714 break;
11715 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011716 }
Adam Powell20232d02010-12-08 21:08:53 -080011717 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011718 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
11719 ? 0 : getHorizontalScrollbarHeight();
11720 }
11721 }
Romain Guy8506ab42009-06-11 17:35:47 -070011722
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011723 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011724 changed = true;
11725 mPaddingLeft = left;
11726 }
11727 if (mPaddingTop != top) {
11728 changed = true;
11729 mPaddingTop = top;
11730 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011731 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011732 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011733 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011734 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011735 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011736 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011737 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011738 }
11739
11740 if (changed) {
11741 requestLayout();
11742 }
11743 }
11744
11745 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011746 * Sets the relative padding. The view may add on the space required to display
11747 * the scrollbars, depending on the style and visibility of the scrollbars.
11748 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
11749 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
11750 * from the values set in this call.
11751 *
11752 * @attr ref android.R.styleable#View_padding
11753 * @attr ref android.R.styleable#View_paddingBottom
11754 * @attr ref android.R.styleable#View_paddingStart
11755 * @attr ref android.R.styleable#View_paddingEnd
11756 * @attr ref android.R.styleable#View_paddingTop
11757 * @param start the start padding in pixels
11758 * @param top the top padding in pixels
11759 * @param end the end padding in pixels
11760 * @param bottom the bottom padding in pixels
11761 *
11762 * @hide
11763 */
11764 public void setPaddingRelative(int start, int top, int end, int bottom) {
11765 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070011766
11767 mUserPaddingStart = start;
11768 mUserPaddingEnd = end;
11769
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011770 switch(getResolvedLayoutDirection()) {
11771 case LAYOUT_DIRECTION_RTL:
11772 setPadding(end, top, start, bottom);
11773 break;
11774 case LAYOUT_DIRECTION_LTR:
11775 default:
11776 setPadding(start, top, end, bottom);
11777 }
11778 }
11779
11780 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011781 * Returns the top padding of this view.
11782 *
11783 * @return the top padding in pixels
11784 */
11785 public int getPaddingTop() {
11786 return mPaddingTop;
11787 }
11788
11789 /**
11790 * Returns the bottom padding of this view. If there are inset and enabled
11791 * scrollbars, this value may include the space required to display the
11792 * scrollbars as well.
11793 *
11794 * @return the bottom padding in pixels
11795 */
11796 public int getPaddingBottom() {
11797 return mPaddingBottom;
11798 }
11799
11800 /**
11801 * Returns the left padding of this view. If there are inset and enabled
11802 * scrollbars, this value may include the space required to display the
11803 * scrollbars as well.
11804 *
11805 * @return the left padding in pixels
11806 */
11807 public int getPaddingLeft() {
11808 return mPaddingLeft;
11809 }
11810
11811 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011812 * Returns the start padding of this view. If there are inset and enabled
11813 * scrollbars, this value may include the space required to display the
11814 * scrollbars as well.
11815 *
11816 * @return the start padding in pixels
11817 *
11818 * @hide
11819 */
11820 public int getPaddingStart() {
11821 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11822 mPaddingRight : mPaddingLeft;
11823 }
11824
11825 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011826 * Returns the right padding of this view. If there are inset and enabled
11827 * scrollbars, this value may include the space required to display the
11828 * scrollbars as well.
11829 *
11830 * @return the right padding in pixels
11831 */
11832 public int getPaddingRight() {
11833 return mPaddingRight;
11834 }
11835
11836 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011837 * Returns the end padding of this view. If there are inset and enabled
11838 * scrollbars, this value may include the space required to display the
11839 * scrollbars as well.
11840 *
11841 * @return the end padding in pixels
11842 *
11843 * @hide
11844 */
11845 public int getPaddingEnd() {
11846 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11847 mPaddingLeft : mPaddingRight;
11848 }
11849
11850 /**
11851 * Return if the padding as been set thru relative values
11852 * {@link #setPaddingRelative(int, int, int, int)} or thru
11853 * @attr ref android.R.styleable#View_paddingStart or
11854 * @attr ref android.R.styleable#View_paddingEnd
11855 *
11856 * @return true if the padding is relative or false if it is not.
11857 *
11858 * @hide
11859 */
11860 public boolean isPaddingRelative() {
11861 return mUserPaddingRelative;
11862 }
11863
11864 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011865 * Changes the selection state of this view. A view can be selected or not.
11866 * Note that selection is not the same as focus. Views are typically
11867 * selected in the context of an AdapterView like ListView or GridView;
11868 * the selected view is the view that is highlighted.
11869 *
11870 * @param selected true if the view must be selected, false otherwise
11871 */
11872 public void setSelected(boolean selected) {
11873 if (((mPrivateFlags & SELECTED) != 0) != selected) {
11874 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070011875 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080011876 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011877 refreshDrawableState();
11878 dispatchSetSelected(selected);
11879 }
11880 }
11881
11882 /**
11883 * Dispatch setSelected to all of this View's children.
11884 *
11885 * @see #setSelected(boolean)
11886 *
11887 * @param selected The new selected state
11888 */
11889 protected void dispatchSetSelected(boolean selected) {
11890 }
11891
11892 /**
11893 * Indicates the selection state of this view.
11894 *
11895 * @return true if the view is selected, false otherwise
11896 */
11897 @ViewDebug.ExportedProperty
11898 public boolean isSelected() {
11899 return (mPrivateFlags & SELECTED) != 0;
11900 }
11901
11902 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011903 * Changes the activated state of this view. A view can be activated or not.
11904 * Note that activation is not the same as selection. Selection is
11905 * a transient property, representing the view (hierarchy) the user is
11906 * currently interacting with. Activation is a longer-term state that the
11907 * user can move views in and out of. For example, in a list view with
11908 * single or multiple selection enabled, the views in the current selection
11909 * set are activated. (Um, yeah, we are deeply sorry about the terminology
11910 * here.) The activated state is propagated down to children of the view it
11911 * is set on.
11912 *
11913 * @param activated true if the view must be activated, false otherwise
11914 */
11915 public void setActivated(boolean activated) {
11916 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
11917 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011918 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011919 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070011920 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011921 }
11922 }
11923
11924 /**
11925 * Dispatch setActivated to all of this View's children.
11926 *
11927 * @see #setActivated(boolean)
11928 *
11929 * @param activated The new activated state
11930 */
11931 protected void dispatchSetActivated(boolean activated) {
11932 }
11933
11934 /**
11935 * Indicates the activation state of this view.
11936 *
11937 * @return true if the view is activated, false otherwise
11938 */
11939 @ViewDebug.ExportedProperty
11940 public boolean isActivated() {
11941 return (mPrivateFlags & ACTIVATED) != 0;
11942 }
11943
11944 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011945 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
11946 * observer can be used to get notifications when global events, like
11947 * layout, happen.
11948 *
11949 * The returned ViewTreeObserver observer is not guaranteed to remain
11950 * valid for the lifetime of this View. If the caller of this method keeps
11951 * a long-lived reference to ViewTreeObserver, it should always check for
11952 * the return value of {@link ViewTreeObserver#isAlive()}.
11953 *
11954 * @return The ViewTreeObserver for this view's hierarchy.
11955 */
11956 public ViewTreeObserver getViewTreeObserver() {
11957 if (mAttachInfo != null) {
11958 return mAttachInfo.mTreeObserver;
11959 }
11960 if (mFloatingTreeObserver == null) {
11961 mFloatingTreeObserver = new ViewTreeObserver();
11962 }
11963 return mFloatingTreeObserver;
11964 }
11965
11966 /**
11967 * <p>Finds the topmost view in the current view hierarchy.</p>
11968 *
11969 * @return the topmost view containing this view
11970 */
11971 public View getRootView() {
11972 if (mAttachInfo != null) {
11973 final View v = mAttachInfo.mRootView;
11974 if (v != null) {
11975 return v;
11976 }
11977 }
Romain Guy8506ab42009-06-11 17:35:47 -070011978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011979 View parent = this;
11980
11981 while (parent.mParent != null && parent.mParent instanceof View) {
11982 parent = (View) parent.mParent;
11983 }
11984
11985 return parent;
11986 }
11987
11988 /**
11989 * <p>Computes the coordinates of this view on the screen. The argument
11990 * must be an array of two integers. After the method returns, the array
11991 * contains the x and y location in that order.</p>
11992 *
11993 * @param location an array of two integers in which to hold the coordinates
11994 */
11995 public void getLocationOnScreen(int[] location) {
11996 getLocationInWindow(location);
11997
11998 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070011999 if (info != null) {
12000 location[0] += info.mWindowLeft;
12001 location[1] += info.mWindowTop;
12002 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012003 }
12004
12005 /**
12006 * <p>Computes the coordinates of this view in its window. The argument
12007 * must be an array of two integers. After the method returns, the array
12008 * contains the x and y location in that order.</p>
12009 *
12010 * @param location an array of two integers in which to hold the coordinates
12011 */
12012 public void getLocationInWindow(int[] location) {
12013 if (location == null || location.length < 2) {
12014 throw new IllegalArgumentException("location must be an array of "
12015 + "two integers");
12016 }
12017
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012018 location[0] = mLeft;
12019 location[1] = mTop;
12020 if (mTransformationInfo != null) {
12021 location[0] += (int) (mTransformationInfo.mTranslationX + 0.5f);
12022 location[1] += (int) (mTransformationInfo.mTranslationY + 0.5f);
12023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012024
12025 ViewParent viewParent = mParent;
12026 while (viewParent instanceof View) {
12027 final View view = (View)viewParent;
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012028 location[0] += view.mLeft - view.mScrollX;
12029 location[1] += view.mTop - view.mScrollY;
12030 if (view.mTransformationInfo != null) {
12031 location[0] += (int) (view.mTransformationInfo.mTranslationX + 0.5f);
12032 location[1] += (int) (view.mTransformationInfo.mTranslationY + 0.5f);
12033 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012034 viewParent = view.mParent;
12035 }
Romain Guy8506ab42009-06-11 17:35:47 -070012036
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012037 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012038 // *cough*
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012039 final ViewRootImpl vr = (ViewRootImpl)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012040 location[1] -= vr.mCurScrollY;
12041 }
12042 }
12043
12044 /**
12045 * {@hide}
12046 * @param id the id of the view to be found
12047 * @return the view of the specified id, null if cannot be found
12048 */
12049 protected View findViewTraversal(int id) {
12050 if (id == mID) {
12051 return this;
12052 }
12053 return null;
12054 }
12055
12056 /**
12057 * {@hide}
12058 * @param tag the tag of the view to be found
12059 * @return the view of specified tag, null if cannot be found
12060 */
12061 protected View findViewWithTagTraversal(Object tag) {
12062 if (tag != null && tag.equals(mTag)) {
12063 return this;
12064 }
12065 return null;
12066 }
12067
12068 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012069 * {@hide}
12070 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070012071 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080012072 * @return The first view that matches the predicate or null.
12073 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070012074 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080012075 if (predicate.apply(this)) {
12076 return this;
12077 }
12078 return null;
12079 }
12080
12081 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012082 * Look for a child view with the given id. If this view has the given
12083 * id, return this view.
12084 *
12085 * @param id The id to search for.
12086 * @return The view that has the given id in the hierarchy or null
12087 */
12088 public final View findViewById(int id) {
12089 if (id < 0) {
12090 return null;
12091 }
12092 return findViewTraversal(id);
12093 }
12094
12095 /**
12096 * Look for a child view with the given tag. If this view has the given
12097 * tag, return this view.
12098 *
12099 * @param tag The tag to search for, using "tag.equals(getTag())".
12100 * @return The View that has the given tag in the hierarchy or null
12101 */
12102 public final View findViewWithTag(Object tag) {
12103 if (tag == null) {
12104 return null;
12105 }
12106 return findViewWithTagTraversal(tag);
12107 }
12108
12109 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012110 * {@hide}
12111 * Look for a child view that matches the specified predicate.
12112 * If this view matches the predicate, return this view.
12113 *
12114 * @param predicate The predicate to evaluate.
12115 * @return The first view that matches the predicate or null.
12116 */
12117 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070012118 return findViewByPredicateTraversal(predicate, null);
12119 }
12120
12121 /**
12122 * {@hide}
12123 * Look for a child view that matches the specified predicate,
12124 * starting with the specified view and its descendents and then
12125 * recusively searching the ancestors and siblings of that view
12126 * until this view is reached.
12127 *
12128 * This method is useful in cases where the predicate does not match
12129 * a single unique view (perhaps multiple views use the same id)
12130 * and we are trying to find the view that is "closest" in scope to the
12131 * starting view.
12132 *
12133 * @param start The view to start from.
12134 * @param predicate The predicate to evaluate.
12135 * @return The first view that matches the predicate or null.
12136 */
12137 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
12138 View childToSkip = null;
12139 for (;;) {
12140 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
12141 if (view != null || start == this) {
12142 return view;
12143 }
12144
12145 ViewParent parent = start.getParent();
12146 if (parent == null || !(parent instanceof View)) {
12147 return null;
12148 }
12149
12150 childToSkip = start;
12151 start = (View) parent;
12152 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080012153 }
12154
12155 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012156 * Sets the identifier for this view. The identifier does not have to be
12157 * unique in this view's hierarchy. The identifier should be a positive
12158 * number.
12159 *
12160 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070012161 * @see #getId()
12162 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012163 *
12164 * @param id a number used to identify the view
12165 *
12166 * @attr ref android.R.styleable#View_id
12167 */
12168 public void setId(int id) {
12169 mID = id;
12170 }
12171
12172 /**
12173 * {@hide}
12174 *
12175 * @param isRoot true if the view belongs to the root namespace, false
12176 * otherwise
12177 */
12178 public void setIsRootNamespace(boolean isRoot) {
12179 if (isRoot) {
12180 mPrivateFlags |= IS_ROOT_NAMESPACE;
12181 } else {
12182 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
12183 }
12184 }
12185
12186 /**
12187 * {@hide}
12188 *
12189 * @return true if the view belongs to the root namespace, false otherwise
12190 */
12191 public boolean isRootNamespace() {
12192 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
12193 }
12194
12195 /**
12196 * Returns this view's identifier.
12197 *
12198 * @return a positive integer used to identify the view or {@link #NO_ID}
12199 * if the view has no ID
12200 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012201 * @see #setId(int)
12202 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012203 * @attr ref android.R.styleable#View_id
12204 */
12205 @ViewDebug.CapturedViewProperty
12206 public int getId() {
12207 return mID;
12208 }
12209
12210 /**
12211 * Returns this view's tag.
12212 *
12213 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070012214 *
12215 * @see #setTag(Object)
12216 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012217 */
12218 @ViewDebug.ExportedProperty
12219 public Object getTag() {
12220 return mTag;
12221 }
12222
12223 /**
12224 * Sets the tag associated with this view. A tag can be used to mark
12225 * a view in its hierarchy and does not have to be unique within the
12226 * hierarchy. Tags can also be used to store data within a view without
12227 * resorting to another data structure.
12228 *
12229 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070012230 *
12231 * @see #getTag()
12232 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012233 */
12234 public void setTag(final Object tag) {
12235 mTag = tag;
12236 }
12237
12238 /**
Romain Guyd90a3312009-05-06 14:54:28 -070012239 * Returns the tag associated with this view and the specified key.
12240 *
12241 * @param key The key identifying the tag
12242 *
12243 * @return the Object stored in this view as a tag
12244 *
12245 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070012246 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070012247 */
12248 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012249 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070012250 return null;
12251 }
12252
12253 /**
12254 * Sets a tag associated with this view and a key. A tag can be used
12255 * to mark a view in its hierarchy and does not have to be unique within
12256 * the hierarchy. Tags can also be used to store data within a view
12257 * without resorting to another data structure.
12258 *
12259 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070012260 * application to ensure it is unique (see the <a
12261 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
12262 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070012263 * the Android framework or not associated with any package will cause
12264 * an {@link IllegalArgumentException} to be thrown.
12265 *
12266 * @param key The key identifying the tag
12267 * @param tag An Object to tag the view with
12268 *
12269 * @throws IllegalArgumentException If they specified key is not valid
12270 *
12271 * @see #setTag(Object)
12272 * @see #getTag(int)
12273 */
12274 public void setTag(int key, final Object tag) {
12275 // If the package id is 0x00 or 0x01, it's either an undefined package
12276 // or a framework id
12277 if ((key >>> 24) < 2) {
12278 throw new IllegalArgumentException("The key must be an application-specific "
12279 + "resource id.");
12280 }
12281
Adam Powell2b2f6d62011-09-23 11:15:39 -070012282 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012283 }
12284
12285 /**
12286 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
12287 * framework id.
12288 *
12289 * @hide
12290 */
12291 public void setTagInternal(int key, Object tag) {
12292 if ((key >>> 24) != 0x1) {
12293 throw new IllegalArgumentException("The key must be a framework-specific "
12294 + "resource id.");
12295 }
12296
Adam Powell2b2f6d62011-09-23 11:15:39 -070012297 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012298 }
12299
Adam Powell2b2f6d62011-09-23 11:15:39 -070012300 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012301 if (mKeyedTags == null) {
12302 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070012303 }
12304
Adam Powell7db82ac2011-09-22 19:44:04 -070012305 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012306 }
12307
12308 /**
Romain Guy13922e02009-05-12 17:56:14 -070012309 * @param consistency The type of consistency. See ViewDebug for more information.
12310 *
12311 * @hide
12312 */
12313 protected boolean dispatchConsistencyCheck(int consistency) {
12314 return onConsistencyCheck(consistency);
12315 }
12316
12317 /**
12318 * Method that subclasses should implement to check their consistency. The type of
12319 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070012320 *
Romain Guy13922e02009-05-12 17:56:14 -070012321 * @param consistency The type of consistency. See ViewDebug for more information.
12322 *
12323 * @throws IllegalStateException if the view is in an inconsistent state.
12324 *
12325 * @hide
12326 */
12327 protected boolean onConsistencyCheck(int consistency) {
12328 boolean result = true;
12329
12330 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
12331 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
12332
12333 if (checkLayout) {
12334 if (getParent() == null) {
12335 result = false;
12336 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12337 "View " + this + " does not have a parent.");
12338 }
12339
12340 if (mAttachInfo == null) {
12341 result = false;
12342 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12343 "View " + this + " is not attached to a window.");
12344 }
12345 }
12346
12347 if (checkDrawing) {
12348 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
12349 // from their draw() method
12350
12351 if ((mPrivateFlags & DRAWN) != DRAWN &&
12352 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
12353 result = false;
12354 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12355 "View " + this + " was invalidated but its drawing cache is valid.");
12356 }
12357 }
12358
12359 return result;
12360 }
12361
12362 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012363 * Prints information about this view in the log output, with the tag
12364 * {@link #VIEW_LOG_TAG}.
12365 *
12366 * @hide
12367 */
12368 public void debug() {
12369 debug(0);
12370 }
12371
12372 /**
12373 * Prints information about this view in the log output, with the tag
12374 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
12375 * indentation defined by the <code>depth</code>.
12376 *
12377 * @param depth the indentation level
12378 *
12379 * @hide
12380 */
12381 protected void debug(int depth) {
12382 String output = debugIndent(depth - 1);
12383
12384 output += "+ " + this;
12385 int id = getId();
12386 if (id != -1) {
12387 output += " (id=" + id + ")";
12388 }
12389 Object tag = getTag();
12390 if (tag != null) {
12391 output += " (tag=" + tag + ")";
12392 }
12393 Log.d(VIEW_LOG_TAG, output);
12394
12395 if ((mPrivateFlags & FOCUSED) != 0) {
12396 output = debugIndent(depth) + " FOCUSED";
12397 Log.d(VIEW_LOG_TAG, output);
12398 }
12399
12400 output = debugIndent(depth);
12401 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
12402 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
12403 + "} ";
12404 Log.d(VIEW_LOG_TAG, output);
12405
12406 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
12407 || mPaddingBottom != 0) {
12408 output = debugIndent(depth);
12409 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
12410 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
12411 Log.d(VIEW_LOG_TAG, output);
12412 }
12413
12414 output = debugIndent(depth);
12415 output += "mMeasureWidth=" + mMeasuredWidth +
12416 " mMeasureHeight=" + mMeasuredHeight;
12417 Log.d(VIEW_LOG_TAG, output);
12418
12419 output = debugIndent(depth);
12420 if (mLayoutParams == null) {
12421 output += "BAD! no layout params";
12422 } else {
12423 output = mLayoutParams.debug(output);
12424 }
12425 Log.d(VIEW_LOG_TAG, output);
12426
12427 output = debugIndent(depth);
12428 output += "flags={";
12429 output += View.printFlags(mViewFlags);
12430 output += "}";
12431 Log.d(VIEW_LOG_TAG, output);
12432
12433 output = debugIndent(depth);
12434 output += "privateFlags={";
12435 output += View.printPrivateFlags(mPrivateFlags);
12436 output += "}";
12437 Log.d(VIEW_LOG_TAG, output);
12438 }
12439
12440 /**
12441 * Creates an string of whitespaces used for indentation.
12442 *
12443 * @param depth the indentation level
12444 * @return a String containing (depth * 2 + 3) * 2 white spaces
12445 *
12446 * @hide
12447 */
12448 protected static String debugIndent(int depth) {
12449 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
12450 for (int i = 0; i < (depth * 2) + 3; i++) {
12451 spaces.append(' ').append(' ');
12452 }
12453 return spaces.toString();
12454 }
12455
12456 /**
12457 * <p>Return the offset of the widget's text baseline from the widget's top
12458 * boundary. If this widget does not support baseline alignment, this
12459 * method returns -1. </p>
12460 *
12461 * @return the offset of the baseline within the widget's bounds or -1
12462 * if baseline alignment is not supported
12463 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012464 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012465 public int getBaseline() {
12466 return -1;
12467 }
12468
12469 /**
12470 * Call this when something has changed which has invalidated the
12471 * layout of this view. This will schedule a layout pass of the view
12472 * tree.
12473 */
12474 public void requestLayout() {
12475 if (ViewDebug.TRACE_HIERARCHY) {
12476 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
12477 }
12478
12479 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012480 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012481
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070012482 if (mParent != null) {
12483 if (mLayoutParams != null) {
12484 mLayoutParams.resolveWithDirection(getResolvedLayoutDirection());
12485 }
12486 if (!mParent.isLayoutRequested()) {
12487 mParent.requestLayout();
12488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012489 }
12490 }
12491
12492 /**
12493 * Forces this view to be laid out during the next layout pass.
12494 * This method does not call requestLayout() or forceLayout()
12495 * on the parent.
12496 */
12497 public void forceLayout() {
12498 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012499 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012500 }
12501
12502 /**
12503 * <p>
12504 * This is called to find out how big a view should be. The parent
12505 * supplies constraint information in the width and height parameters.
12506 * </p>
12507 *
12508 * <p>
12509 * The actual mesurement work of a view is performed in
12510 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
12511 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
12512 * </p>
12513 *
12514 *
12515 * @param widthMeasureSpec Horizontal space requirements as imposed by the
12516 * parent
12517 * @param heightMeasureSpec Vertical space requirements as imposed by the
12518 * parent
12519 *
12520 * @see #onMeasure(int, int)
12521 */
12522 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
12523 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
12524 widthMeasureSpec != mOldWidthMeasureSpec ||
12525 heightMeasureSpec != mOldHeightMeasureSpec) {
12526
12527 // first clears the measured dimension flag
12528 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
12529
12530 if (ViewDebug.TRACE_HIERARCHY) {
12531 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
12532 }
12533
12534 // measure ourselves, this should set the measured dimension flag back
12535 onMeasure(widthMeasureSpec, heightMeasureSpec);
12536
12537 // flag not set, setMeasuredDimension() was not invoked, we raise
12538 // an exception to warn the developer
12539 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
12540 throw new IllegalStateException("onMeasure() did not set the"
12541 + " measured dimension by calling"
12542 + " setMeasuredDimension()");
12543 }
12544
12545 mPrivateFlags |= LAYOUT_REQUIRED;
12546 }
12547
12548 mOldWidthMeasureSpec = widthMeasureSpec;
12549 mOldHeightMeasureSpec = heightMeasureSpec;
12550 }
12551
12552 /**
12553 * <p>
12554 * Measure the view and its content to determine the measured width and the
12555 * measured height. This method is invoked by {@link #measure(int, int)} and
12556 * should be overriden by subclasses to provide accurate and efficient
12557 * measurement of their contents.
12558 * </p>
12559 *
12560 * <p>
12561 * <strong>CONTRACT:</strong> When overriding this method, you
12562 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
12563 * measured width and height of this view. Failure to do so will trigger an
12564 * <code>IllegalStateException</code>, thrown by
12565 * {@link #measure(int, int)}. Calling the superclass'
12566 * {@link #onMeasure(int, int)} is a valid use.
12567 * </p>
12568 *
12569 * <p>
12570 * The base class implementation of measure defaults to the background size,
12571 * unless a larger size is allowed by the MeasureSpec. Subclasses should
12572 * override {@link #onMeasure(int, int)} to provide better measurements of
12573 * their content.
12574 * </p>
12575 *
12576 * <p>
12577 * If this method is overridden, it is the subclass's responsibility to make
12578 * sure the measured height and width are at least the view's minimum height
12579 * and width ({@link #getSuggestedMinimumHeight()} and
12580 * {@link #getSuggestedMinimumWidth()}).
12581 * </p>
12582 *
12583 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
12584 * The requirements are encoded with
12585 * {@link android.view.View.MeasureSpec}.
12586 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
12587 * The requirements are encoded with
12588 * {@link android.view.View.MeasureSpec}.
12589 *
12590 * @see #getMeasuredWidth()
12591 * @see #getMeasuredHeight()
12592 * @see #setMeasuredDimension(int, int)
12593 * @see #getSuggestedMinimumHeight()
12594 * @see #getSuggestedMinimumWidth()
12595 * @see android.view.View.MeasureSpec#getMode(int)
12596 * @see android.view.View.MeasureSpec#getSize(int)
12597 */
12598 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
12599 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
12600 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
12601 }
12602
12603 /**
12604 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
12605 * measured width and measured height. Failing to do so will trigger an
12606 * exception at measurement time.</p>
12607 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080012608 * @param measuredWidth The measured width of this view. May be a complex
12609 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12610 * {@link #MEASURED_STATE_TOO_SMALL}.
12611 * @param measuredHeight The measured height of this view. May be a complex
12612 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12613 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012614 */
12615 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
12616 mMeasuredWidth = measuredWidth;
12617 mMeasuredHeight = measuredHeight;
12618
12619 mPrivateFlags |= MEASURED_DIMENSION_SET;
12620 }
12621
12622 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080012623 * Merge two states as returned by {@link #getMeasuredState()}.
12624 * @param curState The current state as returned from a view or the result
12625 * of combining multiple views.
12626 * @param newState The new view state to combine.
12627 * @return Returns a new integer reflecting the combination of the two
12628 * states.
12629 */
12630 public static int combineMeasuredStates(int curState, int newState) {
12631 return curState | newState;
12632 }
12633
12634 /**
12635 * Version of {@link #resolveSizeAndState(int, int, int)}
12636 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
12637 */
12638 public static int resolveSize(int size, int measureSpec) {
12639 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
12640 }
12641
12642 /**
12643 * Utility to reconcile a desired size and state, with constraints imposed
12644 * by a MeasureSpec. Will take the desired size, unless a different size
12645 * is imposed by the constraints. The returned value is a compound integer,
12646 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
12647 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
12648 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012649 *
12650 * @param size How big the view wants to be
12651 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080012652 * @return Size information bit mask as defined by
12653 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012654 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080012655 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012656 int result = size;
12657 int specMode = MeasureSpec.getMode(measureSpec);
12658 int specSize = MeasureSpec.getSize(measureSpec);
12659 switch (specMode) {
12660 case MeasureSpec.UNSPECIFIED:
12661 result = size;
12662 break;
12663 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080012664 if (specSize < size) {
12665 result = specSize | MEASURED_STATE_TOO_SMALL;
12666 } else {
12667 result = size;
12668 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012669 break;
12670 case MeasureSpec.EXACTLY:
12671 result = specSize;
12672 break;
12673 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080012674 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012675 }
12676
12677 /**
12678 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070012679 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012680 * by the MeasureSpec.
12681 *
12682 * @param size Default size for this view
12683 * @param measureSpec Constraints imposed by the parent
12684 * @return The size this view should be.
12685 */
12686 public static int getDefaultSize(int size, int measureSpec) {
12687 int result = size;
12688 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070012689 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012690
12691 switch (specMode) {
12692 case MeasureSpec.UNSPECIFIED:
12693 result = size;
12694 break;
12695 case MeasureSpec.AT_MOST:
12696 case MeasureSpec.EXACTLY:
12697 result = specSize;
12698 break;
12699 }
12700 return result;
12701 }
12702
12703 /**
12704 * Returns the suggested minimum height that the view should use. This
12705 * returns the maximum of the view's minimum height
12706 * and the background's minimum height
12707 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
12708 * <p>
12709 * When being used in {@link #onMeasure(int, int)}, the caller should still
12710 * ensure the returned height is within the requirements of the parent.
12711 *
12712 * @return The suggested minimum height of the view.
12713 */
12714 protected int getSuggestedMinimumHeight() {
12715 int suggestedMinHeight = mMinHeight;
12716
12717 if (mBGDrawable != null) {
12718 final int bgMinHeight = mBGDrawable.getMinimumHeight();
12719 if (suggestedMinHeight < bgMinHeight) {
12720 suggestedMinHeight = bgMinHeight;
12721 }
12722 }
12723
12724 return suggestedMinHeight;
12725 }
12726
12727 /**
12728 * Returns the suggested minimum width that the view should use. This
12729 * returns the maximum of the view's minimum width)
12730 * and the background's minimum width
12731 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
12732 * <p>
12733 * When being used in {@link #onMeasure(int, int)}, the caller should still
12734 * ensure the returned width is within the requirements of the parent.
12735 *
12736 * @return The suggested minimum width of the view.
12737 */
12738 protected int getSuggestedMinimumWidth() {
12739 int suggestedMinWidth = mMinWidth;
12740
12741 if (mBGDrawable != null) {
12742 final int bgMinWidth = mBGDrawable.getMinimumWidth();
12743 if (suggestedMinWidth < bgMinWidth) {
12744 suggestedMinWidth = bgMinWidth;
12745 }
12746 }
12747
12748 return suggestedMinWidth;
12749 }
12750
12751 /**
12752 * Sets the minimum height of the view. It is not guaranteed the view will
12753 * be able to achieve this minimum height (for example, if its parent layout
12754 * constrains it with less available height).
12755 *
12756 * @param minHeight The minimum height the view will try to be.
12757 */
12758 public void setMinimumHeight(int minHeight) {
12759 mMinHeight = minHeight;
12760 }
12761
12762 /**
12763 * Sets the minimum width of the view. It is not guaranteed the view will
12764 * be able to achieve this minimum width (for example, if its parent layout
12765 * constrains it with less available width).
12766 *
12767 * @param minWidth The minimum width the view will try to be.
12768 */
12769 public void setMinimumWidth(int minWidth) {
12770 mMinWidth = minWidth;
12771 }
12772
12773 /**
12774 * Get the animation currently associated with this view.
12775 *
12776 * @return The animation that is currently playing or
12777 * scheduled to play for this view.
12778 */
12779 public Animation getAnimation() {
12780 return mCurrentAnimation;
12781 }
12782
12783 /**
12784 * Start the specified animation now.
12785 *
12786 * @param animation the animation to start now
12787 */
12788 public void startAnimation(Animation animation) {
12789 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
12790 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012791 invalidateParentCaches();
12792 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012793 }
12794
12795 /**
12796 * Cancels any animations for this view.
12797 */
12798 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080012799 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080012800 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080012801 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012802 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012803 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012804 }
12805
12806 /**
12807 * Sets the next animation to play for this view.
12808 * If you want the animation to play immediately, use
12809 * startAnimation. This method provides allows fine-grained
12810 * control over the start time and invalidation, but you
12811 * must make sure that 1) the animation has a start time set, and
12812 * 2) the view will be invalidated when the animation is supposed to
12813 * start.
12814 *
12815 * @param animation The next animation, or null.
12816 */
12817 public void setAnimation(Animation animation) {
12818 mCurrentAnimation = animation;
12819 if (animation != null) {
12820 animation.reset();
12821 }
12822 }
12823
12824 /**
12825 * Invoked by a parent ViewGroup to notify the start of the animation
12826 * currently associated with this view. If you override this method,
12827 * always call super.onAnimationStart();
12828 *
12829 * @see #setAnimation(android.view.animation.Animation)
12830 * @see #getAnimation()
12831 */
12832 protected void onAnimationStart() {
12833 mPrivateFlags |= ANIMATION_STARTED;
12834 }
12835
12836 /**
12837 * Invoked by a parent ViewGroup to notify the end of the animation
12838 * currently associated with this view. If you override this method,
12839 * always call super.onAnimationEnd();
12840 *
12841 * @see #setAnimation(android.view.animation.Animation)
12842 * @see #getAnimation()
12843 */
12844 protected void onAnimationEnd() {
12845 mPrivateFlags &= ~ANIMATION_STARTED;
12846 }
12847
12848 /**
12849 * Invoked if there is a Transform that involves alpha. Subclass that can
12850 * draw themselves with the specified alpha should return true, and then
12851 * respect that alpha when their onDraw() is called. If this returns false
12852 * then the view may be redirected to draw into an offscreen buffer to
12853 * fulfill the request, which will look fine, but may be slower than if the
12854 * subclass handles it internally. The default implementation returns false.
12855 *
12856 * @param alpha The alpha (0..255) to apply to the view's drawing
12857 * @return true if the view can draw with the specified alpha.
12858 */
12859 protected boolean onSetAlpha(int alpha) {
12860 return false;
12861 }
12862
12863 /**
12864 * This is used by the RootView to perform an optimization when
12865 * the view hierarchy contains one or several SurfaceView.
12866 * SurfaceView is always considered transparent, but its children are not,
12867 * therefore all View objects remove themselves from the global transparent
12868 * region (passed as a parameter to this function).
12869 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012870 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012871 *
12872 * @return Returns true if the effective visibility of the view at this
12873 * point is opaque, regardless of the transparent region; returns false
12874 * if it is possible for underlying windows to be seen behind the view.
12875 *
12876 * {@hide}
12877 */
12878 public boolean gatherTransparentRegion(Region region) {
12879 final AttachInfo attachInfo = mAttachInfo;
12880 if (region != null && attachInfo != null) {
12881 final int pflags = mPrivateFlags;
12882 if ((pflags & SKIP_DRAW) == 0) {
12883 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
12884 // remove it from the transparent region.
12885 final int[] location = attachInfo.mTransparentLocation;
12886 getLocationInWindow(location);
12887 region.op(location[0], location[1], location[0] + mRight - mLeft,
12888 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
12889 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
12890 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
12891 // exists, so we remove the background drawable's non-transparent
12892 // parts from this transparent region.
12893 applyDrawableToTransparentRegion(mBGDrawable, region);
12894 }
12895 }
12896 return true;
12897 }
12898
12899 /**
12900 * Play a sound effect for this view.
12901 *
12902 * <p>The framework will play sound effects for some built in actions, such as
12903 * clicking, but you may wish to play these effects in your widget,
12904 * for instance, for internal navigation.
12905 *
12906 * <p>The sound effect will only be played if sound effects are enabled by the user, and
12907 * {@link #isSoundEffectsEnabled()} is true.
12908 *
12909 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
12910 */
12911 public void playSoundEffect(int soundConstant) {
12912 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
12913 return;
12914 }
12915 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
12916 }
12917
12918 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012919 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012920 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012921 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012922 *
12923 * <p>The framework will provide haptic feedback for some built in actions,
12924 * such as long presses, but you may wish to provide feedback for your
12925 * own widget.
12926 *
12927 * <p>The feedback will only be performed if
12928 * {@link #isHapticFeedbackEnabled()} is true.
12929 *
12930 * @param feedbackConstant One of the constants defined in
12931 * {@link HapticFeedbackConstants}
12932 */
12933 public boolean performHapticFeedback(int feedbackConstant) {
12934 return performHapticFeedback(feedbackConstant, 0);
12935 }
12936
12937 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012938 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012939 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012940 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012941 *
12942 * @param feedbackConstant One of the constants defined in
12943 * {@link HapticFeedbackConstants}
12944 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
12945 */
12946 public boolean performHapticFeedback(int feedbackConstant, int flags) {
12947 if (mAttachInfo == null) {
12948 return false;
12949 }
Romain Guyf607bdc2010-09-10 19:20:06 -070012950 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070012951 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012952 && !isHapticFeedbackEnabled()) {
12953 return false;
12954 }
Romain Guy812ccbe2010-06-01 14:07:24 -070012955 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
12956 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012957 }
12958
12959 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012960 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040012961 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
12962 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080012963 */
12964 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040012965 if (visibility != mSystemUiVisibility) {
12966 mSystemUiVisibility = visibility;
12967 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12968 mParent.recomputeViewAttributes(this);
12969 }
Joe Onorato664644d2011-01-23 17:53:23 -080012970 }
12971 }
12972
12973 /**
12974 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040012975 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
12976 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080012977 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080012978 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080012979 return mSystemUiVisibility;
12980 }
12981
Scott Mainec6331b2011-05-24 16:55:56 -070012982 /**
12983 * Set a listener to receive callbacks when the visibility of the system bar changes.
12984 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
12985 */
Joe Onorato664644d2011-01-23 17:53:23 -080012986 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
12987 mOnSystemUiVisibilityChangeListener = l;
12988 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12989 mParent.recomputeViewAttributes(this);
12990 }
12991 }
12992
12993 /**
12994 */
12995 public void dispatchSystemUiVisibilityChanged(int visibility) {
12996 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080012997 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080012998 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080012999 }
13000 }
13001
13002 /**
Joe Malin32736f02011-01-19 16:14:20 -080013003 * Creates an image that the system displays during the drag and drop
13004 * operation. This is called a &quot;drag shadow&quot;. The default implementation
13005 * for a DragShadowBuilder based on a View returns an image that has exactly the same
13006 * appearance as the given View. The default also positions the center of the drag shadow
13007 * directly under the touch point. If no View is provided (the constructor with no parameters
13008 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
13009 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
13010 * default is an invisible drag shadow.
13011 * <p>
13012 * You are not required to use the View you provide to the constructor as the basis of the
13013 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
13014 * anything you want as the drag shadow.
13015 * </p>
13016 * <p>
13017 * You pass a DragShadowBuilder object to the system when you start the drag. The system
13018 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
13019 * size and position of the drag shadow. It uses this data to construct a
13020 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
13021 * so that your application can draw the shadow image in the Canvas.
13022 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013023 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013024 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070013025 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070013026
13027 /**
Joe Malin32736f02011-01-19 16:14:20 -080013028 * Constructs a shadow image builder based on a View. By default, the resulting drag
13029 * shadow will have the same appearance and dimensions as the View, with the touch point
13030 * over the center of the View.
13031 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070013032 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013033 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070013034 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070013035 }
13036
Christopher Tate17ed60c2011-01-18 12:50:26 -080013037 /**
13038 * Construct a shadow builder object with no associated View. This
13039 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
13040 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
13041 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080013042 * reference to any View object. If they are not overridden, then the result is an
13043 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080013044 */
13045 public DragShadowBuilder() {
13046 mView = new WeakReference<View>(null);
13047 }
13048
13049 /**
13050 * Returns the View object that had been passed to the
13051 * {@link #View.DragShadowBuilder(View)}
13052 * constructor. If that View parameter was {@code null} or if the
13053 * {@link #View.DragShadowBuilder()}
13054 * constructor was used to instantiate the builder object, this method will return
13055 * null.
13056 *
13057 * @return The View object associate with this builder object.
13058 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070013059 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070013060 final public View getView() {
13061 return mView.get();
13062 }
13063
Christopher Tate2c095f32010-10-04 14:13:40 -070013064 /**
Joe Malin32736f02011-01-19 16:14:20 -080013065 * Provides the metrics for the shadow image. These include the dimensions of
13066 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070013067 * be centered under the touch location while dragging.
13068 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013069 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080013070 * same as the dimensions of the View itself and centers the shadow under
13071 * the touch point.
13072 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013073 *
Joe Malin32736f02011-01-19 16:14:20 -080013074 * @param shadowSize A {@link android.graphics.Point} containing the width and height
13075 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
13076 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
13077 * image.
13078 *
13079 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
13080 * shadow image that should be underneath the touch point during the drag and drop
13081 * operation. Your application must set {@link android.graphics.Point#x} to the
13082 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070013083 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013084 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070013085 final View view = mView.get();
13086 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013087 shadowSize.set(view.getWidth(), view.getHeight());
13088 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070013089 } else {
13090 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
13091 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013092 }
13093
13094 /**
Joe Malin32736f02011-01-19 16:14:20 -080013095 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
13096 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013097 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070013098 *
Joe Malin32736f02011-01-19 16:14:20 -080013099 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070013100 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013101 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070013102 final View view = mView.get();
13103 if (view != null) {
13104 view.draw(canvas);
13105 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013106 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070013107 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013108 }
13109 }
13110
13111 /**
Joe Malin32736f02011-01-19 16:14:20 -080013112 * Starts a drag and drop operation. When your application calls this method, it passes a
13113 * {@link android.view.View.DragShadowBuilder} object to the system. The
13114 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
13115 * to get metrics for the drag shadow, and then calls the object's
13116 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
13117 * <p>
13118 * Once the system has the drag shadow, it begins the drag and drop operation by sending
13119 * drag events to all the View objects in your application that are currently visible. It does
13120 * this either by calling the View object's drag listener (an implementation of
13121 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
13122 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
13123 * Both are passed a {@link android.view.DragEvent} object that has a
13124 * {@link android.view.DragEvent#getAction()} value of
13125 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
13126 * </p>
13127 * <p>
13128 * Your application can invoke startDrag() on any attached View object. The View object does not
13129 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
13130 * be related to the View the user selected for dragging.
13131 * </p>
13132 * @param data A {@link android.content.ClipData} object pointing to the data to be
13133 * transferred by the drag and drop operation.
13134 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
13135 * drag shadow.
13136 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
13137 * drop operation. This Object is put into every DragEvent object sent by the system during the
13138 * current drag.
13139 * <p>
13140 * myLocalState is a lightweight mechanism for the sending information from the dragged View
13141 * to the target Views. For example, it can contain flags that differentiate between a
13142 * a copy operation and a move operation.
13143 * </p>
13144 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
13145 * so the parameter should be set to 0.
13146 * @return {@code true} if the method completes successfully, or
13147 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
13148 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070013149 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013150 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013151 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070013152 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013153 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070013154 }
13155 boolean okay = false;
13156
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013157 Point shadowSize = new Point();
13158 Point shadowTouchPoint = new Point();
13159 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070013160
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013161 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
13162 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
13163 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070013164 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013165
Chris Tatea32dcf72010-10-14 12:13:50 -070013166 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013167 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
13168 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070013169 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013170 Surface surface = new Surface();
13171 try {
13172 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013173 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070013174 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070013175 + " surface=" + surface);
13176 if (token != null) {
13177 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070013178 try {
Chris Tate6b391282010-10-14 15:48:59 -070013179 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013180 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070013181 } finally {
13182 surface.unlockCanvasAndPost(canvas);
13183 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013184
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013185 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080013186
13187 // Cache the local state object for delivery with DragEvents
13188 root.setLocalDragState(myLocalState);
13189
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013190 // repurpose 'shadowSize' for the last touch point
13191 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070013192
Christopher Tatea53146c2010-09-07 11:57:52 -070013193 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013194 shadowSize.x, shadowSize.y,
13195 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070013196 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070013197
13198 // Off and running! Release our local surface instance; the drag
13199 // shadow surface is now managed by the system process.
13200 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070013201 }
13202 } catch (Exception e) {
13203 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
13204 surface.destroy();
13205 }
13206
13207 return okay;
13208 }
13209
Christopher Tatea53146c2010-09-07 11:57:52 -070013210 /**
Joe Malin32736f02011-01-19 16:14:20 -080013211 * Handles drag events sent by the system following a call to
13212 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
13213 *<p>
13214 * When the system calls this method, it passes a
13215 * {@link android.view.DragEvent} object. A call to
13216 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
13217 * in DragEvent. The method uses these to determine what is happening in the drag and drop
13218 * operation.
13219 * @param event The {@link android.view.DragEvent} sent by the system.
13220 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
13221 * in DragEvent, indicating the type of drag event represented by this object.
13222 * @return {@code true} if the method was successful, otherwise {@code false}.
13223 * <p>
13224 * The method should return {@code true} in response to an action type of
13225 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
13226 * operation.
13227 * </p>
13228 * <p>
13229 * The method should also return {@code true} in response to an action type of
13230 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
13231 * {@code false} if it didn't.
13232 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013233 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070013234 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070013235 return false;
13236 }
13237
13238 /**
Joe Malin32736f02011-01-19 16:14:20 -080013239 * Detects if this View is enabled and has a drag event listener.
13240 * If both are true, then it calls the drag event listener with the
13241 * {@link android.view.DragEvent} it received. If the drag event listener returns
13242 * {@code true}, then dispatchDragEvent() returns {@code true}.
13243 * <p>
13244 * For all other cases, the method calls the
13245 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
13246 * method and returns its result.
13247 * </p>
13248 * <p>
13249 * This ensures that a drag event is always consumed, even if the View does not have a drag
13250 * event listener. However, if the View has a listener and the listener returns true, then
13251 * onDragEvent() is not called.
13252 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013253 */
13254 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080013255 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070013256 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
13257 && mOnDragListener.onDrag(this, event)) {
13258 return true;
13259 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013260 return onDragEvent(event);
13261 }
13262
Christopher Tate3d4bf172011-03-28 16:16:46 -070013263 boolean canAcceptDrag() {
13264 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
13265 }
13266
Christopher Tatea53146c2010-09-07 11:57:52 -070013267 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070013268 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
13269 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070013270 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070013271 */
13272 public void onCloseSystemDialogs(String reason) {
13273 }
Joe Malin32736f02011-01-19 16:14:20 -080013274
Dianne Hackbornffa42482009-09-23 22:20:11 -070013275 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013276 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070013277 * update a Region being computed for
13278 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013279 * that any non-transparent parts of the Drawable are removed from the
13280 * given transparent region.
13281 *
13282 * @param dr The Drawable whose transparency is to be applied to the region.
13283 * @param region A Region holding the current transparency information,
13284 * where any parts of the region that are set are considered to be
13285 * transparent. On return, this region will be modified to have the
13286 * transparency information reduced by the corresponding parts of the
13287 * Drawable that are not transparent.
13288 * {@hide}
13289 */
13290 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
13291 if (DBG) {
13292 Log.i("View", "Getting transparent region for: " + this);
13293 }
13294 final Region r = dr.getTransparentRegion();
13295 final Rect db = dr.getBounds();
13296 final AttachInfo attachInfo = mAttachInfo;
13297 if (r != null && attachInfo != null) {
13298 final int w = getRight()-getLeft();
13299 final int h = getBottom()-getTop();
13300 if (db.left > 0) {
13301 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
13302 r.op(0, 0, db.left, h, Region.Op.UNION);
13303 }
13304 if (db.right < w) {
13305 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
13306 r.op(db.right, 0, w, h, Region.Op.UNION);
13307 }
13308 if (db.top > 0) {
13309 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
13310 r.op(0, 0, w, db.top, Region.Op.UNION);
13311 }
13312 if (db.bottom < h) {
13313 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
13314 r.op(0, db.bottom, w, h, Region.Op.UNION);
13315 }
13316 final int[] location = attachInfo.mTransparentLocation;
13317 getLocationInWindow(location);
13318 r.translate(location[0], location[1]);
13319 region.op(r, Region.Op.INTERSECT);
13320 } else {
13321 region.op(db, Region.Op.DIFFERENCE);
13322 }
13323 }
13324
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013325 private void checkForLongClick(int delayOffset) {
13326 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
13327 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013328
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013329 if (mPendingCheckForLongPress == null) {
13330 mPendingCheckForLongPress = new CheckForLongPress();
13331 }
13332 mPendingCheckForLongPress.rememberWindowAttachCount();
13333 postDelayed(mPendingCheckForLongPress,
13334 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013335 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013336 }
13337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013338 /**
13339 * Inflate a view from an XML resource. This convenience method wraps the {@link
13340 * LayoutInflater} class, which provides a full range of options for view inflation.
13341 *
13342 * @param context The Context object for your activity or application.
13343 * @param resource The resource ID to inflate
13344 * @param root A view group that will be the parent. Used to properly inflate the
13345 * layout_* parameters.
13346 * @see LayoutInflater
13347 */
13348 public static View inflate(Context context, int resource, ViewGroup root) {
13349 LayoutInflater factory = LayoutInflater.from(context);
13350 return factory.inflate(resource, root);
13351 }
Romain Guy33e72ae2010-07-17 12:40:29 -070013352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013353 /**
Adam Powell637d3372010-08-25 14:37:03 -070013354 * Scroll the view with standard behavior for scrolling beyond the normal
13355 * content boundaries. Views that call this method should override
13356 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
13357 * results of an over-scroll operation.
13358 *
13359 * Views can use this method to handle any touch or fling-based scrolling.
13360 *
13361 * @param deltaX Change in X in pixels
13362 * @param deltaY Change in Y in pixels
13363 * @param scrollX Current X scroll value in pixels before applying deltaX
13364 * @param scrollY Current Y scroll value in pixels before applying deltaY
13365 * @param scrollRangeX Maximum content scroll range along the X axis
13366 * @param scrollRangeY Maximum content scroll range along the Y axis
13367 * @param maxOverScrollX Number of pixels to overscroll by in either direction
13368 * along the X axis.
13369 * @param maxOverScrollY Number of pixels to overscroll by in either direction
13370 * along the Y axis.
13371 * @param isTouchEvent true if this scroll operation is the result of a touch event.
13372 * @return true if scrolling was clamped to an over-scroll boundary along either
13373 * axis, false otherwise.
13374 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013375 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070013376 protected boolean overScrollBy(int deltaX, int deltaY,
13377 int scrollX, int scrollY,
13378 int scrollRangeX, int scrollRangeY,
13379 int maxOverScrollX, int maxOverScrollY,
13380 boolean isTouchEvent) {
13381 final int overScrollMode = mOverScrollMode;
13382 final boolean canScrollHorizontal =
13383 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
13384 final boolean canScrollVertical =
13385 computeVerticalScrollRange() > computeVerticalScrollExtent();
13386 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
13387 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
13388 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
13389 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
13390
13391 int newScrollX = scrollX + deltaX;
13392 if (!overScrollHorizontal) {
13393 maxOverScrollX = 0;
13394 }
13395
13396 int newScrollY = scrollY + deltaY;
13397 if (!overScrollVertical) {
13398 maxOverScrollY = 0;
13399 }
13400
13401 // Clamp values if at the limits and record
13402 final int left = -maxOverScrollX;
13403 final int right = maxOverScrollX + scrollRangeX;
13404 final int top = -maxOverScrollY;
13405 final int bottom = maxOverScrollY + scrollRangeY;
13406
13407 boolean clampedX = false;
13408 if (newScrollX > right) {
13409 newScrollX = right;
13410 clampedX = true;
13411 } else if (newScrollX < left) {
13412 newScrollX = left;
13413 clampedX = true;
13414 }
13415
13416 boolean clampedY = false;
13417 if (newScrollY > bottom) {
13418 newScrollY = bottom;
13419 clampedY = true;
13420 } else if (newScrollY < top) {
13421 newScrollY = top;
13422 clampedY = true;
13423 }
13424
13425 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
13426
13427 return clampedX || clampedY;
13428 }
13429
13430 /**
13431 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
13432 * respond to the results of an over-scroll operation.
13433 *
13434 * @param scrollX New X scroll value in pixels
13435 * @param scrollY New Y scroll value in pixels
13436 * @param clampedX True if scrollX was clamped to an over-scroll boundary
13437 * @param clampedY True if scrollY was clamped to an over-scroll boundary
13438 */
13439 protected void onOverScrolled(int scrollX, int scrollY,
13440 boolean clampedX, boolean clampedY) {
13441 // Intentionally empty.
13442 }
13443
13444 /**
13445 * Returns the over-scroll mode for this view. The result will be
13446 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13447 * (allow over-scrolling only if the view content is larger than the container),
13448 * or {@link #OVER_SCROLL_NEVER}.
13449 *
13450 * @return This view's over-scroll mode.
13451 */
13452 public int getOverScrollMode() {
13453 return mOverScrollMode;
13454 }
13455
13456 /**
13457 * Set the over-scroll mode for this view. Valid over-scroll modes are
13458 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13459 * (allow over-scrolling only if the view content is larger than the container),
13460 * or {@link #OVER_SCROLL_NEVER}.
13461 *
13462 * Setting the over-scroll mode of a view will have an effect only if the
13463 * view is capable of scrolling.
13464 *
13465 * @param overScrollMode The new over-scroll mode for this view.
13466 */
13467 public void setOverScrollMode(int overScrollMode) {
13468 if (overScrollMode != OVER_SCROLL_ALWAYS &&
13469 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
13470 overScrollMode != OVER_SCROLL_NEVER) {
13471 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
13472 }
13473 mOverScrollMode = overScrollMode;
13474 }
13475
13476 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013477 * Gets a scale factor that determines the distance the view should scroll
13478 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
13479 * @return The vertical scroll scale factor.
13480 * @hide
13481 */
13482 protected float getVerticalScrollFactor() {
13483 if (mVerticalScrollFactor == 0) {
13484 TypedValue outValue = new TypedValue();
13485 if (!mContext.getTheme().resolveAttribute(
13486 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
13487 throw new IllegalStateException(
13488 "Expected theme to define listPreferredItemHeight.");
13489 }
13490 mVerticalScrollFactor = outValue.getDimension(
13491 mContext.getResources().getDisplayMetrics());
13492 }
13493 return mVerticalScrollFactor;
13494 }
13495
13496 /**
13497 * Gets a scale factor that determines the distance the view should scroll
13498 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
13499 * @return The horizontal scroll scale factor.
13500 * @hide
13501 */
13502 protected float getHorizontalScrollFactor() {
13503 // TODO: Should use something else.
13504 return getVerticalScrollFactor();
13505 }
13506
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013507 /**
13508 * Return the value specifying the text direction or policy that was set with
13509 * {@link #setTextDirection(int)}.
13510 *
13511 * @return the defined text direction. It can be one of:
13512 *
13513 * {@link #TEXT_DIRECTION_INHERIT},
13514 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13515 * {@link #TEXT_DIRECTION_ANY_RTL},
13516 * {@link #TEXT_DIRECTION_LTR},
13517 * {@link #TEXT_DIRECTION_RTL},
13518 *
13519 * @hide
13520 */
13521 public int getTextDirection() {
13522 return mTextDirection;
13523 }
13524
13525 /**
13526 * Set the text direction.
13527 *
13528 * @param textDirection the direction to set. Should be one of:
13529 *
13530 * {@link #TEXT_DIRECTION_INHERIT},
13531 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13532 * {@link #TEXT_DIRECTION_ANY_RTL},
13533 * {@link #TEXT_DIRECTION_LTR},
13534 * {@link #TEXT_DIRECTION_RTL},
13535 *
13536 * @hide
13537 */
13538 public void setTextDirection(int textDirection) {
13539 if (textDirection != mTextDirection) {
13540 mTextDirection = textDirection;
13541 resetResolvedTextDirection();
13542 requestLayout();
13543 }
13544 }
13545
13546 /**
13547 * Return the resolved text direction.
13548 *
13549 * @return the resolved text direction. Return one of:
13550 *
Doug Feltcb3791202011-07-07 11:57:48 -070013551 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13552 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013553 * {@link #TEXT_DIRECTION_LTR},
13554 * {@link #TEXT_DIRECTION_RTL},
13555 *
13556 * @hide
13557 */
13558 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013559 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013560 resolveTextDirection();
13561 }
13562 return mResolvedTextDirection;
13563 }
13564
13565 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013566 * Resolve the text direction.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013567 *
13568 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013569 */
13570 protected void resolveTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013571 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
13572 mResolvedTextDirection = mTextDirection;
13573 return;
13574 }
13575 if (mParent != null && mParent instanceof ViewGroup) {
13576 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
13577 return;
13578 }
13579 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013580 }
13581
13582 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013583 * Reset resolved text direction. Will be resolved during a call to getResolvedTextDirection().
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013584 *
13585 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013586 */
13587 protected void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013588 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013589 }
13590
Chet Haaseb39f0512011-05-24 14:36:40 -070013591 //
13592 // Properties
13593 //
13594 /**
13595 * A Property wrapper around the <code>alpha</code> functionality handled by the
13596 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
13597 */
Romain Guy02ccac62011-06-24 13:20:23 -070013598 public static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013599 @Override
13600 public void setValue(View object, float value) {
13601 object.setAlpha(value);
13602 }
13603
13604 @Override
13605 public Float get(View object) {
13606 return object.getAlpha();
13607 }
13608 };
13609
13610 /**
13611 * A Property wrapper around the <code>translationX</code> functionality handled by the
13612 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
13613 */
13614 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
13615 @Override
13616 public void setValue(View object, float value) {
13617 object.setTranslationX(value);
13618 }
13619
13620 @Override
13621 public Float get(View object) {
13622 return object.getTranslationX();
13623 }
13624 };
13625
13626 /**
13627 * A Property wrapper around the <code>translationY</code> functionality handled by the
13628 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
13629 */
13630 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
13631 @Override
13632 public void setValue(View object, float value) {
13633 object.setTranslationY(value);
13634 }
13635
13636 @Override
13637 public Float get(View object) {
13638 return object.getTranslationY();
13639 }
13640 };
13641
13642 /**
13643 * A Property wrapper around the <code>x</code> functionality handled by the
13644 * {@link View#setX(float)} and {@link View#getX()} methods.
13645 */
13646 public static Property<View, Float> X = new FloatProperty<View>("x") {
13647 @Override
13648 public void setValue(View object, float value) {
13649 object.setX(value);
13650 }
13651
13652 @Override
13653 public Float get(View object) {
13654 return object.getX();
13655 }
13656 };
13657
13658 /**
13659 * A Property wrapper around the <code>y</code> functionality handled by the
13660 * {@link View#setY(float)} and {@link View#getY()} methods.
13661 */
13662 public static Property<View, Float> Y = new FloatProperty<View>("y") {
13663 @Override
13664 public void setValue(View object, float value) {
13665 object.setY(value);
13666 }
13667
13668 @Override
13669 public Float get(View object) {
13670 return object.getY();
13671 }
13672 };
13673
13674 /**
13675 * A Property wrapper around the <code>rotation</code> functionality handled by the
13676 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
13677 */
13678 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
13679 @Override
13680 public void setValue(View object, float value) {
13681 object.setRotation(value);
13682 }
13683
13684 @Override
13685 public Float get(View object) {
13686 return object.getRotation();
13687 }
13688 };
13689
13690 /**
13691 * A Property wrapper around the <code>rotationX</code> functionality handled by the
13692 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
13693 */
13694 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
13695 @Override
13696 public void setValue(View object, float value) {
13697 object.setRotationX(value);
13698 }
13699
13700 @Override
13701 public Float get(View object) {
13702 return object.getRotationX();
13703 }
13704 };
13705
13706 /**
13707 * A Property wrapper around the <code>rotationY</code> functionality handled by the
13708 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
13709 */
13710 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
13711 @Override
13712 public void setValue(View object, float value) {
13713 object.setRotationY(value);
13714 }
13715
13716 @Override
13717 public Float get(View object) {
13718 return object.getRotationY();
13719 }
13720 };
13721
13722 /**
13723 * A Property wrapper around the <code>scaleX</code> functionality handled by the
13724 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
13725 */
13726 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
13727 @Override
13728 public void setValue(View object, float value) {
13729 object.setScaleX(value);
13730 }
13731
13732 @Override
13733 public Float get(View object) {
13734 return object.getScaleX();
13735 }
13736 };
13737
13738 /**
13739 * A Property wrapper around the <code>scaleY</code> functionality handled by the
13740 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
13741 */
13742 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
13743 @Override
13744 public void setValue(View object, float value) {
13745 object.setScaleY(value);
13746 }
13747
13748 @Override
13749 public Float get(View object) {
13750 return object.getScaleY();
13751 }
13752 };
13753
Jeff Brown33bbfd22011-02-24 20:55:35 -080013754 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013755 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
13756 * Each MeasureSpec represents a requirement for either the width or the height.
13757 * A MeasureSpec is comprised of a size and a mode. There are three possible
13758 * modes:
13759 * <dl>
13760 * <dt>UNSPECIFIED</dt>
13761 * <dd>
13762 * The parent has not imposed any constraint on the child. It can be whatever size
13763 * it wants.
13764 * </dd>
13765 *
13766 * <dt>EXACTLY</dt>
13767 * <dd>
13768 * The parent has determined an exact size for the child. The child is going to be
13769 * given those bounds regardless of how big it wants to be.
13770 * </dd>
13771 *
13772 * <dt>AT_MOST</dt>
13773 * <dd>
13774 * The child can be as large as it wants up to the specified size.
13775 * </dd>
13776 * </dl>
13777 *
13778 * MeasureSpecs are implemented as ints to reduce object allocation. This class
13779 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
13780 */
13781 public static class MeasureSpec {
13782 private static final int MODE_SHIFT = 30;
13783 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
13784
13785 /**
13786 * Measure specification mode: The parent has not imposed any constraint
13787 * on the child. It can be whatever size it wants.
13788 */
13789 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
13790
13791 /**
13792 * Measure specification mode: The parent has determined an exact size
13793 * for the child. The child is going to be given those bounds regardless
13794 * of how big it wants to be.
13795 */
13796 public static final int EXACTLY = 1 << MODE_SHIFT;
13797
13798 /**
13799 * Measure specification mode: The child can be as large as it wants up
13800 * to the specified size.
13801 */
13802 public static final int AT_MOST = 2 << MODE_SHIFT;
13803
13804 /**
13805 * Creates a measure specification based on the supplied size and mode.
13806 *
13807 * The mode must always be one of the following:
13808 * <ul>
13809 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
13810 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
13811 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
13812 * </ul>
13813 *
13814 * @param size the size of the measure specification
13815 * @param mode the mode of the measure specification
13816 * @return the measure specification based on size and mode
13817 */
13818 public static int makeMeasureSpec(int size, int mode) {
13819 return size + mode;
13820 }
13821
13822 /**
13823 * Extracts the mode from the supplied measure specification.
13824 *
13825 * @param measureSpec the measure specification to extract the mode from
13826 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
13827 * {@link android.view.View.MeasureSpec#AT_MOST} or
13828 * {@link android.view.View.MeasureSpec#EXACTLY}
13829 */
13830 public static int getMode(int measureSpec) {
13831 return (measureSpec & MODE_MASK);
13832 }
13833
13834 /**
13835 * Extracts the size from the supplied measure specification.
13836 *
13837 * @param measureSpec the measure specification to extract the size from
13838 * @return the size in pixels defined in the supplied measure specification
13839 */
13840 public static int getSize(int measureSpec) {
13841 return (measureSpec & ~MODE_MASK);
13842 }
13843
13844 /**
13845 * Returns a String representation of the specified measure
13846 * specification.
13847 *
13848 * @param measureSpec the measure specification to convert to a String
13849 * @return a String with the following format: "MeasureSpec: MODE SIZE"
13850 */
13851 public static String toString(int measureSpec) {
13852 int mode = getMode(measureSpec);
13853 int size = getSize(measureSpec);
13854
13855 StringBuilder sb = new StringBuilder("MeasureSpec: ");
13856
13857 if (mode == UNSPECIFIED)
13858 sb.append("UNSPECIFIED ");
13859 else if (mode == EXACTLY)
13860 sb.append("EXACTLY ");
13861 else if (mode == AT_MOST)
13862 sb.append("AT_MOST ");
13863 else
13864 sb.append(mode).append(" ");
13865
13866 sb.append(size);
13867 return sb.toString();
13868 }
13869 }
13870
13871 class CheckForLongPress implements Runnable {
13872
13873 private int mOriginalWindowAttachCount;
13874
13875 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070013876 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013877 && mOriginalWindowAttachCount == mWindowAttachCount) {
13878 if (performLongClick()) {
13879 mHasPerformedLongPress = true;
13880 }
13881 }
13882 }
13883
13884 public void rememberWindowAttachCount() {
13885 mOriginalWindowAttachCount = mWindowAttachCount;
13886 }
13887 }
Joe Malin32736f02011-01-19 16:14:20 -080013888
Adam Powelle14579b2009-12-16 18:39:52 -080013889 private final class CheckForTap implements Runnable {
13890 public void run() {
13891 mPrivateFlags &= ~PREPRESSED;
13892 mPrivateFlags |= PRESSED;
13893 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013894 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080013895 }
13896 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013897
Adam Powella35d7682010-03-12 14:48:13 -080013898 private final class PerformClick implements Runnable {
13899 public void run() {
13900 performClick();
13901 }
13902 }
13903
Dianne Hackborn63042d62011-01-26 18:56:29 -080013904 /** @hide */
13905 public void hackTurnOffWindowResizeAnim(boolean off) {
13906 mAttachInfo.mTurnOffWindowResizeAnim = off;
13907 }
Joe Malin32736f02011-01-19 16:14:20 -080013908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013909 /**
Chet Haasea00f3862011-02-22 06:34:40 -080013910 * This method returns a ViewPropertyAnimator object, which can be used to animate
13911 * specific properties on this View.
13912 *
13913 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
13914 */
13915 public ViewPropertyAnimator animate() {
13916 if (mAnimator == null) {
13917 mAnimator = new ViewPropertyAnimator(this);
13918 }
13919 return mAnimator;
13920 }
13921
13922 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013923 * Interface definition for a callback to be invoked when a key event is
13924 * dispatched to this view. The callback will be invoked before the key
13925 * event is given to the view.
13926 */
13927 public interface OnKeyListener {
13928 /**
13929 * Called when a key is dispatched to a view. This allows listeners to
13930 * get a chance to respond before the target view.
13931 *
13932 * @param v The view the key has been dispatched to.
13933 * @param keyCode The code for the physical key that was pressed
13934 * @param event The KeyEvent object containing full information about
13935 * the event.
13936 * @return True if the listener has consumed the event, false otherwise.
13937 */
13938 boolean onKey(View v, int keyCode, KeyEvent event);
13939 }
13940
13941 /**
13942 * Interface definition for a callback to be invoked when a touch event is
13943 * dispatched to this view. The callback will be invoked before the touch
13944 * event is given to the view.
13945 */
13946 public interface OnTouchListener {
13947 /**
13948 * Called when a touch event is dispatched to a view. This allows listeners to
13949 * get a chance to respond before the target view.
13950 *
13951 * @param v The view the touch event has been dispatched to.
13952 * @param event The MotionEvent object containing full information about
13953 * the event.
13954 * @return True if the listener has consumed the event, false otherwise.
13955 */
13956 boolean onTouch(View v, MotionEvent event);
13957 }
13958
13959 /**
Jeff Brown10b62902011-06-20 16:40:37 -070013960 * Interface definition for a callback to be invoked when a hover event is
13961 * dispatched to this view. The callback will be invoked before the hover
13962 * event is given to the view.
13963 */
13964 public interface OnHoverListener {
13965 /**
13966 * Called when a hover event is dispatched to a view. This allows listeners to
13967 * get a chance to respond before the target view.
13968 *
13969 * @param v The view the hover event has been dispatched to.
13970 * @param event The MotionEvent object containing full information about
13971 * the event.
13972 * @return True if the listener has consumed the event, false otherwise.
13973 */
13974 boolean onHover(View v, MotionEvent event);
13975 }
13976
13977 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013978 * Interface definition for a callback to be invoked when a generic motion event is
13979 * dispatched to this view. The callback will be invoked before the generic motion
13980 * event is given to the view.
13981 */
13982 public interface OnGenericMotionListener {
13983 /**
13984 * Called when a generic motion event is dispatched to a view. This allows listeners to
13985 * get a chance to respond before the target view.
13986 *
13987 * @param v The view the generic motion event has been dispatched to.
13988 * @param event The MotionEvent object containing full information about
13989 * the event.
13990 * @return True if the listener has consumed the event, false otherwise.
13991 */
13992 boolean onGenericMotion(View v, MotionEvent event);
13993 }
13994
13995 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013996 * Interface definition for a callback to be invoked when a view has been clicked and held.
13997 */
13998 public interface OnLongClickListener {
13999 /**
14000 * Called when a view has been clicked and held.
14001 *
14002 * @param v The view that was clicked and held.
14003 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080014004 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014005 */
14006 boolean onLongClick(View v);
14007 }
14008
14009 /**
Chris Tate32affef2010-10-18 15:29:21 -070014010 * Interface definition for a callback to be invoked when a drag is being dispatched
14011 * to this view. The callback will be invoked before the hosting view's own
14012 * onDrag(event) method. If the listener wants to fall back to the hosting view's
14013 * onDrag(event) behavior, it should return 'false' from this callback.
14014 */
14015 public interface OnDragListener {
14016 /**
14017 * Called when a drag event is dispatched to a view. This allows listeners
14018 * to get a chance to override base View behavior.
14019 *
Joe Malin32736f02011-01-19 16:14:20 -080014020 * @param v The View that received the drag event.
14021 * @param event The {@link android.view.DragEvent} object for the drag event.
14022 * @return {@code true} if the drag event was handled successfully, or {@code false}
14023 * if the drag event was not handled. Note that {@code false} will trigger the View
14024 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070014025 */
14026 boolean onDrag(View v, DragEvent event);
14027 }
14028
14029 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014030 * Interface definition for a callback to be invoked when the focus state of
14031 * a view changed.
14032 */
14033 public interface OnFocusChangeListener {
14034 /**
14035 * Called when the focus state of a view has changed.
14036 *
14037 * @param v The view whose state has changed.
14038 * @param hasFocus The new focus state of v.
14039 */
14040 void onFocusChange(View v, boolean hasFocus);
14041 }
14042
14043 /**
14044 * Interface definition for a callback to be invoked when a view is clicked.
14045 */
14046 public interface OnClickListener {
14047 /**
14048 * Called when a view has been clicked.
14049 *
14050 * @param v The view that was clicked.
14051 */
14052 void onClick(View v);
14053 }
14054
14055 /**
14056 * Interface definition for a callback to be invoked when the context menu
14057 * for this view is being built.
14058 */
14059 public interface OnCreateContextMenuListener {
14060 /**
14061 * Called when the context menu for this view is being built. It is not
14062 * safe to hold onto the menu after this method returns.
14063 *
14064 * @param menu The context menu that is being built
14065 * @param v The view for which the context menu is being built
14066 * @param menuInfo Extra information about the item for which the
14067 * context menu should be shown. This information will vary
14068 * depending on the class of v.
14069 */
14070 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
14071 }
14072
Joe Onorato664644d2011-01-23 17:53:23 -080014073 /**
14074 * Interface definition for a callback to be invoked when the status bar changes
14075 * visibility.
14076 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070014077 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080014078 */
14079 public interface OnSystemUiVisibilityChangeListener {
14080 /**
14081 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070014082 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080014083 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040014084 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
14085 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080014086 */
14087 public void onSystemUiVisibilityChange(int visibility);
14088 }
14089
Adam Powell4afd62b2011-02-18 15:02:18 -080014090 /**
14091 * Interface definition for a callback to be invoked when this view is attached
14092 * or detached from its window.
14093 */
14094 public interface OnAttachStateChangeListener {
14095 /**
14096 * Called when the view is attached to a window.
14097 * @param v The view that was attached
14098 */
14099 public void onViewAttachedToWindow(View v);
14100 /**
14101 * Called when the view is detached from a window.
14102 * @param v The view that was detached
14103 */
14104 public void onViewDetachedFromWindow(View v);
14105 }
14106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014107 private final class UnsetPressedState implements Runnable {
14108 public void run() {
14109 setPressed(false);
14110 }
14111 }
14112
14113 /**
14114 * Base class for derived classes that want to save and restore their own
14115 * state in {@link android.view.View#onSaveInstanceState()}.
14116 */
14117 public static class BaseSavedState extends AbsSavedState {
14118 /**
14119 * Constructor used when reading from a parcel. Reads the state of the superclass.
14120 *
14121 * @param source
14122 */
14123 public BaseSavedState(Parcel source) {
14124 super(source);
14125 }
14126
14127 /**
14128 * Constructor called by derived classes when creating their SavedState objects
14129 *
14130 * @param superState The state of the superclass of this view
14131 */
14132 public BaseSavedState(Parcelable superState) {
14133 super(superState);
14134 }
14135
14136 public static final Parcelable.Creator<BaseSavedState> CREATOR =
14137 new Parcelable.Creator<BaseSavedState>() {
14138 public BaseSavedState createFromParcel(Parcel in) {
14139 return new BaseSavedState(in);
14140 }
14141
14142 public BaseSavedState[] newArray(int size) {
14143 return new BaseSavedState[size];
14144 }
14145 };
14146 }
14147
14148 /**
14149 * A set of information given to a view when it is attached to its parent
14150 * window.
14151 */
14152 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014153 interface Callbacks {
14154 void playSoundEffect(int effectId);
14155 boolean performHapticFeedback(int effectId, boolean always);
14156 }
14157
14158 /**
14159 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
14160 * to a Handler. This class contains the target (View) to invalidate and
14161 * the coordinates of the dirty rectangle.
14162 *
14163 * For performance purposes, this class also implements a pool of up to
14164 * POOL_LIMIT objects that get reused. This reduces memory allocations
14165 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014166 */
Romain Guyd928d682009-03-31 17:52:16 -070014167 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014168 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070014169 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
14170 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070014171 public InvalidateInfo newInstance() {
14172 return new InvalidateInfo();
14173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014174
Romain Guyd928d682009-03-31 17:52:16 -070014175 public void onAcquired(InvalidateInfo element) {
14176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014177
Romain Guyd928d682009-03-31 17:52:16 -070014178 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070014179 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070014180 }
14181 }, POOL_LIMIT)
14182 );
14183
14184 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014185 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014186
14187 View target;
14188
14189 int left;
14190 int top;
14191 int right;
14192 int bottom;
14193
Romain Guyd928d682009-03-31 17:52:16 -070014194 public void setNextPoolable(InvalidateInfo element) {
14195 mNext = element;
14196 }
14197
14198 public InvalidateInfo getNextPoolable() {
14199 return mNext;
14200 }
14201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014202 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070014203 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014204 }
14205
14206 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070014207 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014208 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014209
14210 public boolean isPooled() {
14211 return mIsPooled;
14212 }
14213
14214 public void setPooled(boolean isPooled) {
14215 mIsPooled = isPooled;
14216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014217 }
14218
14219 final IWindowSession mSession;
14220
14221 final IWindow mWindow;
14222
14223 final IBinder mWindowToken;
14224
14225 final Callbacks mRootCallbacks;
14226
Romain Guy59a12ca2011-06-09 17:48:21 -070014227 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080014228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014229 /**
14230 * The top view of the hierarchy.
14231 */
14232 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070014233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014234 IBinder mPanelParentWindowToken;
14235 Surface mSurface;
14236
Romain Guyb051e892010-09-28 19:09:36 -070014237 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014238 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070014239 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080014240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014241 /**
Romain Guy8506ab42009-06-11 17:35:47 -070014242 * Scale factor used by the compatibility mode
14243 */
14244 float mApplicationScale;
14245
14246 /**
14247 * Indicates whether the application is in compatibility mode
14248 */
14249 boolean mScalingRequired;
14250
14251 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014252 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080014253 */
14254 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080014255
Dianne Hackborn63042d62011-01-26 18:56:29 -080014256 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014257 * Left position of this view's window
14258 */
14259 int mWindowLeft;
14260
14261 /**
14262 * Top position of this view's window
14263 */
14264 int mWindowTop;
14265
14266 /**
Adam Powell26153a32010-11-08 15:22:27 -080014267 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070014268 */
Adam Powell26153a32010-11-08 15:22:27 -080014269 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070014270
14271 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014272 * For windows that are full-screen but using insets to layout inside
14273 * of the screen decorations, these are the current insets for the
14274 * content of the window.
14275 */
14276 final Rect mContentInsets = new Rect();
14277
14278 /**
14279 * For windows that are full-screen but using insets to layout inside
14280 * of the screen decorations, these are the current insets for the
14281 * actual visible parts of the window.
14282 */
14283 final Rect mVisibleInsets = new Rect();
14284
14285 /**
14286 * The internal insets given by this window. This value is
14287 * supplied by the client (through
14288 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
14289 * be given to the window manager when changed to be used in laying
14290 * out windows behind it.
14291 */
14292 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
14293 = new ViewTreeObserver.InternalInsetsInfo();
14294
14295 /**
14296 * All views in the window's hierarchy that serve as scroll containers,
14297 * used to determine if the window can be resized or must be panned
14298 * to adjust for a soft input area.
14299 */
14300 final ArrayList<View> mScrollContainers = new ArrayList<View>();
14301
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070014302 final KeyEvent.DispatcherState mKeyDispatchState
14303 = new KeyEvent.DispatcherState();
14304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014305 /**
14306 * Indicates whether the view's window currently has the focus.
14307 */
14308 boolean mHasWindowFocus;
14309
14310 /**
14311 * The current visibility of the window.
14312 */
14313 int mWindowVisibility;
14314
14315 /**
14316 * Indicates the time at which drawing started to occur.
14317 */
14318 long mDrawingTime;
14319
14320 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070014321 * Indicates whether or not ignoring the DIRTY_MASK flags.
14322 */
14323 boolean mIgnoreDirtyState;
14324
14325 /**
Romain Guy02ccac62011-06-24 13:20:23 -070014326 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
14327 * to avoid clearing that flag prematurely.
14328 */
14329 boolean mSetIgnoreDirtyState = false;
14330
14331 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014332 * Indicates whether the view's window is currently in touch mode.
14333 */
14334 boolean mInTouchMode;
14335
14336 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014337 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014338 * the next time it performs a traversal
14339 */
14340 boolean mRecomputeGlobalAttributes;
14341
14342 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014343 * Set during a traveral if any views want to keep the screen on.
14344 */
14345 boolean mKeepScreenOn;
14346
14347 /**
Joe Onorato664644d2011-01-23 17:53:23 -080014348 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
14349 */
14350 int mSystemUiVisibility;
14351
14352 /**
14353 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
14354 * attached.
14355 */
14356 boolean mHasSystemUiListeners;
14357
14358 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014359 * Set if the visibility of any views has changed.
14360 */
14361 boolean mViewVisibilityChanged;
14362
14363 /**
14364 * Set to true if a view has been scrolled.
14365 */
14366 boolean mViewScrollChanged;
14367
14368 /**
14369 * Global to the view hierarchy used as a temporary for dealing with
14370 * x/y points in the transparent region computations.
14371 */
14372 final int[] mTransparentLocation = new int[2];
14373
14374 /**
14375 * Global to the view hierarchy used as a temporary for dealing with
14376 * x/y points in the ViewGroup.invalidateChild implementation.
14377 */
14378 final int[] mInvalidateChildLocation = new int[2];
14379
Chet Haasec3aa3612010-06-17 08:50:37 -070014380
14381 /**
14382 * Global to the view hierarchy used as a temporary for dealing with
14383 * x/y location when view is transformed.
14384 */
14385 final float[] mTmpTransformLocation = new float[2];
14386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014387 /**
14388 * The view tree observer used to dispatch global events like
14389 * layout, pre-draw, touch mode change, etc.
14390 */
14391 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
14392
14393 /**
14394 * A Canvas used by the view hierarchy to perform bitmap caching.
14395 */
14396 Canvas mCanvas;
14397
14398 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014399 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014400 * handler can be used to pump events in the UI events queue.
14401 */
14402 final Handler mHandler;
14403
14404 /**
14405 * Identifier for messages requesting the view to be invalidated.
14406 * Such messages should be sent to {@link #mHandler}.
14407 */
14408 static final int INVALIDATE_MSG = 0x1;
14409
14410 /**
14411 * Identifier for messages requesting the view to invalidate a region.
14412 * Such messages should be sent to {@link #mHandler}.
14413 */
14414 static final int INVALIDATE_RECT_MSG = 0x2;
14415
14416 /**
14417 * Temporary for use in computing invalidate rectangles while
14418 * calling up the hierarchy.
14419 */
14420 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070014421
14422 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070014423 * Temporary for use in computing hit areas with transformed views
14424 */
14425 final RectF mTmpTransformRect = new RectF();
14426
14427 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070014428 * Temporary list for use in collecting focusable descendents of a view.
14429 */
14430 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
14431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014432 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014433 * The id of the window for accessibility purposes.
14434 */
14435 int mAccessibilityWindowId = View.NO_ID;
14436
14437 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014438 * Creates a new set of attachment information with the specified
14439 * events handler and thread.
14440 *
14441 * @param handler the events handler the view must use
14442 */
14443 AttachInfo(IWindowSession session, IWindow window,
14444 Handler handler, Callbacks effectPlayer) {
14445 mSession = session;
14446 mWindow = window;
14447 mWindowToken = window.asBinder();
14448 mHandler = handler;
14449 mRootCallbacks = effectPlayer;
14450 }
14451 }
14452
14453 /**
14454 * <p>ScrollabilityCache holds various fields used by a View when scrolling
14455 * is supported. This avoids keeping too many unused fields in most
14456 * instances of View.</p>
14457 */
Mike Cleronf116bf82009-09-27 19:14:12 -070014458 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080014459
Mike Cleronf116bf82009-09-27 19:14:12 -070014460 /**
14461 * Scrollbars are not visible
14462 */
14463 public static final int OFF = 0;
14464
14465 /**
14466 * Scrollbars are visible
14467 */
14468 public static final int ON = 1;
14469
14470 /**
14471 * Scrollbars are fading away
14472 */
14473 public static final int FADING = 2;
14474
14475 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080014476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014477 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070014478 public int scrollBarDefaultDelayBeforeFade;
14479 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014480
14481 public int scrollBarSize;
14482 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070014483 public float[] interpolatorValues;
14484 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014485
14486 public final Paint paint;
14487 public final Matrix matrix;
14488 public Shader shader;
14489
Mike Cleronf116bf82009-09-27 19:14:12 -070014490 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
14491
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014492 private static final float[] OPAQUE = { 255 };
14493 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080014494
Mike Cleronf116bf82009-09-27 19:14:12 -070014495 /**
14496 * When fading should start. This time moves into the future every time
14497 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
14498 */
14499 public long fadeStartTime;
14500
14501
14502 /**
14503 * The current state of the scrollbars: ON, OFF, or FADING
14504 */
14505 public int state = OFF;
14506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014507 private int mLastColor;
14508
Mike Cleronf116bf82009-09-27 19:14:12 -070014509 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014510 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
14511 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070014512 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
14513 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014514
14515 paint = new Paint();
14516 matrix = new Matrix();
14517 // use use a height of 1, and then wack the matrix each time we
14518 // actually use it.
14519 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014521 paint.setShader(shader);
14522 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070014523 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014524 }
Romain Guy8506ab42009-06-11 17:35:47 -070014525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014526 public void setFadeColor(int color) {
14527 if (color != 0 && color != mLastColor) {
14528 mLastColor = color;
14529 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070014530
Romain Guye55e1a72009-08-27 10:42:26 -070014531 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
14532 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014534 paint.setShader(shader);
14535 // Restore the default transfer mode (src_over)
14536 paint.setXfermode(null);
14537 }
14538 }
Joe Malin32736f02011-01-19 16:14:20 -080014539
Mike Cleronf116bf82009-09-27 19:14:12 -070014540 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070014541 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070014542 if (now >= fadeStartTime) {
14543
14544 // the animation fades the scrollbars out by changing
14545 // the opacity (alpha) from fully opaque to fully
14546 // transparent
14547 int nextFrame = (int) now;
14548 int framesCount = 0;
14549
14550 Interpolator interpolator = scrollBarInterpolator;
14551
14552 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014553 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070014554
14555 // End transparent
14556 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014557 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070014558
14559 state = FADING;
14560
14561 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080014562 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070014563 }
14564 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070014565 }
Mike Cleronf116bf82009-09-27 19:14:12 -070014566
Svetoslav Ganova0156172011-06-26 17:55:44 -070014567 /**
14568 * Resuable callback for sending
14569 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
14570 */
14571 private class SendViewScrolledAccessibilityEvent implements Runnable {
14572 public volatile boolean mIsPending;
14573
14574 public void run() {
14575 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
14576 mIsPending = false;
14577 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014578 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070014579
14580 /**
14581 * <p>
14582 * This class represents a delegate that can be registered in a {@link View}
14583 * to enhance accessibility support via composition rather via inheritance.
14584 * It is specifically targeted to widget developers that extend basic View
14585 * classes i.e. classes in package android.view, that would like their
14586 * applications to be backwards compatible.
14587 * </p>
14588 * <p>
14589 * A scenario in which a developer would like to use an accessibility delegate
14590 * is overriding a method introduced in a later API version then the minimal API
14591 * version supported by the application. For example, the method
14592 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
14593 * in API version 4 when the accessibility APIs were first introduced. If a
14594 * developer would like his application to run on API version 4 devices (assuming
14595 * all other APIs used by the application are version 4 or lower) and take advantage
14596 * of this method, instead of overriding the method which would break the application's
14597 * backwards compatibility, he can override the corresponding method in this
14598 * delegate and register the delegate in the target View if the API version of
14599 * the system is high enough i.e. the API version is same or higher to the API
14600 * version that introduced
14601 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
14602 * </p>
14603 * <p>
14604 * Here is an example implementation:
14605 * </p>
14606 * <code><pre><p>
14607 * if (Build.VERSION.SDK_INT >= 14) {
14608 * // If the API version is equal of higher than the version in
14609 * // which onInitializeAccessibilityNodeInfo was introduced we
14610 * // register a delegate with a customized implementation.
14611 * View view = findViewById(R.id.view_id);
14612 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
14613 * public void onInitializeAccessibilityNodeInfo(View host,
14614 * AccessibilityNodeInfo info) {
14615 * // Let the default implementation populate the info.
14616 * super.onInitializeAccessibilityNodeInfo(host, info);
14617 * // Set some other information.
14618 * info.setEnabled(host.isEnabled());
14619 * }
14620 * });
14621 * }
14622 * </code></pre></p>
14623 * <p>
14624 * This delegate contains methods that correspond to the accessibility methods
14625 * in View. If a delegate has been specified the implementation in View hands
14626 * off handling to the corresponding method in this delegate. The default
14627 * implementation the delegate methods behaves exactly as the corresponding
14628 * method in View for the case of no accessibility delegate been set. Hence,
14629 * to customize the behavior of a View method, clients can override only the
14630 * corresponding delegate method without altering the behavior of the rest
14631 * accessibility related methods of the host view.
14632 * </p>
14633 */
14634 public static class AccessibilityDelegate {
14635
14636 /**
14637 * Sends an accessibility event of the given type. If accessibility is not
14638 * enabled this method has no effect.
14639 * <p>
14640 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
14641 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
14642 * been set.
14643 * </p>
14644 *
14645 * @param host The View hosting the delegate.
14646 * @param eventType The type of the event to send.
14647 *
14648 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
14649 */
14650 public void sendAccessibilityEvent(View host, int eventType) {
14651 host.sendAccessibilityEventInternal(eventType);
14652 }
14653
14654 /**
14655 * Sends an accessibility event. This method behaves exactly as
14656 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
14657 * empty {@link AccessibilityEvent} and does not perform a check whether
14658 * accessibility is enabled.
14659 * <p>
14660 * The default implementation behaves as
14661 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14662 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
14663 * the case of no accessibility delegate been set.
14664 * </p>
14665 *
14666 * @param host The View hosting the delegate.
14667 * @param event The event to send.
14668 *
14669 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14670 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14671 */
14672 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
14673 host.sendAccessibilityEventUncheckedInternal(event);
14674 }
14675
14676 /**
14677 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
14678 * to its children for adding their text content to the event.
14679 * <p>
14680 * The default implementation behaves as
14681 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14682 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
14683 * the case of no accessibility delegate been set.
14684 * </p>
14685 *
14686 * @param host The View hosting the delegate.
14687 * @param event The event.
14688 * @return True if the event population was completed.
14689 *
14690 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14691 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14692 */
14693 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14694 return host.dispatchPopulateAccessibilityEventInternal(event);
14695 }
14696
14697 /**
14698 * Gives a chance to the host View to populate the accessibility event with its
14699 * text content.
14700 * <p>
14701 * The default implementation behaves as
14702 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
14703 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
14704 * the case of no accessibility delegate been set.
14705 * </p>
14706 *
14707 * @param host The View hosting the delegate.
14708 * @param event The accessibility event which to populate.
14709 *
14710 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
14711 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
14712 */
14713 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14714 host.onPopulateAccessibilityEventInternal(event);
14715 }
14716
14717 /**
14718 * Initializes an {@link AccessibilityEvent} with information about the
14719 * the host View which is the event source.
14720 * <p>
14721 * The default implementation behaves as
14722 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
14723 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
14724 * the case of no accessibility delegate been set.
14725 * </p>
14726 *
14727 * @param host The View hosting the delegate.
14728 * @param event The event to initialize.
14729 *
14730 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
14731 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
14732 */
14733 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
14734 host.onInitializeAccessibilityEventInternal(event);
14735 }
14736
14737 /**
14738 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
14739 * <p>
14740 * The default implementation behaves as
14741 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14742 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
14743 * the case of no accessibility delegate been set.
14744 * </p>
14745 *
14746 * @param host The View hosting the delegate.
14747 * @param info The instance to initialize.
14748 *
14749 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14750 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14751 */
14752 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
14753 host.onInitializeAccessibilityNodeInfoInternal(info);
14754 }
14755
14756 /**
14757 * Called when a child of the host View has requested sending an
14758 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
14759 * to augment the event.
14760 * <p>
14761 * The default implementation behaves as
14762 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14763 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
14764 * the case of no accessibility delegate been set.
14765 * </p>
14766 *
14767 * @param host The View hosting the delegate.
14768 * @param child The child which requests sending the event.
14769 * @param event The event to be sent.
14770 * @return True if the event should be sent
14771 *
14772 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14773 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14774 */
14775 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
14776 AccessibilityEvent event) {
14777 return host.onRequestSendAccessibilityEventInternal(child, event);
14778 }
14779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014780}