blob: 65e98578ac4bdc541f05ec2a624627f67f591845 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070049import android.util.FloatProperty;
50import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070052import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070053import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070056import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080058import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.view.accessibility.AccessibilityEvent;
61import android.view.accessibility.AccessibilityEventSource;
62import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070063import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070065import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.view.inputmethod.InputConnection;
68import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.widget.ScrollBarDrawable;
70
Romain Guy1ef3fdb2011-09-09 15:30:30 -070071import static android.os.Build.VERSION_CODES.*;
72
Doug Feltcb3791202011-07-07 11:57:48 -070073import com.android.internal.R;
74import com.android.internal.util.Predicate;
75import com.android.internal.view.menu.MenuBuilder;
76
Christopher Tatea0374192010-10-05 13:06:41 -070077import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070078import java.lang.reflect.InvocationTargetException;
79import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import java.util.ArrayList;
81import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070082import java.util.Locale;
Romain Guyd90a3312009-05-06 14:54:28 -070083import java.util.WeakHashMap;
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 */
1499 private static WeakHashMap<View, SparseArray<Object>> sTags;
1500
1501 /**
1502 * Lock used to access sTags.
1503 */
1504 private static final Object sTagsLock = new Object();
1505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001507 * The next available accessiiblity id.
1508 */
1509 private static int sNextAccessibilityViewId;
1510
1511 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 * The animation currently associated with this view.
1513 * @hide
1514 */
1515 protected Animation mCurrentAnimation = null;
1516
1517 /**
1518 * Width as measured during measure pass.
1519 * {@hide}
1520 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001521 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001522 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523
1524 /**
1525 * Height as measured during measure pass.
1526 * {@hide}
1527 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001528 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001529 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530
1531 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001532 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1533 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1534 * its display list. This flag, used only when hw accelerated, allows us to clear the
1535 * flag while retaining this information until it's needed (at getDisplayList() time and
1536 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1537 *
1538 * {@hide}
1539 */
1540 boolean mRecreateDisplayList = false;
1541
1542 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 * The view's identifier.
1544 * {@hide}
1545 *
1546 * @see #setId(int)
1547 * @see #getId()
1548 */
1549 @ViewDebug.ExportedProperty(resolveId = true)
1550 int mID = NO_ID;
1551
1552 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001553 * The stable ID of this view for accessibility porposes.
1554 */
1555 int mAccessibilityViewId = NO_ID;
1556
1557 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 * The view's tag.
1559 * {@hide}
1560 *
1561 * @see #setTag(Object)
1562 * @see #getTag()
1563 */
1564 protected Object mTag;
1565
1566 // for mPrivateFlags:
1567 /** {@hide} */
1568 static final int WANTS_FOCUS = 0x00000001;
1569 /** {@hide} */
1570 static final int FOCUSED = 0x00000002;
1571 /** {@hide} */
1572 static final int SELECTED = 0x00000004;
1573 /** {@hide} */
1574 static final int IS_ROOT_NAMESPACE = 0x00000008;
1575 /** {@hide} */
1576 static final int HAS_BOUNDS = 0x00000010;
1577 /** {@hide} */
1578 static final int DRAWN = 0x00000020;
1579 /**
1580 * When this flag is set, this view is running an animation on behalf of its
1581 * children and should therefore not cancel invalidate requests, even if they
1582 * lie outside of this view's bounds.
1583 *
1584 * {@hide}
1585 */
1586 static final int DRAW_ANIMATION = 0x00000040;
1587 /** {@hide} */
1588 static final int SKIP_DRAW = 0x00000080;
1589 /** {@hide} */
1590 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1591 /** {@hide} */
1592 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1593 /** {@hide} */
1594 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1595 /** {@hide} */
1596 static final int MEASURED_DIMENSION_SET = 0x00000800;
1597 /** {@hide} */
1598 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001599 /** {@hide} */
1600 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601
1602 private static final int PRESSED = 0x00004000;
1603
1604 /** {@hide} */
1605 static final int DRAWING_CACHE_VALID = 0x00008000;
1606 /**
1607 * Flag used to indicate that this view should be drawn once more (and only once
1608 * more) after its animation has completed.
1609 * {@hide}
1610 */
1611 static final int ANIMATION_STARTED = 0x00010000;
1612
1613 private static final int SAVE_STATE_CALLED = 0x00020000;
1614
1615 /**
1616 * Indicates that the View returned true when onSetAlpha() was called and that
1617 * the alpha must be restored.
1618 * {@hide}
1619 */
1620 static final int ALPHA_SET = 0x00040000;
1621
1622 /**
1623 * Set by {@link #setScrollContainer(boolean)}.
1624 */
1625 static final int SCROLL_CONTAINER = 0x00080000;
1626
1627 /**
1628 * Set by {@link #setScrollContainer(boolean)}.
1629 */
1630 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1631
1632 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001633 * View flag indicating whether this view was invalidated (fully or partially.)
1634 *
1635 * @hide
1636 */
1637 static final int DIRTY = 0x00200000;
1638
1639 /**
1640 * View flag indicating whether this view was invalidated by an opaque
1641 * invalidate request.
1642 *
1643 * @hide
1644 */
1645 static final int DIRTY_OPAQUE = 0x00400000;
1646
1647 /**
1648 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1649 *
1650 * @hide
1651 */
1652 static final int DIRTY_MASK = 0x00600000;
1653
1654 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001655 * Indicates whether the background is opaque.
1656 *
1657 * @hide
1658 */
1659 static final int OPAQUE_BACKGROUND = 0x00800000;
1660
1661 /**
1662 * Indicates whether the scrollbars are opaque.
1663 *
1664 * @hide
1665 */
1666 static final int OPAQUE_SCROLLBARS = 0x01000000;
1667
1668 /**
1669 * Indicates whether the view is opaque.
1670 *
1671 * @hide
1672 */
1673 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001674
Adam Powelle14579b2009-12-16 18:39:52 -08001675 /**
1676 * Indicates a prepressed state;
1677 * the short time between ACTION_DOWN and recognizing
1678 * a 'real' press. Prepressed is used to recognize quick taps
1679 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001680 *
Adam Powelle14579b2009-12-16 18:39:52 -08001681 * @hide
1682 */
1683 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001684
Adam Powellc9fbaab2010-02-16 17:16:19 -08001685 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001686 * Indicates whether the view is temporarily detached.
1687 *
1688 * @hide
1689 */
1690 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001691
Adam Powell8568c3a2010-04-19 14:26:11 -07001692 /**
1693 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001694 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001695 * @hide
1696 */
1697 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001698
1699 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001700 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1701 * @hide
1702 */
1703 private static final int HOVERED = 0x10000000;
1704
1705 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001706 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1707 * for transform operations
1708 *
1709 * @hide
1710 */
Adam Powellf37df072010-09-17 16:22:49 -07001711 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001712
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001713 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001714 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001715
Chet Haasefd2b0022010-08-06 13:08:56 -07001716 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001717 * Indicates that this view was specifically invalidated, not just dirtied because some
1718 * child view was invalidated. The flag is used to determine when we need to recreate
1719 * a view's display list (as opposed to just returning a reference to its existing
1720 * display list).
1721 *
1722 * @hide
1723 */
1724 static final int INVALIDATED = 0x80000000;
1725
Christopher Tate3d4bf172011-03-28 16:16:46 -07001726 /* Masks for mPrivateFlags2 */
1727
1728 /**
1729 * Indicates that this view has reported that it can accept the current drag's content.
1730 * Cleared when the drag operation concludes.
1731 * @hide
1732 */
1733 static final int DRAG_CAN_ACCEPT = 0x00000001;
1734
1735 /**
1736 * Indicates that this view is currently directly under the drag location in a
1737 * drag-and-drop operation involving content that it can accept. Cleared when
1738 * the drag exits the view, or when the drag operation concludes.
1739 * @hide
1740 */
1741 static final int DRAG_HOVERED = 0x00000002;
1742
Cibu Johny86666632010-02-22 13:01:02 -08001743 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001744 * Indicates whether the view layout direction has been resolved and drawn to the
1745 * right-to-left direction.
Cibu Johny86666632010-02-22 13:01:02 -08001746 *
1747 * @hide
1748 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001749 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004;
1750
1751 /**
1752 * Indicates whether the view layout direction has been resolved.
1753 *
1754 * @hide
1755 */
1756 static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008;
1757
Cibu Johny86666632010-02-22 13:01:02 -08001758
Christopher Tate3d4bf172011-03-28 16:16:46 -07001759 /* End of masks for mPrivateFlags2 */
1760
1761 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1762
Chet Haasedaf98e92011-01-10 14:10:36 -08001763 /**
Adam Powell637d3372010-08-25 14:37:03 -07001764 * Always allow a user to over-scroll this view, provided it is a
1765 * view that can scroll.
1766 *
1767 * @see #getOverScrollMode()
1768 * @see #setOverScrollMode(int)
1769 */
1770 public static final int OVER_SCROLL_ALWAYS = 0;
1771
1772 /**
1773 * Allow a user to over-scroll this view only if the content is large
1774 * enough to meaningfully scroll, provided it is a view that can scroll.
1775 *
1776 * @see #getOverScrollMode()
1777 * @see #setOverScrollMode(int)
1778 */
1779 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1780
1781 /**
1782 * Never allow a user to over-scroll this view.
1783 *
1784 * @see #getOverScrollMode()
1785 * @see #setOverScrollMode(int)
1786 */
1787 public static final int OVER_SCROLL_NEVER = 2;
1788
1789 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001790 * View has requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08001791 *
Joe Malin32736f02011-01-19 16:14:20 -08001792 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001793 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001794 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08001795
1796 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001797 * View has requested the system UI to enter an unobtrusive "low profile" mode.
1798 *
1799 * This is for use in games, book readers, video players, or any other "immersive" application
1800 * where the usual system chrome is deemed too distracting.
1801 *
1802 * In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08001803 *
Joe Malin32736f02011-01-19 16:14:20 -08001804 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001805 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001806 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
1807
1808 /**
1809 * View has requested that the system navigation be temporarily hidden.
1810 *
1811 * This is an even less obtrusive state than that called for by
1812 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
1813 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
1814 * those to disappear. This is useful (in conjunction with the
1815 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
1816 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
1817 * window flags) for displaying content using every last pixel on the display.
1818 *
1819 * There is a limitation: because navigation controls are so important, the least user
1820 * interaction will cause them to reappear immediately.
1821 *
1822 * @see #setSystemUiVisibility(int)
1823 */
1824 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
1825
1826 /**
1827 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
1828 */
1829 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
1830
1831 /**
1832 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
1833 */
1834 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08001835
1836 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001837 * @hide
1838 *
1839 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1840 * out of the public fields to keep the undefined bits out of the developer's way.
1841 *
1842 * Flag to make the status bar not expandable. Unless you also
1843 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1844 */
1845 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1846
1847 /**
1848 * @hide
1849 *
1850 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1851 * out of the public fields to keep the undefined bits out of the developer's way.
1852 *
1853 * Flag to hide notification icons and scrolling ticker text.
1854 */
1855 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1856
1857 /**
1858 * @hide
1859 *
1860 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1861 * out of the public fields to keep the undefined bits out of the developer's way.
1862 *
1863 * Flag to disable incoming notification alerts. This will not block
1864 * icons, but it will block sound, vibrating and other visual or aural notifications.
1865 */
1866 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1867
1868 /**
1869 * @hide
1870 *
1871 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1872 * out of the public fields to keep the undefined bits out of the developer's way.
1873 *
1874 * Flag to hide only the scrolling ticker. Note that
1875 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1876 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1877 */
1878 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1879
1880 /**
1881 * @hide
1882 *
1883 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1884 * out of the public fields to keep the undefined bits out of the developer's way.
1885 *
1886 * Flag to hide the center system info area.
1887 */
1888 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1889
1890 /**
1891 * @hide
1892 *
1893 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1894 * out of the public fields to keep the undefined bits out of the developer's way.
1895 *
1896 * Flag to hide only the navigation buttons. Don't use this
1897 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001898 *
1899 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001900 */
1901 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1902
1903 /**
1904 * @hide
1905 *
1906 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1907 * out of the public fields to keep the undefined bits out of the developer's way.
1908 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001909 * Flag to hide only the back button. Don't use this
1910 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1911 */
1912 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1913
1914 /**
1915 * @hide
1916 *
1917 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1918 * out of the public fields to keep the undefined bits out of the developer's way.
1919 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001920 * Flag to hide only the clock. You might use this if your activity has
1921 * its own clock making the status bar's clock redundant.
1922 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001923 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1924
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001925 /**
1926 * @hide
1927 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001928 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001929
1930 /**
Adam Powell637d3372010-08-25 14:37:03 -07001931 * Controls the over-scroll mode for this view.
1932 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1933 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1934 * and {@link #OVER_SCROLL_NEVER}.
1935 */
1936 private int mOverScrollMode;
1937
1938 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 * The parent this view is attached to.
1940 * {@hide}
1941 *
1942 * @see #getParent()
1943 */
1944 protected ViewParent mParent;
1945
1946 /**
1947 * {@hide}
1948 */
1949 AttachInfo mAttachInfo;
1950
1951 /**
1952 * {@hide}
1953 */
Romain Guy809a7f62009-05-14 15:44:42 -07001954 @ViewDebug.ExportedProperty(flagMapping = {
1955 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1956 name = "FORCE_LAYOUT"),
1957 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1958 name = "LAYOUT_REQUIRED"),
1959 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001960 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001961 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1962 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1963 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1964 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1965 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001967 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968
1969 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001970 * This view's request for the visibility of the status bar.
1971 * @hide
1972 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001973 @ViewDebug.ExportedProperty(flagMapping = {
1974 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
1975 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
1976 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
1977 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
1978 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
1979 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
1980 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
1981 equals = SYSTEM_UI_FLAG_VISIBLE,
1982 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
1983 })
Joe Onorato664644d2011-01-23 17:53:23 -08001984 int mSystemUiVisibility;
1985
1986 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 * Count of how many windows this view has been attached to.
1988 */
1989 int mWindowAttachCount;
1990
1991 /**
1992 * The layout parameters associated with this view and used by the parent
1993 * {@link android.view.ViewGroup} to determine how this view should be
1994 * laid out.
1995 * {@hide}
1996 */
1997 protected ViewGroup.LayoutParams mLayoutParams;
1998
1999 /**
2000 * The view flags hold various views states.
2001 * {@hide}
2002 */
2003 @ViewDebug.ExportedProperty
2004 int mViewFlags;
2005
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002006 static class TransformationInfo {
2007 /**
2008 * The transform matrix for the View. This transform is calculated internally
2009 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2010 * is used by default. Do *not* use this variable directly; instead call
2011 * getMatrix(), which will automatically recalculate the matrix if necessary
2012 * to get the correct matrix based on the latest rotation and scale properties.
2013 */
2014 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002015
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002016 /**
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 * getInverseMatrix(), 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 Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002024
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002025 /**
2026 * An internal variable that tracks whether we need to recalculate the
2027 * transform matrix, based on whether the rotation or scaleX/Y properties
2028 * have changed since the matrix was last calculated.
2029 */
2030 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002031
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002032 /**
2033 * An internal variable that tracks whether we need to recalculate the
2034 * transform matrix, based on whether the rotation or scaleX/Y properties
2035 * have changed since the matrix was last calculated.
2036 */
2037 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002038
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002039 /**
2040 * A variable that tracks whether we need to recalculate the
2041 * transform matrix, based on whether the rotation or scaleX/Y properties
2042 * have changed since the matrix was last calculated. This variable
2043 * is only valid after a call to updateMatrix() or to a function that
2044 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2045 */
2046 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002047
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002048 /**
2049 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2050 */
2051 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002052
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002053 /**
2054 * This matrix is used when computing the matrix for 3D rotations.
2055 */
2056 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002057
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002058 /**
2059 * These prev values are used to recalculate a centered pivot point when necessary. The
2060 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2061 * set), so thes values are only used then as well.
2062 */
2063 private int mPrevWidth = -1;
2064 private int mPrevHeight = -1;
2065
2066 /**
2067 * The degrees rotation around the vertical axis through the pivot point.
2068 */
2069 @ViewDebug.ExportedProperty
2070 float mRotationY = 0f;
2071
2072 /**
2073 * The degrees rotation around the horizontal axis through the pivot point.
2074 */
2075 @ViewDebug.ExportedProperty
2076 float mRotationX = 0f;
2077
2078 /**
2079 * The degrees rotation around the pivot point.
2080 */
2081 @ViewDebug.ExportedProperty
2082 float mRotation = 0f;
2083
2084 /**
2085 * The amount of translation of the object away from its left property (post-layout).
2086 */
2087 @ViewDebug.ExportedProperty
2088 float mTranslationX = 0f;
2089
2090 /**
2091 * The amount of translation of the object away from its top property (post-layout).
2092 */
2093 @ViewDebug.ExportedProperty
2094 float mTranslationY = 0f;
2095
2096 /**
2097 * The amount of scale in the x direction around the pivot point. A
2098 * value of 1 means no scaling is applied.
2099 */
2100 @ViewDebug.ExportedProperty
2101 float mScaleX = 1f;
2102
2103 /**
2104 * The amount of scale in the y direction around the pivot point. A
2105 * value of 1 means no scaling is applied.
2106 */
2107 @ViewDebug.ExportedProperty
2108 float mScaleY = 1f;
2109
2110 /**
2111 * The amount of scale in the x direction around the pivot point. A
2112 * value of 1 means no scaling is applied.
2113 */
2114 @ViewDebug.ExportedProperty
2115 float mPivotX = 0f;
2116
2117 /**
2118 * The amount of scale in the y direction around the pivot point. A
2119 * value of 1 means no scaling is applied.
2120 */
2121 @ViewDebug.ExportedProperty
2122 float mPivotY = 0f;
2123
2124 /**
2125 * The opacity of the View. This is a value from 0 to 1, where 0 means
2126 * completely transparent and 1 means completely opaque.
2127 */
2128 @ViewDebug.ExportedProperty
2129 float mAlpha = 1f;
2130 }
2131
2132 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002133
Joe Malin32736f02011-01-19 16:14:20 -08002134 private boolean mLastIsOpaque;
2135
Chet Haasefd2b0022010-08-06 13:08:56 -07002136 /**
2137 * Convenience value to check for float values that are close enough to zero to be considered
2138 * zero.
2139 */
Romain Guy2542d192010-08-18 11:47:12 -07002140 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002141
2142 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 * The distance in pixels from the left edge of this view's parent
2144 * to the left edge of this view.
2145 * {@hide}
2146 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002147 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 protected int mLeft;
2149 /**
2150 * The distance in pixels from the left edge of this view's parent
2151 * to the right edge of this view.
2152 * {@hide}
2153 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002154 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 protected int mRight;
2156 /**
2157 * The distance in pixels from the top edge of this view's parent
2158 * to the top edge of this view.
2159 * {@hide}
2160 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002161 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 protected int mTop;
2163 /**
2164 * The distance in pixels from the top edge of this view's parent
2165 * to the bottom edge of this view.
2166 * {@hide}
2167 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002168 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 protected int mBottom;
2170
2171 /**
2172 * The offset, in pixels, by which the content of this view is scrolled
2173 * horizontally.
2174 * {@hide}
2175 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002176 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 protected int mScrollX;
2178 /**
2179 * The offset, in pixels, by which the content of this view is scrolled
2180 * vertically.
2181 * {@hide}
2182 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002183 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 protected int mScrollY;
2185
2186 /**
2187 * The left padding in pixels, that is the distance in pixels between the
2188 * left edge of this view and the left edge of its content.
2189 * {@hide}
2190 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002191 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 protected int mPaddingLeft;
2193 /**
2194 * The right padding in pixels, that is the distance in pixels between the
2195 * right edge of this view and the right edge of its content.
2196 * {@hide}
2197 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002198 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 protected int mPaddingRight;
2200 /**
2201 * The top padding in pixels, that is the distance in pixels between the
2202 * top edge of this view and the top edge of its content.
2203 * {@hide}
2204 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002205 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 protected int mPaddingTop;
2207 /**
2208 * The bottom padding in pixels, that is the distance in pixels between the
2209 * bottom edge of this view and the bottom edge of its content.
2210 * {@hide}
2211 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002212 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 protected int mPaddingBottom;
2214
2215 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002216 * Briefly describes the view and is primarily used for accessibility support.
2217 */
2218 private CharSequence mContentDescription;
2219
2220 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002222 *
2223 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002225 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002226 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227
2228 /**
2229 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002230 *
2231 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002233 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002234 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002236 /**
Adam Powell20232d02010-12-08 21:08:53 -08002237 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002238 *
2239 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002240 */
2241 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002242 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002243
2244 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002245 * Cache if the user padding is relative.
2246 *
2247 */
2248 @ViewDebug.ExportedProperty(category = "padding")
2249 boolean mUserPaddingRelative;
2250
2251 /**
2252 * Cache the paddingStart set by the user to append to the scrollbar's size.
2253 *
2254 */
2255 @ViewDebug.ExportedProperty(category = "padding")
2256 int mUserPaddingStart;
2257
2258 /**
2259 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2260 *
2261 */
2262 @ViewDebug.ExportedProperty(category = "padding")
2263 int mUserPaddingEnd;
2264
2265 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002266 * @hide
2267 */
2268 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2269 /**
2270 * @hide
2271 */
2272 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 private Drawable mBGDrawable;
2275
2276 private int mBackgroundResource;
2277 private boolean mBackgroundSizeChanged;
2278
2279 /**
2280 * Listener used to dispatch focus change events.
2281 * This field should be made private, so it is hidden from the SDK.
2282 * {@hide}
2283 */
2284 protected OnFocusChangeListener mOnFocusChangeListener;
2285
2286 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002287 * Listeners for layout change events.
2288 */
2289 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2290
2291 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002292 * Listeners for attach events.
2293 */
2294 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2295
2296 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 * Listener used to dispatch click events.
2298 * This field should be made private, so it is hidden from the SDK.
2299 * {@hide}
2300 */
2301 protected OnClickListener mOnClickListener;
2302
2303 /**
2304 * Listener used to dispatch long click events.
2305 * This field should be made private, so it is hidden from the SDK.
2306 * {@hide}
2307 */
2308 protected OnLongClickListener mOnLongClickListener;
2309
2310 /**
2311 * Listener used to build the context menu.
2312 * This field should be made private, so it is hidden from the SDK.
2313 * {@hide}
2314 */
2315 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2316
2317 private OnKeyListener mOnKeyListener;
2318
2319 private OnTouchListener mOnTouchListener;
2320
Jeff Brown10b62902011-06-20 16:40:37 -07002321 private OnHoverListener mOnHoverListener;
2322
Jeff Brown33bbfd22011-02-24 20:55:35 -08002323 private OnGenericMotionListener mOnGenericMotionListener;
2324
Chris Tate32affef2010-10-18 15:29:21 -07002325 private OnDragListener mOnDragListener;
2326
Joe Onorato664644d2011-01-23 17:53:23 -08002327 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 /**
2330 * The application environment this view lives in.
2331 * This field should be made private, so it is hidden from the SDK.
2332 * {@hide}
2333 */
2334 protected Context mContext;
2335
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002336 private final Resources mResources;
2337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 private ScrollabilityCache mScrollCache;
2339
2340 private int[] mDrawableState = null;
2341
Romain Guy0211a0a2011-02-14 16:34:59 -08002342 /**
2343 * Set to true when drawing cache is enabled and cannot be created.
2344 *
2345 * @hide
2346 */
2347 public boolean mCachingFailed;
2348
Romain Guy02890fd2010-08-06 17:58:44 -07002349 private Bitmap mDrawingCache;
2350 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002351 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002352 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353
2354 /**
2355 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2356 * the user may specify which view to go to next.
2357 */
2358 private int mNextFocusLeftId = View.NO_ID;
2359
2360 /**
2361 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2362 * the user may specify which view to go to next.
2363 */
2364 private int mNextFocusRightId = View.NO_ID;
2365
2366 /**
2367 * When this view has focus and the next focus is {@link #FOCUS_UP},
2368 * the user may specify which view to go to next.
2369 */
2370 private int mNextFocusUpId = View.NO_ID;
2371
2372 /**
2373 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2374 * the user may specify which view to go to next.
2375 */
2376 private int mNextFocusDownId = View.NO_ID;
2377
Jeff Brown4e6319b2010-12-13 10:36:51 -08002378 /**
2379 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2380 * the user may specify which view to go to next.
2381 */
2382 int mNextFocusForwardId = View.NO_ID;
2383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002385 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002386 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002387 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 private UnsetPressedState mUnsetPressedState;
2390
2391 /**
2392 * Whether the long press's action has been invoked. The tap's action is invoked on the
2393 * up event while a long press is invoked as soon as the long press duration is reached, so
2394 * a long press could be performed before the tap is checked, in which case the tap's action
2395 * should not be invoked.
2396 */
2397 private boolean mHasPerformedLongPress;
2398
2399 /**
2400 * The minimum height of the view. We'll try our best to have the height
2401 * of this view to at least this amount.
2402 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002403 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 private int mMinHeight;
2405
2406 /**
2407 * The minimum width of the view. We'll try our best to have the width
2408 * of this view to at least this amount.
2409 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002410 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 private int mMinWidth;
2412
2413 /**
2414 * The delegate to handle touch events that are physically in this view
2415 * but should be handled by another view.
2416 */
2417 private TouchDelegate mTouchDelegate = null;
2418
2419 /**
2420 * Solid color to use as a background when creating the drawing cache. Enables
2421 * the cache to use 16 bit bitmaps instead of 32 bit.
2422 */
2423 private int mDrawingCacheBackgroundColor = 0;
2424
2425 /**
2426 * Special tree observer used when mAttachInfo is null.
2427 */
2428 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002429
Adam Powelle14579b2009-12-16 18:39:52 -08002430 /**
2431 * Cache the touch slop from the context that created the view.
2432 */
2433 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002436 * Object that handles automatic animation of view properties.
2437 */
2438 private ViewPropertyAnimator mAnimator = null;
2439
2440 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002441 * Flag indicating that a drag can cross window boundaries. When
2442 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2443 * with this flag set, all visible applications will be able to participate
2444 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002445 *
2446 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002447 */
2448 public static final int DRAG_FLAG_GLOBAL = 1;
2449
2450 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002451 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2452 */
2453 private float mVerticalScrollFactor;
2454
2455 /**
Adam Powell20232d02010-12-08 21:08:53 -08002456 * Position of the vertical scroll bar.
2457 */
2458 private int mVerticalScrollbarPosition;
2459
2460 /**
2461 * Position the scroll bar at the default position as determined by the system.
2462 */
2463 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2464
2465 /**
2466 * Position the scroll bar along the left edge.
2467 */
2468 public static final int SCROLLBAR_POSITION_LEFT = 1;
2469
2470 /**
2471 * Position the scroll bar along the right edge.
2472 */
2473 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2474
2475 /**
Romain Guy171c5922011-01-06 10:04:23 -08002476 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002477 *
2478 * @see #getLayerType()
2479 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002480 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002481 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002482 */
2483 public static final int LAYER_TYPE_NONE = 0;
2484
2485 /**
2486 * <p>Indicates that the view has a software layer. A software layer is backed
2487 * by a bitmap and causes the view to be rendered using Android's software
2488 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002489 *
Romain Guy171c5922011-01-06 10:04:23 -08002490 * <p>Software layers have various usages:</p>
2491 * <p>When the application is not using hardware acceleration, a software layer
2492 * is useful to apply a specific color filter and/or blending mode and/or
2493 * translucency to a view and all its children.</p>
2494 * <p>When the application is using hardware acceleration, a software layer
2495 * is useful to render drawing primitives not supported by the hardware
2496 * accelerated pipeline. It can also be used to cache a complex view tree
2497 * into a texture and reduce the complexity of drawing operations. For instance,
2498 * when animating a complex view tree with a translation, a software layer can
2499 * be used to render the view tree only once.</p>
2500 * <p>Software layers should be avoided when the affected view tree updates
2501 * often. Every update will require to re-render the software layer, which can
2502 * potentially be slow (particularly when hardware acceleration is turned on
2503 * since the layer will have to be uploaded into a hardware texture after every
2504 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002505 *
2506 * @see #getLayerType()
2507 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002508 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002509 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002510 */
2511 public static final int LAYER_TYPE_SOFTWARE = 1;
2512
2513 /**
2514 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2515 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2516 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2517 * rendering pipeline, but only if hardware acceleration is turned on for the
2518 * view hierarchy. When hardware acceleration is turned off, hardware layers
2519 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002520 *
Romain Guy171c5922011-01-06 10:04:23 -08002521 * <p>A hardware layer is useful to apply a specific color filter and/or
2522 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002523 * <p>A hardware layer can be used to cache a complex view tree into a
2524 * texture and reduce the complexity of drawing operations. For instance,
2525 * when animating a complex view tree with a translation, a hardware layer can
2526 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002527 * <p>A hardware layer can also be used to increase the rendering quality when
2528 * rotation transformations are applied on a view. It can also be used to
2529 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002530 *
2531 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002532 * @see #setLayerType(int, android.graphics.Paint)
2533 * @see #LAYER_TYPE_NONE
2534 * @see #LAYER_TYPE_SOFTWARE
2535 */
2536 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002537
Romain Guy3aaff3a2011-01-12 14:18:47 -08002538 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2539 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2540 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2541 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2542 })
Romain Guy171c5922011-01-06 10:04:23 -08002543 int mLayerType = LAYER_TYPE_NONE;
2544 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002545 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002546
2547 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002548 * Set to true when the view is sending hover accessibility events because it
2549 * is the innermost hovered view.
2550 */
2551 private boolean mSendingHoverAccessibilityEvents;
2552
2553 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002554 * Delegate for injecting accessiblity functionality.
2555 */
2556 AccessibilityDelegate mAccessibilityDelegate;
2557
2558 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002559 * Text direction is inherited thru {@link ViewGroup}
2560 * @hide
2561 */
2562 public static final int TEXT_DIRECTION_INHERIT = 0;
2563
2564 /**
2565 * Text direction is using "first strong algorithm". The first strong directional character
2566 * determines the paragraph direction. If there is no strong directional character, the
Doug Feltcb3791202011-07-07 11:57:48 -07002567 * paragraph direction is the view's resolved ayout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002568 *
2569 * @hide
2570 */
2571 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2572
2573 /**
2574 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2575 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002576 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002577 *
2578 * @hide
2579 */
2580 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2581
2582 /**
2583 * Text direction is forced to LTR.
2584 *
2585 * @hide
2586 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002587 public static final int TEXT_DIRECTION_LTR = 3;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002588
2589 /**
2590 * Text direction is forced to RTL.
2591 *
2592 * @hide
2593 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002594 public static final int TEXT_DIRECTION_RTL = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002595
2596 /**
2597 * Default text direction is inherited
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07002598 *
2599 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002600 */
2601 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2602
2603 /**
2604 * The text direction that has been defined by {@link #setTextDirection(int)}.
2605 *
2606 * {@hide}
2607 */
2608 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002609 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2610 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2611 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2612 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2613 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2614 })
Doug Feltcb3791202011-07-07 11:57:48 -07002615 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002616
2617 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002618 * The resolved text direction. This needs resolution if the value is
2619 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if that is
2620 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2621 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002622 *
2623 * {@hide}
2624 */
2625 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002626 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2627 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2628 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002629 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2630 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2631 })
Doug Feltcb3791202011-07-07 11:57:48 -07002632 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002633
2634 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002635 * Consistency verifier for debugging purposes.
2636 * @hide
2637 */
2638 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2639 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2640 new InputEventConsistencyVerifier(this, 0) : null;
2641
2642 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 * Simple constructor to use when creating a view from code.
2644 *
2645 * @param context The Context the view is running in, through which it can
2646 * access the current theme, resources, etc.
2647 */
2648 public View(Context context) {
2649 mContext = context;
2650 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002651 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002652 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002653 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002654 mUserPaddingStart = -1;
2655 mUserPaddingEnd = -1;
2656 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 }
2658
2659 /**
2660 * Constructor that is called when inflating a view from XML. This is called
2661 * when a view is being constructed from an XML file, supplying attributes
2662 * that were specified in the XML file. This version uses a default style of
2663 * 0, so the only attribute values applied are those in the Context's Theme
2664 * and the given AttributeSet.
2665 *
2666 * <p>
2667 * The method onFinishInflate() will be called after all children have been
2668 * added.
2669 *
2670 * @param context The Context the view is running in, through which it can
2671 * access the current theme, resources, etc.
2672 * @param attrs The attributes of the XML tag that is inflating the view.
2673 * @see #View(Context, AttributeSet, int)
2674 */
2675 public View(Context context, AttributeSet attrs) {
2676 this(context, attrs, 0);
2677 }
2678
2679 /**
2680 * Perform inflation from XML and apply a class-specific base style. This
2681 * constructor of View allows subclasses to use their own base style when
2682 * they are inflating. For example, a Button class's constructor would call
2683 * this version of the super class constructor and supply
2684 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2685 * the theme's button style to modify all of the base view attributes (in
2686 * particular its background) as well as the Button class's attributes.
2687 *
2688 * @param context The Context the view is running in, through which it can
2689 * access the current theme, resources, etc.
2690 * @param attrs The attributes of the XML tag that is inflating the view.
2691 * @param defStyle The default style to apply to this view. If 0, no style
2692 * will be applied (beyond what is included in the theme). This may
2693 * either be an attribute resource, whose value will be retrieved
2694 * from the current theme, or an explicit style resource.
2695 * @see #View(Context, AttributeSet)
2696 */
2697 public View(Context context, AttributeSet attrs, int defStyle) {
2698 this(context);
2699
2700 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2701 defStyle, 0);
2702
2703 Drawable background = null;
2704
2705 int leftPadding = -1;
2706 int topPadding = -1;
2707 int rightPadding = -1;
2708 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002709 int startPadding = -1;
2710 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711
2712 int padding = -1;
2713
2714 int viewFlagValues = 0;
2715 int viewFlagMasks = 0;
2716
2717 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 int x = 0;
2720 int y = 0;
2721
Chet Haase73066682010-11-29 15:55:32 -08002722 float tx = 0;
2723 float ty = 0;
2724 float rotation = 0;
2725 float rotationX = 0;
2726 float rotationY = 0;
2727 float sx = 1f;
2728 float sy = 1f;
2729 boolean transformSet = false;
2730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2732
Adam Powell637d3372010-08-25 14:37:03 -07002733 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 final int N = a.getIndexCount();
2735 for (int i = 0; i < N; i++) {
2736 int attr = a.getIndex(i);
2737 switch (attr) {
2738 case com.android.internal.R.styleable.View_background:
2739 background = a.getDrawable(attr);
2740 break;
2741 case com.android.internal.R.styleable.View_padding:
2742 padding = a.getDimensionPixelSize(attr, -1);
2743 break;
2744 case com.android.internal.R.styleable.View_paddingLeft:
2745 leftPadding = a.getDimensionPixelSize(attr, -1);
2746 break;
2747 case com.android.internal.R.styleable.View_paddingTop:
2748 topPadding = a.getDimensionPixelSize(attr, -1);
2749 break;
2750 case com.android.internal.R.styleable.View_paddingRight:
2751 rightPadding = a.getDimensionPixelSize(attr, -1);
2752 break;
2753 case com.android.internal.R.styleable.View_paddingBottom:
2754 bottomPadding = a.getDimensionPixelSize(attr, -1);
2755 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002756 case com.android.internal.R.styleable.View_paddingStart:
2757 startPadding = a.getDimensionPixelSize(attr, -1);
2758 break;
2759 case com.android.internal.R.styleable.View_paddingEnd:
2760 endPadding = a.getDimensionPixelSize(attr, -1);
2761 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 case com.android.internal.R.styleable.View_scrollX:
2763 x = a.getDimensionPixelOffset(attr, 0);
2764 break;
2765 case com.android.internal.R.styleable.View_scrollY:
2766 y = a.getDimensionPixelOffset(attr, 0);
2767 break;
Chet Haase73066682010-11-29 15:55:32 -08002768 case com.android.internal.R.styleable.View_alpha:
2769 setAlpha(a.getFloat(attr, 1f));
2770 break;
2771 case com.android.internal.R.styleable.View_transformPivotX:
2772 setPivotX(a.getDimensionPixelOffset(attr, 0));
2773 break;
2774 case com.android.internal.R.styleable.View_transformPivotY:
2775 setPivotY(a.getDimensionPixelOffset(attr, 0));
2776 break;
2777 case com.android.internal.R.styleable.View_translationX:
2778 tx = a.getDimensionPixelOffset(attr, 0);
2779 transformSet = true;
2780 break;
2781 case com.android.internal.R.styleable.View_translationY:
2782 ty = a.getDimensionPixelOffset(attr, 0);
2783 transformSet = true;
2784 break;
2785 case com.android.internal.R.styleable.View_rotation:
2786 rotation = a.getFloat(attr, 0);
2787 transformSet = true;
2788 break;
2789 case com.android.internal.R.styleable.View_rotationX:
2790 rotationX = a.getFloat(attr, 0);
2791 transformSet = true;
2792 break;
2793 case com.android.internal.R.styleable.View_rotationY:
2794 rotationY = a.getFloat(attr, 0);
2795 transformSet = true;
2796 break;
2797 case com.android.internal.R.styleable.View_scaleX:
2798 sx = a.getFloat(attr, 1f);
2799 transformSet = true;
2800 break;
2801 case com.android.internal.R.styleable.View_scaleY:
2802 sy = a.getFloat(attr, 1f);
2803 transformSet = true;
2804 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 case com.android.internal.R.styleable.View_id:
2806 mID = a.getResourceId(attr, NO_ID);
2807 break;
2808 case com.android.internal.R.styleable.View_tag:
2809 mTag = a.getText(attr);
2810 break;
2811 case com.android.internal.R.styleable.View_fitsSystemWindows:
2812 if (a.getBoolean(attr, false)) {
2813 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2814 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2815 }
2816 break;
2817 case com.android.internal.R.styleable.View_focusable:
2818 if (a.getBoolean(attr, false)) {
2819 viewFlagValues |= FOCUSABLE;
2820 viewFlagMasks |= FOCUSABLE_MASK;
2821 }
2822 break;
2823 case com.android.internal.R.styleable.View_focusableInTouchMode:
2824 if (a.getBoolean(attr, false)) {
2825 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2826 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2827 }
2828 break;
2829 case com.android.internal.R.styleable.View_clickable:
2830 if (a.getBoolean(attr, false)) {
2831 viewFlagValues |= CLICKABLE;
2832 viewFlagMasks |= CLICKABLE;
2833 }
2834 break;
2835 case com.android.internal.R.styleable.View_longClickable:
2836 if (a.getBoolean(attr, false)) {
2837 viewFlagValues |= LONG_CLICKABLE;
2838 viewFlagMasks |= LONG_CLICKABLE;
2839 }
2840 break;
2841 case com.android.internal.R.styleable.View_saveEnabled:
2842 if (!a.getBoolean(attr, true)) {
2843 viewFlagValues |= SAVE_DISABLED;
2844 viewFlagMasks |= SAVE_DISABLED_MASK;
2845 }
2846 break;
2847 case com.android.internal.R.styleable.View_duplicateParentState:
2848 if (a.getBoolean(attr, false)) {
2849 viewFlagValues |= DUPLICATE_PARENT_STATE;
2850 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2851 }
2852 break;
2853 case com.android.internal.R.styleable.View_visibility:
2854 final int visibility = a.getInt(attr, 0);
2855 if (visibility != 0) {
2856 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2857 viewFlagMasks |= VISIBILITY_MASK;
2858 }
2859 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002860 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002861 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002862 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002863 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002864 final int layoutDirection = a.getInt(attr, -1);
2865 if (layoutDirection != -1) {
2866 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002867 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002868 // Set to default (LAYOUT_DIRECTION_INHERIT)
2869 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002870 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002871 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002872 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 case com.android.internal.R.styleable.View_drawingCacheQuality:
2874 final int cacheQuality = a.getInt(attr, 0);
2875 if (cacheQuality != 0) {
2876 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2877 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2878 }
2879 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002880 case com.android.internal.R.styleable.View_contentDescription:
2881 mContentDescription = a.getString(attr);
2882 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2884 if (!a.getBoolean(attr, true)) {
2885 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2886 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2887 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002888 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2890 if (!a.getBoolean(attr, true)) {
2891 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2892 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2893 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002894 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 case R.styleable.View_scrollbars:
2896 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2897 if (scrollbars != SCROLLBARS_NONE) {
2898 viewFlagValues |= scrollbars;
2899 viewFlagMasks |= SCROLLBARS_MASK;
2900 initializeScrollbars(a);
2901 }
2902 break;
2903 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07002904 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
2905 // Ignore the attribute starting with ICS
2906 break;
2907 }
2908 // With builds < ICS, fall through and apply fading edges
2909 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2911 if (fadingEdge != FADING_EDGE_NONE) {
2912 viewFlagValues |= fadingEdge;
2913 viewFlagMasks |= FADING_EDGE_MASK;
2914 initializeFadingEdge(a);
2915 }
2916 break;
2917 case R.styleable.View_scrollbarStyle:
2918 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2919 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2920 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2921 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2922 }
2923 break;
2924 case R.styleable.View_isScrollContainer:
2925 setScrollContainer = true;
2926 if (a.getBoolean(attr, false)) {
2927 setScrollContainer(true);
2928 }
2929 break;
2930 case com.android.internal.R.styleable.View_keepScreenOn:
2931 if (a.getBoolean(attr, false)) {
2932 viewFlagValues |= KEEP_SCREEN_ON;
2933 viewFlagMasks |= KEEP_SCREEN_ON;
2934 }
2935 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002936 case R.styleable.View_filterTouchesWhenObscured:
2937 if (a.getBoolean(attr, false)) {
2938 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2939 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2940 }
2941 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942 case R.styleable.View_nextFocusLeft:
2943 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2944 break;
2945 case R.styleable.View_nextFocusRight:
2946 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2947 break;
2948 case R.styleable.View_nextFocusUp:
2949 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2950 break;
2951 case R.styleable.View_nextFocusDown:
2952 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2953 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002954 case R.styleable.View_nextFocusForward:
2955 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2956 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 case R.styleable.View_minWidth:
2958 mMinWidth = a.getDimensionPixelSize(attr, 0);
2959 break;
2960 case R.styleable.View_minHeight:
2961 mMinHeight = a.getDimensionPixelSize(attr, 0);
2962 break;
Romain Guy9a817362009-05-01 10:57:14 -07002963 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002964 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002965 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002966 + "be used within a restricted context");
2967 }
2968
Romain Guy9a817362009-05-01 10:57:14 -07002969 final String handlerName = a.getString(attr);
2970 if (handlerName != null) {
2971 setOnClickListener(new OnClickListener() {
2972 private Method mHandler;
2973
2974 public void onClick(View v) {
2975 if (mHandler == null) {
2976 try {
2977 mHandler = getContext().getClass().getMethod(handlerName,
2978 View.class);
2979 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002980 int id = getId();
2981 String idText = id == NO_ID ? "" : " with id '"
2982 + getContext().getResources().getResourceEntryName(
2983 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002984 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002985 handlerName + "(View) in the activity "
2986 + getContext().getClass() + " for onClick handler"
2987 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002988 }
2989 }
2990
2991 try {
2992 mHandler.invoke(getContext(), View.this);
2993 } catch (IllegalAccessException e) {
2994 throw new IllegalStateException("Could not execute non "
2995 + "public method of the activity", e);
2996 } catch (InvocationTargetException e) {
2997 throw new IllegalStateException("Could not execute "
2998 + "method of the activity", e);
2999 }
3000 }
3001 });
3002 }
3003 break;
Adam Powell637d3372010-08-25 14:37:03 -07003004 case R.styleable.View_overScrollMode:
3005 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3006 break;
Adam Powell20232d02010-12-08 21:08:53 -08003007 case R.styleable.View_verticalScrollbarPosition:
3008 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3009 break;
Romain Guy171c5922011-01-06 10:04:23 -08003010 case R.styleable.View_layerType:
3011 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3012 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003013 case R.styleable.View_textDirection:
3014 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
3015 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 }
3017 }
3018
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003019 a.recycle();
3020
Adam Powell637d3372010-08-25 14:37:03 -07003021 setOverScrollMode(overScrollMode);
3022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 if (background != null) {
3024 setBackgroundDrawable(background);
3025 }
3026
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003027 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
3028
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003029 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3030 // layout direction). Those cached values will be used later during padding resolution.
3031 mUserPaddingStart = startPadding;
3032 mUserPaddingEnd = endPadding;
3033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 if (padding >= 0) {
3035 leftPadding = padding;
3036 topPadding = padding;
3037 rightPadding = padding;
3038 bottomPadding = padding;
3039 }
3040
3041 // If the user specified the padding (either with android:padding or
3042 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3043 // use the default padding or the padding from the background drawable
3044 // (stored at this point in mPadding*)
3045 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3046 topPadding >= 0 ? topPadding : mPaddingTop,
3047 rightPadding >= 0 ? rightPadding : mPaddingRight,
3048 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3049
3050 if (viewFlagMasks != 0) {
3051 setFlags(viewFlagValues, viewFlagMasks);
3052 }
3053
3054 // Needs to be called after mViewFlags is set
3055 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3056 recomputePadding();
3057 }
3058
3059 if (x != 0 || y != 0) {
3060 scrollTo(x, y);
3061 }
3062
Chet Haase73066682010-11-29 15:55:32 -08003063 if (transformSet) {
3064 setTranslationX(tx);
3065 setTranslationY(ty);
3066 setRotation(rotation);
3067 setRotationX(rotationX);
3068 setRotationY(rotationY);
3069 setScaleX(sx);
3070 setScaleY(sy);
3071 }
3072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003073 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3074 setScrollContainer(true);
3075 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003076
3077 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078 }
3079
3080 /**
3081 * Non-public constructor for use in testing
3082 */
3083 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003084 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 }
3086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 /**
3088 * <p>
3089 * Initializes the fading edges from a given set of styled attributes. This
3090 * method should be called by subclasses that need fading edges and when an
3091 * instance of these subclasses is created programmatically rather than
3092 * being inflated from XML. This method is automatically called when the XML
3093 * is inflated.
3094 * </p>
3095 *
3096 * @param a the styled attributes set to initialize the fading edges from
3097 */
3098 protected void initializeFadingEdge(TypedArray a) {
3099 initScrollCache();
3100
3101 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3102 R.styleable.View_fadingEdgeLength,
3103 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3104 }
3105
3106 /**
3107 * Returns the size of the vertical faded edges used to indicate that more
3108 * content in this view is visible.
3109 *
3110 * @return The size in pixels of the vertical faded edge or 0 if vertical
3111 * faded edges are not enabled for this view.
3112 * @attr ref android.R.styleable#View_fadingEdgeLength
3113 */
3114 public int getVerticalFadingEdgeLength() {
3115 if (isVerticalFadingEdgeEnabled()) {
3116 ScrollabilityCache cache = mScrollCache;
3117 if (cache != null) {
3118 return cache.fadingEdgeLength;
3119 }
3120 }
3121 return 0;
3122 }
3123
3124 /**
3125 * Set the size of the faded edge used to indicate that more content in this
3126 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003127 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3128 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3129 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003130 *
3131 * @param length The size in pixels of the faded edge used to indicate that more
3132 * content in this view is visible.
3133 */
3134 public void setFadingEdgeLength(int length) {
3135 initScrollCache();
3136 mScrollCache.fadingEdgeLength = length;
3137 }
3138
3139 /**
3140 * Returns the size of the horizontal faded edges used to indicate that more
3141 * content in this view is visible.
3142 *
3143 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3144 * faded edges are not enabled for this view.
3145 * @attr ref android.R.styleable#View_fadingEdgeLength
3146 */
3147 public int getHorizontalFadingEdgeLength() {
3148 if (isHorizontalFadingEdgeEnabled()) {
3149 ScrollabilityCache cache = mScrollCache;
3150 if (cache != null) {
3151 return cache.fadingEdgeLength;
3152 }
3153 }
3154 return 0;
3155 }
3156
3157 /**
3158 * Returns the width of the vertical scrollbar.
3159 *
3160 * @return The width in pixels of the vertical scrollbar or 0 if there
3161 * is no vertical scrollbar.
3162 */
3163 public int getVerticalScrollbarWidth() {
3164 ScrollabilityCache cache = mScrollCache;
3165 if (cache != null) {
3166 ScrollBarDrawable scrollBar = cache.scrollBar;
3167 if (scrollBar != null) {
3168 int size = scrollBar.getSize(true);
3169 if (size <= 0) {
3170 size = cache.scrollBarSize;
3171 }
3172 return size;
3173 }
3174 return 0;
3175 }
3176 return 0;
3177 }
3178
3179 /**
3180 * Returns the height of the horizontal scrollbar.
3181 *
3182 * @return The height in pixels of the horizontal scrollbar or 0 if
3183 * there is no horizontal scrollbar.
3184 */
3185 protected int getHorizontalScrollbarHeight() {
3186 ScrollabilityCache cache = mScrollCache;
3187 if (cache != null) {
3188 ScrollBarDrawable scrollBar = cache.scrollBar;
3189 if (scrollBar != null) {
3190 int size = scrollBar.getSize(false);
3191 if (size <= 0) {
3192 size = cache.scrollBarSize;
3193 }
3194 return size;
3195 }
3196 return 0;
3197 }
3198 return 0;
3199 }
3200
3201 /**
3202 * <p>
3203 * Initializes the scrollbars from a given set of styled attributes. This
3204 * method should be called by subclasses that need scrollbars and when an
3205 * instance of these subclasses is created programmatically rather than
3206 * being inflated from XML. This method is automatically called when the XML
3207 * is inflated.
3208 * </p>
3209 *
3210 * @param a the styled attributes set to initialize the scrollbars from
3211 */
3212 protected void initializeScrollbars(TypedArray a) {
3213 initScrollCache();
3214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003216
Mike Cleronf116bf82009-09-27 19:14:12 -07003217 if (scrollabilityCache.scrollBar == null) {
3218 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3219 }
Joe Malin32736f02011-01-19 16:14:20 -08003220
Romain Guy8bda2482010-03-02 11:42:11 -08003221 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222
Mike Cleronf116bf82009-09-27 19:14:12 -07003223 if (!fadeScrollbars) {
3224 scrollabilityCache.state = ScrollabilityCache.ON;
3225 }
3226 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003227
3228
Mike Cleronf116bf82009-09-27 19:14:12 -07003229 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3230 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3231 .getScrollBarFadeDuration());
3232 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3233 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3234 ViewConfiguration.getScrollDefaultDelay());
3235
Joe Malin32736f02011-01-19 16:14:20 -08003236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3238 com.android.internal.R.styleable.View_scrollbarSize,
3239 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3240
3241 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3242 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3243
3244 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3245 if (thumb != null) {
3246 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3247 }
3248
3249 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3250 false);
3251 if (alwaysDraw) {
3252 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3253 }
3254
3255 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3256 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3257
3258 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3259 if (thumb != null) {
3260 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3261 }
3262
3263 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3264 false);
3265 if (alwaysDraw) {
3266 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3267 }
3268
3269 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003270 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003271 }
3272
3273 /**
3274 * <p>
3275 * Initalizes the scrollability cache if necessary.
3276 * </p>
3277 */
3278 private void initScrollCache() {
3279 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003280 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 }
3282 }
3283
3284 /**
Adam Powell20232d02010-12-08 21:08:53 -08003285 * Set the position of the vertical scroll bar. Should be one of
3286 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3287 * {@link #SCROLLBAR_POSITION_RIGHT}.
3288 *
3289 * @param position Where the vertical scroll bar should be positioned.
3290 */
3291 public void setVerticalScrollbarPosition(int position) {
3292 if (mVerticalScrollbarPosition != position) {
3293 mVerticalScrollbarPosition = position;
3294 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003295 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003296 }
3297 }
3298
3299 /**
3300 * @return The position where the vertical scroll bar will show, if applicable.
3301 * @see #setVerticalScrollbarPosition(int)
3302 */
3303 public int getVerticalScrollbarPosition() {
3304 return mVerticalScrollbarPosition;
3305 }
3306
3307 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 * Register a callback to be invoked when focus of this view changed.
3309 *
3310 * @param l The callback that will run.
3311 */
3312 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3313 mOnFocusChangeListener = l;
3314 }
3315
3316 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003317 * Add a listener that will be called when the bounds of the view change due to
3318 * layout processing.
3319 *
3320 * @param listener The listener that will be called when layout bounds change.
3321 */
3322 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3323 if (mOnLayoutChangeListeners == null) {
3324 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3325 }
3326 mOnLayoutChangeListeners.add(listener);
3327 }
3328
3329 /**
3330 * Remove a listener for layout changes.
3331 *
3332 * @param listener The listener for layout bounds change.
3333 */
3334 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3335 if (mOnLayoutChangeListeners == null) {
3336 return;
3337 }
3338 mOnLayoutChangeListeners.remove(listener);
3339 }
3340
3341 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003342 * Add a listener for attach state changes.
3343 *
3344 * This listener will be called whenever this view is attached or detached
3345 * from a window. Remove the listener using
3346 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3347 *
3348 * @param listener Listener to attach
3349 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3350 */
3351 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3352 if (mOnAttachStateChangeListeners == null) {
3353 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3354 }
3355 mOnAttachStateChangeListeners.add(listener);
3356 }
3357
3358 /**
3359 * Remove a listener for attach state changes. The listener will receive no further
3360 * notification of window attach/detach events.
3361 *
3362 * @param listener Listener to remove
3363 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3364 */
3365 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3366 if (mOnAttachStateChangeListeners == null) {
3367 return;
3368 }
3369 mOnAttachStateChangeListeners.remove(listener);
3370 }
3371
3372 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 * Returns the focus-change callback registered for this view.
3374 *
3375 * @return The callback, or null if one is not registered.
3376 */
3377 public OnFocusChangeListener getOnFocusChangeListener() {
3378 return mOnFocusChangeListener;
3379 }
3380
3381 /**
3382 * Register a callback to be invoked when this view is clicked. If this view is not
3383 * clickable, it becomes clickable.
3384 *
3385 * @param l The callback that will run
3386 *
3387 * @see #setClickable(boolean)
3388 */
3389 public void setOnClickListener(OnClickListener l) {
3390 if (!isClickable()) {
3391 setClickable(true);
3392 }
3393 mOnClickListener = l;
3394 }
3395
3396 /**
3397 * Register a callback to be invoked when this view is clicked and held. If this view is not
3398 * long clickable, it becomes long clickable.
3399 *
3400 * @param l The callback that will run
3401 *
3402 * @see #setLongClickable(boolean)
3403 */
3404 public void setOnLongClickListener(OnLongClickListener l) {
3405 if (!isLongClickable()) {
3406 setLongClickable(true);
3407 }
3408 mOnLongClickListener = l;
3409 }
3410
3411 /**
3412 * Register a callback to be invoked when the context menu for this view is
3413 * being built. If this view is not long clickable, it becomes long clickable.
3414 *
3415 * @param l The callback that will run
3416 *
3417 */
3418 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3419 if (!isLongClickable()) {
3420 setLongClickable(true);
3421 }
3422 mOnCreateContextMenuListener = l;
3423 }
3424
3425 /**
3426 * Call this view's OnClickListener, if it is defined.
3427 *
3428 * @return True there was an assigned OnClickListener that was called, false
3429 * otherwise is returned.
3430 */
3431 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003432 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 if (mOnClickListener != null) {
3435 playSoundEffect(SoundEffectConstants.CLICK);
3436 mOnClickListener.onClick(this);
3437 return true;
3438 }
3439
3440 return false;
3441 }
3442
3443 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003444 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3445 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003447 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 */
3449 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003450 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 boolean handled = false;
3453 if (mOnLongClickListener != null) {
3454 handled = mOnLongClickListener.onLongClick(View.this);
3455 }
3456 if (!handled) {
3457 handled = showContextMenu();
3458 }
3459 if (handled) {
3460 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3461 }
3462 return handled;
3463 }
3464
3465 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003466 * Performs button-related actions during a touch down event.
3467 *
3468 * @param event The event.
3469 * @return True if the down was consumed.
3470 *
3471 * @hide
3472 */
3473 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3474 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3475 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3476 return true;
3477 }
3478 }
3479 return false;
3480 }
3481
3482 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003483 * Bring up the context menu for this view.
3484 *
3485 * @return Whether a context menu was displayed.
3486 */
3487 public boolean showContextMenu() {
3488 return getParent().showContextMenuForChild(this);
3489 }
3490
3491 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003492 * Bring up the context menu for this view, referring to the item under the specified point.
3493 *
3494 * @param x The referenced x coordinate.
3495 * @param y The referenced y coordinate.
3496 * @param metaState The keyboard modifiers that were pressed.
3497 * @return Whether a context menu was displayed.
3498 *
3499 * @hide
3500 */
3501 public boolean showContextMenu(float x, float y, int metaState) {
3502 return showContextMenu();
3503 }
3504
3505 /**
Adam Powell6e346362010-07-23 10:18:23 -07003506 * Start an action mode.
3507 *
3508 * @param callback Callback that will control the lifecycle of the action mode
3509 * @return The new action mode if it is started, null otherwise
3510 *
3511 * @see ActionMode
3512 */
3513 public ActionMode startActionMode(ActionMode.Callback callback) {
3514 return getParent().startActionModeForChild(this, callback);
3515 }
3516
3517 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 * Register a callback to be invoked when a key is pressed in this view.
3519 * @param l the key listener to attach to this view
3520 */
3521 public void setOnKeyListener(OnKeyListener l) {
3522 mOnKeyListener = l;
3523 }
3524
3525 /**
3526 * Register a callback to be invoked when a touch event is sent to this view.
3527 * @param l the touch listener to attach to this view
3528 */
3529 public void setOnTouchListener(OnTouchListener l) {
3530 mOnTouchListener = l;
3531 }
3532
3533 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003534 * Register a callback to be invoked when a generic motion event is sent to this view.
3535 * @param l the generic motion listener to attach to this view
3536 */
3537 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3538 mOnGenericMotionListener = l;
3539 }
3540
3541 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003542 * Register a callback to be invoked when a hover event is sent to this view.
3543 * @param l the hover listener to attach to this view
3544 */
3545 public void setOnHoverListener(OnHoverListener l) {
3546 mOnHoverListener = l;
3547 }
3548
3549 /**
Joe Malin32736f02011-01-19 16:14:20 -08003550 * Register a drag event listener callback object for this View. The parameter is
3551 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3552 * View, the system calls the
3553 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3554 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003555 */
3556 public void setOnDragListener(OnDragListener l) {
3557 mOnDragListener = l;
3558 }
3559
3560 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003561 * Give this view focus. This will cause
3562 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003563 *
3564 * Note: this does not check whether this {@link View} should get focus, it just
3565 * gives it focus no matter what. It should only be called internally by framework
3566 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3567 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003568 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3569 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 * focus moved when requestFocus() is called. It may not always
3571 * apply, in which case use the default View.FOCUS_DOWN.
3572 * @param previouslyFocusedRect The rectangle of the view that had focus
3573 * prior in this View's coordinate system.
3574 */
3575 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3576 if (DBG) {
3577 System.out.println(this + " requestFocus()");
3578 }
3579
3580 if ((mPrivateFlags & FOCUSED) == 0) {
3581 mPrivateFlags |= FOCUSED;
3582
3583 if (mParent != null) {
3584 mParent.requestChildFocus(this, this);
3585 }
3586
3587 onFocusChanged(true, direction, previouslyFocusedRect);
3588 refreshDrawableState();
3589 }
3590 }
3591
3592 /**
3593 * Request that a rectangle of this view be visible on the screen,
3594 * scrolling if necessary just enough.
3595 *
3596 * <p>A View should call this if it maintains some notion of which part
3597 * of its content is interesting. For example, a text editing view
3598 * should call this when its cursor moves.
3599 *
3600 * @param rectangle The rectangle.
3601 * @return Whether any parent scrolled.
3602 */
3603 public boolean requestRectangleOnScreen(Rect rectangle) {
3604 return requestRectangleOnScreen(rectangle, false);
3605 }
3606
3607 /**
3608 * Request that a rectangle of this view be visible on the screen,
3609 * scrolling if necessary just enough.
3610 *
3611 * <p>A View should call this if it maintains some notion of which part
3612 * of its content is interesting. For example, a text editing view
3613 * should call this when its cursor moves.
3614 *
3615 * <p>When <code>immediate</code> is set to true, scrolling will not be
3616 * animated.
3617 *
3618 * @param rectangle The rectangle.
3619 * @param immediate True to forbid animated scrolling, false otherwise
3620 * @return Whether any parent scrolled.
3621 */
3622 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3623 View child = this;
3624 ViewParent parent = mParent;
3625 boolean scrolled = false;
3626 while (parent != null) {
3627 scrolled |= parent.requestChildRectangleOnScreen(child,
3628 rectangle, immediate);
3629
3630 // offset rect so next call has the rectangle in the
3631 // coordinate system of its direct child.
3632 rectangle.offset(child.getLeft(), child.getTop());
3633 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3634
3635 if (!(parent instanceof View)) {
3636 break;
3637 }
Romain Guy8506ab42009-06-11 17:35:47 -07003638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003639 child = (View) parent;
3640 parent = child.getParent();
3641 }
3642 return scrolled;
3643 }
3644
3645 /**
3646 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003647 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003648 */
3649 public void clearFocus() {
3650 if (DBG) {
3651 System.out.println(this + " clearFocus()");
3652 }
3653
3654 if ((mPrivateFlags & FOCUSED) != 0) {
3655 mPrivateFlags &= ~FOCUSED;
3656
3657 if (mParent != null) {
3658 mParent.clearChildFocus(this);
3659 }
3660
3661 onFocusChanged(false, 0, null);
3662 refreshDrawableState();
3663 }
3664 }
3665
3666 /**
3667 * Called to clear the focus of a view that is about to be removed.
3668 * Doesn't call clearChildFocus, which prevents this view from taking
3669 * focus again before it has been removed from the parent
3670 */
3671 void clearFocusForRemoval() {
3672 if ((mPrivateFlags & FOCUSED) != 0) {
3673 mPrivateFlags &= ~FOCUSED;
3674
3675 onFocusChanged(false, 0, null);
3676 refreshDrawableState();
3677 }
3678 }
3679
3680 /**
3681 * Called internally by the view system when a new view is getting focus.
3682 * This is what clears the old focus.
3683 */
3684 void unFocus() {
3685 if (DBG) {
3686 System.out.println(this + " unFocus()");
3687 }
3688
3689 if ((mPrivateFlags & FOCUSED) != 0) {
3690 mPrivateFlags &= ~FOCUSED;
3691
3692 onFocusChanged(false, 0, null);
3693 refreshDrawableState();
3694 }
3695 }
3696
3697 /**
3698 * Returns true if this view has focus iteself, or is the ancestor of the
3699 * view that has focus.
3700 *
3701 * @return True if this view has or contains focus, false otherwise.
3702 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003703 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003704 public boolean hasFocus() {
3705 return (mPrivateFlags & FOCUSED) != 0;
3706 }
3707
3708 /**
3709 * Returns true if this view is focusable or if it contains a reachable View
3710 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3711 * is a View whose parents do not block descendants focus.
3712 *
3713 * Only {@link #VISIBLE} views are considered focusable.
3714 *
3715 * @return True if the view is focusable or if the view contains a focusable
3716 * View, false otherwise.
3717 *
3718 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3719 */
3720 public boolean hasFocusable() {
3721 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3722 }
3723
3724 /**
3725 * Called by the view system when the focus state of this view changes.
3726 * When the focus change event is caused by directional navigation, direction
3727 * and previouslyFocusedRect provide insight into where the focus is coming from.
3728 * When overriding, be sure to call up through to the super class so that
3729 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003730 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003731 * @param gainFocus True if the View has focus; false otherwise.
3732 * @param direction The direction focus has moved when requestFocus()
3733 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003734 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3735 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3736 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003737 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3738 * system, of the previously focused view. If applicable, this will be
3739 * passed in as finer grained information about where the focus is coming
3740 * from (in addition to direction). Will be <code>null</code> otherwise.
3741 */
3742 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003743 if (gainFocus) {
3744 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3745 }
3746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747 InputMethodManager imm = InputMethodManager.peekInstance();
3748 if (!gainFocus) {
3749 if (isPressed()) {
3750 setPressed(false);
3751 }
3752 if (imm != null && mAttachInfo != null
3753 && mAttachInfo.mHasWindowFocus) {
3754 imm.focusOut(this);
3755 }
Romain Guya2431d02009-04-30 16:30:00 -07003756 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 } else if (imm != null && mAttachInfo != null
3758 && mAttachInfo.mHasWindowFocus) {
3759 imm.focusIn(this);
3760 }
Romain Guy8506ab42009-06-11 17:35:47 -07003761
Romain Guy0fd89bf2011-01-26 15:41:30 -08003762 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 if (mOnFocusChangeListener != null) {
3764 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3765 }
Joe Malin32736f02011-01-19 16:14:20 -08003766
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003767 if (mAttachInfo != null) {
3768 mAttachInfo.mKeyDispatchState.reset(this);
3769 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 }
3771
3772 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003773 * Sends an accessibility event of the given type. If accessiiblity is
3774 * not enabled this method has no effect. The default implementation calls
3775 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3776 * to populate information about the event source (this View), then calls
3777 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3778 * populate the text content of the event source including its descendants,
3779 * and last calls
3780 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3781 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003782 * <p>
3783 * If an {@link AccessibilityDelegate} has been specified via calling
3784 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3785 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
3786 * responsible for handling this call.
3787 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003788 *
3789 * @param eventType The type of the event to send.
3790 *
3791 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3792 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3793 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003794 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07003795 */
3796 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003797 if (mAccessibilityDelegate != null) {
3798 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
3799 } else {
3800 sendAccessibilityEventInternal(eventType);
3801 }
3802 }
3803
3804 /**
3805 * @see #sendAccessibilityEvent(int)
3806 *
3807 * Note: Called from the default {@link AccessibilityDelegate}.
3808 */
3809 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003810 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3811 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3812 }
3813 }
3814
3815 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003816 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3817 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003818 * perform a check whether accessibility is enabled.
3819 * <p>
3820 * If an {@link AccessibilityDelegate} has been specified via calling
3821 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3822 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
3823 * is responsible for handling this call.
3824 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003825 *
3826 * @param event The event to send.
3827 *
3828 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003829 */
3830 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003831 if (mAccessibilityDelegate != null) {
3832 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
3833 } else {
3834 sendAccessibilityEventUncheckedInternal(event);
3835 }
3836 }
3837
3838 /**
3839 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
3840 *
3841 * Note: Called from the default {@link AccessibilityDelegate}.
3842 */
3843 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003844 if (!isShown()) {
3845 return;
3846 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003847 onInitializeAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003848 dispatchPopulateAccessibilityEvent(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003849 // In the beginning we called #isShown(), so we know that getParent() is not null.
3850 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003851 }
3852
3853 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003854 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3855 * to its children for adding their text content to the event. Note that the
3856 * event text is populated in a separate dispatch path since we add to the
3857 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003858 * A typical implementation will call
3859 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3860 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3861 * on each child. Override this method if custom population of the event text
3862 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003863 * <p>
3864 * If an {@link AccessibilityDelegate} has been specified via calling
3865 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3866 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
3867 * is responsible for handling this call.
3868 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07003869 *
3870 * @param event The event.
3871 *
3872 * @return True if the event population was completed.
3873 */
3874 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003875 if (mAccessibilityDelegate != null) {
3876 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
3877 } else {
3878 return dispatchPopulateAccessibilityEventInternal(event);
3879 }
3880 }
3881
3882 /**
3883 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3884 *
3885 * Note: Called from the default {@link AccessibilityDelegate}.
3886 */
3887 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003888 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003889 return false;
3890 }
3891
3892 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003893 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003894 * giving a chance to this View to populate the accessibility event with its
3895 * text content. While the implementation is free to modify other event
3896 * attributes this should be performed in
3897 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
3898 * <p>
3899 * Example: Adding formatted date string to an accessibility event in addition
3900 * to the text added by the super implementation.
3901 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003902 * public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3903 * super.onPopulateAccessibilityEvent(event);
3904 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
3905 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
3906 * mCurrentDate.getTimeInMillis(), flags);
3907 * event.getText().add(selectedDateUtterance);
3908 * }
3909 * </code></pre></p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003910 * <p>
3911 * If an {@link AccessibilityDelegate} has been specified via calling
3912 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3913 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
3914 * is responsible for handling this call.
3915 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003916 *
3917 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003918 *
3919 * @see #sendAccessibilityEvent(int)
3920 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003921 */
3922 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003923 if (mAccessibilityDelegate != null) {
3924 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
3925 } else {
3926 onPopulateAccessibilityEventInternal(event);
3927 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003928 }
3929
3930 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003931 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
3932 *
3933 * Note: Called from the default {@link AccessibilityDelegate}.
3934 */
3935 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
3936
3937 }
3938
3939 /**
3940 * Initializes an {@link AccessibilityEvent} with information about
3941 * this View which is the event source. In other words, the source of
3942 * an accessibility event is the view whose state change triggered firing
3943 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003944 * <p>
3945 * Example: Setting the password property of an event in addition
3946 * to properties set by the super implementation.
3947 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003948 * public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
3949 * super.onInitializeAccessibilityEvent(event);
3950 * event.setPassword(true);
3951 * }
3952 * </code></pre></p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003953 * <p>
3954 * If an {@link AccessibilityDelegate} has been specified via calling
3955 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3956 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
3957 * is responsible for handling this call.
3958 * </p>
3959 *
3960 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003961 *
3962 * @see #sendAccessibilityEvent(int)
3963 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3964 */
3965 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003966 if (mAccessibilityDelegate != null) {
3967 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
3968 } else {
3969 onInitializeAccessibilityEventInternal(event);
3970 }
3971 }
3972
3973 /**
3974 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3975 *
3976 * Note: Called from the default {@link AccessibilityDelegate}.
3977 */
3978 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003979 event.setSource(this);
Svetoslav Ganov30401322011-05-12 18:53:45 -07003980 event.setClassName(getClass().getName());
3981 event.setPackageName(getContext().getPackageName());
3982 event.setEnabled(isEnabled());
3983 event.setContentDescription(mContentDescription);
3984
Svetoslav Ganova0156172011-06-26 17:55:44 -07003985 final int eventType = event.getEventType();
3986 switch (eventType) {
3987 case AccessibilityEvent.TYPE_VIEW_FOCUSED: {
3988 if (mAttachInfo != null) {
3989 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3990 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
3991 FOCUSABLES_ALL);
3992 event.setItemCount(focusablesTempList.size());
3993 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3994 focusablesTempList.clear();
3995 }
3996 } break;
3997 case AccessibilityEvent.TYPE_VIEW_SCROLLED: {
3998 event.setScrollX(mScrollX);
3999 event.setScrollY(mScrollY);
4000 event.setItemCount(getHeight());
4001 } break;
Svetoslav Ganov30401322011-05-12 18:53:45 -07004002 }
4003 }
4004
4005 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004006 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4007 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4008 * This method is responsible for obtaining an accessibility node info from a
4009 * pool of reusable instances and calling
4010 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4011 * initialize the former.
4012 * <p>
4013 * Note: The client is responsible for recycling the obtained instance by calling
4014 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4015 * </p>
4016 * @return A populated {@link AccessibilityNodeInfo}.
4017 *
4018 * @see AccessibilityNodeInfo
4019 */
4020 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
4021 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4022 onInitializeAccessibilityNodeInfo(info);
4023 return info;
4024 }
4025
4026 /**
4027 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4028 * The base implementation sets:
4029 * <ul>
4030 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004031 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4032 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004033 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4034 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4035 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4036 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4037 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4038 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4039 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4040 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4041 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4042 * </ul>
4043 * <p>
4044 * Subclasses should override this method, call the super implementation,
4045 * and set additional attributes.
4046 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004047 * <p>
4048 * If an {@link AccessibilityDelegate} has been specified via calling
4049 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4050 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4051 * is responsible for handling this call.
4052 * </p>
4053 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004054 * @param info The instance to initialize.
4055 */
4056 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004057 if (mAccessibilityDelegate != null) {
4058 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4059 } else {
4060 onInitializeAccessibilityNodeInfoInternal(info);
4061 }
4062 }
4063
4064 /**
4065 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4066 *
4067 * Note: Called from the default {@link AccessibilityDelegate}.
4068 */
4069 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004070 Rect bounds = mAttachInfo.mTmpInvalRect;
4071 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004072 info.setBoundsInParent(bounds);
4073
4074 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4075 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004076 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004077 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4078 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004079
4080 ViewParent parent = getParent();
4081 if (parent instanceof View) {
4082 View parentView = (View) parent;
4083 info.setParent(parentView);
4084 }
4085
4086 info.setPackageName(mContext.getPackageName());
4087 info.setClassName(getClass().getName());
4088 info.setContentDescription(getContentDescription());
4089
4090 info.setEnabled(isEnabled());
4091 info.setClickable(isClickable());
4092 info.setFocusable(isFocusable());
4093 info.setFocused(isFocused());
4094 info.setSelected(isSelected());
4095 info.setLongClickable(isLongClickable());
4096
4097 // TODO: These make sense only if we are in an AdapterView but all
4098 // views can be selected. Maybe from accessiiblity perspective
4099 // we should report as selectable view in an AdapterView.
4100 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4101 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4102
4103 if (isFocusable()) {
4104 if (isFocused()) {
4105 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4106 } else {
4107 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4108 }
4109 }
4110 }
4111
4112 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004113 * Sets a delegate for implementing accessibility support via compositon as
4114 * opposed to inheritance. The delegate's primary use is for implementing
4115 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4116 *
4117 * @param delegate The delegate instance.
4118 *
4119 * @see AccessibilityDelegate
4120 */
4121 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4122 mAccessibilityDelegate = delegate;
4123 }
4124
4125 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004126 * Gets the unique identifier of this view on the screen for accessibility purposes.
4127 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4128 *
4129 * @return The view accessibility id.
4130 *
4131 * @hide
4132 */
4133 public int getAccessibilityViewId() {
4134 if (mAccessibilityViewId == NO_ID) {
4135 mAccessibilityViewId = sNextAccessibilityViewId++;
4136 }
4137 return mAccessibilityViewId;
4138 }
4139
4140 /**
4141 * Gets the unique identifier of the window in which this View reseides.
4142 *
4143 * @return The window accessibility id.
4144 *
4145 * @hide
4146 */
4147 public int getAccessibilityWindowId() {
4148 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4149 }
4150
4151 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004152 * Gets the {@link View} description. It briefly describes the view and is
4153 * primarily used for accessibility support. Set this property to enable
4154 * better accessibility support for your application. This is especially
4155 * true for views that do not have textual representation (For example,
4156 * ImageButton).
4157 *
4158 * @return The content descriptiopn.
4159 *
4160 * @attr ref android.R.styleable#View_contentDescription
4161 */
4162 public CharSequence getContentDescription() {
4163 return mContentDescription;
4164 }
4165
4166 /**
4167 * Sets the {@link View} description. It briefly describes the view and is
4168 * primarily used for accessibility support. Set this property to enable
4169 * better accessibility support for your application. This is especially
4170 * true for views that do not have textual representation (For example,
4171 * ImageButton).
4172 *
4173 * @param contentDescription The content description.
4174 *
4175 * @attr ref android.R.styleable#View_contentDescription
4176 */
4177 public void setContentDescription(CharSequence contentDescription) {
4178 mContentDescription = contentDescription;
4179 }
4180
4181 /**
Romain Guya2431d02009-04-30 16:30:00 -07004182 * Invoked whenever this view loses focus, either by losing window focus or by losing
4183 * focus within its window. This method can be used to clear any state tied to the
4184 * focus. For instance, if a button is held pressed with the trackball and the window
4185 * loses focus, this method can be used to cancel the press.
4186 *
4187 * Subclasses of View overriding this method should always call super.onFocusLost().
4188 *
4189 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004190 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004191 *
4192 * @hide pending API council approval
4193 */
4194 protected void onFocusLost() {
4195 resetPressedState();
4196 }
4197
4198 private void resetPressedState() {
4199 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4200 return;
4201 }
4202
4203 if (isPressed()) {
4204 setPressed(false);
4205
4206 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004207 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004208 }
4209 }
4210 }
4211
4212 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004213 * Returns true if this view has focus
4214 *
4215 * @return True if this view has focus, false otherwise.
4216 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004217 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004218 public boolean isFocused() {
4219 return (mPrivateFlags & FOCUSED) != 0;
4220 }
4221
4222 /**
4223 * Find the view in the hierarchy rooted at this view that currently has
4224 * focus.
4225 *
4226 * @return The view that currently has focus, or null if no focused view can
4227 * be found.
4228 */
4229 public View findFocus() {
4230 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4231 }
4232
4233 /**
4234 * Change whether this view is one of the set of scrollable containers in
4235 * its window. This will be used to determine whether the window can
4236 * resize or must pan when a soft input area is open -- scrollable
4237 * containers allow the window to use resize mode since the container
4238 * will appropriately shrink.
4239 */
4240 public void setScrollContainer(boolean isScrollContainer) {
4241 if (isScrollContainer) {
4242 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4243 mAttachInfo.mScrollContainers.add(this);
4244 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4245 }
4246 mPrivateFlags |= SCROLL_CONTAINER;
4247 } else {
4248 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4249 mAttachInfo.mScrollContainers.remove(this);
4250 }
4251 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4252 }
4253 }
4254
4255 /**
4256 * Returns the quality of the drawing cache.
4257 *
4258 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4259 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4260 *
4261 * @see #setDrawingCacheQuality(int)
4262 * @see #setDrawingCacheEnabled(boolean)
4263 * @see #isDrawingCacheEnabled()
4264 *
4265 * @attr ref android.R.styleable#View_drawingCacheQuality
4266 */
4267 public int getDrawingCacheQuality() {
4268 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4269 }
4270
4271 /**
4272 * Set the drawing cache quality of this view. This value is used only when the
4273 * drawing cache is enabled
4274 *
4275 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4276 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4277 *
4278 * @see #getDrawingCacheQuality()
4279 * @see #setDrawingCacheEnabled(boolean)
4280 * @see #isDrawingCacheEnabled()
4281 *
4282 * @attr ref android.R.styleable#View_drawingCacheQuality
4283 */
4284 public void setDrawingCacheQuality(int quality) {
4285 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4286 }
4287
4288 /**
4289 * Returns whether the screen should remain on, corresponding to the current
4290 * value of {@link #KEEP_SCREEN_ON}.
4291 *
4292 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4293 *
4294 * @see #setKeepScreenOn(boolean)
4295 *
4296 * @attr ref android.R.styleable#View_keepScreenOn
4297 */
4298 public boolean getKeepScreenOn() {
4299 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4300 }
4301
4302 /**
4303 * Controls whether the screen should remain on, modifying the
4304 * value of {@link #KEEP_SCREEN_ON}.
4305 *
4306 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4307 *
4308 * @see #getKeepScreenOn()
4309 *
4310 * @attr ref android.R.styleable#View_keepScreenOn
4311 */
4312 public void setKeepScreenOn(boolean keepScreenOn) {
4313 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4314 }
4315
4316 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004317 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4318 * @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 -08004319 *
4320 * @attr ref android.R.styleable#View_nextFocusLeft
4321 */
4322 public int getNextFocusLeftId() {
4323 return mNextFocusLeftId;
4324 }
4325
4326 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004327 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4328 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4329 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004330 *
4331 * @attr ref android.R.styleable#View_nextFocusLeft
4332 */
4333 public void setNextFocusLeftId(int nextFocusLeftId) {
4334 mNextFocusLeftId = nextFocusLeftId;
4335 }
4336
4337 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004338 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4339 * @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 -08004340 *
4341 * @attr ref android.R.styleable#View_nextFocusRight
4342 */
4343 public int getNextFocusRightId() {
4344 return mNextFocusRightId;
4345 }
4346
4347 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004348 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4349 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4350 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004351 *
4352 * @attr ref android.R.styleable#View_nextFocusRight
4353 */
4354 public void setNextFocusRightId(int nextFocusRightId) {
4355 mNextFocusRightId = nextFocusRightId;
4356 }
4357
4358 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004359 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4360 * @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 -08004361 *
4362 * @attr ref android.R.styleable#View_nextFocusUp
4363 */
4364 public int getNextFocusUpId() {
4365 return mNextFocusUpId;
4366 }
4367
4368 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004369 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4370 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4371 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004372 *
4373 * @attr ref android.R.styleable#View_nextFocusUp
4374 */
4375 public void setNextFocusUpId(int nextFocusUpId) {
4376 mNextFocusUpId = nextFocusUpId;
4377 }
4378
4379 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004380 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4381 * @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 -08004382 *
4383 * @attr ref android.R.styleable#View_nextFocusDown
4384 */
4385 public int getNextFocusDownId() {
4386 return mNextFocusDownId;
4387 }
4388
4389 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004390 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4391 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4392 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004393 *
4394 * @attr ref android.R.styleable#View_nextFocusDown
4395 */
4396 public void setNextFocusDownId(int nextFocusDownId) {
4397 mNextFocusDownId = nextFocusDownId;
4398 }
4399
4400 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004401 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4402 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4403 *
4404 * @attr ref android.R.styleable#View_nextFocusForward
4405 */
4406 public int getNextFocusForwardId() {
4407 return mNextFocusForwardId;
4408 }
4409
4410 /**
4411 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4412 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4413 * decide automatically.
4414 *
4415 * @attr ref android.R.styleable#View_nextFocusForward
4416 */
4417 public void setNextFocusForwardId(int nextFocusForwardId) {
4418 mNextFocusForwardId = nextFocusForwardId;
4419 }
4420
4421 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004422 * Returns the visibility of this view and all of its ancestors
4423 *
4424 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4425 */
4426 public boolean isShown() {
4427 View current = this;
4428 //noinspection ConstantConditions
4429 do {
4430 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4431 return false;
4432 }
4433 ViewParent parent = current.mParent;
4434 if (parent == null) {
4435 return false; // We are not attached to the view root
4436 }
4437 if (!(parent instanceof View)) {
4438 return true;
4439 }
4440 current = (View) parent;
4441 } while (current != null);
4442
4443 return false;
4444 }
4445
4446 /**
4447 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4448 * is set
4449 *
4450 * @param insets Insets for system windows
4451 *
4452 * @return True if this view applied the insets, false otherwise
4453 */
4454 protected boolean fitSystemWindows(Rect insets) {
4455 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4456 mPaddingLeft = insets.left;
4457 mPaddingTop = insets.top;
4458 mPaddingRight = insets.right;
4459 mPaddingBottom = insets.bottom;
4460 requestLayout();
4461 return true;
4462 }
4463 return false;
4464 }
4465
4466 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004467 * Set whether or not this view should account for system screen decorations
4468 * such as the status bar and inset its content. This allows this view to be
4469 * positioned in absolute screen coordinates and remain visible to the user.
4470 *
4471 * <p>This should only be used by top-level window decor views.
4472 *
4473 * @param fitSystemWindows true to inset content for system screen decorations, false for
4474 * default behavior.
4475 *
4476 * @attr ref android.R.styleable#View_fitsSystemWindows
4477 */
4478 public void setFitsSystemWindows(boolean fitSystemWindows) {
4479 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4480 }
4481
4482 /**
4483 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4484 * will account for system screen decorations such as the status bar and inset its
4485 * content. This allows the view to be positioned in absolute screen coordinates
4486 * and remain visible to the user.
4487 *
4488 * @return true if this view will adjust its content bounds for system screen decorations.
4489 *
4490 * @attr ref android.R.styleable#View_fitsSystemWindows
4491 */
4492 public boolean fitsSystemWindows() {
4493 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4494 }
4495
4496 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004497 * Returns the visibility status for this view.
4498 *
4499 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4500 * @attr ref android.R.styleable#View_visibility
4501 */
4502 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004503 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4504 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4505 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004506 })
4507 public int getVisibility() {
4508 return mViewFlags & VISIBILITY_MASK;
4509 }
4510
4511 /**
4512 * Set the enabled state of this view.
4513 *
4514 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4515 * @attr ref android.R.styleable#View_visibility
4516 */
4517 @RemotableViewMethod
4518 public void setVisibility(int visibility) {
4519 setFlags(visibility, VISIBILITY_MASK);
4520 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4521 }
4522
4523 /**
4524 * Returns the enabled status for this view. The interpretation of the
4525 * enabled state varies by subclass.
4526 *
4527 * @return True if this view is enabled, false otherwise.
4528 */
4529 @ViewDebug.ExportedProperty
4530 public boolean isEnabled() {
4531 return (mViewFlags & ENABLED_MASK) == ENABLED;
4532 }
4533
4534 /**
4535 * Set the enabled state of this view. The interpretation of the enabled
4536 * state varies by subclass.
4537 *
4538 * @param enabled True if this view is enabled, false otherwise.
4539 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004540 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004542 if (enabled == isEnabled()) return;
4543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004544 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4545
4546 /*
4547 * The View most likely has to change its appearance, so refresh
4548 * the drawable state.
4549 */
4550 refreshDrawableState();
4551
4552 // Invalidate too, since the default behavior for views is to be
4553 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004554 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004555 }
4556
4557 /**
4558 * Set whether this view can receive the focus.
4559 *
4560 * Setting this to false will also ensure that this view is not focusable
4561 * in touch mode.
4562 *
4563 * @param focusable If true, this view can receive the focus.
4564 *
4565 * @see #setFocusableInTouchMode(boolean)
4566 * @attr ref android.R.styleable#View_focusable
4567 */
4568 public void setFocusable(boolean focusable) {
4569 if (!focusable) {
4570 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4571 }
4572 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4573 }
4574
4575 /**
4576 * Set whether this view can receive focus while in touch mode.
4577 *
4578 * Setting this to true will also ensure that this view is focusable.
4579 *
4580 * @param focusableInTouchMode If true, this view can receive the focus while
4581 * in touch mode.
4582 *
4583 * @see #setFocusable(boolean)
4584 * @attr ref android.R.styleable#View_focusableInTouchMode
4585 */
4586 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4587 // Focusable in touch mode should always be set before the focusable flag
4588 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4589 // which, in touch mode, will not successfully request focus on this view
4590 // because the focusable in touch mode flag is not set
4591 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4592 if (focusableInTouchMode) {
4593 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4594 }
4595 }
4596
4597 /**
4598 * Set whether this view should have sound effects enabled for events such as
4599 * clicking and touching.
4600 *
4601 * <p>You may wish to disable sound effects for a view if you already play sounds,
4602 * for instance, a dial key that plays dtmf tones.
4603 *
4604 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4605 * @see #isSoundEffectsEnabled()
4606 * @see #playSoundEffect(int)
4607 * @attr ref android.R.styleable#View_soundEffectsEnabled
4608 */
4609 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4610 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4611 }
4612
4613 /**
4614 * @return whether this view should have sound effects enabled for events such as
4615 * clicking and touching.
4616 *
4617 * @see #setSoundEffectsEnabled(boolean)
4618 * @see #playSoundEffect(int)
4619 * @attr ref android.R.styleable#View_soundEffectsEnabled
4620 */
4621 @ViewDebug.ExportedProperty
4622 public boolean isSoundEffectsEnabled() {
4623 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4624 }
4625
4626 /**
4627 * Set whether this view should have haptic feedback for events such as
4628 * long presses.
4629 *
4630 * <p>You may wish to disable haptic feedback if your view already controls
4631 * its own haptic feedback.
4632 *
4633 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4634 * @see #isHapticFeedbackEnabled()
4635 * @see #performHapticFeedback(int)
4636 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4637 */
4638 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4639 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4640 }
4641
4642 /**
4643 * @return whether this view should have haptic feedback enabled for events
4644 * long presses.
4645 *
4646 * @see #setHapticFeedbackEnabled(boolean)
4647 * @see #performHapticFeedback(int)
4648 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4649 */
4650 @ViewDebug.ExportedProperty
4651 public boolean isHapticFeedbackEnabled() {
4652 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4653 }
4654
4655 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004656 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004657 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004658 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4659 * {@link #LAYOUT_DIRECTION_RTL},
4660 * {@link #LAYOUT_DIRECTION_INHERIT} or
4661 * {@link #LAYOUT_DIRECTION_LOCALE}.
4662 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004663 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004664 * @hide
4665 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004666 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004667 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4668 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4669 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4670 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004671 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004672 public int getLayoutDirection() {
4673 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004674 }
4675
4676 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004677 * Set the layout direction for this view. This will propagate a reset of layout direction
4678 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004679 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004680 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4681 * {@link #LAYOUT_DIRECTION_RTL},
4682 * {@link #LAYOUT_DIRECTION_INHERIT} or
4683 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004684 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004685 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004686 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004687 * @hide
4688 */
4689 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004690 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004691 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004692 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004693 // Setting the flag will also request a layout.
4694 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4695 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004696 }
4697
4698 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004699 * Returns the resolved layout direction for this view.
4700 *
4701 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4702 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4703 *
4704 * @hide
4705 */
4706 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4707 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4708 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4709 })
4710 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004711 resolveLayoutDirectionIfNeeded();
4712 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004713 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4714 }
4715
4716 /**
4717 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4718 * layout attribute and/or the inherited value from the parent.</p>
4719 *
4720 * @return true if the layout is right-to-left.
4721 *
4722 * @hide
4723 */
4724 @ViewDebug.ExportedProperty(category = "layout")
4725 public boolean isLayoutRtl() {
4726 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4727 }
4728
4729 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004730 * If this view doesn't do any drawing on its own, set this flag to
4731 * allow further optimizations. By default, this flag is not set on
4732 * View, but could be set on some View subclasses such as ViewGroup.
4733 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004734 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4735 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004736 *
4737 * @param willNotDraw whether or not this View draw on its own
4738 */
4739 public void setWillNotDraw(boolean willNotDraw) {
4740 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4741 }
4742
4743 /**
4744 * Returns whether or not this View draws on its own.
4745 *
4746 * @return true if this view has nothing to draw, false otherwise
4747 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004748 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004749 public boolean willNotDraw() {
4750 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4751 }
4752
4753 /**
4754 * When a View's drawing cache is enabled, drawing is redirected to an
4755 * offscreen bitmap. Some views, like an ImageView, must be able to
4756 * bypass this mechanism if they already draw a single bitmap, to avoid
4757 * unnecessary usage of the memory.
4758 *
4759 * @param willNotCacheDrawing true if this view does not cache its
4760 * drawing, false otherwise
4761 */
4762 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4763 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4764 }
4765
4766 /**
4767 * Returns whether or not this View can cache its drawing or not.
4768 *
4769 * @return true if this view does not cache its drawing, false otherwise
4770 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004771 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004772 public boolean willNotCacheDrawing() {
4773 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4774 }
4775
4776 /**
4777 * Indicates whether this view reacts to click events or not.
4778 *
4779 * @return true if the view is clickable, false otherwise
4780 *
4781 * @see #setClickable(boolean)
4782 * @attr ref android.R.styleable#View_clickable
4783 */
4784 @ViewDebug.ExportedProperty
4785 public boolean isClickable() {
4786 return (mViewFlags & CLICKABLE) == CLICKABLE;
4787 }
4788
4789 /**
4790 * Enables or disables click events for this view. When a view
4791 * is clickable it will change its state to "pressed" on every click.
4792 * Subclasses should set the view clickable to visually react to
4793 * user's clicks.
4794 *
4795 * @param clickable true to make the view clickable, false otherwise
4796 *
4797 * @see #isClickable()
4798 * @attr ref android.R.styleable#View_clickable
4799 */
4800 public void setClickable(boolean clickable) {
4801 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4802 }
4803
4804 /**
4805 * Indicates whether this view reacts to long click events or not.
4806 *
4807 * @return true if the view is long clickable, false otherwise
4808 *
4809 * @see #setLongClickable(boolean)
4810 * @attr ref android.R.styleable#View_longClickable
4811 */
4812 public boolean isLongClickable() {
4813 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4814 }
4815
4816 /**
4817 * Enables or disables long click events for this view. When a view is long
4818 * clickable it reacts to the user holding down the button for a longer
4819 * duration than a tap. This event can either launch the listener or a
4820 * context menu.
4821 *
4822 * @param longClickable true to make the view long clickable, false otherwise
4823 * @see #isLongClickable()
4824 * @attr ref android.R.styleable#View_longClickable
4825 */
4826 public void setLongClickable(boolean longClickable) {
4827 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4828 }
4829
4830 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004831 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004832 *
4833 * @see #isClickable()
4834 * @see #setClickable(boolean)
4835 *
4836 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4837 * the View's internal state from a previously set "pressed" state.
4838 */
4839 public void setPressed(boolean pressed) {
4840 if (pressed) {
4841 mPrivateFlags |= PRESSED;
4842 } else {
4843 mPrivateFlags &= ~PRESSED;
4844 }
4845 refreshDrawableState();
4846 dispatchSetPressed(pressed);
4847 }
4848
4849 /**
4850 * Dispatch setPressed to all of this View's children.
4851 *
4852 * @see #setPressed(boolean)
4853 *
4854 * @param pressed The new pressed state
4855 */
4856 protected void dispatchSetPressed(boolean pressed) {
4857 }
4858
4859 /**
4860 * Indicates whether the view is currently in pressed state. Unless
4861 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4862 * the pressed state.
4863 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004864 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004865 * @see #isClickable()
4866 * @see #setClickable(boolean)
4867 *
4868 * @return true if the view is currently pressed, false otherwise
4869 */
4870 public boolean isPressed() {
4871 return (mPrivateFlags & PRESSED) == PRESSED;
4872 }
4873
4874 /**
4875 * Indicates whether this view will save its state (that is,
4876 * whether its {@link #onSaveInstanceState} method will be called).
4877 *
4878 * @return Returns true if the view state saving is enabled, else false.
4879 *
4880 * @see #setSaveEnabled(boolean)
4881 * @attr ref android.R.styleable#View_saveEnabled
4882 */
4883 public boolean isSaveEnabled() {
4884 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4885 }
4886
4887 /**
4888 * Controls whether the saving of this view's state is
4889 * enabled (that is, whether its {@link #onSaveInstanceState} method
4890 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004891 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004892 * for its state to be saved. This flag can only disable the
4893 * saving of this view; any child views may still have their state saved.
4894 *
4895 * @param enabled Set to false to <em>disable</em> state saving, or true
4896 * (the default) to allow it.
4897 *
4898 * @see #isSaveEnabled()
4899 * @see #setId(int)
4900 * @see #onSaveInstanceState()
4901 * @attr ref android.R.styleable#View_saveEnabled
4902 */
4903 public void setSaveEnabled(boolean enabled) {
4904 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4905 }
4906
Jeff Brown85a31762010-09-01 17:01:00 -07004907 /**
4908 * Gets whether the framework should discard touches when the view's
4909 * window is obscured by another visible window.
4910 * Refer to the {@link View} security documentation for more details.
4911 *
4912 * @return True if touch filtering is enabled.
4913 *
4914 * @see #setFilterTouchesWhenObscured(boolean)
4915 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4916 */
4917 @ViewDebug.ExportedProperty
4918 public boolean getFilterTouchesWhenObscured() {
4919 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4920 }
4921
4922 /**
4923 * Sets whether the framework should discard touches when the view's
4924 * window is obscured by another visible window.
4925 * Refer to the {@link View} security documentation for more details.
4926 *
4927 * @param enabled True if touch filtering should be enabled.
4928 *
4929 * @see #getFilterTouchesWhenObscured
4930 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4931 */
4932 public void setFilterTouchesWhenObscured(boolean enabled) {
4933 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4934 FILTER_TOUCHES_WHEN_OBSCURED);
4935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004936
4937 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004938 * Indicates whether the entire hierarchy under this view will save its
4939 * state when a state saving traversal occurs from its parent. The default
4940 * is true; if false, these views will not be saved unless
4941 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4942 *
4943 * @return Returns true if the view state saving from parent is enabled, else false.
4944 *
4945 * @see #setSaveFromParentEnabled(boolean)
4946 */
4947 public boolean isSaveFromParentEnabled() {
4948 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4949 }
4950
4951 /**
4952 * Controls whether the entire hierarchy under this view will save its
4953 * state when a state saving traversal occurs from its parent. The default
4954 * is true; if false, these views will not be saved unless
4955 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4956 *
4957 * @param enabled Set to false to <em>disable</em> state saving, or true
4958 * (the default) to allow it.
4959 *
4960 * @see #isSaveFromParentEnabled()
4961 * @see #setId(int)
4962 * @see #onSaveInstanceState()
4963 */
4964 public void setSaveFromParentEnabled(boolean enabled) {
4965 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4966 }
4967
4968
4969 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004970 * Returns whether this View is able to take focus.
4971 *
4972 * @return True if this view can take focus, or false otherwise.
4973 * @attr ref android.R.styleable#View_focusable
4974 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004975 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 public final boolean isFocusable() {
4977 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4978 }
4979
4980 /**
4981 * When a view is focusable, it may not want to take focus when in touch mode.
4982 * For example, a button would like focus when the user is navigating via a D-pad
4983 * so that the user can click on it, but once the user starts touching the screen,
4984 * the button shouldn't take focus
4985 * @return Whether the view is focusable in touch mode.
4986 * @attr ref android.R.styleable#View_focusableInTouchMode
4987 */
4988 @ViewDebug.ExportedProperty
4989 public final boolean isFocusableInTouchMode() {
4990 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4991 }
4992
4993 /**
4994 * Find the nearest view in the specified direction that can take focus.
4995 * This does not actually give focus to that view.
4996 *
4997 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4998 *
4999 * @return The nearest focusable in the specified direction, or null if none
5000 * can be found.
5001 */
5002 public View focusSearch(int direction) {
5003 if (mParent != null) {
5004 return mParent.focusSearch(this, direction);
5005 } else {
5006 return null;
5007 }
5008 }
5009
5010 /**
5011 * This method is the last chance for the focused view and its ancestors to
5012 * respond to an arrow key. This is called when the focused view did not
5013 * consume the key internally, nor could the view system find a new view in
5014 * the requested direction to give focus to.
5015 *
5016 * @param focused The currently focused view.
5017 * @param direction The direction focus wants to move. One of FOCUS_UP,
5018 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5019 * @return True if the this view consumed this unhandled move.
5020 */
5021 public boolean dispatchUnhandledMove(View focused, int direction) {
5022 return false;
5023 }
5024
5025 /**
5026 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005027 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005028 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005029 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5030 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005031 * @return The user specified next view, or null if there is none.
5032 */
5033 View findUserSetNextFocus(View root, int direction) {
5034 switch (direction) {
5035 case FOCUS_LEFT:
5036 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005037 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005038 case FOCUS_RIGHT:
5039 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005040 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005041 case FOCUS_UP:
5042 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005043 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005044 case FOCUS_DOWN:
5045 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005046 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005047 case FOCUS_FORWARD:
5048 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005049 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005050 case FOCUS_BACKWARD: {
5051 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005052 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005053 @Override
5054 public boolean apply(View t) {
5055 return t.mNextFocusForwardId == id;
5056 }
5057 });
5058 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005059 }
5060 return null;
5061 }
5062
Jeff Brown4dfbec22011-08-15 14:55:37 -07005063 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5064 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5065 @Override
5066 public boolean apply(View t) {
5067 return t.mID == childViewId;
5068 }
5069 });
5070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005071 if (result == null) {
5072 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5073 + "by user for id " + childViewId);
5074 }
5075 return result;
5076 }
5077
5078 /**
5079 * Find and return all focusable views that are descendants of this view,
5080 * possibly including this view if it is focusable itself.
5081 *
5082 * @param direction The direction of the focus
5083 * @return A list of focusable views
5084 */
5085 public ArrayList<View> getFocusables(int direction) {
5086 ArrayList<View> result = new ArrayList<View>(24);
5087 addFocusables(result, direction);
5088 return result;
5089 }
5090
5091 /**
5092 * Add any focusable views that are descendants of this view (possibly
5093 * including this view if it is focusable itself) to views. If we are in touch mode,
5094 * only add views that are also focusable in touch mode.
5095 *
5096 * @param views Focusable views found so far
5097 * @param direction The direction of the focus
5098 */
5099 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005100 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5101 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005102
svetoslavganov75986cf2009-05-14 22:28:01 -07005103 /**
5104 * Adds any focusable views that are descendants of this view (possibly
5105 * including this view if it is focusable itself) to views. This method
5106 * adds all focusable views regardless if we are in touch mode or
5107 * only views focusable in touch mode if we are in touch mode depending on
5108 * the focusable mode paramater.
5109 *
5110 * @param views Focusable views found so far or null if all we are interested is
5111 * the number of focusables.
5112 * @param direction The direction of the focus.
5113 * @param focusableMode The type of focusables to be added.
5114 *
5115 * @see #FOCUSABLES_ALL
5116 * @see #FOCUSABLES_TOUCH_MODE
5117 */
5118 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5119 if (!isFocusable()) {
5120 return;
5121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005122
svetoslavganov75986cf2009-05-14 22:28:01 -07005123 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5124 isInTouchMode() && !isFocusableInTouchMode()) {
5125 return;
5126 }
5127
5128 if (views != null) {
5129 views.add(this);
5130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005131 }
5132
5133 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005134 * Finds the Views that contain given text. The containment is case insensitive.
5135 * As View's text is considered any text content that View renders.
5136 *
5137 * @param outViews The output list of matching Views.
5138 * @param text The text to match against.
5139 */
5140 public void findViewsWithText(ArrayList<View> outViews, CharSequence text) {
5141 }
5142
5143 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005144 * Find and return all touchable views that are descendants of this view,
5145 * possibly including this view if it is touchable itself.
5146 *
5147 * @return A list of touchable views
5148 */
5149 public ArrayList<View> getTouchables() {
5150 ArrayList<View> result = new ArrayList<View>();
5151 addTouchables(result);
5152 return result;
5153 }
5154
5155 /**
5156 * Add any touchable views that are descendants of this view (possibly
5157 * including this view if it is touchable itself) to views.
5158 *
5159 * @param views Touchable views found so far
5160 */
5161 public void addTouchables(ArrayList<View> views) {
5162 final int viewFlags = mViewFlags;
5163
5164 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5165 && (viewFlags & ENABLED_MASK) == ENABLED) {
5166 views.add(this);
5167 }
5168 }
5169
5170 /**
5171 * Call this to try to give focus to a specific view or to one of its
5172 * descendants.
5173 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005174 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5175 * false), or if it is focusable and it is not focusable in touch mode
5176 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005177 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005178 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005179 * have focus, and you want your parent to look for the next one.
5180 *
5181 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5182 * {@link #FOCUS_DOWN} and <code>null</code>.
5183 *
5184 * @return Whether this view or one of its descendants actually took focus.
5185 */
5186 public final boolean requestFocus() {
5187 return requestFocus(View.FOCUS_DOWN);
5188 }
5189
5190
5191 /**
5192 * Call this to try to give focus to a specific view or to one of its
5193 * descendants and give it a hint about what direction focus is heading.
5194 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005195 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5196 * false), or if it is focusable and it is not focusable in touch mode
5197 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005198 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005199 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005200 * have focus, and you want your parent to look for the next one.
5201 *
5202 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5203 * <code>null</code> set for the previously focused rectangle.
5204 *
5205 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5206 * @return Whether this view or one of its descendants actually took focus.
5207 */
5208 public final boolean requestFocus(int direction) {
5209 return requestFocus(direction, null);
5210 }
5211
5212 /**
5213 * Call this to try to give focus to a specific view or to one of its descendants
5214 * and give it hints about the direction and a specific rectangle that the focus
5215 * is coming from. The rectangle can help give larger views a finer grained hint
5216 * about where focus is coming from, and therefore, where to show selection, or
5217 * forward focus change internally.
5218 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005219 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5220 * false), or if it is focusable and it is not focusable in touch mode
5221 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005222 *
5223 * A View will not take focus if it is not visible.
5224 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005225 * A View will not take focus if one of its parents has
5226 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5227 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005228 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005229 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005230 * have focus, and you want your parent to look for the next one.
5231 *
5232 * You may wish to override this method if your custom {@link View} has an internal
5233 * {@link View} that it wishes to forward the request to.
5234 *
5235 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5236 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5237 * to give a finer grained hint about where focus is coming from. May be null
5238 * if there is no hint.
5239 * @return Whether this view or one of its descendants actually took focus.
5240 */
5241 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5242 // need to be focusable
5243 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5244 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5245 return false;
5246 }
5247
5248 // need to be focusable in touch mode if in touch mode
5249 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005250 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5251 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005252 }
5253
5254 // need to not have any parents blocking us
5255 if (hasAncestorThatBlocksDescendantFocus()) {
5256 return false;
5257 }
5258
5259 handleFocusGainInternal(direction, previouslyFocusedRect);
5260 return true;
5261 }
5262
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005263 /** Gets the ViewAncestor, or null if not attached. */
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005264 /*package*/ ViewRootImpl getViewRootImpl() {
Christopher Tate2c095f32010-10-04 14:13:40 -07005265 View root = getRootView();
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005266 return root != null ? (ViewRootImpl)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07005267 }
5268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005269 /**
5270 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5271 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5272 * touch mode to request focus when they are touched.
5273 *
5274 * @return Whether this view or one of its descendants actually took focus.
5275 *
5276 * @see #isInTouchMode()
5277 *
5278 */
5279 public final boolean requestFocusFromTouch() {
5280 // Leave touch mode if we need to
5281 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005282 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005283 if (viewRoot != null) {
5284 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005285 }
5286 }
5287 return requestFocus(View.FOCUS_DOWN);
5288 }
5289
5290 /**
5291 * @return Whether any ancestor of this view blocks descendant focus.
5292 */
5293 private boolean hasAncestorThatBlocksDescendantFocus() {
5294 ViewParent ancestor = mParent;
5295 while (ancestor instanceof ViewGroup) {
5296 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5297 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5298 return true;
5299 } else {
5300 ancestor = vgAncestor.getParent();
5301 }
5302 }
5303 return false;
5304 }
5305
5306 /**
Romain Guya440b002010-02-24 15:57:54 -08005307 * @hide
5308 */
5309 public void dispatchStartTemporaryDetach() {
5310 onStartTemporaryDetach();
5311 }
5312
5313 /**
5314 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005315 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5316 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005317 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005318 */
5319 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005320 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005321 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005322 }
5323
5324 /**
5325 * @hide
5326 */
5327 public void dispatchFinishTemporaryDetach() {
5328 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005329 }
Romain Guy8506ab42009-06-11 17:35:47 -07005330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005331 /**
5332 * Called after {@link #onStartTemporaryDetach} when the container is done
5333 * changing the view.
5334 */
5335 public void onFinishTemporaryDetach() {
5336 }
Romain Guy8506ab42009-06-11 17:35:47 -07005337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005338 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005339 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5340 * for this view's window. Returns null if the view is not currently attached
5341 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005342 * just use the standard high-level event callbacks like
5343 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005344 */
5345 public KeyEvent.DispatcherState getKeyDispatcherState() {
5346 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5347 }
Joe Malin32736f02011-01-19 16:14:20 -08005348
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005349 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005350 * Dispatch a key event before it is processed by any input method
5351 * associated with the view hierarchy. This can be used to intercept
5352 * key events in special situations before the IME consumes them; a
5353 * typical example would be handling the BACK key to update the application's
5354 * UI instead of allowing the IME to see it and close itself.
5355 *
5356 * @param event The key event to be dispatched.
5357 * @return True if the event was handled, false otherwise.
5358 */
5359 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5360 return onKeyPreIme(event.getKeyCode(), event);
5361 }
5362
5363 /**
5364 * Dispatch a key event to the next view on the focus path. This path runs
5365 * from the top of the view tree down to the currently focused view. If this
5366 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5367 * the next node down the focus path. This method also fires any key
5368 * listeners.
5369 *
5370 * @param event The key event to be dispatched.
5371 * @return True if the event was handled, false otherwise.
5372 */
5373 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005374 if (mInputEventConsistencyVerifier != null) {
5375 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005377
Jeff Brown21bc5c92011-02-28 18:27:14 -08005378 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005379 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005380 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5381 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
5382 return true;
5383 }
5384
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005385 if (event.dispatch(this, mAttachInfo != null
5386 ? mAttachInfo.mKeyDispatchState : null, this)) {
5387 return true;
5388 }
5389
5390 if (mInputEventConsistencyVerifier != null) {
5391 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5392 }
5393 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005394 }
5395
5396 /**
5397 * Dispatches a key shortcut event.
5398 *
5399 * @param event The key event to be dispatched.
5400 * @return True if the event was handled by the view, false otherwise.
5401 */
5402 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5403 return onKeyShortcut(event.getKeyCode(), event);
5404 }
5405
5406 /**
5407 * Pass the touch screen motion event down to the target view, or this
5408 * view if it is the target.
5409 *
5410 * @param event The motion event to be dispatched.
5411 * @return True if the event was handled by the view, false otherwise.
5412 */
5413 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005414 if (mInputEventConsistencyVerifier != null) {
5415 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5416 }
5417
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005418 if (onFilterTouchEventForSecurity(event)) {
5419 //noinspection SimplifiableIfStatement
5420 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
5421 mOnTouchListener.onTouch(this, event)) {
5422 return true;
5423 }
5424
5425 if (onTouchEvent(event)) {
5426 return true;
5427 }
Jeff Brown85a31762010-09-01 17:01:00 -07005428 }
5429
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005430 if (mInputEventConsistencyVerifier != null) {
5431 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005432 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005433 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005434 }
5435
5436 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005437 * Filter the touch event to apply security policies.
5438 *
5439 * @param event The motion event to be filtered.
5440 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005441 *
Jeff Brown85a31762010-09-01 17:01:00 -07005442 * @see #getFilterTouchesWhenObscured
5443 */
5444 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005445 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005446 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5447 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5448 // Window is obscured, drop this touch.
5449 return false;
5450 }
5451 return true;
5452 }
5453
5454 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005455 * Pass a trackball motion event down to the focused view.
5456 *
5457 * @param event The motion event to be dispatched.
5458 * @return True if the event was handled by the view, false otherwise.
5459 */
5460 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005461 if (mInputEventConsistencyVerifier != null) {
5462 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5463 }
5464
Romain Guy02ccac62011-06-24 13:20:23 -07005465 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005466 }
5467
5468 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005469 * Dispatch a generic motion event.
5470 * <p>
5471 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5472 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005473 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005474 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005475 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005476 *
5477 * @param event The motion event to be dispatched.
5478 * @return True if the event was handled by the view, false otherwise.
5479 */
5480 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005481 if (mInputEventConsistencyVerifier != null) {
5482 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5483 }
5484
Jeff Browna032cc02011-03-07 16:56:21 -08005485 final int source = event.getSource();
5486 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5487 final int action = event.getAction();
5488 if (action == MotionEvent.ACTION_HOVER_ENTER
5489 || action == MotionEvent.ACTION_HOVER_MOVE
5490 || action == MotionEvent.ACTION_HOVER_EXIT) {
5491 if (dispatchHoverEvent(event)) {
5492 return true;
5493 }
5494 } else if (dispatchGenericPointerEvent(event)) {
5495 return true;
5496 }
5497 } else if (dispatchGenericFocusedEvent(event)) {
5498 return true;
5499 }
5500
Jeff Brown10b62902011-06-20 16:40:37 -07005501 if (dispatchGenericMotionEventInternal(event)) {
5502 return true;
5503 }
5504
5505 if (mInputEventConsistencyVerifier != null) {
5506 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5507 }
5508 return false;
5509 }
5510
5511 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005512 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08005513 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5514 && mOnGenericMotionListener.onGenericMotion(this, event)) {
5515 return true;
5516 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005517
5518 if (onGenericMotionEvent(event)) {
5519 return true;
5520 }
5521
5522 if (mInputEventConsistencyVerifier != null) {
5523 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5524 }
5525 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005526 }
5527
5528 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005529 * Dispatch a hover event.
5530 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005531 * Do not call this method directly.
5532 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005533 * </p>
5534 *
5535 * @param event The motion event to be dispatched.
5536 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005537 */
5538 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005539 //noinspection SimplifiableIfStatement
Jeff Brown10b62902011-06-20 16:40:37 -07005540 if (mOnHoverListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5541 && mOnHoverListener.onHover(this, event)) {
5542 return true;
5543 }
5544
Jeff Browna032cc02011-03-07 16:56:21 -08005545 return onHoverEvent(event);
5546 }
5547
5548 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005549 * Returns true if the view has a child to which it has recently sent
5550 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5551 * it does not have a hovered child, then it must be the innermost hovered view.
5552 * @hide
5553 */
5554 protected boolean hasHoveredChild() {
5555 return false;
5556 }
5557
5558 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005559 * Dispatch a generic motion event to the view under the first pointer.
5560 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005561 * Do not call this method directly.
5562 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005563 * </p>
5564 *
5565 * @param event The motion event to be dispatched.
5566 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005567 */
5568 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5569 return false;
5570 }
5571
5572 /**
5573 * Dispatch a generic motion event to the currently focused view.
5574 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005575 * Do not call this method directly.
5576 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005577 * </p>
5578 *
5579 * @param event The motion event to be dispatched.
5580 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005581 */
5582 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5583 return false;
5584 }
5585
5586 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005587 * Dispatch a pointer event.
5588 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005589 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5590 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5591 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005592 * and should not be expected to handle other pointing device features.
5593 * </p>
5594 *
5595 * @param event The motion event to be dispatched.
5596 * @return True if the event was handled by the view, false otherwise.
5597 * @hide
5598 */
5599 public final boolean dispatchPointerEvent(MotionEvent event) {
5600 if (event.isTouchEvent()) {
5601 return dispatchTouchEvent(event);
5602 } else {
5603 return dispatchGenericMotionEvent(event);
5604 }
5605 }
5606
5607 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005608 * Called when the window containing this view gains or loses window focus.
5609 * ViewGroups should override to route to their children.
5610 *
5611 * @param hasFocus True if the window containing this view now has focus,
5612 * false otherwise.
5613 */
5614 public void dispatchWindowFocusChanged(boolean hasFocus) {
5615 onWindowFocusChanged(hasFocus);
5616 }
5617
5618 /**
5619 * Called when the window containing this view gains or loses focus. Note
5620 * that this is separate from view focus: to receive key events, both
5621 * your view and its window must have focus. If a window is displayed
5622 * on top of yours that takes input focus, then your own window will lose
5623 * focus but the view focus will remain unchanged.
5624 *
5625 * @param hasWindowFocus True if the window containing this view now has
5626 * focus, false otherwise.
5627 */
5628 public void onWindowFocusChanged(boolean hasWindowFocus) {
5629 InputMethodManager imm = InputMethodManager.peekInstance();
5630 if (!hasWindowFocus) {
5631 if (isPressed()) {
5632 setPressed(false);
5633 }
5634 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5635 imm.focusOut(this);
5636 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005637 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005638 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005639 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005640 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5641 imm.focusIn(this);
5642 }
5643 refreshDrawableState();
5644 }
5645
5646 /**
5647 * Returns true if this view is in a window that currently has window focus.
5648 * Note that this is not the same as the view itself having focus.
5649 *
5650 * @return True if this view is in a window that currently has window focus.
5651 */
5652 public boolean hasWindowFocus() {
5653 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5654 }
5655
5656 /**
Adam Powell326d8082009-12-09 15:10:07 -08005657 * Dispatch a view visibility change down the view hierarchy.
5658 * ViewGroups should override to route to their children.
5659 * @param changedView The view whose visibility changed. Could be 'this' or
5660 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005661 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5662 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005663 */
5664 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5665 onVisibilityChanged(changedView, visibility);
5666 }
5667
5668 /**
5669 * Called when the visibility of the view or an ancestor of the view is changed.
5670 * @param changedView The view whose visibility changed. Could be 'this' or
5671 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005672 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5673 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005674 */
5675 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005676 if (visibility == VISIBLE) {
5677 if (mAttachInfo != null) {
5678 initialAwakenScrollBars();
5679 } else {
5680 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5681 }
5682 }
Adam Powell326d8082009-12-09 15:10:07 -08005683 }
5684
5685 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005686 * Dispatch a hint about whether this view is displayed. For instance, when
5687 * a View moves out of the screen, it might receives a display hint indicating
5688 * the view is not displayed. Applications should not <em>rely</em> on this hint
5689 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005690 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005691 * @param hint A hint about whether or not this view is displayed:
5692 * {@link #VISIBLE} or {@link #INVISIBLE}.
5693 */
5694 public void dispatchDisplayHint(int hint) {
5695 onDisplayHint(hint);
5696 }
5697
5698 /**
5699 * Gives this view a hint about whether is displayed or not. For instance, when
5700 * a View moves out of the screen, it might receives a display hint indicating
5701 * the view is not displayed. Applications should not <em>rely</em> on this hint
5702 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005703 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005704 * @param hint A hint about whether or not this view is displayed:
5705 * {@link #VISIBLE} or {@link #INVISIBLE}.
5706 */
5707 protected void onDisplayHint(int hint) {
5708 }
5709
5710 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005711 * Dispatch a window visibility change down the view hierarchy.
5712 * ViewGroups should override to route to their children.
5713 *
5714 * @param visibility The new visibility of the window.
5715 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005716 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005717 */
5718 public void dispatchWindowVisibilityChanged(int visibility) {
5719 onWindowVisibilityChanged(visibility);
5720 }
5721
5722 /**
5723 * Called when the window containing has change its visibility
5724 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5725 * that this tells you whether or not your window is being made visible
5726 * to the window manager; this does <em>not</em> tell you whether or not
5727 * your window is obscured by other windows on the screen, even if it
5728 * is itself visible.
5729 *
5730 * @param visibility The new visibility of the window.
5731 */
5732 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005733 if (visibility == VISIBLE) {
5734 initialAwakenScrollBars();
5735 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005736 }
5737
5738 /**
5739 * Returns the current visibility of the window this view is attached to
5740 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5741 *
5742 * @return Returns the current visibility of the view's window.
5743 */
5744 public int getWindowVisibility() {
5745 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5746 }
5747
5748 /**
5749 * Retrieve the overall visible display size in which the window this view is
5750 * attached to has been positioned in. This takes into account screen
5751 * decorations above the window, for both cases where the window itself
5752 * is being position inside of them or the window is being placed under
5753 * then and covered insets are used for the window to position its content
5754 * inside. In effect, this tells you the available area where content can
5755 * be placed and remain visible to users.
5756 *
5757 * <p>This function requires an IPC back to the window manager to retrieve
5758 * the requested information, so should not be used in performance critical
5759 * code like drawing.
5760 *
5761 * @param outRect Filled in with the visible display frame. If the view
5762 * is not attached to a window, this is simply the raw display size.
5763 */
5764 public void getWindowVisibleDisplayFrame(Rect outRect) {
5765 if (mAttachInfo != null) {
5766 try {
5767 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5768 } catch (RemoteException e) {
5769 return;
5770 }
5771 // XXX This is really broken, and probably all needs to be done
5772 // in the window manager, and we need to know more about whether
5773 // we want the area behind or in front of the IME.
5774 final Rect insets = mAttachInfo.mVisibleInsets;
5775 outRect.left += insets.left;
5776 outRect.top += insets.top;
5777 outRect.right -= insets.right;
5778 outRect.bottom -= insets.bottom;
5779 return;
5780 }
5781 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005782 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005783 }
5784
5785 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005786 * Dispatch a notification about a resource configuration change down
5787 * the view hierarchy.
5788 * ViewGroups should override to route to their children.
5789 *
5790 * @param newConfig The new resource configuration.
5791 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005792 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005793 */
5794 public void dispatchConfigurationChanged(Configuration newConfig) {
5795 onConfigurationChanged(newConfig);
5796 }
5797
5798 /**
5799 * Called when the current configuration of the resources being used
5800 * by the application have changed. You can use this to decide when
5801 * to reload resources that can changed based on orientation and other
5802 * configuration characterstics. You only need to use this if you are
5803 * not relying on the normal {@link android.app.Activity} mechanism of
5804 * recreating the activity instance upon a configuration change.
5805 *
5806 * @param newConfig The new resource configuration.
5807 */
5808 protected void onConfigurationChanged(Configuration newConfig) {
5809 }
5810
5811 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005812 * Private function to aggregate all per-view attributes in to the view
5813 * root.
5814 */
5815 void dispatchCollectViewAttributes(int visibility) {
5816 performCollectViewAttributes(visibility);
5817 }
5818
5819 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005820 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005821 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5822 mAttachInfo.mKeepScreenOn = true;
5823 }
5824 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5825 if (mOnSystemUiVisibilityChangeListener != null) {
5826 mAttachInfo.mHasSystemUiListeners = true;
5827 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005828 }
5829 }
5830
5831 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005832 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005833 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005834 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5835 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005836 ai.mRecomputeGlobalAttributes = true;
5837 }
5838 }
5839 }
5840
5841 /**
5842 * Returns whether the device is currently in touch mode. Touch mode is entered
5843 * once the user begins interacting with the device by touch, and affects various
5844 * things like whether focus is always visible to the user.
5845 *
5846 * @return Whether the device is in touch mode.
5847 */
5848 @ViewDebug.ExportedProperty
5849 public boolean isInTouchMode() {
5850 if (mAttachInfo != null) {
5851 return mAttachInfo.mInTouchMode;
5852 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005853 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005854 }
5855 }
5856
5857 /**
5858 * Returns the context the view is running in, through which it can
5859 * access the current theme, resources, etc.
5860 *
5861 * @return The view's Context.
5862 */
5863 @ViewDebug.CapturedViewProperty
5864 public final Context getContext() {
5865 return mContext;
5866 }
5867
5868 /**
5869 * Handle a key event before it is processed by any input method
5870 * associated with the view hierarchy. This can be used to intercept
5871 * key events in special situations before the IME consumes them; a
5872 * typical example would be handling the BACK key to update the application's
5873 * UI instead of allowing the IME to see it and close itself.
5874 *
5875 * @param keyCode The value in event.getKeyCode().
5876 * @param event Description of the key event.
5877 * @return If you handled the event, return true. If you want to allow the
5878 * event to be handled by the next receiver, return false.
5879 */
5880 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5881 return false;
5882 }
5883
5884 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005885 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5886 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005887 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5888 * is released, if the view is enabled and clickable.
5889 *
5890 * @param keyCode A key code that represents the button pressed, from
5891 * {@link android.view.KeyEvent}.
5892 * @param event The KeyEvent object that defines the button action.
5893 */
5894 public boolean onKeyDown(int keyCode, KeyEvent event) {
5895 boolean result = false;
5896
5897 switch (keyCode) {
5898 case KeyEvent.KEYCODE_DPAD_CENTER:
5899 case KeyEvent.KEYCODE_ENTER: {
5900 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5901 return true;
5902 }
5903 // Long clickable items don't necessarily have to be clickable
5904 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5905 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5906 (event.getRepeatCount() == 0)) {
5907 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005908 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005909 return true;
5910 }
5911 break;
5912 }
5913 }
5914 return result;
5915 }
5916
5917 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005918 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5919 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5920 * the event).
5921 */
5922 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5923 return false;
5924 }
5925
5926 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005927 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5928 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005929 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5930 * {@link KeyEvent#KEYCODE_ENTER} is released.
5931 *
5932 * @param keyCode A key code that represents the button pressed, from
5933 * {@link android.view.KeyEvent}.
5934 * @param event The KeyEvent object that defines the button action.
5935 */
5936 public boolean onKeyUp(int keyCode, KeyEvent event) {
5937 boolean result = false;
5938
5939 switch (keyCode) {
5940 case KeyEvent.KEYCODE_DPAD_CENTER:
5941 case KeyEvent.KEYCODE_ENTER: {
5942 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5943 return true;
5944 }
5945 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5946 setPressed(false);
5947
5948 if (!mHasPerformedLongPress) {
5949 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005950 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005951
5952 result = performClick();
5953 }
5954 }
5955 break;
5956 }
5957 }
5958 return result;
5959 }
5960
5961 /**
5962 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5963 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5964 * the event).
5965 *
5966 * @param keyCode A key code that represents the button pressed, from
5967 * {@link android.view.KeyEvent}.
5968 * @param repeatCount The number of times the action was made.
5969 * @param event The KeyEvent object that defines the button action.
5970 */
5971 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5972 return false;
5973 }
5974
5975 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005976 * Called on the focused view when a key shortcut event is not handled.
5977 * Override this method to implement local key shortcuts for the View.
5978 * Key shortcuts can also be implemented by setting the
5979 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005980 *
5981 * @param keyCode The value in event.getKeyCode().
5982 * @param event Description of the key event.
5983 * @return If you handled the event, return true. If you want to allow the
5984 * event to be handled by the next receiver, return false.
5985 */
5986 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5987 return false;
5988 }
5989
5990 /**
5991 * Check whether the called view is a text editor, in which case it
5992 * would make sense to automatically display a soft input window for
5993 * it. Subclasses should override this if they implement
5994 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005995 * a call on that method would return a non-null InputConnection, and
5996 * they are really a first-class editor that the user would normally
5997 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005998 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005999 * <p>The default implementation always returns false. This does
6000 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6001 * will not be called or the user can not otherwise perform edits on your
6002 * view; it is just a hint to the system that this is not the primary
6003 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006004 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006005 * @return Returns true if this view is a text editor, else false.
6006 */
6007 public boolean onCheckIsTextEditor() {
6008 return false;
6009 }
Romain Guy8506ab42009-06-11 17:35:47 -07006010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006011 /**
6012 * Create a new InputConnection for an InputMethod to interact
6013 * with the view. The default implementation returns null, since it doesn't
6014 * support input methods. You can override this to implement such support.
6015 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006016 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006017 * <p>When implementing this, you probably also want to implement
6018 * {@link #onCheckIsTextEditor()} to indicate you will return a
6019 * non-null InputConnection.
6020 *
6021 * @param outAttrs Fill in with attribute information about the connection.
6022 */
6023 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6024 return null;
6025 }
6026
6027 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006028 * Called by the {@link android.view.inputmethod.InputMethodManager}
6029 * when a view who is not the current
6030 * input connection target is trying to make a call on the manager. The
6031 * default implementation returns false; you can override this to return
6032 * true for certain views if you are performing InputConnection proxying
6033 * to them.
6034 * @param view The View that is making the InputMethodManager call.
6035 * @return Return true to allow the call, false to reject.
6036 */
6037 public boolean checkInputConnectionProxy(View view) {
6038 return false;
6039 }
Romain Guy8506ab42009-06-11 17:35:47 -07006040
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006041 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006042 * Show the context menu for this view. It is not safe to hold on to the
6043 * menu after returning from this method.
6044 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006045 * You should normally not overload this method. Overload
6046 * {@link #onCreateContextMenu(ContextMenu)} or define an
6047 * {@link OnCreateContextMenuListener} to add items to the context menu.
6048 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006049 * @param menu The context menu to populate
6050 */
6051 public void createContextMenu(ContextMenu menu) {
6052 ContextMenuInfo menuInfo = getContextMenuInfo();
6053
6054 // Sets the current menu info so all items added to menu will have
6055 // my extra info set.
6056 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6057
6058 onCreateContextMenu(menu);
6059 if (mOnCreateContextMenuListener != null) {
6060 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
6061 }
6062
6063 // Clear the extra information so subsequent items that aren't mine don't
6064 // have my extra info.
6065 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6066
6067 if (mParent != null) {
6068 mParent.createContextMenu(menu);
6069 }
6070 }
6071
6072 /**
6073 * Views should implement this if they have extra information to associate
6074 * with the context menu. The return result is supplied as a parameter to
6075 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6076 * callback.
6077 *
6078 * @return Extra information about the item for which the context menu
6079 * should be shown. This information will vary across different
6080 * subclasses of View.
6081 */
6082 protected ContextMenuInfo getContextMenuInfo() {
6083 return null;
6084 }
6085
6086 /**
6087 * Views should implement this if the view itself is going to add items to
6088 * the context menu.
6089 *
6090 * @param menu the context menu to populate
6091 */
6092 protected void onCreateContextMenu(ContextMenu menu) {
6093 }
6094
6095 /**
6096 * Implement this method to handle trackball motion events. The
6097 * <em>relative</em> movement of the trackball since the last event
6098 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6099 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6100 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6101 * they will often be fractional values, representing the more fine-grained
6102 * movement information available from a trackball).
6103 *
6104 * @param event The motion event.
6105 * @return True if the event was handled, false otherwise.
6106 */
6107 public boolean onTrackballEvent(MotionEvent event) {
6108 return false;
6109 }
6110
6111 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006112 * Implement this method to handle generic motion events.
6113 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006114 * Generic motion events describe joystick movements, mouse hovers, track pad
6115 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006116 * {@link MotionEvent#getSource() source} of the motion event specifies
6117 * the class of input that was received. Implementations of this method
6118 * must examine the bits in the source before processing the event.
6119 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006120 * </p><p>
6121 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6122 * are delivered to the view under the pointer. All other generic motion events are
6123 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006124 * </p>
6125 * <code>
6126 * public boolean onGenericMotionEvent(MotionEvent event) {
6127 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006128 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6129 * // process the joystick movement...
6130 * return true;
6131 * }
6132 * }
6133 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6134 * switch (event.getAction()) {
6135 * case MotionEvent.ACTION_HOVER_MOVE:
6136 * // process the mouse hover movement...
6137 * return true;
6138 * case MotionEvent.ACTION_SCROLL:
6139 * // process the scroll wheel movement...
6140 * return true;
6141 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006142 * }
6143 * return super.onGenericMotionEvent(event);
6144 * }
6145 * </code>
6146 *
6147 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006148 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006149 */
6150 public boolean onGenericMotionEvent(MotionEvent event) {
6151 return false;
6152 }
6153
6154 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006155 * Implement this method to handle hover events.
6156 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006157 * This method is called whenever a pointer is hovering into, over, or out of the
6158 * bounds of a view and the view is not currently being touched.
6159 * Hover events are represented as pointer events with action
6160 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6161 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6162 * </p>
6163 * <ul>
6164 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6165 * when the pointer enters the bounds of the view.</li>
6166 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6167 * when the pointer has already entered the bounds of the view and has moved.</li>
6168 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6169 * when the pointer has exited the bounds of the view or when the pointer is
6170 * about to go down due to a button click, tap, or similar user action that
6171 * causes the view to be touched.</li>
6172 * </ul>
6173 * <p>
6174 * The view should implement this method to return true to indicate that it is
6175 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006176 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006177 * The default implementation calls {@link #setHovered} to update the hovered state
6178 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006179 * is enabled and is clickable. The default implementation also sends hover
6180 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006181 * </p>
6182 *
6183 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006184 * @return True if the view handled the hover event.
6185 *
6186 * @see #isHovered
6187 * @see #setHovered
6188 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006189 */
6190 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006191 // The root view may receive hover (or touch) events that are outside the bounds of
6192 // the window. This code ensures that we only send accessibility events for
6193 // hovers that are actually within the bounds of the root view.
6194 final int action = event.getAction();
6195 if (!mSendingHoverAccessibilityEvents) {
6196 if ((action == MotionEvent.ACTION_HOVER_ENTER
6197 || action == MotionEvent.ACTION_HOVER_MOVE)
6198 && !hasHoveredChild()
6199 && pointInView(event.getX(), event.getY())) {
6200 mSendingHoverAccessibilityEvents = true;
6201 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6202 }
6203 } else {
6204 if (action == MotionEvent.ACTION_HOVER_EXIT
6205 || (action == MotionEvent.ACTION_HOVER_MOVE
6206 && !pointInView(event.getX(), event.getY()))) {
6207 mSendingHoverAccessibilityEvents = false;
6208 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6209 }
Jeff Browna1b24182011-07-28 13:38:24 -07006210 }
6211
Jeff Brown87b7f802011-06-21 18:35:45 -07006212 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006213 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006214 case MotionEvent.ACTION_HOVER_ENTER:
6215 setHovered(true);
6216 break;
6217 case MotionEvent.ACTION_HOVER_EXIT:
6218 setHovered(false);
6219 break;
6220 }
Jeff Browna1b24182011-07-28 13:38:24 -07006221
6222 // Dispatch the event to onGenericMotionEvent before returning true.
6223 // This is to provide compatibility with existing applications that
6224 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6225 // break because of the new default handling for hoverable views
6226 // in onHoverEvent.
6227 // Note that onGenericMotionEvent will be called by default when
6228 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6229 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006230 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006231 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006232 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006233 }
6234
6235 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006236 * Returns true if the view should handle {@link #onHoverEvent}
6237 * by calling {@link #setHovered} to change its hovered state.
6238 *
6239 * @return True if the view is hoverable.
6240 */
6241 private boolean isHoverable() {
6242 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006243 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006244 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6245 return false;
6246 }
6247
6248 return (viewFlags & CLICKABLE) == CLICKABLE
6249 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6250 }
6251
6252 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006253 * Returns true if the view is currently hovered.
6254 *
6255 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006256 *
6257 * @see #setHovered
6258 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006259 */
Jeff Brown10b62902011-06-20 16:40:37 -07006260 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006261 public boolean isHovered() {
6262 return (mPrivateFlags & HOVERED) != 0;
6263 }
6264
6265 /**
6266 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006267 * <p>
6268 * Calling this method also changes the drawable state of the view. This
6269 * enables the view to react to hover by using different drawable resources
6270 * to change its appearance.
6271 * </p><p>
6272 * The {@link #onHoverChanged} method is called when the hovered state changes.
6273 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006274 *
6275 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006276 *
6277 * @see #isHovered
6278 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006279 */
6280 public void setHovered(boolean hovered) {
6281 if (hovered) {
6282 if ((mPrivateFlags & HOVERED) == 0) {
6283 mPrivateFlags |= HOVERED;
6284 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006285 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006286 }
6287 } else {
6288 if ((mPrivateFlags & HOVERED) != 0) {
6289 mPrivateFlags &= ~HOVERED;
6290 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006291 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006292 }
6293 }
6294 }
6295
6296 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006297 * Implement this method to handle hover state changes.
6298 * <p>
6299 * This method is called whenever the hover state changes as a result of a
6300 * call to {@link #setHovered}.
6301 * </p>
6302 *
6303 * @param hovered The current hover state, as returned by {@link #isHovered}.
6304 *
6305 * @see #isHovered
6306 * @see #setHovered
6307 */
6308 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006309 }
6310
6311 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006312 * Implement this method to handle touch screen motion events.
6313 *
6314 * @param event The motion event.
6315 * @return True if the event was handled, false otherwise.
6316 */
6317 public boolean onTouchEvent(MotionEvent event) {
6318 final int viewFlags = mViewFlags;
6319
6320 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006321 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
6322 mPrivateFlags &= ~PRESSED;
6323 refreshDrawableState();
6324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006325 // A disabled view that is clickable still consumes the touch
6326 // events, it just doesn't respond to them.
6327 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6328 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6329 }
6330
6331 if (mTouchDelegate != null) {
6332 if (mTouchDelegate.onTouchEvent(event)) {
6333 return true;
6334 }
6335 }
6336
6337 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6338 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6339 switch (event.getAction()) {
6340 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006341 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6342 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006343 // take focus if we don't have it already and we should in
6344 // touch mode.
6345 boolean focusTaken = false;
6346 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6347 focusTaken = requestFocus();
6348 }
6349
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006350 if (prepressed) {
6351 // The button is being released before we actually
6352 // showed it as pressed. Make it show the pressed
6353 // state now (before scheduling the click) to ensure
6354 // the user sees it.
6355 mPrivateFlags |= PRESSED;
6356 refreshDrawableState();
6357 }
Joe Malin32736f02011-01-19 16:14:20 -08006358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006359 if (!mHasPerformedLongPress) {
6360 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006361 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006362
6363 // Only perform take click actions if we were in the pressed state
6364 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006365 // Use a Runnable and post this rather than calling
6366 // performClick directly. This lets other visual state
6367 // of the view update before click actions start.
6368 if (mPerformClick == null) {
6369 mPerformClick = new PerformClick();
6370 }
6371 if (!post(mPerformClick)) {
6372 performClick();
6373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006374 }
6375 }
6376
6377 if (mUnsetPressedState == null) {
6378 mUnsetPressedState = new UnsetPressedState();
6379 }
6380
Adam Powelle14579b2009-12-16 18:39:52 -08006381 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006382 postDelayed(mUnsetPressedState,
6383 ViewConfiguration.getPressedStateDuration());
6384 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006385 // If the post failed, unpress right now
6386 mUnsetPressedState.run();
6387 }
Adam Powelle14579b2009-12-16 18:39:52 -08006388 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006389 }
6390 break;
6391
6392 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006393 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006394
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006395 if (performButtonActionOnTouchDown(event)) {
6396 break;
6397 }
6398
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006399 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006400 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006401
6402 // For views inside a scrolling container, delay the pressed feedback for
6403 // a short period in case this is a scroll.
6404 if (isInScrollingContainer) {
6405 mPrivateFlags |= PREPRESSED;
6406 if (mPendingCheckForTap == null) {
6407 mPendingCheckForTap = new CheckForTap();
6408 }
6409 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6410 } else {
6411 // Not inside a scrolling container, so show the feedback right away
6412 mPrivateFlags |= PRESSED;
6413 refreshDrawableState();
6414 checkForLongClick(0);
6415 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006416 break;
6417
6418 case MotionEvent.ACTION_CANCEL:
6419 mPrivateFlags &= ~PRESSED;
6420 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08006421 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006422 break;
6423
6424 case MotionEvent.ACTION_MOVE:
6425 final int x = (int) event.getX();
6426 final int y = (int) event.getY();
6427
6428 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006429 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006430 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006431 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006432 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006433 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006434 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006435
6436 // Need to switch from pressed to not pressed
6437 mPrivateFlags &= ~PRESSED;
6438 refreshDrawableState();
6439 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006440 }
6441 break;
6442 }
6443 return true;
6444 }
6445
6446 return false;
6447 }
6448
6449 /**
Adam Powell10298662011-08-14 18:26:30 -07006450 * @hide
6451 */
6452 public boolean isInScrollingContainer() {
6453 ViewParent p = getParent();
6454 while (p != null && p instanceof ViewGroup) {
6455 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6456 return true;
6457 }
6458 p = p.getParent();
6459 }
6460 return false;
6461 }
6462
6463 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006464 * Remove the longpress detection timer.
6465 */
6466 private void removeLongPressCallback() {
6467 if (mPendingCheckForLongPress != null) {
6468 removeCallbacks(mPendingCheckForLongPress);
6469 }
6470 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006471
6472 /**
6473 * Remove the pending click action
6474 */
6475 private void removePerformClickCallback() {
6476 if (mPerformClick != null) {
6477 removeCallbacks(mPerformClick);
6478 }
6479 }
6480
Adam Powelle14579b2009-12-16 18:39:52 -08006481 /**
Romain Guya440b002010-02-24 15:57:54 -08006482 * Remove the prepress detection timer.
6483 */
6484 private void removeUnsetPressCallback() {
6485 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6486 setPressed(false);
6487 removeCallbacks(mUnsetPressedState);
6488 }
6489 }
6490
6491 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006492 * Remove the tap detection timer.
6493 */
6494 private void removeTapCallback() {
6495 if (mPendingCheckForTap != null) {
6496 mPrivateFlags &= ~PREPRESSED;
6497 removeCallbacks(mPendingCheckForTap);
6498 }
6499 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006500
6501 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006502 * Cancels a pending long press. Your subclass can use this if you
6503 * want the context menu to come up if the user presses and holds
6504 * at the same place, but you don't want it to come up if they press
6505 * and then move around enough to cause scrolling.
6506 */
6507 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006508 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006509
6510 /*
6511 * The prepressed state handled by the tap callback is a display
6512 * construct, but the tap callback will post a long press callback
6513 * less its own timeout. Remove it here.
6514 */
6515 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006516 }
6517
6518 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006519 * Remove the pending callback for sending a
6520 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6521 */
6522 private void removeSendViewScrolledAccessibilityEventCallback() {
6523 if (mSendViewScrolledAccessibilityEvent != null) {
6524 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6525 }
6526 }
6527
6528 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006529 * Sets the TouchDelegate for this View.
6530 */
6531 public void setTouchDelegate(TouchDelegate delegate) {
6532 mTouchDelegate = delegate;
6533 }
6534
6535 /**
6536 * Gets the TouchDelegate for this View.
6537 */
6538 public TouchDelegate getTouchDelegate() {
6539 return mTouchDelegate;
6540 }
6541
6542 /**
6543 * Set flags controlling behavior of this view.
6544 *
6545 * @param flags Constant indicating the value which should be set
6546 * @param mask Constant indicating the bit range that should be changed
6547 */
6548 void setFlags(int flags, int mask) {
6549 int old = mViewFlags;
6550 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6551
6552 int changed = mViewFlags ^ old;
6553 if (changed == 0) {
6554 return;
6555 }
6556 int privateFlags = mPrivateFlags;
6557
6558 /* Check if the FOCUSABLE bit has changed */
6559 if (((changed & FOCUSABLE_MASK) != 0) &&
6560 ((privateFlags & HAS_BOUNDS) !=0)) {
6561 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6562 && ((privateFlags & FOCUSED) != 0)) {
6563 /* Give up focus if we are no longer focusable */
6564 clearFocus();
6565 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6566 && ((privateFlags & FOCUSED) == 0)) {
6567 /*
6568 * Tell the view system that we are now available to take focus
6569 * if no one else already has it.
6570 */
6571 if (mParent != null) mParent.focusableViewAvailable(this);
6572 }
6573 }
6574
6575 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6576 if ((changed & VISIBILITY_MASK) != 0) {
6577 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006578 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006579 * it was not visible. Marking it drawn ensures that the invalidation will
6580 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006581 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006582 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006583 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006584
6585 needGlobalAttributesUpdate(true);
6586
6587 // a view becoming visible is worth notifying the parent
6588 // about in case nothing has focus. even if this specific view
6589 // isn't focusable, it may contain something that is, so let
6590 // the root view try to give this focus if nothing else does.
6591 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6592 mParent.focusableViewAvailable(this);
6593 }
6594 }
6595 }
6596
6597 /* Check if the GONE bit has changed */
6598 if ((changed & GONE) != 0) {
6599 needGlobalAttributesUpdate(false);
6600 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006601
Romain Guyecd80ee2009-12-03 17:13:02 -08006602 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6603 if (hasFocus()) clearFocus();
6604 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006605 if (mParent instanceof View) {
6606 // GONE views noop invalidation, so invalidate the parent
6607 ((View) mParent).invalidate(true);
6608 }
6609 // Mark the view drawn to ensure that it gets invalidated properly the next
6610 // time it is visible and gets invalidated
6611 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006612 }
6613 if (mAttachInfo != null) {
6614 mAttachInfo.mViewVisibilityChanged = true;
6615 }
6616 }
6617
6618 /* Check if the VISIBLE bit has changed */
6619 if ((changed & INVISIBLE) != 0) {
6620 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006621 /*
6622 * If this view is becoming invisible, set the DRAWN flag so that
6623 * the next invalidate() will not be skipped.
6624 */
6625 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006626
6627 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6628 // root view becoming invisible shouldn't clear focus
6629 if (getRootView() != this) {
6630 clearFocus();
6631 }
6632 }
6633 if (mAttachInfo != null) {
6634 mAttachInfo.mViewVisibilityChanged = true;
6635 }
6636 }
6637
Adam Powell326d8082009-12-09 15:10:07 -08006638 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006639 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006640 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6641 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006642 } else if (mParent != null) {
6643 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006644 }
Adam Powell326d8082009-12-09 15:10:07 -08006645 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6646 }
6647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006648 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6649 destroyDrawingCache();
6650 }
6651
6652 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6653 destroyDrawingCache();
6654 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006655 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006656 }
6657
6658 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6659 destroyDrawingCache();
6660 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6661 }
6662
6663 if ((changed & DRAW_MASK) != 0) {
6664 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6665 if (mBGDrawable != null) {
6666 mPrivateFlags &= ~SKIP_DRAW;
6667 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6668 } else {
6669 mPrivateFlags |= SKIP_DRAW;
6670 }
6671 } else {
6672 mPrivateFlags &= ~SKIP_DRAW;
6673 }
6674 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006675 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006676 }
6677
6678 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006679 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006680 mParent.recomputeViewAttributes(this);
6681 }
6682 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006683
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006684 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006685 requestLayout();
6686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006687 }
6688
6689 /**
6690 * Change the view's z order in the tree, so it's on top of other sibling
6691 * views
6692 */
6693 public void bringToFront() {
6694 if (mParent != null) {
6695 mParent.bringChildToFront(this);
6696 }
6697 }
6698
6699 /**
6700 * This is called in response to an internal scroll in this view (i.e., the
6701 * view scrolled its own contents). This is typically as a result of
6702 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6703 * called.
6704 *
6705 * @param l Current horizontal scroll origin.
6706 * @param t Current vertical scroll origin.
6707 * @param oldl Previous horizontal scroll origin.
6708 * @param oldt Previous vertical scroll origin.
6709 */
6710 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006711 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6712 postSendViewScrolledAccessibilityEventCallback();
6713 }
6714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006715 mBackgroundSizeChanged = true;
6716
6717 final AttachInfo ai = mAttachInfo;
6718 if (ai != null) {
6719 ai.mViewScrollChanged = true;
6720 }
6721 }
6722
6723 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006724 * Interface definition for a callback to be invoked when the layout bounds of a view
6725 * changes due to layout processing.
6726 */
6727 public interface OnLayoutChangeListener {
6728 /**
6729 * Called when the focus state of a view has changed.
6730 *
6731 * @param v The view whose state has changed.
6732 * @param left The new value of the view's left property.
6733 * @param top The new value of the view's top property.
6734 * @param right The new value of the view's right property.
6735 * @param bottom The new value of the view's bottom property.
6736 * @param oldLeft The previous value of the view's left property.
6737 * @param oldTop The previous value of the view's top property.
6738 * @param oldRight The previous value of the view's right property.
6739 * @param oldBottom The previous value of the view's bottom property.
6740 */
6741 void onLayoutChange(View v, int left, int top, int right, int bottom,
6742 int oldLeft, int oldTop, int oldRight, int oldBottom);
6743 }
6744
6745 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006746 * This is called during layout when the size of this view has changed. If
6747 * you were just added to the view hierarchy, you're called with the old
6748 * values of 0.
6749 *
6750 * @param w Current width of this view.
6751 * @param h Current height of this view.
6752 * @param oldw Old width of this view.
6753 * @param oldh Old height of this view.
6754 */
6755 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6756 }
6757
6758 /**
6759 * Called by draw to draw the child views. This may be overridden
6760 * by derived classes to gain control just before its children are drawn
6761 * (but after its own view has been drawn).
6762 * @param canvas the canvas on which to draw the view
6763 */
6764 protected void dispatchDraw(Canvas canvas) {
6765 }
6766
6767 /**
6768 * Gets the parent of this view. Note that the parent is a
6769 * ViewParent and not necessarily a View.
6770 *
6771 * @return Parent of this view.
6772 */
6773 public final ViewParent getParent() {
6774 return mParent;
6775 }
6776
6777 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006778 * Set the horizontal scrolled position of your view. This will cause a call to
6779 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6780 * invalidated.
6781 * @param value the x position to scroll to
6782 */
6783 public void setScrollX(int value) {
6784 scrollTo(value, mScrollY);
6785 }
6786
6787 /**
6788 * Set the vertical scrolled position of your view. This will cause a call to
6789 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6790 * invalidated.
6791 * @param value the y position to scroll to
6792 */
6793 public void setScrollY(int value) {
6794 scrollTo(mScrollX, value);
6795 }
6796
6797 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006798 * Return the scrolled left position of this view. This is the left edge of
6799 * the displayed part of your view. You do not need to draw any pixels
6800 * farther left, since those are outside of the frame of your view on
6801 * screen.
6802 *
6803 * @return The left edge of the displayed part of your view, in pixels.
6804 */
6805 public final int getScrollX() {
6806 return mScrollX;
6807 }
6808
6809 /**
6810 * Return the scrolled top position of this view. This is the top edge of
6811 * the displayed part of your view. You do not need to draw any pixels above
6812 * it, since those are outside of the frame of your view on screen.
6813 *
6814 * @return The top edge of the displayed part of your view, in pixels.
6815 */
6816 public final int getScrollY() {
6817 return mScrollY;
6818 }
6819
6820 /**
6821 * Return the width of the your view.
6822 *
6823 * @return The width of your view, in pixels.
6824 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006825 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006826 public final int getWidth() {
6827 return mRight - mLeft;
6828 }
6829
6830 /**
6831 * Return the height of your view.
6832 *
6833 * @return The height of your view, in pixels.
6834 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006835 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006836 public final int getHeight() {
6837 return mBottom - mTop;
6838 }
6839
6840 /**
6841 * Return the visible drawing bounds of your view. Fills in the output
6842 * rectangle with the values from getScrollX(), getScrollY(),
6843 * getWidth(), and getHeight().
6844 *
6845 * @param outRect The (scrolled) drawing bounds of the view.
6846 */
6847 public void getDrawingRect(Rect outRect) {
6848 outRect.left = mScrollX;
6849 outRect.top = mScrollY;
6850 outRect.right = mScrollX + (mRight - mLeft);
6851 outRect.bottom = mScrollY + (mBottom - mTop);
6852 }
6853
6854 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006855 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6856 * raw width component (that is the result is masked by
6857 * {@link #MEASURED_SIZE_MASK}).
6858 *
6859 * @return The raw measured width of this view.
6860 */
6861 public final int getMeasuredWidth() {
6862 return mMeasuredWidth & MEASURED_SIZE_MASK;
6863 }
6864
6865 /**
6866 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006867 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006868 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006869 * This should be used during measurement and layout calculations only. Use
6870 * {@link #getWidth()} to see how wide a view is after layout.
6871 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006872 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006873 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006874 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006875 return mMeasuredWidth;
6876 }
6877
6878 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006879 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6880 * raw width component (that is the result is masked by
6881 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006882 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006883 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006884 */
6885 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006886 return mMeasuredHeight & MEASURED_SIZE_MASK;
6887 }
6888
6889 /**
6890 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006891 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006892 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6893 * This should be used during measurement and layout calculations only. Use
6894 * {@link #getHeight()} to see how wide a view is after layout.
6895 *
6896 * @return The measured width of this view as a bit mask.
6897 */
6898 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006899 return mMeasuredHeight;
6900 }
6901
6902 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006903 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6904 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6905 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6906 * and the height component is at the shifted bits
6907 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6908 */
6909 public final int getMeasuredState() {
6910 return (mMeasuredWidth&MEASURED_STATE_MASK)
6911 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6912 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6913 }
6914
6915 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006916 * The transform matrix of this view, which is calculated based on the current
6917 * roation, scale, and pivot properties.
6918 *
6919 * @see #getRotation()
6920 * @see #getScaleX()
6921 * @see #getScaleY()
6922 * @see #getPivotX()
6923 * @see #getPivotY()
6924 * @return The current transform matrix for the view
6925 */
6926 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07006927 if (mTransformationInfo != null) {
6928 updateMatrix();
6929 return mTransformationInfo.mMatrix;
6930 }
6931 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07006932 }
6933
6934 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006935 * Utility function to determine if the value is far enough away from zero to be
6936 * considered non-zero.
6937 * @param value A floating point value to check for zero-ness
6938 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6939 */
6940 private static boolean nonzero(float value) {
6941 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6942 }
6943
6944 /**
Jeff Brown86671742010-09-30 20:00:15 -07006945 * Returns true if the transform matrix is the identity matrix.
6946 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006947 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006948 * @return True if the transform matrix is the identity matrix, false otherwise.
6949 */
Jeff Brown86671742010-09-30 20:00:15 -07006950 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07006951 if (mTransformationInfo != null) {
6952 updateMatrix();
6953 return mTransformationInfo.mMatrixIsIdentity;
6954 }
6955 return true;
6956 }
6957
6958 void ensureTransformationInfo() {
6959 if (mTransformationInfo == null) {
6960 mTransformationInfo = new TransformationInfo();
6961 }
Jeff Brown86671742010-09-30 20:00:15 -07006962 }
6963
6964 /**
6965 * Recomputes the transform matrix if necessary.
6966 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006967 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07006968 final TransformationInfo info = mTransformationInfo;
6969 if (info == null) {
6970 return;
6971 }
6972 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006973 // transform-related properties have changed since the last time someone
6974 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07006975
6976 // Figure out if we need to update the pivot point
6977 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07006978 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
6979 info.mPrevWidth = mRight - mLeft;
6980 info.mPrevHeight = mBottom - mTop;
6981 info.mPivotX = info.mPrevWidth / 2f;
6982 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07006983 }
6984 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07006985 info.mMatrix.reset();
6986 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
6987 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
6988 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
6989 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07006990 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07006991 if (info.mCamera == null) {
6992 info.mCamera = new Camera();
6993 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07006994 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07006995 info.mCamera.save();
6996 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
6997 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
6998 info.mCamera.getMatrix(info.matrix3D);
6999 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7000 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7001 info.mPivotY + info.mTranslationY);
7002 info.mMatrix.postConcat(info.matrix3D);
7003 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007004 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007005 info.mMatrixDirty = false;
7006 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7007 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007008 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007009 }
7010
7011 /**
7012 * Utility method to retrieve the inverse of the current mMatrix property.
7013 * We cache the matrix to avoid recalculating it when transform properties
7014 * have not changed.
7015 *
7016 * @return The inverse of the current matrix of this view.
7017 */
Jeff Brown86671742010-09-30 20:00:15 -07007018 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007019 final TransformationInfo info = mTransformationInfo;
7020 if (info != null) {
7021 updateMatrix();
7022 if (info.mInverseMatrixDirty) {
7023 if (info.mInverseMatrix == null) {
7024 info.mInverseMatrix = new Matrix();
7025 }
7026 info.mMatrix.invert(info.mInverseMatrix);
7027 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007028 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007029 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007030 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007031 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007032 }
7033
7034 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007035 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7036 * views are drawn) from the camera to this view. The camera's distance
7037 * affects 3D transformations, for instance rotations around the X and Y
7038 * axis. If the rotationX or rotationY properties are changed and this view is
7039 * large (more than half the size of the screen), it is recommended to always
7040 * use a camera distance that's greater than the height (X axis rotation) or
7041 * the width (Y axis rotation) of this view.</p>
7042 *
7043 * <p>The distance of the camera from the view plane can have an affect on the
7044 * perspective distortion of the view when it is rotated around the x or y axis.
7045 * For example, a large distance will result in a large viewing angle, and there
7046 * will not be much perspective distortion of the view as it rotates. A short
7047 * distance may cause much more perspective distortion upon rotation, and can
7048 * also result in some drawing artifacts if the rotated view ends up partially
7049 * behind the camera (which is why the recommendation is to use a distance at
7050 * least as far as the size of the view, if the view is to be rotated.)</p>
7051 *
7052 * <p>The distance is expressed in "depth pixels." The default distance depends
7053 * on the screen density. For instance, on a medium density display, the
7054 * default distance is 1280. On a high density display, the default distance
7055 * is 1920.</p>
7056 *
7057 * <p>If you want to specify a distance that leads to visually consistent
7058 * results across various densities, use the following formula:</p>
7059 * <pre>
7060 * float scale = context.getResources().getDisplayMetrics().density;
7061 * view.setCameraDistance(distance * scale);
7062 * </pre>
7063 *
7064 * <p>The density scale factor of a high density display is 1.5,
7065 * and 1920 = 1280 * 1.5.</p>
7066 *
7067 * @param distance The distance in "depth pixels", if negative the opposite
7068 * value is used
7069 *
7070 * @see #setRotationX(float)
7071 * @see #setRotationY(float)
7072 */
7073 public void setCameraDistance(float distance) {
7074 invalidateParentCaches();
7075 invalidate(false);
7076
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007077 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007078 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007079 final TransformationInfo info = mTransformationInfo;
7080 if (info.mCamera == null) {
7081 info.mCamera = new Camera();
7082 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007083 }
7084
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007085 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7086 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007087
7088 invalidate(false);
7089 }
7090
7091 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007092 * The degrees that the view is rotated around the pivot point.
7093 *
Romain Guya5364ee2011-02-24 14:46:04 -08007094 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007095 * @see #getPivotX()
7096 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007097 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007098 * @return The degrees of rotation.
7099 */
7100 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007101 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007102 }
7103
7104 /**
Chet Haase897247b2010-09-09 14:54:47 -07007105 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7106 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007107 *
7108 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007109 *
7110 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007111 * @see #getPivotX()
7112 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007113 * @see #setRotationX(float)
7114 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007115 *
7116 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007117 */
7118 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007119 ensureTransformationInfo();
7120 final TransformationInfo info = mTransformationInfo;
7121 if (info.mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007122 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007123 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007124 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007125 info.mRotation = rotation;
7126 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007127 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007128 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007129 }
7130 }
7131
7132 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007133 * The degrees that the view is rotated around the vertical axis through the pivot point.
7134 *
7135 * @see #getPivotX()
7136 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007137 * @see #setRotationY(float)
7138 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007139 * @return The degrees of Y rotation.
7140 */
7141 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007142 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007143 }
7144
7145 /**
Chet Haase897247b2010-09-09 14:54:47 -07007146 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7147 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7148 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007149 *
7150 * When rotating large views, it is recommended to adjust the camera distance
7151 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007152 *
7153 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007154 *
7155 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007156 * @see #getPivotX()
7157 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007158 * @see #setRotation(float)
7159 * @see #setRotationX(float)
7160 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007161 *
7162 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007163 */
7164 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007165 ensureTransformationInfo();
7166 final TransformationInfo info = mTransformationInfo;
7167 if (info.mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007168 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007169 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007170 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007171 info.mRotationY = rotationY;
7172 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007173 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007174 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007175 }
7176 }
7177
7178 /**
7179 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7180 *
7181 * @see #getPivotX()
7182 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007183 * @see #setRotationX(float)
7184 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007185 * @return The degrees of X rotation.
7186 */
7187 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007188 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007189 }
7190
7191 /**
Chet Haase897247b2010-09-09 14:54:47 -07007192 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7193 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7194 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007195 *
7196 * When rotating large views, it is recommended to adjust the camera distance
7197 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007198 *
7199 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007200 *
7201 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007202 * @see #getPivotX()
7203 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007204 * @see #setRotation(float)
7205 * @see #setRotationY(float)
7206 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007207 *
7208 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007209 */
7210 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007211 ensureTransformationInfo();
7212 final TransformationInfo info = mTransformationInfo;
7213 if (info.mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007214 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007215 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007216 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007217 info.mRotationX = rotationX;
7218 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007219 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007220 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007221 }
7222 }
7223
7224 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007225 * The amount that the view is scaled in x around the pivot point, as a proportion of
7226 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7227 *
Joe Onorato93162322010-09-16 15:42:01 -04007228 * <p>By default, this is 1.0f.
7229 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007230 * @see #getPivotX()
7231 * @see #getPivotY()
7232 * @return The scaling factor.
7233 */
7234 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007235 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007236 }
7237
7238 /**
7239 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7240 * the view's unscaled width. A value of 1 means that no scaling is applied.
7241 *
7242 * @param scaleX The scaling factor.
7243 * @see #getPivotX()
7244 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007245 *
7246 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007247 */
7248 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007249 ensureTransformationInfo();
7250 final TransformationInfo info = mTransformationInfo;
7251 if (info.mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007252 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007253 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007254 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007255 info.mScaleX = scaleX;
7256 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007257 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007258 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007259 }
7260 }
7261
7262 /**
7263 * The amount that the view is scaled in y around the pivot point, as a proportion of
7264 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7265 *
Joe Onorato93162322010-09-16 15:42:01 -04007266 * <p>By default, this is 1.0f.
7267 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007268 * @see #getPivotX()
7269 * @see #getPivotY()
7270 * @return The scaling factor.
7271 */
7272 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007273 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007274 }
7275
7276 /**
7277 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7278 * the view's unscaled width. A value of 1 means that no scaling is applied.
7279 *
7280 * @param scaleY The scaling factor.
7281 * @see #getPivotX()
7282 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007283 *
7284 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007285 */
7286 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007287 ensureTransformationInfo();
7288 final TransformationInfo info = mTransformationInfo;
7289 if (info.mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007290 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007291 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007292 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007293 info.mScaleY = scaleY;
7294 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007295 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007296 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007297 }
7298 }
7299
7300 /**
7301 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7302 * and {@link #setScaleX(float) scaled}.
7303 *
7304 * @see #getRotation()
7305 * @see #getScaleX()
7306 * @see #getScaleY()
7307 * @see #getPivotY()
7308 * @return The x location of the pivot point.
7309 */
7310 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007311 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007312 }
7313
7314 /**
7315 * Sets the x location of the point around which the view is
7316 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007317 * By default, the pivot point is centered on the object.
7318 * Setting this property disables this behavior and causes the view to use only the
7319 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007320 *
7321 * @param pivotX The x location of the pivot point.
7322 * @see #getRotation()
7323 * @see #getScaleX()
7324 * @see #getScaleY()
7325 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007326 *
7327 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007328 */
7329 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007330 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007331 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007332 final TransformationInfo info = mTransformationInfo;
7333 if (info.mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007334 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007335 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007336 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007337 info.mPivotX = pivotX;
7338 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007339 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007340 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007341 }
7342 }
7343
7344 /**
7345 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7346 * and {@link #setScaleY(float) scaled}.
7347 *
7348 * @see #getRotation()
7349 * @see #getScaleX()
7350 * @see #getScaleY()
7351 * @see #getPivotY()
7352 * @return The y location of the pivot point.
7353 */
7354 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007355 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007356 }
7357
7358 /**
7359 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007360 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7361 * Setting this property disables this behavior and causes the view to use only the
7362 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007363 *
7364 * @param pivotY The y location of the pivot point.
7365 * @see #getRotation()
7366 * @see #getScaleX()
7367 * @see #getScaleY()
7368 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007369 *
7370 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007371 */
7372 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007373 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007374 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007375 final TransformationInfo info = mTransformationInfo;
7376 if (info.mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007377 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007378 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007379 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007380 info.mPivotY = pivotY;
7381 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007382 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007383 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007384 }
7385 }
7386
7387 /**
7388 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7389 * completely transparent and 1 means the view is completely opaque.
7390 *
Joe Onorato93162322010-09-16 15:42:01 -04007391 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007392 * @return The opacity of the view.
7393 */
7394 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007395 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007396 }
7397
7398 /**
Romain Guy171c5922011-01-06 10:04:23 -08007399 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7400 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007401 *
Romain Guy171c5922011-01-06 10:04:23 -08007402 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7403 * responsible for applying the opacity itself. Otherwise, calling this method is
7404 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007405 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007406 *
7407 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007408 *
Joe Malin32736f02011-01-19 16:14:20 -08007409 * @see #setLayerType(int, android.graphics.Paint)
7410 *
Chet Haase73066682010-11-29 15:55:32 -08007411 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007412 */
7413 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007414 ensureTransformationInfo();
7415 mTransformationInfo.mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007416 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07007417 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07007418 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007419 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007420 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07007421 } else {
Romain Guya3496a92010-10-12 11:53:24 -07007422 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007423 invalidate(false);
7424 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007425 }
7426
7427 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007428 * Faster version of setAlpha() which performs the same steps except there are
7429 * no calls to invalidate(). The caller of this function should perform proper invalidation
7430 * on the parent and this object. The return value indicates whether the subclass handles
7431 * alpha (the return value for onSetAlpha()).
7432 *
7433 * @param alpha The new value for the alpha property
7434 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
7435 */
7436 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007437 ensureTransformationInfo();
7438 mTransformationInfo.mAlpha = alpha;
Chet Haasea00f3862011-02-22 06:34:40 -08007439 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7440 if (subclassHandlesAlpha) {
7441 mPrivateFlags |= ALPHA_SET;
7442 } else {
7443 mPrivateFlags &= ~ALPHA_SET;
7444 }
7445 return subclassHandlesAlpha;
7446 }
7447
7448 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007449 * Top position of this view relative to its parent.
7450 *
7451 * @return The top of this view, in pixels.
7452 */
7453 @ViewDebug.CapturedViewProperty
7454 public final int getTop() {
7455 return mTop;
7456 }
7457
7458 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007459 * Sets the top position of this view relative to its parent. This method is meant to be called
7460 * by the layout system and should not generally be called otherwise, because the property
7461 * may be changed at any time by the layout.
7462 *
7463 * @param top The top of this view, in pixels.
7464 */
7465 public final void setTop(int top) {
7466 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007467 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007468 final boolean matrixIsIdentity = mTransformationInfo == null
7469 || mTransformationInfo.mMatrixIsIdentity;
7470 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007471 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007472 int minTop;
7473 int yLoc;
7474 if (top < mTop) {
7475 minTop = top;
7476 yLoc = top - mTop;
7477 } else {
7478 minTop = mTop;
7479 yLoc = 0;
7480 }
Chet Haasee9140a72011-02-16 16:23:29 -08007481 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007482 }
7483 } else {
7484 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007485 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007486 }
7487
Chet Haaseed032702010-10-01 14:05:54 -07007488 int width = mRight - mLeft;
7489 int oldHeight = mBottom - mTop;
7490
Chet Haase21cd1382010-09-01 17:42:29 -07007491 mTop = top;
7492
Chet Haaseed032702010-10-01 14:05:54 -07007493 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7494
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007495 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007496 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7497 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007498 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007499 }
Chet Haase21cd1382010-09-01 17:42:29 -07007500 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007501 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007502 }
Chet Haase55dbb652010-12-21 20:15:08 -08007503 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007504 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007505 }
7506 }
7507
7508 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007509 * Bottom position of this view relative to its parent.
7510 *
7511 * @return The bottom of this view, in pixels.
7512 */
7513 @ViewDebug.CapturedViewProperty
7514 public final int getBottom() {
7515 return mBottom;
7516 }
7517
7518 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007519 * True if this view has changed since the last time being drawn.
7520 *
7521 * @return The dirty state of this view.
7522 */
7523 public boolean isDirty() {
7524 return (mPrivateFlags & DIRTY_MASK) != 0;
7525 }
7526
7527 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007528 * Sets the bottom position of this view relative to its parent. This method is meant to be
7529 * called by the layout system and should not generally be called otherwise, because the
7530 * property may be changed at any time by the layout.
7531 *
7532 * @param bottom The bottom of this view, in pixels.
7533 */
7534 public final void setBottom(int bottom) {
7535 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007536 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007537 final boolean matrixIsIdentity = mTransformationInfo == null
7538 || mTransformationInfo.mMatrixIsIdentity;
7539 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007540 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007541 int maxBottom;
7542 if (bottom < mBottom) {
7543 maxBottom = mBottom;
7544 } else {
7545 maxBottom = bottom;
7546 }
Chet Haasee9140a72011-02-16 16:23:29 -08007547 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007548 }
7549 } else {
7550 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007551 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007552 }
7553
Chet Haaseed032702010-10-01 14:05:54 -07007554 int width = mRight - mLeft;
7555 int oldHeight = mBottom - mTop;
7556
Chet Haase21cd1382010-09-01 17:42:29 -07007557 mBottom = bottom;
7558
Chet Haaseed032702010-10-01 14:05:54 -07007559 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7560
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007561 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007562 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7563 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007564 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007565 }
Chet Haase21cd1382010-09-01 17:42:29 -07007566 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007567 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007568 }
Chet Haase55dbb652010-12-21 20:15:08 -08007569 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007570 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007571 }
7572 }
7573
7574 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007575 * Left position of this view relative to its parent.
7576 *
7577 * @return The left edge of this view, in pixels.
7578 */
7579 @ViewDebug.CapturedViewProperty
7580 public final int getLeft() {
7581 return mLeft;
7582 }
7583
7584 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007585 * Sets the left position of this view relative to its parent. This method is meant to be called
7586 * by the layout system and should not generally be called otherwise, because the property
7587 * may be changed at any time by the layout.
7588 *
7589 * @param left The bottom of this view, in pixels.
7590 */
7591 public final void setLeft(int left) {
7592 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007593 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007594 final boolean matrixIsIdentity = mTransformationInfo == null
7595 || mTransformationInfo.mMatrixIsIdentity;
7596 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007597 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007598 int minLeft;
7599 int xLoc;
7600 if (left < mLeft) {
7601 minLeft = left;
7602 xLoc = left - mLeft;
7603 } else {
7604 minLeft = mLeft;
7605 xLoc = 0;
7606 }
Chet Haasee9140a72011-02-16 16:23:29 -08007607 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007608 }
7609 } else {
7610 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007611 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007612 }
7613
Chet Haaseed032702010-10-01 14:05:54 -07007614 int oldWidth = mRight - mLeft;
7615 int height = mBottom - mTop;
7616
Chet Haase21cd1382010-09-01 17:42:29 -07007617 mLeft = left;
7618
Chet Haaseed032702010-10-01 14:05:54 -07007619 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7620
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007621 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007622 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7623 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007624 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007625 }
Chet Haase21cd1382010-09-01 17:42:29 -07007626 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007627 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007628 }
Chet Haase55dbb652010-12-21 20:15:08 -08007629 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007630 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007631 }
7632 }
7633
7634 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007635 * Right position of this view relative to its parent.
7636 *
7637 * @return The right edge of this view, in pixels.
7638 */
7639 @ViewDebug.CapturedViewProperty
7640 public final int getRight() {
7641 return mRight;
7642 }
7643
7644 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007645 * Sets the right position of this view relative to its parent. This method is meant to be called
7646 * by the layout system and should not generally be called otherwise, because the property
7647 * may be changed at any time by the layout.
7648 *
7649 * @param right The bottom of this view, in pixels.
7650 */
7651 public final void setRight(int right) {
7652 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007653 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007654 final boolean matrixIsIdentity = mTransformationInfo == null
7655 || mTransformationInfo.mMatrixIsIdentity;
7656 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007657 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007658 int maxRight;
7659 if (right < mRight) {
7660 maxRight = mRight;
7661 } else {
7662 maxRight = right;
7663 }
Chet Haasee9140a72011-02-16 16:23:29 -08007664 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007665 }
7666 } else {
7667 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007668 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007669 }
7670
Chet Haaseed032702010-10-01 14:05:54 -07007671 int oldWidth = mRight - mLeft;
7672 int height = mBottom - mTop;
7673
Chet Haase21cd1382010-09-01 17:42:29 -07007674 mRight = right;
7675
Chet Haaseed032702010-10-01 14:05:54 -07007676 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7677
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007678 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007679 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7680 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007681 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007682 }
Chet Haase21cd1382010-09-01 17:42:29 -07007683 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007684 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007685 }
Chet Haase55dbb652010-12-21 20:15:08 -08007686 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007687 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007688 }
7689 }
7690
7691 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007692 * The visual x position of this view, in pixels. This is equivalent to the
7693 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007694 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007695 *
Chet Haasedf030d22010-07-30 17:22:38 -07007696 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007697 */
Chet Haasedf030d22010-07-30 17:22:38 -07007698 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007699 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007700 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007701
Chet Haasedf030d22010-07-30 17:22:38 -07007702 /**
7703 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7704 * {@link #setTranslationX(float) translationX} property to be the difference between
7705 * the x value passed in and the current {@link #getLeft() left} property.
7706 *
7707 * @param x The visual x position of this view, in pixels.
7708 */
7709 public void setX(float x) {
7710 setTranslationX(x - mLeft);
7711 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007712
Chet Haasedf030d22010-07-30 17:22:38 -07007713 /**
7714 * The visual y position of this view, in pixels. This is equivalent to the
7715 * {@link #setTranslationY(float) translationY} property plus the current
7716 * {@link #getTop() top} property.
7717 *
7718 * @return The visual y position of this view, in pixels.
7719 */
7720 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007721 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007722 }
7723
7724 /**
7725 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7726 * {@link #setTranslationY(float) translationY} property to be the difference between
7727 * the y value passed in and the current {@link #getTop() top} property.
7728 *
7729 * @param y The visual y position of this view, in pixels.
7730 */
7731 public void setY(float y) {
7732 setTranslationY(y - mTop);
7733 }
7734
7735
7736 /**
7737 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7738 * This position is post-layout, in addition to wherever the object's
7739 * layout placed it.
7740 *
7741 * @return The horizontal position of this view relative to its left position, in pixels.
7742 */
7743 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007744 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07007745 }
7746
7747 /**
7748 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7749 * This effectively positions the object post-layout, in addition to wherever the object's
7750 * layout placed it.
7751 *
7752 * @param translationX The horizontal position of this view relative to its left position,
7753 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007754 *
7755 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007756 */
7757 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007758 ensureTransformationInfo();
7759 final TransformationInfo info = mTransformationInfo;
7760 if (info.mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007761 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007762 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007763 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007764 info.mTranslationX = translationX;
7765 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07007766 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007767 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007768 }
7769 }
7770
7771 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007772 * The horizontal location of this view relative to its {@link #getTop() top} position.
7773 * This position is post-layout, in addition to wherever the object's
7774 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007775 *
Chet Haasedf030d22010-07-30 17:22:38 -07007776 * @return The vertical position of this view relative to its top position,
7777 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007778 */
Chet Haasedf030d22010-07-30 17:22:38 -07007779 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007780 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007781 }
7782
7783 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007784 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7785 * This effectively positions the object post-layout, in addition to wherever the object's
7786 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007787 *
Chet Haasedf030d22010-07-30 17:22:38 -07007788 * @param translationY The vertical position of this view relative to its top position,
7789 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007790 *
7791 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007792 */
Chet Haasedf030d22010-07-30 17:22:38 -07007793 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007794 ensureTransformationInfo();
7795 final TransformationInfo info = mTransformationInfo;
7796 if (info.mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007797 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007798 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007799 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007800 info.mTranslationY = translationY;
7801 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07007802 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007803 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007804 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007805 }
7806
7807 /**
Romain Guyda489792011-02-03 01:05:15 -08007808 * @hide
7809 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007810 public void setFastTranslationX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007811 ensureTransformationInfo();
7812 final TransformationInfo info = mTransformationInfo;
7813 info.mTranslationX = x;
7814 info.mMatrixDirty = true;
Michael Jurkadece29f2011-02-03 01:41:49 -08007815 }
7816
7817 /**
7818 * @hide
7819 */
7820 public void setFastTranslationY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007821 ensureTransformationInfo();
7822 final TransformationInfo info = mTransformationInfo;
7823 info.mTranslationY = y;
7824 info.mMatrixDirty = true;
Michael Jurkadece29f2011-02-03 01:41:49 -08007825 }
7826
7827 /**
7828 * @hide
7829 */
Romain Guyda489792011-02-03 01:05:15 -08007830 public void setFastX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007831 ensureTransformationInfo();
7832 final TransformationInfo info = mTransformationInfo;
7833 info.mTranslationX = x - mLeft;
7834 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007835 }
7836
7837 /**
7838 * @hide
7839 */
7840 public void setFastY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007841 ensureTransformationInfo();
7842 final TransformationInfo info = mTransformationInfo;
7843 info.mTranslationY = y - mTop;
7844 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007845 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007846
Romain Guyda489792011-02-03 01:05:15 -08007847 /**
7848 * @hide
7849 */
7850 public void setFastScaleX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007851 ensureTransformationInfo();
7852 final TransformationInfo info = mTransformationInfo;
7853 info.mScaleX = x;
7854 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007855 }
7856
7857 /**
7858 * @hide
7859 */
7860 public void setFastScaleY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007861 ensureTransformationInfo();
7862 final TransformationInfo info = mTransformationInfo;
7863 info.mScaleY = y;
7864 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007865 }
7866
7867 /**
7868 * @hide
7869 */
7870 public void setFastAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007871 ensureTransformationInfo();
7872 mTransformationInfo.mAlpha = alpha;
Romain Guyda489792011-02-03 01:05:15 -08007873 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007874
Romain Guyda489792011-02-03 01:05:15 -08007875 /**
7876 * @hide
7877 */
7878 public void setFastRotationY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007879 ensureTransformationInfo();
7880 final TransformationInfo info = mTransformationInfo;
7881 info.mRotationY = y;
7882 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007883 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007884
Romain Guyda489792011-02-03 01:05:15 -08007885 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007886 * Hit rectangle in parent's coordinates
7887 *
7888 * @param outRect The hit rectangle of the view.
7889 */
7890 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07007891 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007892 final TransformationInfo info = mTransformationInfo;
7893 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007894 outRect.set(mLeft, mTop, mRight, mBottom);
7895 } else {
7896 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007897 tmpRect.set(-info.mPivotX, -info.mPivotY,
7898 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
7899 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07007900 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
7901 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07007902 }
7903 }
7904
7905 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007906 * Determines whether the given point, in local coordinates is inside the view.
7907 */
7908 /*package*/ final boolean pointInView(float localX, float localY) {
7909 return localX >= 0 && localX < (mRight - mLeft)
7910 && localY >= 0 && localY < (mBottom - mTop);
7911 }
7912
7913 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007914 * Utility method to determine whether the given point, in local coordinates,
7915 * is inside the view, where the area of the view is expanded by the slop factor.
7916 * This method is called while processing touch-move events to determine if the event
7917 * is still within the view.
7918 */
7919 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007920 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007921 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007922 }
7923
7924 /**
7925 * When a view has focus and the user navigates away from it, the next view is searched for
7926 * starting from the rectangle filled in by this method.
7927 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07007928 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
7929 * of the view. However, if your view maintains some idea of internal selection,
7930 * such as a cursor, or a selected row or column, you should override this method and
7931 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007932 *
7933 * @param r The rectangle to fill in, in this view's coordinates.
7934 */
7935 public void getFocusedRect(Rect r) {
7936 getDrawingRect(r);
7937 }
7938
7939 /**
7940 * If some part of this view is not clipped by any of its parents, then
7941 * return that area in r in global (root) coordinates. To convert r to local
7942 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
7943 * -globalOffset.y)) If the view is completely clipped or translated out,
7944 * return false.
7945 *
7946 * @param r If true is returned, r holds the global coordinates of the
7947 * visible portion of this view.
7948 * @param globalOffset If true is returned, globalOffset holds the dx,dy
7949 * between this view and its root. globalOffet may be null.
7950 * @return true if r is non-empty (i.e. part of the view is visible at the
7951 * root level.
7952 */
7953 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7954 int width = mRight - mLeft;
7955 int height = mBottom - mTop;
7956 if (width > 0 && height > 0) {
7957 r.set(0, 0, width, height);
7958 if (globalOffset != null) {
7959 globalOffset.set(-mScrollX, -mScrollY);
7960 }
7961 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7962 }
7963 return false;
7964 }
7965
7966 public final boolean getGlobalVisibleRect(Rect r) {
7967 return getGlobalVisibleRect(r, null);
7968 }
7969
7970 public final boolean getLocalVisibleRect(Rect r) {
7971 Point offset = new Point();
7972 if (getGlobalVisibleRect(r, offset)) {
7973 r.offset(-offset.x, -offset.y); // make r local
7974 return true;
7975 }
7976 return false;
7977 }
7978
7979 /**
7980 * Offset this view's vertical location by the specified number of pixels.
7981 *
7982 * @param offset the number of pixels to offset the view by
7983 */
7984 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007985 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007986 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007987 final boolean matrixIsIdentity = mTransformationInfo == null
7988 || mTransformationInfo.mMatrixIsIdentity;
7989 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007990 final ViewParent p = mParent;
7991 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007992 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007993 int minTop;
7994 int maxBottom;
7995 int yLoc;
7996 if (offset < 0) {
7997 minTop = mTop + offset;
7998 maxBottom = mBottom;
7999 yLoc = offset;
8000 } else {
8001 minTop = mTop;
8002 maxBottom = mBottom + offset;
8003 yLoc = 0;
8004 }
8005 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8006 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008007 }
8008 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008009 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008010 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008011
Chet Haasec3aa3612010-06-17 08:50:37 -07008012 mTop += offset;
8013 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008014
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008015 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008016 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008017 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008018 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008019 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008021 }
8022
8023 /**
8024 * Offset this view's horizontal location by the specified amount of pixels.
8025 *
8026 * @param offset the numer of pixels to offset the view by
8027 */
8028 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008029 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008030 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008031 final boolean matrixIsIdentity = mTransformationInfo == null
8032 || mTransformationInfo.mMatrixIsIdentity;
8033 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008034 final ViewParent p = mParent;
8035 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008036 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008037 int minLeft;
8038 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008039 if (offset < 0) {
8040 minLeft = mLeft + offset;
8041 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008042 } else {
8043 minLeft = mLeft;
8044 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008045 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008046 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008047 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008048 }
8049 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008050 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008051 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008052
Chet Haasec3aa3612010-06-17 08:50:37 -07008053 mLeft += offset;
8054 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008055
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008056 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008057 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008058 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008059 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008060 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008062 }
8063
8064 /**
8065 * Get the LayoutParams associated with this view. All views should have
8066 * layout parameters. These supply parameters to the <i>parent</i> of this
8067 * view specifying how it should be arranged. There are many subclasses of
8068 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8069 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008070 *
8071 * This method may return null if this View is not attached to a parent
8072 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8073 * was not invoked successfully. When a View is attached to a parent
8074 * ViewGroup, this method must not return null.
8075 *
8076 * @return The LayoutParams associated with this view, or null if no
8077 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008078 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008079 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008080 public ViewGroup.LayoutParams getLayoutParams() {
8081 return mLayoutParams;
8082 }
8083
8084 /**
8085 * Set the layout parameters associated with this view. These supply
8086 * parameters to the <i>parent</i> of this view specifying how it should be
8087 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8088 * correspond to the different subclasses of ViewGroup that are responsible
8089 * for arranging their children.
8090 *
Romain Guy01c174b2011-02-22 11:51:06 -08008091 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008092 */
8093 public void setLayoutParams(ViewGroup.LayoutParams params) {
8094 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008095 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008096 }
8097 mLayoutParams = params;
8098 requestLayout();
8099 }
8100
8101 /**
8102 * Set the scrolled position of your view. This will cause a call to
8103 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8104 * invalidated.
8105 * @param x the x position to scroll to
8106 * @param y the y position to scroll to
8107 */
8108 public void scrollTo(int x, int y) {
8109 if (mScrollX != x || mScrollY != y) {
8110 int oldX = mScrollX;
8111 int oldY = mScrollY;
8112 mScrollX = x;
8113 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008114 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008115 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008116 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008117 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008119 }
8120 }
8121
8122 /**
8123 * Move the scrolled position of your view. This will cause a call to
8124 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8125 * invalidated.
8126 * @param x the amount of pixels to scroll by horizontally
8127 * @param y the amount of pixels to scroll by vertically
8128 */
8129 public void scrollBy(int x, int y) {
8130 scrollTo(mScrollX + x, mScrollY + y);
8131 }
8132
8133 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008134 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8135 * animation to fade the scrollbars out after a default delay. If a subclass
8136 * provides animated scrolling, the start delay should equal the duration
8137 * of the scrolling animation.</p>
8138 *
8139 * <p>The animation starts only if at least one of the scrollbars is
8140 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8141 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8142 * this method returns true, and false otherwise. If the animation is
8143 * started, this method calls {@link #invalidate()}; in that case the
8144 * caller should not call {@link #invalidate()}.</p>
8145 *
8146 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008147 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008148 *
8149 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8150 * and {@link #scrollTo(int, int)}.</p>
8151 *
8152 * @return true if the animation is played, false otherwise
8153 *
8154 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008155 * @see #scrollBy(int, int)
8156 * @see #scrollTo(int, int)
8157 * @see #isHorizontalScrollBarEnabled()
8158 * @see #isVerticalScrollBarEnabled()
8159 * @see #setHorizontalScrollBarEnabled(boolean)
8160 * @see #setVerticalScrollBarEnabled(boolean)
8161 */
8162 protected boolean awakenScrollBars() {
8163 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008164 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008165 }
8166
8167 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008168 * Trigger the scrollbars to draw.
8169 * This method differs from awakenScrollBars() only in its default duration.
8170 * initialAwakenScrollBars() will show the scroll bars for longer than
8171 * usual to give the user more of a chance to notice them.
8172 *
8173 * @return true if the animation is played, false otherwise.
8174 */
8175 private boolean initialAwakenScrollBars() {
8176 return mScrollCache != null &&
8177 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8178 }
8179
8180 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008181 * <p>
8182 * Trigger the scrollbars to draw. When invoked this method starts an
8183 * animation to fade the scrollbars out after a fixed delay. If a subclass
8184 * provides animated scrolling, the start delay should equal the duration of
8185 * the scrolling animation.
8186 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008187 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008188 * <p>
8189 * The animation starts only if at least one of the scrollbars is enabled,
8190 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8191 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8192 * this method returns true, and false otherwise. If the animation is
8193 * started, this method calls {@link #invalidate()}; in that case the caller
8194 * should not call {@link #invalidate()}.
8195 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008196 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008197 * <p>
8198 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008199 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008200 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008201 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008202 * @param startDelay the delay, in milliseconds, after which the animation
8203 * should start; when the delay is 0, the animation starts
8204 * immediately
8205 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008206 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008207 * @see #scrollBy(int, int)
8208 * @see #scrollTo(int, int)
8209 * @see #isHorizontalScrollBarEnabled()
8210 * @see #isVerticalScrollBarEnabled()
8211 * @see #setHorizontalScrollBarEnabled(boolean)
8212 * @see #setVerticalScrollBarEnabled(boolean)
8213 */
8214 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008215 return awakenScrollBars(startDelay, true);
8216 }
Joe Malin32736f02011-01-19 16:14:20 -08008217
Mike Cleron290947b2009-09-29 18:34:32 -07008218 /**
8219 * <p>
8220 * Trigger the scrollbars to draw. When invoked this method starts an
8221 * animation to fade the scrollbars out after a fixed delay. If a subclass
8222 * provides animated scrolling, the start delay should equal the duration of
8223 * the scrolling animation.
8224 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008225 *
Mike Cleron290947b2009-09-29 18:34:32 -07008226 * <p>
8227 * The animation starts only if at least one of the scrollbars is enabled,
8228 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8229 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8230 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008231 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008232 * is set to true; in that case the caller
8233 * should not call {@link #invalidate()}.
8234 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008235 *
Mike Cleron290947b2009-09-29 18:34:32 -07008236 * <p>
8237 * This method should be invoked everytime a subclass directly updates the
8238 * scroll parameters.
8239 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008240 *
Mike Cleron290947b2009-09-29 18:34:32 -07008241 * @param startDelay the delay, in milliseconds, after which the animation
8242 * should start; when the delay is 0, the animation starts
8243 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008244 *
Mike Cleron290947b2009-09-29 18:34:32 -07008245 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008246 *
Mike Cleron290947b2009-09-29 18:34:32 -07008247 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008248 *
Mike Cleron290947b2009-09-29 18:34:32 -07008249 * @see #scrollBy(int, int)
8250 * @see #scrollTo(int, int)
8251 * @see #isHorizontalScrollBarEnabled()
8252 * @see #isVerticalScrollBarEnabled()
8253 * @see #setHorizontalScrollBarEnabled(boolean)
8254 * @see #setVerticalScrollBarEnabled(boolean)
8255 */
8256 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008257 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008258
Mike Cleronf116bf82009-09-27 19:14:12 -07008259 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8260 return false;
8261 }
8262
8263 if (scrollCache.scrollBar == null) {
8264 scrollCache.scrollBar = new ScrollBarDrawable();
8265 }
8266
8267 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8268
Mike Cleron290947b2009-09-29 18:34:32 -07008269 if (invalidate) {
8270 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008271 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008272 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008273
8274 if (scrollCache.state == ScrollabilityCache.OFF) {
8275 // FIXME: this is copied from WindowManagerService.
8276 // We should get this value from the system when it
8277 // is possible to do so.
8278 final int KEY_REPEAT_FIRST_DELAY = 750;
8279 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8280 }
8281
8282 // Tell mScrollCache when we should start fading. This may
8283 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008284 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008285 scrollCache.fadeStartTime = fadeStartTime;
8286 scrollCache.state = ScrollabilityCache.ON;
8287
8288 // Schedule our fader to run, unscheduling any old ones first
8289 if (mAttachInfo != null) {
8290 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8291 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8292 }
8293
8294 return true;
8295 }
8296
8297 return false;
8298 }
8299
8300 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008301 * Do not invalidate views which are not visible and which are not running an animation. They
8302 * will not get drawn and they should not set dirty flags as if they will be drawn
8303 */
8304 private boolean skipInvalidate() {
8305 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8306 (!(mParent instanceof ViewGroup) ||
8307 !((ViewGroup) mParent).isViewTransitioning(this));
8308 }
8309 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008310 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008311 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8312 * in the future. This must be called from a UI thread. To call from a non-UI
8313 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008314 *
8315 * WARNING: This method is destructive to dirty.
8316 * @param dirty the rectangle representing the bounds of the dirty region
8317 */
8318 public void invalidate(Rect dirty) {
8319 if (ViewDebug.TRACE_HIERARCHY) {
8320 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8321 }
8322
Chet Haaseaceafe62011-08-26 15:44:33 -07008323 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008324 return;
8325 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008326 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008327 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8328 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008329 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008330 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008331 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008332 final ViewParent p = mParent;
8333 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008334 //noinspection PointlessBooleanExpression,ConstantConditions
8335 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8336 if (p != null && ai != null && ai.mHardwareAccelerated) {
8337 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008338 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008339 p.invalidateChild(this, null);
8340 return;
8341 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008343 if (p != null && ai != null) {
8344 final int scrollX = mScrollX;
8345 final int scrollY = mScrollY;
8346 final Rect r = ai.mTmpInvalRect;
8347 r.set(dirty.left - scrollX, dirty.top - scrollY,
8348 dirty.right - scrollX, dirty.bottom - scrollY);
8349 mParent.invalidateChild(this, r);
8350 }
8351 }
8352 }
8353
8354 /**
8355 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
8356 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008357 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8358 * will be called at some point in the future. This must be called from
8359 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008360 * @param l the left position of the dirty region
8361 * @param t the top position of the dirty region
8362 * @param r the right position of the dirty region
8363 * @param b the bottom position of the dirty region
8364 */
8365 public void invalidate(int l, int t, int r, int b) {
8366 if (ViewDebug.TRACE_HIERARCHY) {
8367 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8368 }
8369
Chet Haaseaceafe62011-08-26 15:44:33 -07008370 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008371 return;
8372 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008373 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008374 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8375 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008376 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008377 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008378 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008379 final ViewParent p = mParent;
8380 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008381 //noinspection PointlessBooleanExpression,ConstantConditions
8382 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8383 if (p != null && ai != null && ai.mHardwareAccelerated) {
8384 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008385 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008386 p.invalidateChild(this, null);
8387 return;
8388 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008389 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008390 if (p != null && ai != null && l < r && t < b) {
8391 final int scrollX = mScrollX;
8392 final int scrollY = mScrollY;
8393 final Rect tmpr = ai.mTmpInvalRect;
8394 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8395 p.invalidateChild(this, tmpr);
8396 }
8397 }
8398 }
8399
8400 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008401 * Invalidate the whole view. If the view is visible,
8402 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8403 * the future. This must be called from a UI thread. To call from a non-UI thread,
8404 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008405 */
8406 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008407 invalidate(true);
8408 }
Joe Malin32736f02011-01-19 16:14:20 -08008409
Chet Haaseed032702010-10-01 14:05:54 -07008410 /**
8411 * This is where the invalidate() work actually happens. A full invalidate()
8412 * causes the drawing cache to be invalidated, but this function can be called with
8413 * invalidateCache set to false to skip that invalidation step for cases that do not
8414 * need it (for example, a component that remains at the same dimensions with the same
8415 * content).
8416 *
8417 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8418 * well. This is usually true for a full invalidate, but may be set to false if the
8419 * View's contents or dimensions have not changed.
8420 */
Romain Guy849d0a32011-02-01 17:20:48 -08008421 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008422 if (ViewDebug.TRACE_HIERARCHY) {
8423 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8424 }
8425
Chet Haaseaceafe62011-08-26 15:44:33 -07008426 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008427 return;
8428 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008429 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008430 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008431 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8432 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008433 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008434 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008435 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008436 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008437 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8438 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008439 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008440 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008441 //noinspection PointlessBooleanExpression,ConstantConditions
8442 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8443 if (p != null && ai != null && ai.mHardwareAccelerated) {
8444 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008445 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008446 p.invalidateChild(this, null);
8447 return;
8448 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008449 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008451 if (p != null && ai != null) {
8452 final Rect r = ai.mTmpInvalRect;
8453 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8454 // Don't call invalidate -- we don't want to internally scroll
8455 // our own bounds
8456 p.invalidateChild(this, r);
8457 }
8458 }
8459 }
8460
8461 /**
Romain Guyda489792011-02-03 01:05:15 -08008462 * @hide
8463 */
8464 public void fastInvalidate() {
Chet Haaseaceafe62011-08-26 15:44:33 -07008465 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008466 return;
8467 }
Romain Guyda489792011-02-03 01:05:15 -08008468 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
8469 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8470 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
8471 if (mParent instanceof View) {
8472 ((View) mParent).mPrivateFlags |= INVALIDATED;
8473 }
8474 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008475 mPrivateFlags |= DIRTY;
Romain Guyda489792011-02-03 01:05:15 -08008476 mPrivateFlags |= INVALIDATED;
8477 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07008478 if (mParent != null && mAttachInfo != null) {
8479 if (mAttachInfo.mHardwareAccelerated) {
8480 mParent.invalidateChild(this, null);
8481 } else {
8482 final Rect r = mAttachInfo.mTmpInvalRect;
8483 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8484 // Don't call invalidate -- we don't want to internally scroll
8485 // our own bounds
8486 mParent.invalidateChild(this, r);
8487 }
Romain Guyda489792011-02-03 01:05:15 -08008488 }
8489 }
8490 }
8491
8492 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008493 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008494 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8495 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008496 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8497 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008498 *
8499 * @hide
8500 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008501 protected void invalidateParentCaches() {
8502 if (mParent instanceof View) {
8503 ((View) mParent).mPrivateFlags |= INVALIDATED;
8504 }
8505 }
Joe Malin32736f02011-01-19 16:14:20 -08008506
Romain Guy0fd89bf2011-01-26 15:41:30 -08008507 /**
8508 * Used to indicate that the parent of this view should be invalidated. This functionality
8509 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8510 * which is necessary when various parent-managed properties of the view change, such as
8511 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8512 * an invalidation event to the parent.
8513 *
8514 * @hide
8515 */
8516 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008517 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008518 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008519 }
8520 }
8521
8522 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008523 * Indicates whether this View is opaque. An opaque View guarantees that it will
8524 * draw all the pixels overlapping its bounds using a fully opaque color.
8525 *
8526 * Subclasses of View should override this method whenever possible to indicate
8527 * whether an instance is opaque. Opaque Views are treated in a special way by
8528 * the View hierarchy, possibly allowing it to perform optimizations during
8529 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008530 *
Romain Guy24443ea2009-05-11 11:56:30 -07008531 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008532 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008533 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008534 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008535 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008536 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8537 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008538 }
8539
Adam Powell20232d02010-12-08 21:08:53 -08008540 /**
8541 * @hide
8542 */
8543 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008544 // Opaque if:
8545 // - Has a background
8546 // - Background is opaque
8547 // - Doesn't have scrollbars or scrollbars are inside overlay
8548
8549 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8550 mPrivateFlags |= OPAQUE_BACKGROUND;
8551 } else {
8552 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8553 }
8554
8555 final int flags = mViewFlags;
8556 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8557 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8558 mPrivateFlags |= OPAQUE_SCROLLBARS;
8559 } else {
8560 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8561 }
8562 }
8563
8564 /**
8565 * @hide
8566 */
8567 protected boolean hasOpaqueScrollbars() {
8568 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008569 }
8570
8571 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008572 * @return A handler associated with the thread running the View. This
8573 * handler can be used to pump events in the UI events queue.
8574 */
8575 public Handler getHandler() {
8576 if (mAttachInfo != null) {
8577 return mAttachInfo.mHandler;
8578 }
8579 return null;
8580 }
8581
8582 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008583 * <p>Causes the Runnable to be added to the message queue.
8584 * The runnable will be run on the user interface thread.</p>
8585 *
8586 * <p>This method can be invoked from outside of the UI thread
8587 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008588 *
8589 * @param action The Runnable that will be executed.
8590 *
8591 * @return Returns true if the Runnable was successfully placed in to the
8592 * message queue. Returns false on failure, usually because the
8593 * looper processing the message queue is exiting.
8594 */
8595 public boolean post(Runnable action) {
8596 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008597 AttachInfo attachInfo = mAttachInfo;
8598 if (attachInfo != null) {
8599 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008600 } else {
8601 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008602 ViewRootImpl.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008603 return true;
8604 }
8605
8606 return handler.post(action);
8607 }
8608
8609 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008610 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008611 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008612 * The runnable will be run on the user interface thread.</p>
8613 *
8614 * <p>This method can be invoked from outside of the UI thread
8615 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008616 *
8617 * @param action The Runnable that will be executed.
8618 * @param delayMillis The delay (in milliseconds) until the Runnable
8619 * will be executed.
8620 *
8621 * @return true if the Runnable was successfully placed in to the
8622 * message queue. Returns false on failure, usually because the
8623 * looper processing the message queue is exiting. Note that a
8624 * result of true does not mean the Runnable will be processed --
8625 * if the looper is quit before the delivery time of the message
8626 * occurs then the message will be dropped.
8627 */
8628 public boolean postDelayed(Runnable action, long delayMillis) {
8629 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008630 AttachInfo attachInfo = mAttachInfo;
8631 if (attachInfo != null) {
8632 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008633 } else {
8634 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008635 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008636 return true;
8637 }
8638
8639 return handler.postDelayed(action, delayMillis);
8640 }
8641
8642 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008643 * <p>Removes the specified Runnable from the message queue.</p>
8644 *
8645 * <p>This method can be invoked from outside of the UI thread
8646 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008647 *
8648 * @param action The Runnable to remove from the message handling queue
8649 *
8650 * @return true if this view could ask the Handler to remove the Runnable,
8651 * false otherwise. When the returned value is true, the Runnable
8652 * may or may not have been actually removed from the message queue
8653 * (for instance, if the Runnable was not in the queue already.)
8654 */
8655 public boolean removeCallbacks(Runnable action) {
8656 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008657 AttachInfo attachInfo = mAttachInfo;
8658 if (attachInfo != null) {
8659 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008660 } else {
8661 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008662 ViewRootImpl.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008663 return true;
8664 }
8665
8666 handler.removeCallbacks(action);
8667 return true;
8668 }
8669
8670 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008671 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
8672 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008673 *
Romain Guye63a4f32011-08-11 11:33:31 -07008674 * <p>This method can be invoked from outside of the UI thread
8675 * only when this View is attached to a window.</p>
8676 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008677 * @see #invalidate()
8678 */
8679 public void postInvalidate() {
8680 postInvalidateDelayed(0);
8681 }
8682
8683 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008684 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8685 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
8686 *
8687 * <p>This method can be invoked from outside of the UI thread
8688 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008689 *
8690 * @param left The left coordinate of the rectangle to invalidate.
8691 * @param top The top coordinate of the rectangle to invalidate.
8692 * @param right The right coordinate of the rectangle to invalidate.
8693 * @param bottom The bottom coordinate of the rectangle to invalidate.
8694 *
8695 * @see #invalidate(int, int, int, int)
8696 * @see #invalidate(Rect)
8697 */
8698 public void postInvalidate(int left, int top, int right, int bottom) {
8699 postInvalidateDelayed(0, left, top, right, bottom);
8700 }
8701
8702 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008703 * <p>Cause an invalidate to happen on a subsequent cycle through the event
8704 * loop. Waits for the specified amount of time.</p>
8705 *
8706 * <p>This method can be invoked from outside of the UI thread
8707 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008708 *
8709 * @param delayMilliseconds the duration in milliseconds to delay the
8710 * invalidation by
8711 */
8712 public void postInvalidateDelayed(long delayMilliseconds) {
8713 // We try only with the AttachInfo because there's no point in invalidating
8714 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008715 AttachInfo attachInfo = mAttachInfo;
8716 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008717 Message msg = Message.obtain();
8718 msg.what = AttachInfo.INVALIDATE_MSG;
8719 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008720 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008721 }
8722 }
8723
8724 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008725 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8726 * through the event loop. Waits for the specified amount of time.</p>
8727 *
8728 * <p>This method can be invoked from outside of the UI thread
8729 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008730 *
8731 * @param delayMilliseconds the duration in milliseconds to delay the
8732 * invalidation by
8733 * @param left The left coordinate of the rectangle to invalidate.
8734 * @param top The top coordinate of the rectangle to invalidate.
8735 * @param right The right coordinate of the rectangle to invalidate.
8736 * @param bottom The bottom coordinate of the rectangle to invalidate.
8737 */
8738 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8739 int right, int bottom) {
8740
8741 // We try only with the AttachInfo because there's no point in invalidating
8742 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008743 AttachInfo attachInfo = mAttachInfo;
8744 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008745 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8746 info.target = this;
8747 info.left = left;
8748 info.top = top;
8749 info.right = right;
8750 info.bottom = bottom;
8751
8752 final Message msg = Message.obtain();
8753 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8754 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008755 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008756 }
8757 }
8758
8759 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008760 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8761 * This event is sent at most once every
8762 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8763 */
8764 private void postSendViewScrolledAccessibilityEventCallback() {
8765 if (mSendViewScrolledAccessibilityEvent == null) {
8766 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8767 }
8768 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8769 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8770 postDelayed(mSendViewScrolledAccessibilityEvent,
8771 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8772 }
8773 }
8774
8775 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008776 * Called by a parent to request that a child update its values for mScrollX
8777 * and mScrollY if necessary. This will typically be done if the child is
8778 * animating a scroll using a {@link android.widget.Scroller Scroller}
8779 * object.
8780 */
8781 public void computeScroll() {
8782 }
8783
8784 /**
8785 * <p>Indicate whether the horizontal edges are faded when the view is
8786 * scrolled horizontally.</p>
8787 *
8788 * @return true if the horizontal edges should are faded on scroll, false
8789 * otherwise
8790 *
8791 * @see #setHorizontalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008792 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008793 */
8794 public boolean isHorizontalFadingEdgeEnabled() {
8795 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8796 }
8797
8798 /**
8799 * <p>Define whether the horizontal edges should be faded when this view
8800 * is scrolled horizontally.</p>
8801 *
8802 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8803 * be faded when the view is scrolled
8804 * horizontally
8805 *
8806 * @see #isHorizontalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008807 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008808 */
8809 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8810 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8811 if (horizontalFadingEdgeEnabled) {
8812 initScrollCache();
8813 }
8814
8815 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8816 }
8817 }
8818
8819 /**
8820 * <p>Indicate whether the vertical edges are faded when the view is
8821 * scrolled horizontally.</p>
8822 *
8823 * @return true if the vertical edges should are faded on scroll, false
8824 * otherwise
8825 *
8826 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008827 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008828 */
8829 public boolean isVerticalFadingEdgeEnabled() {
8830 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8831 }
8832
8833 /**
8834 * <p>Define whether the vertical edges should be faded when this view
8835 * is scrolled vertically.</p>
8836 *
8837 * @param verticalFadingEdgeEnabled true if the vertical edges should
8838 * be faded when the view is scrolled
8839 * vertically
8840 *
8841 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008842 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008843 */
8844 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8845 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8846 if (verticalFadingEdgeEnabled) {
8847 initScrollCache();
8848 }
8849
8850 mViewFlags ^= FADING_EDGE_VERTICAL;
8851 }
8852 }
8853
8854 /**
8855 * Returns the strength, or intensity, of the top faded edge. The strength is
8856 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8857 * returns 0.0 or 1.0 but no value in between.
8858 *
8859 * Subclasses should override this method to provide a smoother fade transition
8860 * when scrolling occurs.
8861 *
8862 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8863 */
8864 protected float getTopFadingEdgeStrength() {
8865 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8866 }
8867
8868 /**
8869 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8870 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8871 * returns 0.0 or 1.0 but no value in between.
8872 *
8873 * Subclasses should override this method to provide a smoother fade transition
8874 * when scrolling occurs.
8875 *
8876 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8877 */
8878 protected float getBottomFadingEdgeStrength() {
8879 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8880 computeVerticalScrollRange() ? 1.0f : 0.0f;
8881 }
8882
8883 /**
8884 * Returns the strength, or intensity, of the left faded edge. The strength is
8885 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8886 * returns 0.0 or 1.0 but no value in between.
8887 *
8888 * Subclasses should override this method to provide a smoother fade transition
8889 * when scrolling occurs.
8890 *
8891 * @return the intensity of the left fade as a float between 0.0f and 1.0f
8892 */
8893 protected float getLeftFadingEdgeStrength() {
8894 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
8895 }
8896
8897 /**
8898 * Returns the strength, or intensity, of the right faded edge. The strength is
8899 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8900 * returns 0.0 or 1.0 but no value in between.
8901 *
8902 * Subclasses should override this method to provide a smoother fade transition
8903 * when scrolling occurs.
8904 *
8905 * @return the intensity of the right fade as a float between 0.0f and 1.0f
8906 */
8907 protected float getRightFadingEdgeStrength() {
8908 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
8909 computeHorizontalScrollRange() ? 1.0f : 0.0f;
8910 }
8911
8912 /**
8913 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
8914 * scrollbar is not drawn by default.</p>
8915 *
8916 * @return true if the horizontal scrollbar should be painted, false
8917 * otherwise
8918 *
8919 * @see #setHorizontalScrollBarEnabled(boolean)
8920 */
8921 public boolean isHorizontalScrollBarEnabled() {
8922 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8923 }
8924
8925 /**
8926 * <p>Define whether the horizontal scrollbar should be drawn or not. The
8927 * scrollbar is not drawn by default.</p>
8928 *
8929 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
8930 * be painted
8931 *
8932 * @see #isHorizontalScrollBarEnabled()
8933 */
8934 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
8935 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
8936 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008937 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008938 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008939 }
8940 }
8941
8942 /**
8943 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
8944 * scrollbar is not drawn by default.</p>
8945 *
8946 * @return true if the vertical scrollbar should be painted, false
8947 * otherwise
8948 *
8949 * @see #setVerticalScrollBarEnabled(boolean)
8950 */
8951 public boolean isVerticalScrollBarEnabled() {
8952 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
8953 }
8954
8955 /**
8956 * <p>Define whether the vertical scrollbar should be drawn or not. The
8957 * scrollbar is not drawn by default.</p>
8958 *
8959 * @param verticalScrollBarEnabled true if the vertical scrollbar should
8960 * be painted
8961 *
8962 * @see #isVerticalScrollBarEnabled()
8963 */
8964 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
8965 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
8966 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008967 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008968 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008969 }
8970 }
8971
Adam Powell20232d02010-12-08 21:08:53 -08008972 /**
8973 * @hide
8974 */
8975 protected void recomputePadding() {
8976 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008977 }
Joe Malin32736f02011-01-19 16:14:20 -08008978
Mike Cleronfe81d382009-09-28 14:22:16 -07008979 /**
8980 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08008981 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008982 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08008983 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008984 */
8985 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
8986 initScrollCache();
8987 final ScrollabilityCache scrollabilityCache = mScrollCache;
8988 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008989 if (fadeScrollbars) {
8990 scrollabilityCache.state = ScrollabilityCache.OFF;
8991 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07008992 scrollabilityCache.state = ScrollabilityCache.ON;
8993 }
8994 }
Joe Malin32736f02011-01-19 16:14:20 -08008995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008996 /**
Joe Malin32736f02011-01-19 16:14:20 -08008997 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008998 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08008999 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009000 * @return true if scrollbar fading is enabled
9001 */
9002 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009003 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009004 }
Joe Malin32736f02011-01-19 16:14:20 -08009005
Mike Cleron52f0a642009-09-28 18:21:37 -07009006 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009007 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9008 * inset. When inset, they add to the padding of the view. And the scrollbars
9009 * can be drawn inside the padding area or on the edge of the view. For example,
9010 * if a view has a background drawable and you want to draw the scrollbars
9011 * inside the padding specified by the drawable, you can use
9012 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9013 * appear at the edge of the view, ignoring the padding, then you can use
9014 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9015 * @param style the style of the scrollbars. Should be one of
9016 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9017 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9018 * @see #SCROLLBARS_INSIDE_OVERLAY
9019 * @see #SCROLLBARS_INSIDE_INSET
9020 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9021 * @see #SCROLLBARS_OUTSIDE_INSET
9022 */
9023 public void setScrollBarStyle(int style) {
9024 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9025 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009026 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009027 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009028 }
9029 }
9030
9031 /**
9032 * <p>Returns the current scrollbar style.</p>
9033 * @return the current scrollbar style
9034 * @see #SCROLLBARS_INSIDE_OVERLAY
9035 * @see #SCROLLBARS_INSIDE_INSET
9036 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9037 * @see #SCROLLBARS_OUTSIDE_INSET
9038 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009039 @ViewDebug.ExportedProperty(mapping = {
9040 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9041 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9042 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9043 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9044 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009045 public int getScrollBarStyle() {
9046 return mViewFlags & SCROLLBARS_STYLE_MASK;
9047 }
9048
9049 /**
9050 * <p>Compute the horizontal range that the horizontal scrollbar
9051 * represents.</p>
9052 *
9053 * <p>The range is expressed in arbitrary units that must be the same as the
9054 * units used by {@link #computeHorizontalScrollExtent()} and
9055 * {@link #computeHorizontalScrollOffset()}.</p>
9056 *
9057 * <p>The default range is the drawing width of this view.</p>
9058 *
9059 * @return the total horizontal range represented by the horizontal
9060 * scrollbar
9061 *
9062 * @see #computeHorizontalScrollExtent()
9063 * @see #computeHorizontalScrollOffset()
9064 * @see android.widget.ScrollBarDrawable
9065 */
9066 protected int computeHorizontalScrollRange() {
9067 return getWidth();
9068 }
9069
9070 /**
9071 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9072 * within the horizontal range. This value is used to compute the position
9073 * of the thumb within the scrollbar's track.</p>
9074 *
9075 * <p>The range is expressed in arbitrary units that must be the same as the
9076 * units used by {@link #computeHorizontalScrollRange()} and
9077 * {@link #computeHorizontalScrollExtent()}.</p>
9078 *
9079 * <p>The default offset is the scroll offset of this view.</p>
9080 *
9081 * @return the horizontal offset of the scrollbar's thumb
9082 *
9083 * @see #computeHorizontalScrollRange()
9084 * @see #computeHorizontalScrollExtent()
9085 * @see android.widget.ScrollBarDrawable
9086 */
9087 protected int computeHorizontalScrollOffset() {
9088 return mScrollX;
9089 }
9090
9091 /**
9092 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9093 * within the horizontal range. This value is used to compute the length
9094 * of the thumb within the scrollbar's track.</p>
9095 *
9096 * <p>The range is expressed in arbitrary units that must be the same as the
9097 * units used by {@link #computeHorizontalScrollRange()} and
9098 * {@link #computeHorizontalScrollOffset()}.</p>
9099 *
9100 * <p>The default extent is the drawing width of this view.</p>
9101 *
9102 * @return the horizontal extent of the scrollbar's thumb
9103 *
9104 * @see #computeHorizontalScrollRange()
9105 * @see #computeHorizontalScrollOffset()
9106 * @see android.widget.ScrollBarDrawable
9107 */
9108 protected int computeHorizontalScrollExtent() {
9109 return getWidth();
9110 }
9111
9112 /**
9113 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9114 *
9115 * <p>The range is expressed in arbitrary units that must be the same as the
9116 * units used by {@link #computeVerticalScrollExtent()} and
9117 * {@link #computeVerticalScrollOffset()}.</p>
9118 *
9119 * @return the total vertical range represented by the vertical scrollbar
9120 *
9121 * <p>The default range is the drawing height of this view.</p>
9122 *
9123 * @see #computeVerticalScrollExtent()
9124 * @see #computeVerticalScrollOffset()
9125 * @see android.widget.ScrollBarDrawable
9126 */
9127 protected int computeVerticalScrollRange() {
9128 return getHeight();
9129 }
9130
9131 /**
9132 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9133 * within the horizontal range. This value is used to compute the position
9134 * of the thumb within the scrollbar's track.</p>
9135 *
9136 * <p>The range is expressed in arbitrary units that must be the same as the
9137 * units used by {@link #computeVerticalScrollRange()} and
9138 * {@link #computeVerticalScrollExtent()}.</p>
9139 *
9140 * <p>The default offset is the scroll offset of this view.</p>
9141 *
9142 * @return the vertical offset of the scrollbar's thumb
9143 *
9144 * @see #computeVerticalScrollRange()
9145 * @see #computeVerticalScrollExtent()
9146 * @see android.widget.ScrollBarDrawable
9147 */
9148 protected int computeVerticalScrollOffset() {
9149 return mScrollY;
9150 }
9151
9152 /**
9153 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9154 * within the vertical range. This value is used to compute the length
9155 * of the thumb within the scrollbar's track.</p>
9156 *
9157 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009158 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009159 * {@link #computeVerticalScrollOffset()}.</p>
9160 *
9161 * <p>The default extent is the drawing height of this view.</p>
9162 *
9163 * @return the vertical extent of the scrollbar's thumb
9164 *
9165 * @see #computeVerticalScrollRange()
9166 * @see #computeVerticalScrollOffset()
9167 * @see android.widget.ScrollBarDrawable
9168 */
9169 protected int computeVerticalScrollExtent() {
9170 return getHeight();
9171 }
9172
9173 /**
Adam Powell69159442011-06-13 17:53:06 -07009174 * Check if this view can be scrolled horizontally in a certain direction.
9175 *
9176 * @param direction Negative to check scrolling left, positive to check scrolling right.
9177 * @return true if this view can be scrolled in the specified direction, false otherwise.
9178 */
9179 public boolean canScrollHorizontally(int direction) {
9180 final int offset = computeHorizontalScrollOffset();
9181 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9182 if (range == 0) return false;
9183 if (direction < 0) {
9184 return offset > 0;
9185 } else {
9186 return offset < range - 1;
9187 }
9188 }
9189
9190 /**
9191 * Check if this view can be scrolled vertically in a certain direction.
9192 *
9193 * @param direction Negative to check scrolling up, positive to check scrolling down.
9194 * @return true if this view can be scrolled in the specified direction, false otherwise.
9195 */
9196 public boolean canScrollVertically(int direction) {
9197 final int offset = computeVerticalScrollOffset();
9198 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9199 if (range == 0) return false;
9200 if (direction < 0) {
9201 return offset > 0;
9202 } else {
9203 return offset < range - 1;
9204 }
9205 }
9206
9207 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009208 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9209 * scrollbars are painted only if they have been awakened first.</p>
9210 *
9211 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009212 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009213 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009214 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009215 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009216 // scrollbars are drawn only when the animation is running
9217 final ScrollabilityCache cache = mScrollCache;
9218 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009219
Mike Cleronf116bf82009-09-27 19:14:12 -07009220 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009221
Mike Cleronf116bf82009-09-27 19:14:12 -07009222 if (state == ScrollabilityCache.OFF) {
9223 return;
9224 }
Joe Malin32736f02011-01-19 16:14:20 -08009225
Mike Cleronf116bf82009-09-27 19:14:12 -07009226 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009227
Mike Cleronf116bf82009-09-27 19:14:12 -07009228 if (state == ScrollabilityCache.FADING) {
9229 // We're fading -- get our fade interpolation
9230 if (cache.interpolatorValues == null) {
9231 cache.interpolatorValues = new float[1];
9232 }
Joe Malin32736f02011-01-19 16:14:20 -08009233
Mike Cleronf116bf82009-09-27 19:14:12 -07009234 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009235
Mike Cleronf116bf82009-09-27 19:14:12 -07009236 // Stops the animation if we're done
9237 if (cache.scrollBarInterpolator.timeToValues(values) ==
9238 Interpolator.Result.FREEZE_END) {
9239 cache.state = ScrollabilityCache.OFF;
9240 } else {
9241 cache.scrollBar.setAlpha(Math.round(values[0]));
9242 }
Joe Malin32736f02011-01-19 16:14:20 -08009243
9244 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009245 // drawing. We only want this when we're fading so that
9246 // we prevent excessive redraws
9247 invalidate = true;
9248 } else {
9249 // We're just on -- but we may have been fading before so
9250 // reset alpha
9251 cache.scrollBar.setAlpha(255);
9252 }
9253
Joe Malin32736f02011-01-19 16:14:20 -08009254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009255 final int viewFlags = mViewFlags;
9256
9257 final boolean drawHorizontalScrollBar =
9258 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9259 final boolean drawVerticalScrollBar =
9260 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9261 && !isVerticalScrollBarHidden();
9262
9263 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9264 final int width = mRight - mLeft;
9265 final int height = mBottom - mTop;
9266
9267 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009268
Mike Reede8853fc2009-09-04 14:01:48 -04009269 final int scrollX = mScrollX;
9270 final int scrollY = mScrollY;
9271 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9272
Mike Cleronf116bf82009-09-27 19:14:12 -07009273 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009275 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009276 int size = scrollBar.getSize(false);
9277 if (size <= 0) {
9278 size = cache.scrollBarSize;
9279 }
9280
Mike Cleronf116bf82009-09-27 19:14:12 -07009281 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009282 computeHorizontalScrollOffset(),
9283 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009284 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009285 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009286 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009287 left = scrollX + (mPaddingLeft & inside);
9288 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9289 bottom = top + size;
9290 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9291 if (invalidate) {
9292 invalidate(left, top, right, bottom);
9293 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009294 }
9295
9296 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009297 int size = scrollBar.getSize(true);
9298 if (size <= 0) {
9299 size = cache.scrollBarSize;
9300 }
9301
Mike Reede8853fc2009-09-04 14:01:48 -04009302 scrollBar.setParameters(computeVerticalScrollRange(),
9303 computeVerticalScrollOffset(),
9304 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009305 switch (mVerticalScrollbarPosition) {
9306 default:
9307 case SCROLLBAR_POSITION_DEFAULT:
9308 case SCROLLBAR_POSITION_RIGHT:
9309 left = scrollX + width - size - (mUserPaddingRight & inside);
9310 break;
9311 case SCROLLBAR_POSITION_LEFT:
9312 left = scrollX + (mUserPaddingLeft & inside);
9313 break;
9314 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009315 top = scrollY + (mPaddingTop & inside);
9316 right = left + size;
9317 bottom = scrollY + height - (mUserPaddingBottom & inside);
9318 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9319 if (invalidate) {
9320 invalidate(left, top, right, bottom);
9321 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009322 }
9323 }
9324 }
9325 }
Romain Guy8506ab42009-06-11 17:35:47 -07009326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009327 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009328 * 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 -08009329 * FastScroller is visible.
9330 * @return whether to temporarily hide the vertical scrollbar
9331 * @hide
9332 */
9333 protected boolean isVerticalScrollBarHidden() {
9334 return false;
9335 }
9336
9337 /**
9338 * <p>Draw the horizontal scrollbar if
9339 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9340 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009341 * @param canvas the canvas on which to draw the scrollbar
9342 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009343 *
9344 * @see #isHorizontalScrollBarEnabled()
9345 * @see #computeHorizontalScrollRange()
9346 * @see #computeHorizontalScrollExtent()
9347 * @see #computeHorizontalScrollOffset()
9348 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009349 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009350 */
Romain Guy8fb95422010-08-17 18:38:51 -07009351 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9352 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009353 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009354 scrollBar.draw(canvas);
9355 }
Mike Reede8853fc2009-09-04 14:01:48 -04009356
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009357 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009358 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9359 * returns true.</p>
9360 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009361 * @param canvas the canvas on which to draw the scrollbar
9362 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009363 *
9364 * @see #isVerticalScrollBarEnabled()
9365 * @see #computeVerticalScrollRange()
9366 * @see #computeVerticalScrollExtent()
9367 * @see #computeVerticalScrollOffset()
9368 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009369 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009370 */
Romain Guy8fb95422010-08-17 18:38:51 -07009371 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9372 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009373 scrollBar.setBounds(l, t, r, b);
9374 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009375 }
9376
9377 /**
9378 * Implement this to do your drawing.
9379 *
9380 * @param canvas the canvas on which the background will be drawn
9381 */
9382 protected void onDraw(Canvas canvas) {
9383 }
9384
9385 /*
9386 * Caller is responsible for calling requestLayout if necessary.
9387 * (This allows addViewInLayout to not request a new layout.)
9388 */
9389 void assignParent(ViewParent parent) {
9390 if (mParent == null) {
9391 mParent = parent;
9392 } else if (parent == null) {
9393 mParent = null;
9394 } else {
9395 throw new RuntimeException("view " + this + " being added, but"
9396 + " it already has a parent");
9397 }
9398 }
9399
9400 /**
9401 * This is called when the view is attached to a window. At this point it
9402 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009403 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9404 * however it may be called any time before the first onDraw -- including
9405 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009406 *
9407 * @see #onDetachedFromWindow()
9408 */
9409 protected void onAttachedToWindow() {
9410 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9411 mParent.requestTransparentRegion(this);
9412 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009413 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9414 initialAwakenScrollBars();
9415 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9416 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009417 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009418 // Order is important here: LayoutDirection MUST be resolved before Padding
9419 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009420 resolveLayoutDirectionIfNeeded();
9421 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009422 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009423 if (isFocused()) {
9424 InputMethodManager imm = InputMethodManager.peekInstance();
9425 imm.focusIn(this);
9426 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009427 }
Cibu Johny86666632010-02-22 13:01:02 -08009428
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009429 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009430 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9431 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009432 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009433 private void resolveLayoutDirectionIfNeeded() {
9434 // Do not resolve if it is not needed
9435 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9436
9437 // Clear any previous layout direction resolution
9438 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9439
Fabrice Di Meglio4b60c302011-08-17 16:56:55 -07009440 // Reset also TextDirection as a change into LayoutDirection may impact the selected
9441 // TextDirectionHeuristic
9442 resetResolvedTextDirection();
9443
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009444 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009445 switch (getLayoutDirection()) {
9446 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009447 // We cannot do the resolution if there is no parent
9448 if (mParent == null) return;
9449
Cibu Johny86666632010-02-22 13:01:02 -08009450 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009451 if (mParent instanceof ViewGroup) {
9452 ViewGroup viewGroup = ((ViewGroup) mParent);
9453
9454 // Check if the parent view group can resolve
9455 if (! viewGroup.canResolveLayoutDirection()) {
9456 return;
9457 }
9458 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9459 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9460 }
Cibu Johny86666632010-02-22 13:01:02 -08009461 }
9462 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009463 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009464 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009465 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009466 case LAYOUT_DIRECTION_LOCALE:
9467 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009468 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009469 }
9470 break;
9471 default:
9472 // Nothing to do, LTR by default
9473 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009474
9475 // Set to resolved
9476 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
9477 }
9478
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009479 /**
9480 * @hide
9481 */
9482 protected void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009483 // If the user specified the absolute padding (either with android:padding or
9484 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9485 // use the default padding or the padding from the background drawable
9486 // (stored at this point in mPadding*)
9487 switch (getResolvedLayoutDirection()) {
9488 case LAYOUT_DIRECTION_RTL:
9489 // Start user padding override Right user padding. Otherwise, if Right user
9490 // padding is not defined, use the default Right padding. If Right user padding
9491 // is defined, just use it.
9492 if (mUserPaddingStart >= 0) {
9493 mUserPaddingRight = mUserPaddingStart;
9494 } else if (mUserPaddingRight < 0) {
9495 mUserPaddingRight = mPaddingRight;
9496 }
9497 if (mUserPaddingEnd >= 0) {
9498 mUserPaddingLeft = mUserPaddingEnd;
9499 } else if (mUserPaddingLeft < 0) {
9500 mUserPaddingLeft = mPaddingLeft;
9501 }
9502 break;
9503 case LAYOUT_DIRECTION_LTR:
9504 default:
9505 // Start user padding override Left user padding. Otherwise, if Left user
9506 // padding is not defined, use the default left padding. If Left user padding
9507 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009508 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009509 mUserPaddingLeft = mUserPaddingStart;
9510 } else if (mUserPaddingLeft < 0) {
9511 mUserPaddingLeft = mPaddingLeft;
9512 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009513 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009514 mUserPaddingRight = mUserPaddingEnd;
9515 } else if (mUserPaddingRight < 0) {
9516 mUserPaddingRight = mPaddingRight;
9517 }
9518 }
9519
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009520 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9521
9522 recomputePadding();
9523 }
9524
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009525 /**
9526 * Return true if layout direction resolution can be done
9527 *
9528 * @hide
9529 */
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009530 protected boolean canResolveLayoutDirection() {
9531 switch (getLayoutDirection()) {
9532 case LAYOUT_DIRECTION_INHERIT:
9533 return (mParent != null);
9534 default:
9535 return true;
9536 }
9537 }
9538
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009539 /**
Doug Feltc0ccf0c2011-06-23 16:13:18 -07009540 * Reset the resolved layout direction.
9541 *
9542 * Subclasses need to override this method to clear cached information that depends on the
9543 * resolved layout direction, or to inform child views that inherit their layout direction.
9544 * Overrides must also call the superclass implementation at the start of their implementation.
9545 *
9546 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009547 */
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009548 protected void resetResolvedLayoutDirection() {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07009549 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009550 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009551 }
9552
9553 /**
9554 * Check if a Locale is corresponding to a RTL script.
9555 *
9556 * @param locale Locale to check
9557 * @return true if a Locale is corresponding to a RTL script.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009558 *
9559 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009560 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009561 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07009562 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
9563 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009564 }
9565
9566 /**
9567 * This is called when the view is detached from a window. At this point it
9568 * no longer has a surface for drawing.
9569 *
9570 * @see #onAttachedToWindow()
9571 */
9572 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009573 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009574
Romain Guya440b002010-02-24 15:57:54 -08009575 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009576 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009577 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009578 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009580 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009581
Romain Guy6d7475d2011-07-27 16:28:21 -07009582 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009583
Chet Haasedaf98e92011-01-10 14:10:36 -08009584 if (mDisplayList != null) {
9585 mDisplayList.invalidate();
9586 }
9587
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009588 if (mAttachInfo != null) {
9589 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009590 }
9591
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009592 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009593
9594 resetResolvedLayoutDirection();
9595 resetResolvedTextDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009596 }
9597
9598 /**
9599 * @return The number of times this view has been attached to a window
9600 */
9601 protected int getWindowAttachCount() {
9602 return mWindowAttachCount;
9603 }
9604
9605 /**
9606 * Retrieve a unique token identifying the window this view is attached to.
9607 * @return Return the window's token for use in
9608 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9609 */
9610 public IBinder getWindowToken() {
9611 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9612 }
9613
9614 /**
9615 * Retrieve a unique token identifying the top-level "real" window of
9616 * the window that this view is attached to. That is, this is like
9617 * {@link #getWindowToken}, except if the window this view in is a panel
9618 * window (attached to another containing window), then the token of
9619 * the containing window is returned instead.
9620 *
9621 * @return Returns the associated window token, either
9622 * {@link #getWindowToken()} or the containing window's token.
9623 */
9624 public IBinder getApplicationWindowToken() {
9625 AttachInfo ai = mAttachInfo;
9626 if (ai != null) {
9627 IBinder appWindowToken = ai.mPanelParentWindowToken;
9628 if (appWindowToken == null) {
9629 appWindowToken = ai.mWindowToken;
9630 }
9631 return appWindowToken;
9632 }
9633 return null;
9634 }
9635
9636 /**
9637 * Retrieve private session object this view hierarchy is using to
9638 * communicate with the window manager.
9639 * @return the session object to communicate with the window manager
9640 */
9641 /*package*/ IWindowSession getWindowSession() {
9642 return mAttachInfo != null ? mAttachInfo.mSession : null;
9643 }
9644
9645 /**
9646 * @param info the {@link android.view.View.AttachInfo} to associated with
9647 * this view
9648 */
9649 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9650 //System.out.println("Attached! " + this);
9651 mAttachInfo = info;
9652 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009653 // We will need to evaluate the drawable state at least once.
9654 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009655 if (mFloatingTreeObserver != null) {
9656 info.mTreeObserver.merge(mFloatingTreeObserver);
9657 mFloatingTreeObserver = null;
9658 }
9659 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9660 mAttachInfo.mScrollContainers.add(this);
9661 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9662 }
9663 performCollectViewAttributes(visibility);
9664 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009665
9666 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9667 mOnAttachStateChangeListeners;
9668 if (listeners != null && listeners.size() > 0) {
9669 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9670 // perform the dispatching. The iterator is a safe guard against listeners that
9671 // could mutate the list by calling the various add/remove methods. This prevents
9672 // the array from being modified while we iterate it.
9673 for (OnAttachStateChangeListener listener : listeners) {
9674 listener.onViewAttachedToWindow(this);
9675 }
9676 }
9677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009678 int vis = info.mWindowVisibility;
9679 if (vis != GONE) {
9680 onWindowVisibilityChanged(vis);
9681 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009682 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9683 // If nobody has evaluated the drawable state yet, then do it now.
9684 refreshDrawableState();
9685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009686 }
9687
9688 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009689 AttachInfo info = mAttachInfo;
9690 if (info != null) {
9691 int vis = info.mWindowVisibility;
9692 if (vis != GONE) {
9693 onWindowVisibilityChanged(GONE);
9694 }
9695 }
9696
9697 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009698
Adam Powell4afd62b2011-02-18 15:02:18 -08009699 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9700 mOnAttachStateChangeListeners;
9701 if (listeners != null && listeners.size() > 0) {
9702 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9703 // perform the dispatching. The iterator is a safe guard against listeners that
9704 // could mutate the list by calling the various add/remove methods. This prevents
9705 // the array from being modified while we iterate it.
9706 for (OnAttachStateChangeListener listener : listeners) {
9707 listener.onViewDetachedFromWindow(this);
9708 }
9709 }
9710
Romain Guy01d5edc2011-01-28 11:28:53 -08009711 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009712 mAttachInfo.mScrollContainers.remove(this);
9713 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9714 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009716 mAttachInfo = null;
9717 }
9718
9719 /**
9720 * Store this view hierarchy's frozen state into the given container.
9721 *
9722 * @param container The SparseArray in which to save the view's state.
9723 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009724 * @see #restoreHierarchyState(android.util.SparseArray)
9725 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9726 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009727 */
9728 public void saveHierarchyState(SparseArray<Parcelable> container) {
9729 dispatchSaveInstanceState(container);
9730 }
9731
9732 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009733 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9734 * this view and its children. May be overridden to modify how freezing happens to a
9735 * 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 -08009736 *
9737 * @param container The SparseArray in which to save the view's state.
9738 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009739 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9740 * @see #saveHierarchyState(android.util.SparseArray)
9741 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009742 */
9743 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9744 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9745 mPrivateFlags &= ~SAVE_STATE_CALLED;
9746 Parcelable state = onSaveInstanceState();
9747 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9748 throw new IllegalStateException(
9749 "Derived class did not call super.onSaveInstanceState()");
9750 }
9751 if (state != null) {
9752 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9753 // + ": " + state);
9754 container.put(mID, state);
9755 }
9756 }
9757 }
9758
9759 /**
9760 * Hook allowing a view to generate a representation of its internal state
9761 * that can later be used to create a new instance with that same state.
9762 * This state should only contain information that is not persistent or can
9763 * not be reconstructed later. For example, you will never store your
9764 * current position on screen because that will be computed again when a
9765 * new instance of the view is placed in its view hierarchy.
9766 * <p>
9767 * Some examples of things you may store here: the current cursor position
9768 * in a text view (but usually not the text itself since that is stored in a
9769 * content provider or other persistent storage), the currently selected
9770 * item in a list view.
9771 *
9772 * @return Returns a Parcelable object containing the view's current dynamic
9773 * state, or null if there is nothing interesting to save. The
9774 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009775 * @see #onRestoreInstanceState(android.os.Parcelable)
9776 * @see #saveHierarchyState(android.util.SparseArray)
9777 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009778 * @see #setSaveEnabled(boolean)
9779 */
9780 protected Parcelable onSaveInstanceState() {
9781 mPrivateFlags |= SAVE_STATE_CALLED;
9782 return BaseSavedState.EMPTY_STATE;
9783 }
9784
9785 /**
9786 * Restore this view hierarchy's frozen state from the given container.
9787 *
9788 * @param container The SparseArray which holds previously frozen states.
9789 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009790 * @see #saveHierarchyState(android.util.SparseArray)
9791 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9792 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009793 */
9794 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9795 dispatchRestoreInstanceState(container);
9796 }
9797
9798 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009799 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9800 * state for this view and its children. May be overridden to modify how restoring
9801 * happens to a view's children; for example, some views may want to not store state
9802 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009803 *
9804 * @param container The SparseArray which holds previously saved state.
9805 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009806 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9807 * @see #restoreHierarchyState(android.util.SparseArray)
9808 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009809 */
9810 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9811 if (mID != NO_ID) {
9812 Parcelable state = container.get(mID);
9813 if (state != null) {
9814 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9815 // + ": " + state);
9816 mPrivateFlags &= ~SAVE_STATE_CALLED;
9817 onRestoreInstanceState(state);
9818 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9819 throw new IllegalStateException(
9820 "Derived class did not call super.onRestoreInstanceState()");
9821 }
9822 }
9823 }
9824 }
9825
9826 /**
9827 * Hook allowing a view to re-apply a representation of its internal state that had previously
9828 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9829 * null state.
9830 *
9831 * @param state The frozen state that had previously been returned by
9832 * {@link #onSaveInstanceState}.
9833 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009834 * @see #onSaveInstanceState()
9835 * @see #restoreHierarchyState(android.util.SparseArray)
9836 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009837 */
9838 protected void onRestoreInstanceState(Parcelable state) {
9839 mPrivateFlags |= SAVE_STATE_CALLED;
9840 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009841 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9842 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009843 + "when two views of different type have the same id in the same hierarchy. "
9844 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009845 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009846 }
9847 }
9848
9849 /**
9850 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9851 *
9852 * @return the drawing start time in milliseconds
9853 */
9854 public long getDrawingTime() {
9855 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9856 }
9857
9858 /**
9859 * <p>Enables or disables the duplication of the parent's state into this view. When
9860 * duplication is enabled, this view gets its drawable state from its parent rather
9861 * than from its own internal properties.</p>
9862 *
9863 * <p>Note: in the current implementation, setting this property to true after the
9864 * view was added to a ViewGroup might have no effect at all. This property should
9865 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
9866 *
9867 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
9868 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009869 *
Gilles Debunnefb817032011-01-13 13:52:49 -08009870 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
9871 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009872 *
9873 * @param enabled True to enable duplication of the parent's drawable state, false
9874 * to disable it.
9875 *
9876 * @see #getDrawableState()
9877 * @see #isDuplicateParentStateEnabled()
9878 */
9879 public void setDuplicateParentStateEnabled(boolean enabled) {
9880 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
9881 }
9882
9883 /**
9884 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
9885 *
9886 * @return True if this view's drawable state is duplicated from the parent,
9887 * false otherwise
9888 *
9889 * @see #getDrawableState()
9890 * @see #setDuplicateParentStateEnabled(boolean)
9891 */
9892 public boolean isDuplicateParentStateEnabled() {
9893 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
9894 }
9895
9896 /**
Romain Guy171c5922011-01-06 10:04:23 -08009897 * <p>Specifies the type of layer backing this view. The layer can be
9898 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
9899 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009900 *
Romain Guy171c5922011-01-06 10:04:23 -08009901 * <p>A layer is associated with an optional {@link android.graphics.Paint}
9902 * instance that controls how the layer is composed on screen. The following
9903 * properties of the paint are taken into account when composing the layer:</p>
9904 * <ul>
9905 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
9906 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
9907 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
9908 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08009909 *
Romain Guy171c5922011-01-06 10:04:23 -08009910 * <p>If this view has an alpha value set to < 1.0 by calling
9911 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
9912 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
9913 * equivalent to setting a hardware layer on this view and providing a paint with
9914 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08009915 *
Romain Guy171c5922011-01-06 10:04:23 -08009916 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
9917 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
9918 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009919 *
Romain Guy171c5922011-01-06 10:04:23 -08009920 * @param layerType The ype of layer to use with this view, must be one of
9921 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9922 * {@link #LAYER_TYPE_HARDWARE}
9923 * @param paint The paint used to compose the layer. This argument is optional
9924 * and can be null. It is ignored when the layer type is
9925 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08009926 *
9927 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08009928 * @see #LAYER_TYPE_NONE
9929 * @see #LAYER_TYPE_SOFTWARE
9930 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08009931 * @see #setAlpha(float)
9932 *
Romain Guy171c5922011-01-06 10:04:23 -08009933 * @attr ref android.R.styleable#View_layerType
9934 */
9935 public void setLayerType(int layerType, Paint paint) {
9936 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08009937 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08009938 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
9939 }
Chet Haasedaf98e92011-01-10 14:10:36 -08009940
Romain Guyd6cd5722011-01-17 14:42:41 -08009941 if (layerType == mLayerType) {
9942 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
9943 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009944 invalidateParentCaches();
9945 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08009946 }
9947 return;
9948 }
Romain Guy171c5922011-01-06 10:04:23 -08009949
9950 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08009951 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -07009952 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -07009953 destroyLayer();
Chet Haase6f33e812011-05-17 12:42:19 -07009954 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -08009955 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -07009956 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009957 break;
Romain Guy6c319ca2011-01-11 14:29:25 -08009958 default:
9959 break;
Romain Guy171c5922011-01-06 10:04:23 -08009960 }
9961
9962 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08009963 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
9964 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
9965 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08009966
Romain Guy0fd89bf2011-01-26 15:41:30 -08009967 invalidateParentCaches();
9968 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08009969 }
9970
9971 /**
9972 * Indicates what type of layer is currently associated with this view. By default
9973 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
9974 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
9975 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08009976 *
Romain Guy171c5922011-01-06 10:04:23 -08009977 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9978 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08009979 *
9980 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08009981 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08009982 * @see #LAYER_TYPE_NONE
9983 * @see #LAYER_TYPE_SOFTWARE
9984 * @see #LAYER_TYPE_HARDWARE
9985 */
9986 public int getLayerType() {
9987 return mLayerType;
9988 }
Joe Malin32736f02011-01-19 16:14:20 -08009989
Romain Guy6c319ca2011-01-11 14:29:25 -08009990 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08009991 * Forces this view's layer to be created and this view to be rendered
9992 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
9993 * invoking this method will have no effect.
9994 *
9995 * This method can for instance be used to render a view into its layer before
9996 * starting an animation. If this view is complex, rendering into the layer
9997 * before starting the animation will avoid skipping frames.
9998 *
9999 * @throws IllegalStateException If this view is not attached to a window
10000 *
10001 * @see #setLayerType(int, android.graphics.Paint)
10002 */
10003 public void buildLayer() {
10004 if (mLayerType == LAYER_TYPE_NONE) return;
10005
10006 if (mAttachInfo == null) {
10007 throw new IllegalStateException("This view must be attached to a window first");
10008 }
10009
10010 switch (mLayerType) {
10011 case LAYER_TYPE_HARDWARE:
10012 getHardwareLayer();
10013 break;
10014 case LAYER_TYPE_SOFTWARE:
10015 buildDrawingCache(true);
10016 break;
10017 }
10018 }
10019
10020 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010021 * <p>Returns a hardware layer that can be used to draw this view again
10022 * without executing its draw method.</p>
10023 *
10024 * @return A HardwareLayer ready to render, or null if an error occurred.
10025 */
Romain Guy5e7f7662011-01-24 22:35:56 -080010026 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010027 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10028 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010029 return null;
10030 }
10031
10032 final int width = mRight - mLeft;
10033 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010034
Romain Guy6c319ca2011-01-11 14:29:25 -080010035 if (width == 0 || height == 0) {
10036 return null;
10037 }
10038
10039 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10040 if (mHardwareLayer == null) {
10041 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10042 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -080010043 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010044 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10045 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -080010046 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010047 }
10048
Romain Guy59a12ca2011-06-09 17:48:21 -070010049 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -080010050 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
10051 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010052 try {
10053 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080010054 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -080010055 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010056
Chet Haase88172fe2011-03-07 17:36:33 -080010057 final int restoreCount = canvas.save();
10058
Romain Guy6c319ca2011-01-11 14:29:25 -080010059 computeScroll();
10060 canvas.translate(-mScrollX, -mScrollY);
10061
Romain Guy6c319ca2011-01-11 14:29:25 -080010062 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -080010063
Romain Guy6c319ca2011-01-11 14:29:25 -080010064 // Fast path for layouts with no backgrounds
10065 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10066 mPrivateFlags &= ~DIRTY_MASK;
10067 dispatchDraw(canvas);
10068 } else {
10069 draw(canvas);
10070 }
Joe Malin32736f02011-01-19 16:14:20 -080010071
Chet Haase88172fe2011-03-07 17:36:33 -080010072 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -080010073 } finally {
10074 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -080010075 mHardwareLayer.end(currentCanvas);
10076 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010077 }
10078 }
10079
10080 return mHardwareLayer;
10081 }
Romain Guy171c5922011-01-06 10:04:23 -080010082
Romain Guy65b345f2011-07-27 18:51:50 -070010083 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -070010084 if (mHardwareLayer != null) {
10085 mHardwareLayer.destroy();
10086 mHardwareLayer = null;
Romain Guy65b345f2011-07-27 18:51:50 -070010087 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010088 }
Romain Guy65b345f2011-07-27 18:51:50 -070010089 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010090 }
10091
Romain Guy171c5922011-01-06 10:04:23 -080010092 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010093 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10094 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10095 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10096 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10097 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10098 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010099 *
Romain Guy171c5922011-01-06 10:04:23 -080010100 * <p>Enabling the drawing cache is similar to
10101 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010102 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10103 * drawing cache has no effect on rendering because the system uses a different mechanism
10104 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10105 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10106 * for information on how to enable software and hardware layers.</p>
10107 *
10108 * <p>This API can be used to manually generate
10109 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10110 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010111 *
10112 * @param enabled true to enable the drawing cache, false otherwise
10113 *
10114 * @see #isDrawingCacheEnabled()
10115 * @see #getDrawingCache()
10116 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010117 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010118 */
10119 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010120 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010121 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10122 }
10123
10124 /**
10125 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10126 *
10127 * @return true if the drawing cache is enabled
10128 *
10129 * @see #setDrawingCacheEnabled(boolean)
10130 * @see #getDrawingCache()
10131 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010132 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010133 public boolean isDrawingCacheEnabled() {
10134 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10135 }
10136
10137 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010138 * Debugging utility which recursively outputs the dirty state of a view and its
10139 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010140 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010141 * @hide
10142 */
Romain Guy676b1732011-02-14 14:45:33 -080010143 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010144 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10145 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10146 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10147 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10148 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10149 if (clear) {
10150 mPrivateFlags &= clearMask;
10151 }
10152 if (this instanceof ViewGroup) {
10153 ViewGroup parent = (ViewGroup) this;
10154 final int count = parent.getChildCount();
10155 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010156 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010157 child.outputDirtyFlags(indent + " ", clear, clearMask);
10158 }
10159 }
10160 }
10161
10162 /**
10163 * This method is used by ViewGroup to cause its children to restore or recreate their
10164 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10165 * to recreate its own display list, which would happen if it went through the normal
10166 * draw/dispatchDraw mechanisms.
10167 *
10168 * @hide
10169 */
10170 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010171
10172 /**
10173 * A view that is not attached or hardware accelerated cannot create a display list.
10174 * This method checks these conditions and returns the appropriate result.
10175 *
10176 * @return true if view has the ability to create a display list, false otherwise.
10177 *
10178 * @hide
10179 */
10180 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010181 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010182 }
Joe Malin32736f02011-01-19 16:14:20 -080010183
Chet Haasedaf98e92011-01-10 14:10:36 -080010184 /**
Romain Guyb051e892010-09-28 19:09:36 -070010185 * <p>Returns a display list that can be used to draw this view again
10186 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010187 *
Romain Guyb051e892010-09-28 19:09:36 -070010188 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010189 *
10190 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010191 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010192 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -080010193 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -070010194 return null;
10195 }
10196
Chet Haasedaf98e92011-01-10 14:10:36 -080010197 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10198 mDisplayList == null || !mDisplayList.isValid() ||
10199 mRecreateDisplayList)) {
10200 // Don't need to recreate the display list, just need to tell our
10201 // children to restore/recreate theirs
10202 if (mDisplayList != null && mDisplayList.isValid() &&
10203 !mRecreateDisplayList) {
10204 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10205 mPrivateFlags &= ~DIRTY_MASK;
10206 dispatchGetDisplayList();
10207
10208 return mDisplayList;
10209 }
10210
10211 // If we got here, we're recreating it. Mark it as such to ensure that
10212 // we copy in child display lists into ours in drawChild()
10213 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -080010214 if (mDisplayList == null) {
Jeff Brown162a0212011-07-21 17:02:54 -070010215 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList();
Chet Haasedaf98e92011-01-10 14:10:36 -080010216 // If we're creating a new display list, make sure our parent gets invalidated
10217 // since they will need to recreate their display list to account for this
10218 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -080010219 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -080010220 }
Romain Guyb051e892010-09-28 19:09:36 -070010221
10222 final HardwareCanvas canvas = mDisplayList.start();
Romain Guye080af32011-09-08 15:03:39 -070010223 int restoreCount = 0;
Romain Guyb051e892010-09-28 19:09:36 -070010224 try {
10225 int width = mRight - mLeft;
10226 int height = mBottom - mTop;
10227
10228 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -080010229 // The dirty rect should always be null for a display list
10230 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -070010231
Chet Haasedaf98e92011-01-10 14:10:36 -080010232 computeScroll();
Romain Guye080af32011-09-08 15:03:39 -070010233
10234 restoreCount = canvas.save();
Chet Haasedaf98e92011-01-10 14:10:36 -080010235 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010236 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -070010237 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -080010238
Romain Guyb051e892010-09-28 19:09:36 -070010239 // Fast path for layouts with no backgrounds
10240 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -070010241 dispatchDraw(canvas);
10242 } else {
10243 draw(canvas);
10244 }
Romain Guyb051e892010-09-28 19:09:36 -070010245 } finally {
Romain Guye080af32011-09-08 15:03:39 -070010246 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -070010247 canvas.onPostDraw();
10248
10249 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -070010250 }
Chet Haase77785f92011-01-25 23:22:09 -080010251 } else {
10252 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10253 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -070010254 }
10255
10256 return mDisplayList;
10257 }
10258
10259 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010260 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010261 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010262 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010263 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010264 * @see #getDrawingCache(boolean)
10265 */
10266 public Bitmap getDrawingCache() {
10267 return getDrawingCache(false);
10268 }
10269
10270 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010271 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10272 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10273 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10274 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10275 * request the drawing cache by calling this method and draw it on screen if the
10276 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010277 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010278 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10279 * this method will create a bitmap of the same size as this view. Because this bitmap
10280 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10281 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10282 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10283 * size than the view. This implies that your application must be able to handle this
10284 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010285 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010286 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10287 * the current density of the screen when the application is in compatibility
10288 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010289 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010290 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010291 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010292 * @see #setDrawingCacheEnabled(boolean)
10293 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010294 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010295 * @see #destroyDrawingCache()
10296 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010297 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010298 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10299 return null;
10300 }
10301 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010302 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010303 }
Romain Guy02890fd2010-08-06 17:58:44 -070010304 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010305 }
10306
10307 /**
10308 * <p>Frees the resources used by the drawing cache. If you call
10309 * {@link #buildDrawingCache()} manually without calling
10310 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10311 * should cleanup the cache with this method afterwards.</p>
10312 *
10313 * @see #setDrawingCacheEnabled(boolean)
10314 * @see #buildDrawingCache()
10315 * @see #getDrawingCache()
10316 */
10317 public void destroyDrawingCache() {
10318 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010319 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010320 mDrawingCache = null;
10321 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010322 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010323 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010324 mUnscaledDrawingCache = null;
10325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010326 }
10327
10328 /**
10329 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070010330 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010331 * view will always be drawn on top of a solid color.
10332 *
10333 * @param color The background color to use for the drawing cache's bitmap
10334 *
10335 * @see #setDrawingCacheEnabled(boolean)
10336 * @see #buildDrawingCache()
10337 * @see #getDrawingCache()
10338 */
10339 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010340 if (color != mDrawingCacheBackgroundColor) {
10341 mDrawingCacheBackgroundColor = color;
10342 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10343 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010344 }
10345
10346 /**
10347 * @see #setDrawingCacheBackgroundColor(int)
10348 *
10349 * @return The background color to used for the drawing cache's bitmap
10350 */
10351 public int getDrawingCacheBackgroundColor() {
10352 return mDrawingCacheBackgroundColor;
10353 }
10354
10355 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010356 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010357 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010358 * @see #buildDrawingCache(boolean)
10359 */
10360 public void buildDrawingCache() {
10361 buildDrawingCache(false);
10362 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010363
Romain Guyfbd8f692009-06-26 14:51:58 -070010364 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010365 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10366 *
10367 * <p>If you call {@link #buildDrawingCache()} manually without calling
10368 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10369 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010370 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010371 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10372 * this method will create a bitmap of the same size as this view. Because this bitmap
10373 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10374 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10375 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10376 * size than the view. This implies that your application must be able to handle this
10377 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010378 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010379 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10380 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010381 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010382 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010383 *
10384 * @see #getDrawingCache()
10385 * @see #destroyDrawingCache()
10386 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010387 public void buildDrawingCache(boolean autoScale) {
10388 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010389 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010390 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010391
10392 if (ViewDebug.TRACE_HIERARCHY) {
10393 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010395
Romain Guy8506ab42009-06-11 17:35:47 -070010396 int width = mRight - mLeft;
10397 int height = mBottom - mTop;
10398
10399 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010400 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010401
Romain Guye1123222009-06-29 14:24:56 -070010402 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010403 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10404 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010405 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010406
10407 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010408 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010409 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010410
10411 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010412 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010413 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010414 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10415 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010416 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010417 return;
10418 }
10419
10420 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010421 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010422
10423 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010424 Bitmap.Config quality;
10425 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010426 // Never pick ARGB_4444 because it looks awful
10427 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010428 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10429 case DRAWING_CACHE_QUALITY_AUTO:
10430 quality = Bitmap.Config.ARGB_8888;
10431 break;
10432 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010433 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010434 break;
10435 case DRAWING_CACHE_QUALITY_HIGH:
10436 quality = Bitmap.Config.ARGB_8888;
10437 break;
10438 default:
10439 quality = Bitmap.Config.ARGB_8888;
10440 break;
10441 }
10442 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010443 // Optimization for translucent windows
10444 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010445 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010446 }
10447
10448 // Try to cleanup memory
10449 if (bitmap != null) bitmap.recycle();
10450
10451 try {
10452 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010453 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010454 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010455 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010456 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010457 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010458 }
Adam Powell26153a32010-11-08 15:22:27 -080010459 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010460 } catch (OutOfMemoryError e) {
10461 // If there is not enough memory to create the bitmap cache, just
10462 // ignore the issue as bitmap caches are not required to draw the
10463 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010464 if (autoScale) {
10465 mDrawingCache = null;
10466 } else {
10467 mUnscaledDrawingCache = null;
10468 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010469 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010470 return;
10471 }
10472
10473 clear = drawingCacheBackgroundColor != 0;
10474 }
10475
10476 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010477 if (attachInfo != null) {
10478 canvas = attachInfo.mCanvas;
10479 if (canvas == null) {
10480 canvas = new Canvas();
10481 }
10482 canvas.setBitmap(bitmap);
10483 // Temporarily clobber the cached Canvas in case one of our children
10484 // is also using a drawing cache. Without this, the children would
10485 // steal the canvas by attaching their own bitmap to it and bad, bad
10486 // thing would happen (invisible views, corrupted drawings, etc.)
10487 attachInfo.mCanvas = null;
10488 } else {
10489 // This case should hopefully never or seldom happen
10490 canvas = new Canvas(bitmap);
10491 }
10492
10493 if (clear) {
10494 bitmap.eraseColor(drawingCacheBackgroundColor);
10495 }
10496
10497 computeScroll();
10498 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010499
Romain Guye1123222009-06-29 14:24:56 -070010500 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010501 final float scale = attachInfo.mApplicationScale;
10502 canvas.scale(scale, scale);
10503 }
Joe Malin32736f02011-01-19 16:14:20 -080010504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010505 canvas.translate(-mScrollX, -mScrollY);
10506
Romain Guy5bcdff42009-05-14 21:27:18 -070010507 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010508 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10509 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010510 mPrivateFlags |= DRAWING_CACHE_VALID;
10511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010512
10513 // Fast path for layouts with no backgrounds
10514 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10515 if (ViewDebug.TRACE_HIERARCHY) {
10516 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10517 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010518 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010519 dispatchDraw(canvas);
10520 } else {
10521 draw(canvas);
10522 }
10523
10524 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010525 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010526
10527 if (attachInfo != null) {
10528 // Restore the cached Canvas for our siblings
10529 attachInfo.mCanvas = canvas;
10530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010531 }
10532 }
10533
10534 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010535 * Create a snapshot of the view into a bitmap. We should probably make
10536 * some form of this public, but should think about the API.
10537 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010538 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010539 int width = mRight - mLeft;
10540 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010541
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010542 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010543 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010544 width = (int) ((width * scale) + 0.5f);
10545 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010546
Romain Guy8c11e312009-09-14 15:15:30 -070010547 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010548 if (bitmap == null) {
10549 throw new OutOfMemoryError();
10550 }
10551
Romain Guyc529d8d2011-09-06 15:01:39 -070010552 Resources resources = getResources();
10553 if (resources != null) {
10554 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
10555 }
Joe Malin32736f02011-01-19 16:14:20 -080010556
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010557 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010558 if (attachInfo != null) {
10559 canvas = attachInfo.mCanvas;
10560 if (canvas == null) {
10561 canvas = new Canvas();
10562 }
10563 canvas.setBitmap(bitmap);
10564 // Temporarily clobber the cached Canvas in case one of our children
10565 // is also using a drawing cache. Without this, the children would
10566 // steal the canvas by attaching their own bitmap to it and bad, bad
10567 // things would happen (invisible views, corrupted drawings, etc.)
10568 attachInfo.mCanvas = null;
10569 } else {
10570 // This case should hopefully never or seldom happen
10571 canvas = new Canvas(bitmap);
10572 }
10573
Romain Guy5bcdff42009-05-14 21:27:18 -070010574 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010575 bitmap.eraseColor(backgroundColor);
10576 }
10577
10578 computeScroll();
10579 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010580 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010581 canvas.translate(-mScrollX, -mScrollY);
10582
Romain Guy5bcdff42009-05-14 21:27:18 -070010583 // Temporarily remove the dirty mask
10584 int flags = mPrivateFlags;
10585 mPrivateFlags &= ~DIRTY_MASK;
10586
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010587 // Fast path for layouts with no backgrounds
10588 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10589 dispatchDraw(canvas);
10590 } else {
10591 draw(canvas);
10592 }
10593
Romain Guy5bcdff42009-05-14 21:27:18 -070010594 mPrivateFlags = flags;
10595
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010596 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010597 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010598
10599 if (attachInfo != null) {
10600 // Restore the cached Canvas for our siblings
10601 attachInfo.mCanvas = canvas;
10602 }
Romain Guy8506ab42009-06-11 17:35:47 -070010603
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010604 return bitmap;
10605 }
10606
10607 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010608 * Indicates whether this View is currently in edit mode. A View is usually
10609 * in edit mode when displayed within a developer tool. For instance, if
10610 * this View is being drawn by a visual user interface builder, this method
10611 * should return true.
10612 *
10613 * Subclasses should check the return value of this method to provide
10614 * different behaviors if their normal behavior might interfere with the
10615 * host environment. For instance: the class spawns a thread in its
10616 * constructor, the drawing code relies on device-specific features, etc.
10617 *
10618 * This method is usually checked in the drawing code of custom widgets.
10619 *
10620 * @return True if this View is in edit mode, false otherwise.
10621 */
10622 public boolean isInEditMode() {
10623 return false;
10624 }
10625
10626 /**
10627 * If the View draws content inside its padding and enables fading edges,
10628 * it needs to support padding offsets. Padding offsets are added to the
10629 * fading edges to extend the length of the fade so that it covers pixels
10630 * drawn inside the padding.
10631 *
10632 * Subclasses of this class should override this method if they need
10633 * to draw content inside the padding.
10634 *
10635 * @return True if padding offset must be applied, false otherwise.
10636 *
10637 * @see #getLeftPaddingOffset()
10638 * @see #getRightPaddingOffset()
10639 * @see #getTopPaddingOffset()
10640 * @see #getBottomPaddingOffset()
10641 *
10642 * @since CURRENT
10643 */
10644 protected boolean isPaddingOffsetRequired() {
10645 return false;
10646 }
10647
10648 /**
10649 * Amount by which to extend the left fading region. Called only when
10650 * {@link #isPaddingOffsetRequired()} returns true.
10651 *
10652 * @return The left padding offset in pixels.
10653 *
10654 * @see #isPaddingOffsetRequired()
10655 *
10656 * @since CURRENT
10657 */
10658 protected int getLeftPaddingOffset() {
10659 return 0;
10660 }
10661
10662 /**
10663 * Amount by which to extend the right fading region. Called only when
10664 * {@link #isPaddingOffsetRequired()} returns true.
10665 *
10666 * @return The right padding offset in pixels.
10667 *
10668 * @see #isPaddingOffsetRequired()
10669 *
10670 * @since CURRENT
10671 */
10672 protected int getRightPaddingOffset() {
10673 return 0;
10674 }
10675
10676 /**
10677 * Amount by which to extend the top fading region. Called only when
10678 * {@link #isPaddingOffsetRequired()} returns true.
10679 *
10680 * @return The top padding offset in pixels.
10681 *
10682 * @see #isPaddingOffsetRequired()
10683 *
10684 * @since CURRENT
10685 */
10686 protected int getTopPaddingOffset() {
10687 return 0;
10688 }
10689
10690 /**
10691 * Amount by which to extend the bottom fading region. Called only when
10692 * {@link #isPaddingOffsetRequired()} returns true.
10693 *
10694 * @return The bottom padding offset in pixels.
10695 *
10696 * @see #isPaddingOffsetRequired()
10697 *
10698 * @since CURRENT
10699 */
10700 protected int getBottomPaddingOffset() {
10701 return 0;
10702 }
10703
10704 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070010705 * @hide
10706 * @param offsetRequired
10707 */
10708 protected int getFadeTop(boolean offsetRequired) {
10709 int top = mPaddingTop;
10710 if (offsetRequired) top += getTopPaddingOffset();
10711 return top;
10712 }
10713
10714 /**
10715 * @hide
10716 * @param offsetRequired
10717 */
10718 protected int getFadeHeight(boolean offsetRequired) {
10719 int padding = mPaddingTop;
10720 if (offsetRequired) padding += getTopPaddingOffset();
10721 return mBottom - mTop - mPaddingBottom - padding;
10722 }
10723
10724 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010725 * <p>Indicates whether this view is attached to an hardware accelerated
10726 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010727 *
Romain Guy2bffd262010-09-12 17:40:02 -070010728 * <p>Even if this method returns true, it does not mean that every call
10729 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10730 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10731 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10732 * window is hardware accelerated,
10733 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10734 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010735 *
Romain Guy2bffd262010-09-12 17:40:02 -070010736 * @return True if the view is attached to a window and the window is
10737 * hardware accelerated; false in any other case.
10738 */
10739 public boolean isHardwareAccelerated() {
10740 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10741 }
Joe Malin32736f02011-01-19 16:14:20 -080010742
Romain Guy2bffd262010-09-12 17:40:02 -070010743 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010744 * Manually render this view (and all of its children) to the given Canvas.
10745 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010746 * called. When implementing a view, implement
10747 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
10748 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010749 *
10750 * @param canvas The Canvas to which the View is rendered.
10751 */
10752 public void draw(Canvas canvas) {
10753 if (ViewDebug.TRACE_HIERARCHY) {
10754 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10755 }
10756
Romain Guy5bcdff42009-05-14 21:27:18 -070010757 final int privateFlags = mPrivateFlags;
10758 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
10759 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
10760 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070010761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010762 /*
10763 * Draw traversal performs several drawing steps which must be executed
10764 * in the appropriate order:
10765 *
10766 * 1. Draw the background
10767 * 2. If necessary, save the canvas' layers to prepare for fading
10768 * 3. Draw view's content
10769 * 4. Draw children
10770 * 5. If necessary, draw the fading edges and restore layers
10771 * 6. Draw decorations (scrollbars for instance)
10772 */
10773
10774 // Step 1, draw the background, if needed
10775 int saveCount;
10776
Romain Guy24443ea2009-05-11 11:56:30 -070010777 if (!dirtyOpaque) {
10778 final Drawable background = mBGDrawable;
10779 if (background != null) {
10780 final int scrollX = mScrollX;
10781 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010782
Romain Guy24443ea2009-05-11 11:56:30 -070010783 if (mBackgroundSizeChanged) {
10784 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
10785 mBackgroundSizeChanged = false;
10786 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010787
Romain Guy24443ea2009-05-11 11:56:30 -070010788 if ((scrollX | scrollY) == 0) {
10789 background.draw(canvas);
10790 } else {
10791 canvas.translate(scrollX, scrollY);
10792 background.draw(canvas);
10793 canvas.translate(-scrollX, -scrollY);
10794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010795 }
10796 }
10797
10798 // skip step 2 & 5 if possible (common case)
10799 final int viewFlags = mViewFlags;
10800 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
10801 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
10802 if (!verticalEdges && !horizontalEdges) {
10803 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010804 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010805
10806 // Step 4, draw the children
10807 dispatchDraw(canvas);
10808
10809 // Step 6, draw decorations (scrollbars)
10810 onDrawScrollBars(canvas);
10811
10812 // we're done...
10813 return;
10814 }
10815
10816 /*
10817 * Here we do the full fledged routine...
10818 * (this is an uncommon case where speed matters less,
10819 * this is why we repeat some of the tests that have been
10820 * done above)
10821 */
10822
10823 boolean drawTop = false;
10824 boolean drawBottom = false;
10825 boolean drawLeft = false;
10826 boolean drawRight = false;
10827
10828 float topFadeStrength = 0.0f;
10829 float bottomFadeStrength = 0.0f;
10830 float leftFadeStrength = 0.0f;
10831 float rightFadeStrength = 0.0f;
10832
10833 // Step 2, save the canvas' layers
10834 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010835
10836 final boolean offsetRequired = isPaddingOffsetRequired();
10837 if (offsetRequired) {
10838 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010839 }
10840
10841 int left = mScrollX + paddingLeft;
10842 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070010843 int top = mScrollY + getFadeTop(offsetRequired);
10844 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010845
10846 if (offsetRequired) {
10847 right += getRightPaddingOffset();
10848 bottom += getBottomPaddingOffset();
10849 }
10850
10851 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010852 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
10853 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010854
10855 // clip the fade length if top and bottom fades overlap
10856 // overlapping fades produce odd-looking artifacts
10857 if (verticalEdges && (top + length > bottom - length)) {
10858 length = (bottom - top) / 2;
10859 }
10860
10861 // also clip horizontal fades if necessary
10862 if (horizontalEdges && (left + length > right - length)) {
10863 length = (right - left) / 2;
10864 }
10865
10866 if (verticalEdges) {
10867 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010868 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010869 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010870 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010871 }
10872
10873 if (horizontalEdges) {
10874 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010875 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010876 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010877 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010878 }
10879
10880 saveCount = canvas.getSaveCount();
10881
10882 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070010883 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010884 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
10885
10886 if (drawTop) {
10887 canvas.saveLayer(left, top, right, top + length, null, flags);
10888 }
10889
10890 if (drawBottom) {
10891 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
10892 }
10893
10894 if (drawLeft) {
10895 canvas.saveLayer(left, top, left + length, bottom, null, flags);
10896 }
10897
10898 if (drawRight) {
10899 canvas.saveLayer(right - length, top, right, bottom, null, flags);
10900 }
10901 } else {
10902 scrollabilityCache.setFadeColor(solidColor);
10903 }
10904
10905 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010906 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010907
10908 // Step 4, draw the children
10909 dispatchDraw(canvas);
10910
10911 // Step 5, draw the fade effect and restore layers
10912 final Paint p = scrollabilityCache.paint;
10913 final Matrix matrix = scrollabilityCache.matrix;
10914 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010915
10916 if (drawTop) {
10917 matrix.setScale(1, fadeHeight * topFadeStrength);
10918 matrix.postTranslate(left, top);
10919 fade.setLocalMatrix(matrix);
10920 canvas.drawRect(left, top, right, top + length, p);
10921 }
10922
10923 if (drawBottom) {
10924 matrix.setScale(1, fadeHeight * bottomFadeStrength);
10925 matrix.postRotate(180);
10926 matrix.postTranslate(left, bottom);
10927 fade.setLocalMatrix(matrix);
10928 canvas.drawRect(left, bottom - length, right, bottom, p);
10929 }
10930
10931 if (drawLeft) {
10932 matrix.setScale(1, fadeHeight * leftFadeStrength);
10933 matrix.postRotate(-90);
10934 matrix.postTranslate(left, top);
10935 fade.setLocalMatrix(matrix);
10936 canvas.drawRect(left, top, left + length, bottom, p);
10937 }
10938
10939 if (drawRight) {
10940 matrix.setScale(1, fadeHeight * rightFadeStrength);
10941 matrix.postRotate(90);
10942 matrix.postTranslate(right, top);
10943 fade.setLocalMatrix(matrix);
10944 canvas.drawRect(right - length, top, right, bottom, p);
10945 }
10946
10947 canvas.restoreToCount(saveCount);
10948
10949 // Step 6, draw decorations (scrollbars)
10950 onDrawScrollBars(canvas);
10951 }
10952
10953 /**
10954 * Override this if your view is known to always be drawn on top of a solid color background,
10955 * and needs to draw fading edges. Returning a non-zero color enables the view system to
10956 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
10957 * should be set to 0xFF.
10958 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010959 * @see #setVerticalFadingEdgeEnabled(boolean)
10960 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010961 *
10962 * @return The known solid color background for this view, or 0 if the color may vary
10963 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010964 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010965 public int getSolidColor() {
10966 return 0;
10967 }
10968
10969 /**
10970 * Build a human readable string representation of the specified view flags.
10971 *
10972 * @param flags the view flags to convert to a string
10973 * @return a String representing the supplied flags
10974 */
10975 private static String printFlags(int flags) {
10976 String output = "";
10977 int numFlags = 0;
10978 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
10979 output += "TAKES_FOCUS";
10980 numFlags++;
10981 }
10982
10983 switch (flags & VISIBILITY_MASK) {
10984 case INVISIBLE:
10985 if (numFlags > 0) {
10986 output += " ";
10987 }
10988 output += "INVISIBLE";
10989 // USELESS HERE numFlags++;
10990 break;
10991 case GONE:
10992 if (numFlags > 0) {
10993 output += " ";
10994 }
10995 output += "GONE";
10996 // USELESS HERE numFlags++;
10997 break;
10998 default:
10999 break;
11000 }
11001 return output;
11002 }
11003
11004 /**
11005 * Build a human readable string representation of the specified private
11006 * view flags.
11007 *
11008 * @param privateFlags the private view flags to convert to a string
11009 * @return a String representing the supplied flags
11010 */
11011 private static String printPrivateFlags(int privateFlags) {
11012 String output = "";
11013 int numFlags = 0;
11014
11015 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
11016 output += "WANTS_FOCUS";
11017 numFlags++;
11018 }
11019
11020 if ((privateFlags & FOCUSED) == FOCUSED) {
11021 if (numFlags > 0) {
11022 output += " ";
11023 }
11024 output += "FOCUSED";
11025 numFlags++;
11026 }
11027
11028 if ((privateFlags & SELECTED) == SELECTED) {
11029 if (numFlags > 0) {
11030 output += " ";
11031 }
11032 output += "SELECTED";
11033 numFlags++;
11034 }
11035
11036 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
11037 if (numFlags > 0) {
11038 output += " ";
11039 }
11040 output += "IS_ROOT_NAMESPACE";
11041 numFlags++;
11042 }
11043
11044 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
11045 if (numFlags > 0) {
11046 output += " ";
11047 }
11048 output += "HAS_BOUNDS";
11049 numFlags++;
11050 }
11051
11052 if ((privateFlags & DRAWN) == DRAWN) {
11053 if (numFlags > 0) {
11054 output += " ";
11055 }
11056 output += "DRAWN";
11057 // USELESS HERE numFlags++;
11058 }
11059 return output;
11060 }
11061
11062 /**
11063 * <p>Indicates whether or not this view's layout will be requested during
11064 * the next hierarchy layout pass.</p>
11065 *
11066 * @return true if the layout will be forced during next layout pass
11067 */
11068 public boolean isLayoutRequested() {
11069 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
11070 }
11071
11072 /**
11073 * Assign a size and position to a view and all of its
11074 * descendants
11075 *
11076 * <p>This is the second phase of the layout mechanism.
11077 * (The first is measuring). In this phase, each parent calls
11078 * layout on all of its children to position them.
11079 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080011080 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011081 *
Chet Haase9c087442011-01-12 16:20:16 -080011082 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011083 * Derived classes with children should override
11084 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080011085 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011086 *
11087 * @param l Left position, relative to parent
11088 * @param t Top position, relative to parent
11089 * @param r Right position, relative to parent
11090 * @param b Bottom position, relative to parent
11091 */
Romain Guy5429e1d2010-09-07 12:38:00 -070011092 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080011093 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070011094 int oldL = mLeft;
11095 int oldT = mTop;
11096 int oldB = mBottom;
11097 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011098 boolean changed = setFrame(l, t, r, b);
11099 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
11100 if (ViewDebug.TRACE_HIERARCHY) {
11101 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
11102 }
11103
11104 onLayout(changed, l, t, r, b);
11105 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070011106
11107 if (mOnLayoutChangeListeners != null) {
11108 ArrayList<OnLayoutChangeListener> listenersCopy =
11109 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
11110 int numListeners = listenersCopy.size();
11111 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070011112 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070011113 }
11114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011115 }
11116 mPrivateFlags &= ~FORCE_LAYOUT;
11117 }
11118
11119 /**
11120 * Called from layout when this view should
11121 * assign a size and position to each of its children.
11122 *
11123 * Derived classes with children should override
11124 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070011125 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011126 * @param changed This is a new size or position for this view
11127 * @param left Left position, relative to parent
11128 * @param top Top position, relative to parent
11129 * @param right Right position, relative to parent
11130 * @param bottom Bottom position, relative to parent
11131 */
11132 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
11133 }
11134
11135 /**
11136 * Assign a size and position to this view.
11137 *
11138 * This is called from layout.
11139 *
11140 * @param left Left position, relative to parent
11141 * @param top Top position, relative to parent
11142 * @param right Right position, relative to parent
11143 * @param bottom Bottom position, relative to parent
11144 * @return true if the new size and position are different than the
11145 * previous ones
11146 * {@hide}
11147 */
11148 protected boolean setFrame(int left, int top, int right, int bottom) {
11149 boolean changed = false;
11150
11151 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070011152 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011153 + right + "," + bottom + ")");
11154 }
11155
11156 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
11157 changed = true;
11158
11159 // Remember our drawn bit
11160 int drawn = mPrivateFlags & DRAWN;
11161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011162 int oldWidth = mRight - mLeft;
11163 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070011164 int newWidth = right - left;
11165 int newHeight = bottom - top;
11166 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
11167
11168 // Invalidate our old position
11169 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011170
11171 mLeft = left;
11172 mTop = top;
11173 mRight = right;
11174 mBottom = bottom;
11175
11176 mPrivateFlags |= HAS_BOUNDS;
11177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011178
Chet Haase75755e22011-07-18 17:48:25 -070011179 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011180 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
11181 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070011182 if (mTransformationInfo != null) {
11183 mTransformationInfo.mMatrixDirty = true;
11184 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011185 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011186 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
11187 }
11188
11189 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
11190 // If we are visible, force the DRAWN bit to on so that
11191 // this invalidate will go through (at least to our parent).
11192 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011193 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011194 // the DRAWN bit.
11195 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070011196 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080011197 // parent display list may need to be recreated based on a change in the bounds
11198 // of any child
11199 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011200 }
11201
11202 // Reset drawn bit to original value (invalidate turns it off)
11203 mPrivateFlags |= drawn;
11204
11205 mBackgroundSizeChanged = true;
11206 }
11207 return changed;
11208 }
11209
11210 /**
11211 * Finalize inflating a view from XML. This is called as the last phase
11212 * of inflation, after all child views have been added.
11213 *
11214 * <p>Even if the subclass overrides onFinishInflate, they should always be
11215 * sure to call the super method, so that we get called.
11216 */
11217 protected void onFinishInflate() {
11218 }
11219
11220 /**
11221 * Returns the resources associated with this view.
11222 *
11223 * @return Resources object.
11224 */
11225 public Resources getResources() {
11226 return mResources;
11227 }
11228
11229 /**
11230 * Invalidates the specified Drawable.
11231 *
11232 * @param drawable the drawable to invalidate
11233 */
11234 public void invalidateDrawable(Drawable drawable) {
11235 if (verifyDrawable(drawable)) {
11236 final Rect dirty = drawable.getBounds();
11237 final int scrollX = mScrollX;
11238 final int scrollY = mScrollY;
11239
11240 invalidate(dirty.left + scrollX, dirty.top + scrollY,
11241 dirty.right + scrollX, dirty.bottom + scrollY);
11242 }
11243 }
11244
11245 /**
11246 * Schedules an action on a drawable to occur at a specified time.
11247 *
11248 * @param who the recipient of the action
11249 * @param what the action to run on the drawable
11250 * @param when the time at which the action must occur. Uses the
11251 * {@link SystemClock#uptimeMillis} timebase.
11252 */
11253 public void scheduleDrawable(Drawable who, Runnable what, long when) {
11254 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
11255 mAttachInfo.mHandler.postAtTime(what, who, when);
11256 }
11257 }
11258
11259 /**
11260 * Cancels a scheduled action on a drawable.
11261 *
11262 * @param who the recipient of the action
11263 * @param what the action to cancel
11264 */
11265 public void unscheduleDrawable(Drawable who, Runnable what) {
11266 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
11267 mAttachInfo.mHandler.removeCallbacks(what, who);
11268 }
11269 }
11270
11271 /**
11272 * Unschedule any events associated with the given Drawable. This can be
11273 * used when selecting a new Drawable into a view, so that the previous
11274 * one is completely unscheduled.
11275 *
11276 * @param who The Drawable to unschedule.
11277 *
11278 * @see #drawableStateChanged
11279 */
11280 public void unscheduleDrawable(Drawable who) {
11281 if (mAttachInfo != null) {
11282 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
11283 }
11284 }
11285
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011286 /**
11287 * Return the layout direction of a given Drawable.
11288 *
11289 * @param who the Drawable to query
11290 *
11291 * @hide
11292 */
11293 public int getResolvedLayoutDirection(Drawable who) {
11294 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070011295 }
11296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011297 /**
11298 * If your view subclass is displaying its own Drawable objects, it should
11299 * override this function and return true for any Drawable it is
11300 * displaying. This allows animations for those drawables to be
11301 * scheduled.
11302 *
11303 * <p>Be sure to call through to the super class when overriding this
11304 * function.
11305 *
11306 * @param who The Drawable to verify. Return true if it is one you are
11307 * displaying, else return the result of calling through to the
11308 * super class.
11309 *
11310 * @return boolean If true than the Drawable is being displayed in the
11311 * view; else false and it is not allowed to animate.
11312 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011313 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
11314 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011315 */
11316 protected boolean verifyDrawable(Drawable who) {
11317 return who == mBGDrawable;
11318 }
11319
11320 /**
11321 * This function is called whenever the state of the view changes in such
11322 * a way that it impacts the state of drawables being shown.
11323 *
11324 * <p>Be sure to call through to the superclass when overriding this
11325 * function.
11326 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011327 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011328 */
11329 protected void drawableStateChanged() {
11330 Drawable d = mBGDrawable;
11331 if (d != null && d.isStateful()) {
11332 d.setState(getDrawableState());
11333 }
11334 }
11335
11336 /**
11337 * Call this to force a view to update its drawable state. This will cause
11338 * drawableStateChanged to be called on this view. Views that are interested
11339 * in the new state should call getDrawableState.
11340 *
11341 * @see #drawableStateChanged
11342 * @see #getDrawableState
11343 */
11344 public void refreshDrawableState() {
11345 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
11346 drawableStateChanged();
11347
11348 ViewParent parent = mParent;
11349 if (parent != null) {
11350 parent.childDrawableStateChanged(this);
11351 }
11352 }
11353
11354 /**
11355 * Return an array of resource IDs of the drawable states representing the
11356 * current state of the view.
11357 *
11358 * @return The current drawable state
11359 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011360 * @see Drawable#setState(int[])
11361 * @see #drawableStateChanged()
11362 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011363 */
11364 public final int[] getDrawableState() {
11365 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
11366 return mDrawableState;
11367 } else {
11368 mDrawableState = onCreateDrawableState(0);
11369 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
11370 return mDrawableState;
11371 }
11372 }
11373
11374 /**
11375 * Generate the new {@link android.graphics.drawable.Drawable} state for
11376 * this view. This is called by the view
11377 * system when the cached Drawable state is determined to be invalid. To
11378 * retrieve the current state, you should use {@link #getDrawableState}.
11379 *
11380 * @param extraSpace if non-zero, this is the number of extra entries you
11381 * would like in the returned array in which you can place your own
11382 * states.
11383 *
11384 * @return Returns an array holding the current {@link Drawable} state of
11385 * the view.
11386 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011387 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011388 */
11389 protected int[] onCreateDrawableState(int extraSpace) {
11390 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
11391 mParent instanceof View) {
11392 return ((View) mParent).onCreateDrawableState(extraSpace);
11393 }
11394
11395 int[] drawableState;
11396
11397 int privateFlags = mPrivateFlags;
11398
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011399 int viewStateIndex = 0;
11400 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
11401 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
11402 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070011403 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011404 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
11405 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070011406 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
11407 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011408 // This is set if HW acceleration is requested, even if the current
11409 // process doesn't allow it. This is just to allow app preview
11410 // windows to better match their app.
11411 viewStateIndex |= VIEW_STATE_ACCELERATED;
11412 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070011413 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011414
Christopher Tate3d4bf172011-03-28 16:16:46 -070011415 final int privateFlags2 = mPrivateFlags2;
11416 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
11417 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
11418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011419 drawableState = VIEW_STATE_SETS[viewStateIndex];
11420
11421 //noinspection ConstantIfStatement
11422 if (false) {
11423 Log.i("View", "drawableStateIndex=" + viewStateIndex);
11424 Log.i("View", toString()
11425 + " pressed=" + ((privateFlags & PRESSED) != 0)
11426 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
11427 + " fo=" + hasFocus()
11428 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011429 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011430 + ": " + Arrays.toString(drawableState));
11431 }
11432
11433 if (extraSpace == 0) {
11434 return drawableState;
11435 }
11436
11437 final int[] fullState;
11438 if (drawableState != null) {
11439 fullState = new int[drawableState.length + extraSpace];
11440 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
11441 } else {
11442 fullState = new int[extraSpace];
11443 }
11444
11445 return fullState;
11446 }
11447
11448 /**
11449 * Merge your own state values in <var>additionalState</var> into the base
11450 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011451 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011452 *
11453 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011454 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011455 * own additional state values.
11456 *
11457 * @param additionalState The additional state values you would like
11458 * added to <var>baseState</var>; this array is not modified.
11459 *
11460 * @return As a convenience, the <var>baseState</var> array you originally
11461 * passed into the function is returned.
11462 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011463 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011464 */
11465 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
11466 final int N = baseState.length;
11467 int i = N - 1;
11468 while (i >= 0 && baseState[i] == 0) {
11469 i--;
11470 }
11471 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
11472 return baseState;
11473 }
11474
11475 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070011476 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
11477 * on all Drawable objects associated with this view.
11478 */
11479 public void jumpDrawablesToCurrentState() {
11480 if (mBGDrawable != null) {
11481 mBGDrawable.jumpToCurrentState();
11482 }
11483 }
11484
11485 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011486 * Sets the background color for this view.
11487 * @param color the color of the background
11488 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011489 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011490 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070011491 if (mBGDrawable instanceof ColorDrawable) {
11492 ((ColorDrawable) mBGDrawable).setColor(color);
11493 } else {
11494 setBackgroundDrawable(new ColorDrawable(color));
11495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011496 }
11497
11498 /**
11499 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070011500 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011501 * @param resid The identifier of the resource.
11502 * @attr ref android.R.styleable#View_background
11503 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011504 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011505 public void setBackgroundResource(int resid) {
11506 if (resid != 0 && resid == mBackgroundResource) {
11507 return;
11508 }
11509
11510 Drawable d= null;
11511 if (resid != 0) {
11512 d = mResources.getDrawable(resid);
11513 }
11514 setBackgroundDrawable(d);
11515
11516 mBackgroundResource = resid;
11517 }
11518
11519 /**
11520 * Set the background to a given Drawable, or remove the background. If the
11521 * background has padding, this View's padding is set to the background's
11522 * padding. However, when a background is removed, this View's padding isn't
11523 * touched. If setting the padding is desired, please use
11524 * {@link #setPadding(int, int, int, int)}.
11525 *
11526 * @param d The Drawable to use as the background, or null to remove the
11527 * background
11528 */
11529 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070011530 if (d == mBGDrawable) {
11531 return;
11532 }
11533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011534 boolean requestLayout = false;
11535
11536 mBackgroundResource = 0;
11537
11538 /*
11539 * Regardless of whether we're setting a new background or not, we want
11540 * to clear the previous drawable.
11541 */
11542 if (mBGDrawable != null) {
11543 mBGDrawable.setCallback(null);
11544 unscheduleDrawable(mBGDrawable);
11545 }
11546
11547 if (d != null) {
11548 Rect padding = sThreadLocal.get();
11549 if (padding == null) {
11550 padding = new Rect();
11551 sThreadLocal.set(padding);
11552 }
11553 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011554 switch (d.getResolvedLayoutDirectionSelf()) {
11555 case LAYOUT_DIRECTION_RTL:
11556 setPadding(padding.right, padding.top, padding.left, padding.bottom);
11557 break;
11558 case LAYOUT_DIRECTION_LTR:
11559 default:
11560 setPadding(padding.left, padding.top, padding.right, padding.bottom);
11561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011562 }
11563
11564 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
11565 // if it has a different minimum size, we should layout again
11566 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
11567 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
11568 requestLayout = true;
11569 }
11570
11571 d.setCallback(this);
11572 if (d.isStateful()) {
11573 d.setState(getDrawableState());
11574 }
11575 d.setVisible(getVisibility() == VISIBLE, false);
11576 mBGDrawable = d;
11577
11578 if ((mPrivateFlags & SKIP_DRAW) != 0) {
11579 mPrivateFlags &= ~SKIP_DRAW;
11580 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
11581 requestLayout = true;
11582 }
11583 } else {
11584 /* Remove the background */
11585 mBGDrawable = null;
11586
11587 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
11588 /*
11589 * This view ONLY drew the background before and we're removing
11590 * the background, so now it won't draw anything
11591 * (hence we SKIP_DRAW)
11592 */
11593 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
11594 mPrivateFlags |= SKIP_DRAW;
11595 }
11596
11597 /*
11598 * When the background is set, we try to apply its padding to this
11599 * View. When the background is removed, we don't touch this View's
11600 * padding. This is noted in the Javadocs. Hence, we don't need to
11601 * requestLayout(), the invalidate() below is sufficient.
11602 */
11603
11604 // The old background's minimum size could have affected this
11605 // View's layout, so let's requestLayout
11606 requestLayout = true;
11607 }
11608
Romain Guy8f1344f52009-05-15 16:03:59 -070011609 computeOpaqueFlags();
11610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011611 if (requestLayout) {
11612 requestLayout();
11613 }
11614
11615 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011616 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011617 }
11618
11619 /**
11620 * Gets the background drawable
11621 * @return The drawable used as the background for this view, if any.
11622 */
11623 public Drawable getBackground() {
11624 return mBGDrawable;
11625 }
11626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011627 /**
11628 * Sets the padding. The view may add on the space required to display
11629 * the scrollbars, depending on the style and visibility of the scrollbars.
11630 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
11631 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
11632 * from the values set in this call.
11633 *
11634 * @attr ref android.R.styleable#View_padding
11635 * @attr ref android.R.styleable#View_paddingBottom
11636 * @attr ref android.R.styleable#View_paddingLeft
11637 * @attr ref android.R.styleable#View_paddingRight
11638 * @attr ref android.R.styleable#View_paddingTop
11639 * @param left the left padding in pixels
11640 * @param top the top padding in pixels
11641 * @param right the right padding in pixels
11642 * @param bottom the bottom padding in pixels
11643 */
11644 public void setPadding(int left, int top, int right, int bottom) {
11645 boolean changed = false;
11646
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011647 mUserPaddingRelative = false;
11648
Adam Powell20232d02010-12-08 21:08:53 -080011649 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011650 mUserPaddingRight = right;
11651 mUserPaddingBottom = bottom;
11652
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011653 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070011654
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011655 // Common case is there are no scroll bars.
11656 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011657 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080011658 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011659 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080011660 switch (mVerticalScrollbarPosition) {
11661 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011662 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11663 left += offset;
11664 } else {
11665 right += offset;
11666 }
11667 break;
Adam Powell20232d02010-12-08 21:08:53 -080011668 case SCROLLBAR_POSITION_RIGHT:
11669 right += offset;
11670 break;
11671 case SCROLLBAR_POSITION_LEFT:
11672 left += offset;
11673 break;
11674 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011675 }
Adam Powell20232d02010-12-08 21:08:53 -080011676 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011677 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
11678 ? 0 : getHorizontalScrollbarHeight();
11679 }
11680 }
Romain Guy8506ab42009-06-11 17:35:47 -070011681
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011682 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011683 changed = true;
11684 mPaddingLeft = left;
11685 }
11686 if (mPaddingTop != top) {
11687 changed = true;
11688 mPaddingTop = top;
11689 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011690 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011691 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011692 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011693 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011694 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011695 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011696 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011697 }
11698
11699 if (changed) {
11700 requestLayout();
11701 }
11702 }
11703
11704 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011705 * Sets the relative padding. The view may add on the space required to display
11706 * the scrollbars, depending on the style and visibility of the scrollbars.
11707 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
11708 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
11709 * from the values set in this call.
11710 *
11711 * @attr ref android.R.styleable#View_padding
11712 * @attr ref android.R.styleable#View_paddingBottom
11713 * @attr ref android.R.styleable#View_paddingStart
11714 * @attr ref android.R.styleable#View_paddingEnd
11715 * @attr ref android.R.styleable#View_paddingTop
11716 * @param start the start padding in pixels
11717 * @param top the top padding in pixels
11718 * @param end the end padding in pixels
11719 * @param bottom the bottom padding in pixels
11720 *
11721 * @hide
11722 */
11723 public void setPaddingRelative(int start, int top, int end, int bottom) {
11724 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070011725
11726 mUserPaddingStart = start;
11727 mUserPaddingEnd = end;
11728
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011729 switch(getResolvedLayoutDirection()) {
11730 case LAYOUT_DIRECTION_RTL:
11731 setPadding(end, top, start, bottom);
11732 break;
11733 case LAYOUT_DIRECTION_LTR:
11734 default:
11735 setPadding(start, top, end, bottom);
11736 }
11737 }
11738
11739 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011740 * Returns the top padding of this view.
11741 *
11742 * @return the top padding in pixels
11743 */
11744 public int getPaddingTop() {
11745 return mPaddingTop;
11746 }
11747
11748 /**
11749 * Returns the bottom padding of this view. If there are inset and enabled
11750 * scrollbars, this value may include the space required to display the
11751 * scrollbars as well.
11752 *
11753 * @return the bottom padding in pixels
11754 */
11755 public int getPaddingBottom() {
11756 return mPaddingBottom;
11757 }
11758
11759 /**
11760 * Returns the left padding of this view. If there are inset and enabled
11761 * scrollbars, this value may include the space required to display the
11762 * scrollbars as well.
11763 *
11764 * @return the left padding in pixels
11765 */
11766 public int getPaddingLeft() {
11767 return mPaddingLeft;
11768 }
11769
11770 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011771 * Returns the start padding of this view. If there are inset and enabled
11772 * scrollbars, this value may include the space required to display the
11773 * scrollbars as well.
11774 *
11775 * @return the start padding in pixels
11776 *
11777 * @hide
11778 */
11779 public int getPaddingStart() {
11780 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11781 mPaddingRight : mPaddingLeft;
11782 }
11783
11784 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011785 * Returns the right padding of this view. If there are inset and enabled
11786 * scrollbars, this value may include the space required to display the
11787 * scrollbars as well.
11788 *
11789 * @return the right padding in pixels
11790 */
11791 public int getPaddingRight() {
11792 return mPaddingRight;
11793 }
11794
11795 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011796 * Returns the end padding of this view. If there are inset and enabled
11797 * scrollbars, this value may include the space required to display the
11798 * scrollbars as well.
11799 *
11800 * @return the end padding in pixels
11801 *
11802 * @hide
11803 */
11804 public int getPaddingEnd() {
11805 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11806 mPaddingLeft : mPaddingRight;
11807 }
11808
11809 /**
11810 * Return if the padding as been set thru relative values
11811 * {@link #setPaddingRelative(int, int, int, int)} or thru
11812 * @attr ref android.R.styleable#View_paddingStart or
11813 * @attr ref android.R.styleable#View_paddingEnd
11814 *
11815 * @return true if the padding is relative or false if it is not.
11816 *
11817 * @hide
11818 */
11819 public boolean isPaddingRelative() {
11820 return mUserPaddingRelative;
11821 }
11822
11823 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011824 * Changes the selection state of this view. A view can be selected or not.
11825 * Note that selection is not the same as focus. Views are typically
11826 * selected in the context of an AdapterView like ListView or GridView;
11827 * the selected view is the view that is highlighted.
11828 *
11829 * @param selected true if the view must be selected, false otherwise
11830 */
11831 public void setSelected(boolean selected) {
11832 if (((mPrivateFlags & SELECTED) != 0) != selected) {
11833 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070011834 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080011835 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011836 refreshDrawableState();
11837 dispatchSetSelected(selected);
11838 }
11839 }
11840
11841 /**
11842 * Dispatch setSelected to all of this View's children.
11843 *
11844 * @see #setSelected(boolean)
11845 *
11846 * @param selected The new selected state
11847 */
11848 protected void dispatchSetSelected(boolean selected) {
11849 }
11850
11851 /**
11852 * Indicates the selection state of this view.
11853 *
11854 * @return true if the view is selected, false otherwise
11855 */
11856 @ViewDebug.ExportedProperty
11857 public boolean isSelected() {
11858 return (mPrivateFlags & SELECTED) != 0;
11859 }
11860
11861 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011862 * Changes the activated state of this view. A view can be activated or not.
11863 * Note that activation is not the same as selection. Selection is
11864 * a transient property, representing the view (hierarchy) the user is
11865 * currently interacting with. Activation is a longer-term state that the
11866 * user can move views in and out of. For example, in a list view with
11867 * single or multiple selection enabled, the views in the current selection
11868 * set are activated. (Um, yeah, we are deeply sorry about the terminology
11869 * here.) The activated state is propagated down to children of the view it
11870 * is set on.
11871 *
11872 * @param activated true if the view must be activated, false otherwise
11873 */
11874 public void setActivated(boolean activated) {
11875 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
11876 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011877 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011878 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070011879 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011880 }
11881 }
11882
11883 /**
11884 * Dispatch setActivated to all of this View's children.
11885 *
11886 * @see #setActivated(boolean)
11887 *
11888 * @param activated The new activated state
11889 */
11890 protected void dispatchSetActivated(boolean activated) {
11891 }
11892
11893 /**
11894 * Indicates the activation state of this view.
11895 *
11896 * @return true if the view is activated, false otherwise
11897 */
11898 @ViewDebug.ExportedProperty
11899 public boolean isActivated() {
11900 return (mPrivateFlags & ACTIVATED) != 0;
11901 }
11902
11903 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011904 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
11905 * observer can be used to get notifications when global events, like
11906 * layout, happen.
11907 *
11908 * The returned ViewTreeObserver observer is not guaranteed to remain
11909 * valid for the lifetime of this View. If the caller of this method keeps
11910 * a long-lived reference to ViewTreeObserver, it should always check for
11911 * the return value of {@link ViewTreeObserver#isAlive()}.
11912 *
11913 * @return The ViewTreeObserver for this view's hierarchy.
11914 */
11915 public ViewTreeObserver getViewTreeObserver() {
11916 if (mAttachInfo != null) {
11917 return mAttachInfo.mTreeObserver;
11918 }
11919 if (mFloatingTreeObserver == null) {
11920 mFloatingTreeObserver = new ViewTreeObserver();
11921 }
11922 return mFloatingTreeObserver;
11923 }
11924
11925 /**
11926 * <p>Finds the topmost view in the current view hierarchy.</p>
11927 *
11928 * @return the topmost view containing this view
11929 */
11930 public View getRootView() {
11931 if (mAttachInfo != null) {
11932 final View v = mAttachInfo.mRootView;
11933 if (v != null) {
11934 return v;
11935 }
11936 }
Romain Guy8506ab42009-06-11 17:35:47 -070011937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011938 View parent = this;
11939
11940 while (parent.mParent != null && parent.mParent instanceof View) {
11941 parent = (View) parent.mParent;
11942 }
11943
11944 return parent;
11945 }
11946
11947 /**
11948 * <p>Computes the coordinates of this view on the screen. The argument
11949 * must be an array of two integers. After the method returns, the array
11950 * contains the x and y location in that order.</p>
11951 *
11952 * @param location an array of two integers in which to hold the coordinates
11953 */
11954 public void getLocationOnScreen(int[] location) {
11955 getLocationInWindow(location);
11956
11957 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070011958 if (info != null) {
11959 location[0] += info.mWindowLeft;
11960 location[1] += info.mWindowTop;
11961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011962 }
11963
11964 /**
11965 * <p>Computes the coordinates of this view in its window. The argument
11966 * must be an array of two integers. After the method returns, the array
11967 * contains the x and y location in that order.</p>
11968 *
11969 * @param location an array of two integers in which to hold the coordinates
11970 */
11971 public void getLocationInWindow(int[] location) {
11972 if (location == null || location.length < 2) {
11973 throw new IllegalArgumentException("location must be an array of "
11974 + "two integers");
11975 }
11976
Dianne Hackbornddb715b2011-09-09 14:43:39 -070011977 location[0] = mLeft;
11978 location[1] = mTop;
11979 if (mTransformationInfo != null) {
11980 location[0] += (int) (mTransformationInfo.mTranslationX + 0.5f);
11981 location[1] += (int) (mTransformationInfo.mTranslationY + 0.5f);
11982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011983
11984 ViewParent viewParent = mParent;
11985 while (viewParent instanceof View) {
11986 final View view = (View)viewParent;
Dianne Hackbornddb715b2011-09-09 14:43:39 -070011987 location[0] += view.mLeft - view.mScrollX;
11988 location[1] += view.mTop - view.mScrollY;
11989 if (view.mTransformationInfo != null) {
11990 location[0] += (int) (view.mTransformationInfo.mTranslationX + 0.5f);
11991 location[1] += (int) (view.mTransformationInfo.mTranslationY + 0.5f);
11992 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011993 viewParent = view.mParent;
11994 }
Romain Guy8506ab42009-06-11 17:35:47 -070011995
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070011996 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011997 // *cough*
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070011998 final ViewRootImpl vr = (ViewRootImpl)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011999 location[1] -= vr.mCurScrollY;
12000 }
12001 }
12002
12003 /**
12004 * {@hide}
12005 * @param id the id of the view to be found
12006 * @return the view of the specified id, null if cannot be found
12007 */
12008 protected View findViewTraversal(int id) {
12009 if (id == mID) {
12010 return this;
12011 }
12012 return null;
12013 }
12014
12015 /**
12016 * {@hide}
12017 * @param tag the tag of the view to be found
12018 * @return the view of specified tag, null if cannot be found
12019 */
12020 protected View findViewWithTagTraversal(Object tag) {
12021 if (tag != null && tag.equals(mTag)) {
12022 return this;
12023 }
12024 return null;
12025 }
12026
12027 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012028 * {@hide}
12029 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070012030 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080012031 * @return The first view that matches the predicate or null.
12032 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070012033 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080012034 if (predicate.apply(this)) {
12035 return this;
12036 }
12037 return null;
12038 }
12039
12040 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012041 * Look for a child view with the given id. If this view has the given
12042 * id, return this view.
12043 *
12044 * @param id The id to search for.
12045 * @return The view that has the given id in the hierarchy or null
12046 */
12047 public final View findViewById(int id) {
12048 if (id < 0) {
12049 return null;
12050 }
12051 return findViewTraversal(id);
12052 }
12053
12054 /**
12055 * Look for a child view with the given tag. If this view has the given
12056 * tag, return this view.
12057 *
12058 * @param tag The tag to search for, using "tag.equals(getTag())".
12059 * @return The View that has the given tag in the hierarchy or null
12060 */
12061 public final View findViewWithTag(Object tag) {
12062 if (tag == null) {
12063 return null;
12064 }
12065 return findViewWithTagTraversal(tag);
12066 }
12067
12068 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012069 * {@hide}
12070 * Look for a child view that matches the specified predicate.
12071 * If this view matches the predicate, return this view.
12072 *
12073 * @param predicate The predicate to evaluate.
12074 * @return The first view that matches the predicate or null.
12075 */
12076 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070012077 return findViewByPredicateTraversal(predicate, null);
12078 }
12079
12080 /**
12081 * {@hide}
12082 * Look for a child view that matches the specified predicate,
12083 * starting with the specified view and its descendents and then
12084 * recusively searching the ancestors and siblings of that view
12085 * until this view is reached.
12086 *
12087 * This method is useful in cases where the predicate does not match
12088 * a single unique view (perhaps multiple views use the same id)
12089 * and we are trying to find the view that is "closest" in scope to the
12090 * starting view.
12091 *
12092 * @param start The view to start from.
12093 * @param predicate The predicate to evaluate.
12094 * @return The first view that matches the predicate or null.
12095 */
12096 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
12097 View childToSkip = null;
12098 for (;;) {
12099 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
12100 if (view != null || start == this) {
12101 return view;
12102 }
12103
12104 ViewParent parent = start.getParent();
12105 if (parent == null || !(parent instanceof View)) {
12106 return null;
12107 }
12108
12109 childToSkip = start;
12110 start = (View) parent;
12111 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080012112 }
12113
12114 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012115 * Sets the identifier for this view. The identifier does not have to be
12116 * unique in this view's hierarchy. The identifier should be a positive
12117 * number.
12118 *
12119 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070012120 * @see #getId()
12121 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012122 *
12123 * @param id a number used to identify the view
12124 *
12125 * @attr ref android.R.styleable#View_id
12126 */
12127 public void setId(int id) {
12128 mID = id;
12129 }
12130
12131 /**
12132 * {@hide}
12133 *
12134 * @param isRoot true if the view belongs to the root namespace, false
12135 * otherwise
12136 */
12137 public void setIsRootNamespace(boolean isRoot) {
12138 if (isRoot) {
12139 mPrivateFlags |= IS_ROOT_NAMESPACE;
12140 } else {
12141 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
12142 }
12143 }
12144
12145 /**
12146 * {@hide}
12147 *
12148 * @return true if the view belongs to the root namespace, false otherwise
12149 */
12150 public boolean isRootNamespace() {
12151 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
12152 }
12153
12154 /**
12155 * Returns this view's identifier.
12156 *
12157 * @return a positive integer used to identify the view or {@link #NO_ID}
12158 * if the view has no ID
12159 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012160 * @see #setId(int)
12161 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012162 * @attr ref android.R.styleable#View_id
12163 */
12164 @ViewDebug.CapturedViewProperty
12165 public int getId() {
12166 return mID;
12167 }
12168
12169 /**
12170 * Returns this view's tag.
12171 *
12172 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070012173 *
12174 * @see #setTag(Object)
12175 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012176 */
12177 @ViewDebug.ExportedProperty
12178 public Object getTag() {
12179 return mTag;
12180 }
12181
12182 /**
12183 * Sets the tag associated with this view. A tag can be used to mark
12184 * a view in its hierarchy and does not have to be unique within the
12185 * hierarchy. Tags can also be used to store data within a view without
12186 * resorting to another data structure.
12187 *
12188 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070012189 *
12190 * @see #getTag()
12191 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012192 */
12193 public void setTag(final Object tag) {
12194 mTag = tag;
12195 }
12196
12197 /**
Romain Guyd90a3312009-05-06 14:54:28 -070012198 * Returns the tag associated with this view and the specified key.
12199 *
12200 * @param key The key identifying the tag
12201 *
12202 * @return the Object stored in this view as a tag
12203 *
12204 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070012205 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070012206 */
12207 public Object getTag(int key) {
12208 SparseArray<Object> tags = null;
12209 synchronized (sTagsLock) {
12210 if (sTags != null) {
12211 tags = sTags.get(this);
12212 }
12213 }
12214
12215 if (tags != null) return tags.get(key);
12216 return null;
12217 }
12218
12219 /**
12220 * Sets a tag associated with this view and a key. A tag can be used
12221 * to mark a view in its hierarchy and does not have to be unique within
12222 * the hierarchy. Tags can also be used to store data within a view
12223 * without resorting to another data structure.
12224 *
12225 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070012226 * application to ensure it is unique (see the <a
12227 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
12228 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070012229 * the Android framework or not associated with any package will cause
12230 * an {@link IllegalArgumentException} to be thrown.
12231 *
12232 * @param key The key identifying the tag
12233 * @param tag An Object to tag the view with
12234 *
12235 * @throws IllegalArgumentException If they specified key is not valid
12236 *
12237 * @see #setTag(Object)
12238 * @see #getTag(int)
12239 */
12240 public void setTag(int key, final Object tag) {
12241 // If the package id is 0x00 or 0x01, it's either an undefined package
12242 // or a framework id
12243 if ((key >>> 24) < 2) {
12244 throw new IllegalArgumentException("The key must be an application-specific "
12245 + "resource id.");
12246 }
12247
12248 setTagInternal(this, key, tag);
12249 }
12250
12251 /**
12252 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
12253 * framework id.
12254 *
12255 * @hide
12256 */
12257 public void setTagInternal(int key, Object tag) {
12258 if ((key >>> 24) != 0x1) {
12259 throw new IllegalArgumentException("The key must be a framework-specific "
12260 + "resource id.");
12261 }
12262
Romain Guy8506ab42009-06-11 17:35:47 -070012263 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012264 }
12265
12266 private static void setTagInternal(View view, int key, Object tag) {
12267 SparseArray<Object> tags = null;
12268 synchronized (sTagsLock) {
12269 if (sTags == null) {
12270 sTags = new WeakHashMap<View, SparseArray<Object>>();
12271 } else {
12272 tags = sTags.get(view);
12273 }
12274 }
12275
12276 if (tags == null) {
12277 tags = new SparseArray<Object>(2);
12278 synchronized (sTagsLock) {
12279 sTags.put(view, tags);
12280 }
12281 }
12282
12283 tags.put(key, tag);
12284 }
12285
12286 /**
Romain Guy13922e02009-05-12 17:56:14 -070012287 * @param consistency The type of consistency. See ViewDebug for more information.
12288 *
12289 * @hide
12290 */
12291 protected boolean dispatchConsistencyCheck(int consistency) {
12292 return onConsistencyCheck(consistency);
12293 }
12294
12295 /**
12296 * Method that subclasses should implement to check their consistency. The type of
12297 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070012298 *
Romain Guy13922e02009-05-12 17:56:14 -070012299 * @param consistency The type of consistency. See ViewDebug for more information.
12300 *
12301 * @throws IllegalStateException if the view is in an inconsistent state.
12302 *
12303 * @hide
12304 */
12305 protected boolean onConsistencyCheck(int consistency) {
12306 boolean result = true;
12307
12308 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
12309 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
12310
12311 if (checkLayout) {
12312 if (getParent() == null) {
12313 result = false;
12314 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12315 "View " + this + " does not have a parent.");
12316 }
12317
12318 if (mAttachInfo == null) {
12319 result = false;
12320 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12321 "View " + this + " is not attached to a window.");
12322 }
12323 }
12324
12325 if (checkDrawing) {
12326 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
12327 // from their draw() method
12328
12329 if ((mPrivateFlags & DRAWN) != DRAWN &&
12330 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
12331 result = false;
12332 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12333 "View " + this + " was invalidated but its drawing cache is valid.");
12334 }
12335 }
12336
12337 return result;
12338 }
12339
12340 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012341 * Prints information about this view in the log output, with the tag
12342 * {@link #VIEW_LOG_TAG}.
12343 *
12344 * @hide
12345 */
12346 public void debug() {
12347 debug(0);
12348 }
12349
12350 /**
12351 * Prints information about this view in the log output, with the tag
12352 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
12353 * indentation defined by the <code>depth</code>.
12354 *
12355 * @param depth the indentation level
12356 *
12357 * @hide
12358 */
12359 protected void debug(int depth) {
12360 String output = debugIndent(depth - 1);
12361
12362 output += "+ " + this;
12363 int id = getId();
12364 if (id != -1) {
12365 output += " (id=" + id + ")";
12366 }
12367 Object tag = getTag();
12368 if (tag != null) {
12369 output += " (tag=" + tag + ")";
12370 }
12371 Log.d(VIEW_LOG_TAG, output);
12372
12373 if ((mPrivateFlags & FOCUSED) != 0) {
12374 output = debugIndent(depth) + " FOCUSED";
12375 Log.d(VIEW_LOG_TAG, output);
12376 }
12377
12378 output = debugIndent(depth);
12379 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
12380 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
12381 + "} ";
12382 Log.d(VIEW_LOG_TAG, output);
12383
12384 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
12385 || mPaddingBottom != 0) {
12386 output = debugIndent(depth);
12387 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
12388 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
12389 Log.d(VIEW_LOG_TAG, output);
12390 }
12391
12392 output = debugIndent(depth);
12393 output += "mMeasureWidth=" + mMeasuredWidth +
12394 " mMeasureHeight=" + mMeasuredHeight;
12395 Log.d(VIEW_LOG_TAG, output);
12396
12397 output = debugIndent(depth);
12398 if (mLayoutParams == null) {
12399 output += "BAD! no layout params";
12400 } else {
12401 output = mLayoutParams.debug(output);
12402 }
12403 Log.d(VIEW_LOG_TAG, output);
12404
12405 output = debugIndent(depth);
12406 output += "flags={";
12407 output += View.printFlags(mViewFlags);
12408 output += "}";
12409 Log.d(VIEW_LOG_TAG, output);
12410
12411 output = debugIndent(depth);
12412 output += "privateFlags={";
12413 output += View.printPrivateFlags(mPrivateFlags);
12414 output += "}";
12415 Log.d(VIEW_LOG_TAG, output);
12416 }
12417
12418 /**
12419 * Creates an string of whitespaces used for indentation.
12420 *
12421 * @param depth the indentation level
12422 * @return a String containing (depth * 2 + 3) * 2 white spaces
12423 *
12424 * @hide
12425 */
12426 protected static String debugIndent(int depth) {
12427 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
12428 for (int i = 0; i < (depth * 2) + 3; i++) {
12429 spaces.append(' ').append(' ');
12430 }
12431 return spaces.toString();
12432 }
12433
12434 /**
12435 * <p>Return the offset of the widget's text baseline from the widget's top
12436 * boundary. If this widget does not support baseline alignment, this
12437 * method returns -1. </p>
12438 *
12439 * @return the offset of the baseline within the widget's bounds or -1
12440 * if baseline alignment is not supported
12441 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012442 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012443 public int getBaseline() {
12444 return -1;
12445 }
12446
12447 /**
12448 * Call this when something has changed which has invalidated the
12449 * layout of this view. This will schedule a layout pass of the view
12450 * tree.
12451 */
12452 public void requestLayout() {
12453 if (ViewDebug.TRACE_HIERARCHY) {
12454 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
12455 }
12456
12457 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012458 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012459
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070012460 if (mParent != null) {
12461 if (mLayoutParams != null) {
12462 mLayoutParams.resolveWithDirection(getResolvedLayoutDirection());
12463 }
12464 if (!mParent.isLayoutRequested()) {
12465 mParent.requestLayout();
12466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012467 }
12468 }
12469
12470 /**
12471 * Forces this view to be laid out during the next layout pass.
12472 * This method does not call requestLayout() or forceLayout()
12473 * on the parent.
12474 */
12475 public void forceLayout() {
12476 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012477 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012478 }
12479
12480 /**
12481 * <p>
12482 * This is called to find out how big a view should be. The parent
12483 * supplies constraint information in the width and height parameters.
12484 * </p>
12485 *
12486 * <p>
12487 * The actual mesurement work of a view is performed in
12488 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
12489 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
12490 * </p>
12491 *
12492 *
12493 * @param widthMeasureSpec Horizontal space requirements as imposed by the
12494 * parent
12495 * @param heightMeasureSpec Vertical space requirements as imposed by the
12496 * parent
12497 *
12498 * @see #onMeasure(int, int)
12499 */
12500 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
12501 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
12502 widthMeasureSpec != mOldWidthMeasureSpec ||
12503 heightMeasureSpec != mOldHeightMeasureSpec) {
12504
12505 // first clears the measured dimension flag
12506 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
12507
12508 if (ViewDebug.TRACE_HIERARCHY) {
12509 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
12510 }
12511
12512 // measure ourselves, this should set the measured dimension flag back
12513 onMeasure(widthMeasureSpec, heightMeasureSpec);
12514
12515 // flag not set, setMeasuredDimension() was not invoked, we raise
12516 // an exception to warn the developer
12517 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
12518 throw new IllegalStateException("onMeasure() did not set the"
12519 + " measured dimension by calling"
12520 + " setMeasuredDimension()");
12521 }
12522
12523 mPrivateFlags |= LAYOUT_REQUIRED;
12524 }
12525
12526 mOldWidthMeasureSpec = widthMeasureSpec;
12527 mOldHeightMeasureSpec = heightMeasureSpec;
12528 }
12529
12530 /**
12531 * <p>
12532 * Measure the view and its content to determine the measured width and the
12533 * measured height. This method is invoked by {@link #measure(int, int)} and
12534 * should be overriden by subclasses to provide accurate and efficient
12535 * measurement of their contents.
12536 * </p>
12537 *
12538 * <p>
12539 * <strong>CONTRACT:</strong> When overriding this method, you
12540 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
12541 * measured width and height of this view. Failure to do so will trigger an
12542 * <code>IllegalStateException</code>, thrown by
12543 * {@link #measure(int, int)}. Calling the superclass'
12544 * {@link #onMeasure(int, int)} is a valid use.
12545 * </p>
12546 *
12547 * <p>
12548 * The base class implementation of measure defaults to the background size,
12549 * unless a larger size is allowed by the MeasureSpec. Subclasses should
12550 * override {@link #onMeasure(int, int)} to provide better measurements of
12551 * their content.
12552 * </p>
12553 *
12554 * <p>
12555 * If this method is overridden, it is the subclass's responsibility to make
12556 * sure the measured height and width are at least the view's minimum height
12557 * and width ({@link #getSuggestedMinimumHeight()} and
12558 * {@link #getSuggestedMinimumWidth()}).
12559 * </p>
12560 *
12561 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
12562 * The requirements are encoded with
12563 * {@link android.view.View.MeasureSpec}.
12564 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
12565 * The requirements are encoded with
12566 * {@link android.view.View.MeasureSpec}.
12567 *
12568 * @see #getMeasuredWidth()
12569 * @see #getMeasuredHeight()
12570 * @see #setMeasuredDimension(int, int)
12571 * @see #getSuggestedMinimumHeight()
12572 * @see #getSuggestedMinimumWidth()
12573 * @see android.view.View.MeasureSpec#getMode(int)
12574 * @see android.view.View.MeasureSpec#getSize(int)
12575 */
12576 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
12577 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
12578 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
12579 }
12580
12581 /**
12582 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
12583 * measured width and measured height. Failing to do so will trigger an
12584 * exception at measurement time.</p>
12585 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080012586 * @param measuredWidth The measured width of this view. May be a complex
12587 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12588 * {@link #MEASURED_STATE_TOO_SMALL}.
12589 * @param measuredHeight The measured height of this view. May be a complex
12590 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12591 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012592 */
12593 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
12594 mMeasuredWidth = measuredWidth;
12595 mMeasuredHeight = measuredHeight;
12596
12597 mPrivateFlags |= MEASURED_DIMENSION_SET;
12598 }
12599
12600 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080012601 * Merge two states as returned by {@link #getMeasuredState()}.
12602 * @param curState The current state as returned from a view or the result
12603 * of combining multiple views.
12604 * @param newState The new view state to combine.
12605 * @return Returns a new integer reflecting the combination of the two
12606 * states.
12607 */
12608 public static int combineMeasuredStates(int curState, int newState) {
12609 return curState | newState;
12610 }
12611
12612 /**
12613 * Version of {@link #resolveSizeAndState(int, int, int)}
12614 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
12615 */
12616 public static int resolveSize(int size, int measureSpec) {
12617 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
12618 }
12619
12620 /**
12621 * Utility to reconcile a desired size and state, with constraints imposed
12622 * by a MeasureSpec. Will take the desired size, unless a different size
12623 * is imposed by the constraints. The returned value is a compound integer,
12624 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
12625 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
12626 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012627 *
12628 * @param size How big the view wants to be
12629 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080012630 * @return Size information bit mask as defined by
12631 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012632 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080012633 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012634 int result = size;
12635 int specMode = MeasureSpec.getMode(measureSpec);
12636 int specSize = MeasureSpec.getSize(measureSpec);
12637 switch (specMode) {
12638 case MeasureSpec.UNSPECIFIED:
12639 result = size;
12640 break;
12641 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080012642 if (specSize < size) {
12643 result = specSize | MEASURED_STATE_TOO_SMALL;
12644 } else {
12645 result = size;
12646 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012647 break;
12648 case MeasureSpec.EXACTLY:
12649 result = specSize;
12650 break;
12651 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080012652 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012653 }
12654
12655 /**
12656 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070012657 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012658 * by the MeasureSpec.
12659 *
12660 * @param size Default size for this view
12661 * @param measureSpec Constraints imposed by the parent
12662 * @return The size this view should be.
12663 */
12664 public static int getDefaultSize(int size, int measureSpec) {
12665 int result = size;
12666 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070012667 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012668
12669 switch (specMode) {
12670 case MeasureSpec.UNSPECIFIED:
12671 result = size;
12672 break;
12673 case MeasureSpec.AT_MOST:
12674 case MeasureSpec.EXACTLY:
12675 result = specSize;
12676 break;
12677 }
12678 return result;
12679 }
12680
12681 /**
12682 * Returns the suggested minimum height that the view should use. This
12683 * returns the maximum of the view's minimum height
12684 * and the background's minimum height
12685 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
12686 * <p>
12687 * When being used in {@link #onMeasure(int, int)}, the caller should still
12688 * ensure the returned height is within the requirements of the parent.
12689 *
12690 * @return The suggested minimum height of the view.
12691 */
12692 protected int getSuggestedMinimumHeight() {
12693 int suggestedMinHeight = mMinHeight;
12694
12695 if (mBGDrawable != null) {
12696 final int bgMinHeight = mBGDrawable.getMinimumHeight();
12697 if (suggestedMinHeight < bgMinHeight) {
12698 suggestedMinHeight = bgMinHeight;
12699 }
12700 }
12701
12702 return suggestedMinHeight;
12703 }
12704
12705 /**
12706 * Returns the suggested minimum width that the view should use. This
12707 * returns the maximum of the view's minimum width)
12708 * and the background's minimum width
12709 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
12710 * <p>
12711 * When being used in {@link #onMeasure(int, int)}, the caller should still
12712 * ensure the returned width is within the requirements of the parent.
12713 *
12714 * @return The suggested minimum width of the view.
12715 */
12716 protected int getSuggestedMinimumWidth() {
12717 int suggestedMinWidth = mMinWidth;
12718
12719 if (mBGDrawable != null) {
12720 final int bgMinWidth = mBGDrawable.getMinimumWidth();
12721 if (suggestedMinWidth < bgMinWidth) {
12722 suggestedMinWidth = bgMinWidth;
12723 }
12724 }
12725
12726 return suggestedMinWidth;
12727 }
12728
12729 /**
12730 * Sets the minimum height of the view. It is not guaranteed the view will
12731 * be able to achieve this minimum height (for example, if its parent layout
12732 * constrains it with less available height).
12733 *
12734 * @param minHeight The minimum height the view will try to be.
12735 */
12736 public void setMinimumHeight(int minHeight) {
12737 mMinHeight = minHeight;
12738 }
12739
12740 /**
12741 * Sets the minimum width of the view. It is not guaranteed the view will
12742 * be able to achieve this minimum width (for example, if its parent layout
12743 * constrains it with less available width).
12744 *
12745 * @param minWidth The minimum width the view will try to be.
12746 */
12747 public void setMinimumWidth(int minWidth) {
12748 mMinWidth = minWidth;
12749 }
12750
12751 /**
12752 * Get the animation currently associated with this view.
12753 *
12754 * @return The animation that is currently playing or
12755 * scheduled to play for this view.
12756 */
12757 public Animation getAnimation() {
12758 return mCurrentAnimation;
12759 }
12760
12761 /**
12762 * Start the specified animation now.
12763 *
12764 * @param animation the animation to start now
12765 */
12766 public void startAnimation(Animation animation) {
12767 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
12768 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012769 invalidateParentCaches();
12770 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012771 }
12772
12773 /**
12774 * Cancels any animations for this view.
12775 */
12776 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080012777 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080012778 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080012779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012780 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012781 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012782 }
12783
12784 /**
12785 * Sets the next animation to play for this view.
12786 * If you want the animation to play immediately, use
12787 * startAnimation. This method provides allows fine-grained
12788 * control over the start time and invalidation, but you
12789 * must make sure that 1) the animation has a start time set, and
12790 * 2) the view will be invalidated when the animation is supposed to
12791 * start.
12792 *
12793 * @param animation The next animation, or null.
12794 */
12795 public void setAnimation(Animation animation) {
12796 mCurrentAnimation = animation;
12797 if (animation != null) {
12798 animation.reset();
12799 }
12800 }
12801
12802 /**
12803 * Invoked by a parent ViewGroup to notify the start of the animation
12804 * currently associated with this view. If you override this method,
12805 * always call super.onAnimationStart();
12806 *
12807 * @see #setAnimation(android.view.animation.Animation)
12808 * @see #getAnimation()
12809 */
12810 protected void onAnimationStart() {
12811 mPrivateFlags |= ANIMATION_STARTED;
12812 }
12813
12814 /**
12815 * Invoked by a parent ViewGroup to notify the end of the animation
12816 * currently associated with this view. If you override this method,
12817 * always call super.onAnimationEnd();
12818 *
12819 * @see #setAnimation(android.view.animation.Animation)
12820 * @see #getAnimation()
12821 */
12822 protected void onAnimationEnd() {
12823 mPrivateFlags &= ~ANIMATION_STARTED;
12824 }
12825
12826 /**
12827 * Invoked if there is a Transform that involves alpha. Subclass that can
12828 * draw themselves with the specified alpha should return true, and then
12829 * respect that alpha when their onDraw() is called. If this returns false
12830 * then the view may be redirected to draw into an offscreen buffer to
12831 * fulfill the request, which will look fine, but may be slower than if the
12832 * subclass handles it internally. The default implementation returns false.
12833 *
12834 * @param alpha The alpha (0..255) to apply to the view's drawing
12835 * @return true if the view can draw with the specified alpha.
12836 */
12837 protected boolean onSetAlpha(int alpha) {
12838 return false;
12839 }
12840
12841 /**
12842 * This is used by the RootView to perform an optimization when
12843 * the view hierarchy contains one or several SurfaceView.
12844 * SurfaceView is always considered transparent, but its children are not,
12845 * therefore all View objects remove themselves from the global transparent
12846 * region (passed as a parameter to this function).
12847 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012848 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012849 *
12850 * @return Returns true if the effective visibility of the view at this
12851 * point is opaque, regardless of the transparent region; returns false
12852 * if it is possible for underlying windows to be seen behind the view.
12853 *
12854 * {@hide}
12855 */
12856 public boolean gatherTransparentRegion(Region region) {
12857 final AttachInfo attachInfo = mAttachInfo;
12858 if (region != null && attachInfo != null) {
12859 final int pflags = mPrivateFlags;
12860 if ((pflags & SKIP_DRAW) == 0) {
12861 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
12862 // remove it from the transparent region.
12863 final int[] location = attachInfo.mTransparentLocation;
12864 getLocationInWindow(location);
12865 region.op(location[0], location[1], location[0] + mRight - mLeft,
12866 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
12867 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
12868 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
12869 // exists, so we remove the background drawable's non-transparent
12870 // parts from this transparent region.
12871 applyDrawableToTransparentRegion(mBGDrawable, region);
12872 }
12873 }
12874 return true;
12875 }
12876
12877 /**
12878 * Play a sound effect for this view.
12879 *
12880 * <p>The framework will play sound effects for some built in actions, such as
12881 * clicking, but you may wish to play these effects in your widget,
12882 * for instance, for internal navigation.
12883 *
12884 * <p>The sound effect will only be played if sound effects are enabled by the user, and
12885 * {@link #isSoundEffectsEnabled()} is true.
12886 *
12887 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
12888 */
12889 public void playSoundEffect(int soundConstant) {
12890 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
12891 return;
12892 }
12893 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
12894 }
12895
12896 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012897 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012898 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012899 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012900 *
12901 * <p>The framework will provide haptic feedback for some built in actions,
12902 * such as long presses, but you may wish to provide feedback for your
12903 * own widget.
12904 *
12905 * <p>The feedback will only be performed if
12906 * {@link #isHapticFeedbackEnabled()} is true.
12907 *
12908 * @param feedbackConstant One of the constants defined in
12909 * {@link HapticFeedbackConstants}
12910 */
12911 public boolean performHapticFeedback(int feedbackConstant) {
12912 return performHapticFeedback(feedbackConstant, 0);
12913 }
12914
12915 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012916 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012917 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012918 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012919 *
12920 * @param feedbackConstant One of the constants defined in
12921 * {@link HapticFeedbackConstants}
12922 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
12923 */
12924 public boolean performHapticFeedback(int feedbackConstant, int flags) {
12925 if (mAttachInfo == null) {
12926 return false;
12927 }
Romain Guyf607bdc2010-09-10 19:20:06 -070012928 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070012929 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012930 && !isHapticFeedbackEnabled()) {
12931 return false;
12932 }
Romain Guy812ccbe2010-06-01 14:07:24 -070012933 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
12934 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012935 }
12936
12937 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012938 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040012939 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
12940 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Daniel Sandlerfad95552011-08-17 14:06:16 -040012941 *
12942 * This value will be re-applied immediately, even if the flags have not changed, so a view may
12943 * easily reassert a particular SystemUiVisibility condition even if the system UI itself has
12944 * since countermanded the original request.
Joe Onorato664644d2011-01-23 17:53:23 -080012945 */
12946 public void setSystemUiVisibility(int visibility) {
Daniel Sandlerfad95552011-08-17 14:06:16 -040012947 mSystemUiVisibility = visibility;
12948 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12949 mParent.recomputeViewAttributes(this);
Joe Onorato664644d2011-01-23 17:53:23 -080012950 }
12951 }
12952
12953 /**
12954 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040012955 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
12956 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080012957 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080012958 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080012959 return mSystemUiVisibility;
12960 }
12961
Scott Mainec6331b2011-05-24 16:55:56 -070012962 /**
12963 * Set a listener to receive callbacks when the visibility of the system bar changes.
12964 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
12965 */
Joe Onorato664644d2011-01-23 17:53:23 -080012966 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
12967 mOnSystemUiVisibilityChangeListener = l;
12968 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12969 mParent.recomputeViewAttributes(this);
12970 }
12971 }
12972
12973 /**
12974 */
12975 public void dispatchSystemUiVisibilityChanged(int visibility) {
12976 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080012977 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080012978 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080012979 }
12980 }
12981
12982 /**
Joe Malin32736f02011-01-19 16:14:20 -080012983 * Creates an image that the system displays during the drag and drop
12984 * operation. This is called a &quot;drag shadow&quot;. The default implementation
12985 * for a DragShadowBuilder based on a View returns an image that has exactly the same
12986 * appearance as the given View. The default also positions the center of the drag shadow
12987 * directly under the touch point. If no View is provided (the constructor with no parameters
12988 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
12989 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
12990 * default is an invisible drag shadow.
12991 * <p>
12992 * You are not required to use the View you provide to the constructor as the basis of the
12993 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
12994 * anything you want as the drag shadow.
12995 * </p>
12996 * <p>
12997 * You pass a DragShadowBuilder object to the system when you start the drag. The system
12998 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
12999 * size and position of the drag shadow. It uses this data to construct a
13000 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
13001 * so that your application can draw the shadow image in the Canvas.
13002 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013003 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013004 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070013005 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070013006
13007 /**
Joe Malin32736f02011-01-19 16:14:20 -080013008 * Constructs a shadow image builder based on a View. By default, the resulting drag
13009 * shadow will have the same appearance and dimensions as the View, with the touch point
13010 * over the center of the View.
13011 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070013012 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013013 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070013014 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070013015 }
13016
Christopher Tate17ed60c2011-01-18 12:50:26 -080013017 /**
13018 * Construct a shadow builder object with no associated View. This
13019 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
13020 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
13021 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080013022 * reference to any View object. If they are not overridden, then the result is an
13023 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080013024 */
13025 public DragShadowBuilder() {
13026 mView = new WeakReference<View>(null);
13027 }
13028
13029 /**
13030 * Returns the View object that had been passed to the
13031 * {@link #View.DragShadowBuilder(View)}
13032 * constructor. If that View parameter was {@code null} or if the
13033 * {@link #View.DragShadowBuilder()}
13034 * constructor was used to instantiate the builder object, this method will return
13035 * null.
13036 *
13037 * @return The View object associate with this builder object.
13038 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070013039 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070013040 final public View getView() {
13041 return mView.get();
13042 }
13043
Christopher Tate2c095f32010-10-04 14:13:40 -070013044 /**
Joe Malin32736f02011-01-19 16:14:20 -080013045 * Provides the metrics for the shadow image. These include the dimensions of
13046 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070013047 * be centered under the touch location while dragging.
13048 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013049 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080013050 * same as the dimensions of the View itself and centers the shadow under
13051 * the touch point.
13052 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013053 *
Joe Malin32736f02011-01-19 16:14:20 -080013054 * @param shadowSize A {@link android.graphics.Point} containing the width and height
13055 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
13056 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
13057 * image.
13058 *
13059 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
13060 * shadow image that should be underneath the touch point during the drag and drop
13061 * operation. Your application must set {@link android.graphics.Point#x} to the
13062 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070013063 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013064 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070013065 final View view = mView.get();
13066 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013067 shadowSize.set(view.getWidth(), view.getHeight());
13068 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070013069 } else {
13070 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
13071 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013072 }
13073
13074 /**
Joe Malin32736f02011-01-19 16:14:20 -080013075 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
13076 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013077 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070013078 *
Joe Malin32736f02011-01-19 16:14:20 -080013079 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070013080 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013081 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070013082 final View view = mView.get();
13083 if (view != null) {
13084 view.draw(canvas);
13085 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013086 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070013087 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013088 }
13089 }
13090
13091 /**
Joe Malin32736f02011-01-19 16:14:20 -080013092 * Starts a drag and drop operation. When your application calls this method, it passes a
13093 * {@link android.view.View.DragShadowBuilder} object to the system. The
13094 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
13095 * to get metrics for the drag shadow, and then calls the object's
13096 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
13097 * <p>
13098 * Once the system has the drag shadow, it begins the drag and drop operation by sending
13099 * drag events to all the View objects in your application that are currently visible. It does
13100 * this either by calling the View object's drag listener (an implementation of
13101 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
13102 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
13103 * Both are passed a {@link android.view.DragEvent} object that has a
13104 * {@link android.view.DragEvent#getAction()} value of
13105 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
13106 * </p>
13107 * <p>
13108 * Your application can invoke startDrag() on any attached View object. The View object does not
13109 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
13110 * be related to the View the user selected for dragging.
13111 * </p>
13112 * @param data A {@link android.content.ClipData} object pointing to the data to be
13113 * transferred by the drag and drop operation.
13114 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
13115 * drag shadow.
13116 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
13117 * drop operation. This Object is put into every DragEvent object sent by the system during the
13118 * current drag.
13119 * <p>
13120 * myLocalState is a lightweight mechanism for the sending information from the dragged View
13121 * to the target Views. For example, it can contain flags that differentiate between a
13122 * a copy operation and a move operation.
13123 * </p>
13124 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
13125 * so the parameter should be set to 0.
13126 * @return {@code true} if the method completes successfully, or
13127 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
13128 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070013129 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013130 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013131 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070013132 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013133 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070013134 }
13135 boolean okay = false;
13136
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013137 Point shadowSize = new Point();
13138 Point shadowTouchPoint = new Point();
13139 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070013140
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013141 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
13142 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
13143 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070013144 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013145
Chris Tatea32dcf72010-10-14 12:13:50 -070013146 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013147 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
13148 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070013149 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013150 Surface surface = new Surface();
13151 try {
13152 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013153 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070013154 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070013155 + " surface=" + surface);
13156 if (token != null) {
13157 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070013158 try {
Chris Tate6b391282010-10-14 15:48:59 -070013159 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013160 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070013161 } finally {
13162 surface.unlockCanvasAndPost(canvas);
13163 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013164
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013165 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080013166
13167 // Cache the local state object for delivery with DragEvents
13168 root.setLocalDragState(myLocalState);
13169
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013170 // repurpose 'shadowSize' for the last touch point
13171 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070013172
Christopher Tatea53146c2010-09-07 11:57:52 -070013173 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013174 shadowSize.x, shadowSize.y,
13175 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070013176 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070013177
13178 // Off and running! Release our local surface instance; the drag
13179 // shadow surface is now managed by the system process.
13180 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070013181 }
13182 } catch (Exception e) {
13183 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
13184 surface.destroy();
13185 }
13186
13187 return okay;
13188 }
13189
Christopher Tatea53146c2010-09-07 11:57:52 -070013190 /**
Joe Malin32736f02011-01-19 16:14:20 -080013191 * Handles drag events sent by the system following a call to
13192 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
13193 *<p>
13194 * When the system calls this method, it passes a
13195 * {@link android.view.DragEvent} object. A call to
13196 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
13197 * in DragEvent. The method uses these to determine what is happening in the drag and drop
13198 * operation.
13199 * @param event The {@link android.view.DragEvent} sent by the system.
13200 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
13201 * in DragEvent, indicating the type of drag event represented by this object.
13202 * @return {@code true} if the method was successful, otherwise {@code false}.
13203 * <p>
13204 * The method should return {@code true} in response to an action type of
13205 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
13206 * operation.
13207 * </p>
13208 * <p>
13209 * The method should also return {@code true} in response to an action type of
13210 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
13211 * {@code false} if it didn't.
13212 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013213 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070013214 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070013215 return false;
13216 }
13217
13218 /**
Joe Malin32736f02011-01-19 16:14:20 -080013219 * Detects if this View is enabled and has a drag event listener.
13220 * If both are true, then it calls the drag event listener with the
13221 * {@link android.view.DragEvent} it received. If the drag event listener returns
13222 * {@code true}, then dispatchDragEvent() returns {@code true}.
13223 * <p>
13224 * For all other cases, the method calls the
13225 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
13226 * method and returns its result.
13227 * </p>
13228 * <p>
13229 * This ensures that a drag event is always consumed, even if the View does not have a drag
13230 * event listener. However, if the View has a listener and the listener returns true, then
13231 * onDragEvent() is not called.
13232 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013233 */
13234 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080013235 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070013236 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
13237 && mOnDragListener.onDrag(this, event)) {
13238 return true;
13239 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013240 return onDragEvent(event);
13241 }
13242
Christopher Tate3d4bf172011-03-28 16:16:46 -070013243 boolean canAcceptDrag() {
13244 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
13245 }
13246
Christopher Tatea53146c2010-09-07 11:57:52 -070013247 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070013248 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
13249 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070013250 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070013251 */
13252 public void onCloseSystemDialogs(String reason) {
13253 }
Joe Malin32736f02011-01-19 16:14:20 -080013254
Dianne Hackbornffa42482009-09-23 22:20:11 -070013255 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013256 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070013257 * update a Region being computed for
13258 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013259 * that any non-transparent parts of the Drawable are removed from the
13260 * given transparent region.
13261 *
13262 * @param dr The Drawable whose transparency is to be applied to the region.
13263 * @param region A Region holding the current transparency information,
13264 * where any parts of the region that are set are considered to be
13265 * transparent. On return, this region will be modified to have the
13266 * transparency information reduced by the corresponding parts of the
13267 * Drawable that are not transparent.
13268 * {@hide}
13269 */
13270 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
13271 if (DBG) {
13272 Log.i("View", "Getting transparent region for: " + this);
13273 }
13274 final Region r = dr.getTransparentRegion();
13275 final Rect db = dr.getBounds();
13276 final AttachInfo attachInfo = mAttachInfo;
13277 if (r != null && attachInfo != null) {
13278 final int w = getRight()-getLeft();
13279 final int h = getBottom()-getTop();
13280 if (db.left > 0) {
13281 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
13282 r.op(0, 0, db.left, h, Region.Op.UNION);
13283 }
13284 if (db.right < w) {
13285 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
13286 r.op(db.right, 0, w, h, Region.Op.UNION);
13287 }
13288 if (db.top > 0) {
13289 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
13290 r.op(0, 0, w, db.top, Region.Op.UNION);
13291 }
13292 if (db.bottom < h) {
13293 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
13294 r.op(0, db.bottom, w, h, Region.Op.UNION);
13295 }
13296 final int[] location = attachInfo.mTransparentLocation;
13297 getLocationInWindow(location);
13298 r.translate(location[0], location[1]);
13299 region.op(r, Region.Op.INTERSECT);
13300 } else {
13301 region.op(db, Region.Op.DIFFERENCE);
13302 }
13303 }
13304
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013305 private void checkForLongClick(int delayOffset) {
13306 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
13307 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013308
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013309 if (mPendingCheckForLongPress == null) {
13310 mPendingCheckForLongPress = new CheckForLongPress();
13311 }
13312 mPendingCheckForLongPress.rememberWindowAttachCount();
13313 postDelayed(mPendingCheckForLongPress,
13314 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013315 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013316 }
13317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013318 /**
13319 * Inflate a view from an XML resource. This convenience method wraps the {@link
13320 * LayoutInflater} class, which provides a full range of options for view inflation.
13321 *
13322 * @param context The Context object for your activity or application.
13323 * @param resource The resource ID to inflate
13324 * @param root A view group that will be the parent. Used to properly inflate the
13325 * layout_* parameters.
13326 * @see LayoutInflater
13327 */
13328 public static View inflate(Context context, int resource, ViewGroup root) {
13329 LayoutInflater factory = LayoutInflater.from(context);
13330 return factory.inflate(resource, root);
13331 }
Romain Guy33e72ae2010-07-17 12:40:29 -070013332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013333 /**
Adam Powell637d3372010-08-25 14:37:03 -070013334 * Scroll the view with standard behavior for scrolling beyond the normal
13335 * content boundaries. Views that call this method should override
13336 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
13337 * results of an over-scroll operation.
13338 *
13339 * Views can use this method to handle any touch or fling-based scrolling.
13340 *
13341 * @param deltaX Change in X in pixels
13342 * @param deltaY Change in Y in pixels
13343 * @param scrollX Current X scroll value in pixels before applying deltaX
13344 * @param scrollY Current Y scroll value in pixels before applying deltaY
13345 * @param scrollRangeX Maximum content scroll range along the X axis
13346 * @param scrollRangeY Maximum content scroll range along the Y axis
13347 * @param maxOverScrollX Number of pixels to overscroll by in either direction
13348 * along the X axis.
13349 * @param maxOverScrollY Number of pixels to overscroll by in either direction
13350 * along the Y axis.
13351 * @param isTouchEvent true if this scroll operation is the result of a touch event.
13352 * @return true if scrolling was clamped to an over-scroll boundary along either
13353 * axis, false otherwise.
13354 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013355 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070013356 protected boolean overScrollBy(int deltaX, int deltaY,
13357 int scrollX, int scrollY,
13358 int scrollRangeX, int scrollRangeY,
13359 int maxOverScrollX, int maxOverScrollY,
13360 boolean isTouchEvent) {
13361 final int overScrollMode = mOverScrollMode;
13362 final boolean canScrollHorizontal =
13363 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
13364 final boolean canScrollVertical =
13365 computeVerticalScrollRange() > computeVerticalScrollExtent();
13366 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
13367 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
13368 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
13369 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
13370
13371 int newScrollX = scrollX + deltaX;
13372 if (!overScrollHorizontal) {
13373 maxOverScrollX = 0;
13374 }
13375
13376 int newScrollY = scrollY + deltaY;
13377 if (!overScrollVertical) {
13378 maxOverScrollY = 0;
13379 }
13380
13381 // Clamp values if at the limits and record
13382 final int left = -maxOverScrollX;
13383 final int right = maxOverScrollX + scrollRangeX;
13384 final int top = -maxOverScrollY;
13385 final int bottom = maxOverScrollY + scrollRangeY;
13386
13387 boolean clampedX = false;
13388 if (newScrollX > right) {
13389 newScrollX = right;
13390 clampedX = true;
13391 } else if (newScrollX < left) {
13392 newScrollX = left;
13393 clampedX = true;
13394 }
13395
13396 boolean clampedY = false;
13397 if (newScrollY > bottom) {
13398 newScrollY = bottom;
13399 clampedY = true;
13400 } else if (newScrollY < top) {
13401 newScrollY = top;
13402 clampedY = true;
13403 }
13404
13405 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
13406
13407 return clampedX || clampedY;
13408 }
13409
13410 /**
13411 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
13412 * respond to the results of an over-scroll operation.
13413 *
13414 * @param scrollX New X scroll value in pixels
13415 * @param scrollY New Y scroll value in pixels
13416 * @param clampedX True if scrollX was clamped to an over-scroll boundary
13417 * @param clampedY True if scrollY was clamped to an over-scroll boundary
13418 */
13419 protected void onOverScrolled(int scrollX, int scrollY,
13420 boolean clampedX, boolean clampedY) {
13421 // Intentionally empty.
13422 }
13423
13424 /**
13425 * Returns the over-scroll mode for this view. The result will be
13426 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13427 * (allow over-scrolling only if the view content is larger than the container),
13428 * or {@link #OVER_SCROLL_NEVER}.
13429 *
13430 * @return This view's over-scroll mode.
13431 */
13432 public int getOverScrollMode() {
13433 return mOverScrollMode;
13434 }
13435
13436 /**
13437 * Set the over-scroll mode for this view. Valid over-scroll modes are
13438 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13439 * (allow over-scrolling only if the view content is larger than the container),
13440 * or {@link #OVER_SCROLL_NEVER}.
13441 *
13442 * Setting the over-scroll mode of a view will have an effect only if the
13443 * view is capable of scrolling.
13444 *
13445 * @param overScrollMode The new over-scroll mode for this view.
13446 */
13447 public void setOverScrollMode(int overScrollMode) {
13448 if (overScrollMode != OVER_SCROLL_ALWAYS &&
13449 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
13450 overScrollMode != OVER_SCROLL_NEVER) {
13451 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
13452 }
13453 mOverScrollMode = overScrollMode;
13454 }
13455
13456 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013457 * Gets a scale factor that determines the distance the view should scroll
13458 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
13459 * @return The vertical scroll scale factor.
13460 * @hide
13461 */
13462 protected float getVerticalScrollFactor() {
13463 if (mVerticalScrollFactor == 0) {
13464 TypedValue outValue = new TypedValue();
13465 if (!mContext.getTheme().resolveAttribute(
13466 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
13467 throw new IllegalStateException(
13468 "Expected theme to define listPreferredItemHeight.");
13469 }
13470 mVerticalScrollFactor = outValue.getDimension(
13471 mContext.getResources().getDisplayMetrics());
13472 }
13473 return mVerticalScrollFactor;
13474 }
13475
13476 /**
13477 * Gets a scale factor that determines the distance the view should scroll
13478 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
13479 * @return The horizontal scroll scale factor.
13480 * @hide
13481 */
13482 protected float getHorizontalScrollFactor() {
13483 // TODO: Should use something else.
13484 return getVerticalScrollFactor();
13485 }
13486
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013487 /**
13488 * Return the value specifying the text direction or policy that was set with
13489 * {@link #setTextDirection(int)}.
13490 *
13491 * @return the defined text direction. It can be one of:
13492 *
13493 * {@link #TEXT_DIRECTION_INHERIT},
13494 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13495 * {@link #TEXT_DIRECTION_ANY_RTL},
13496 * {@link #TEXT_DIRECTION_LTR},
13497 * {@link #TEXT_DIRECTION_RTL},
13498 *
13499 * @hide
13500 */
13501 public int getTextDirection() {
13502 return mTextDirection;
13503 }
13504
13505 /**
13506 * Set the text direction.
13507 *
13508 * @param textDirection the direction to set. Should be one of:
13509 *
13510 * {@link #TEXT_DIRECTION_INHERIT},
13511 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13512 * {@link #TEXT_DIRECTION_ANY_RTL},
13513 * {@link #TEXT_DIRECTION_LTR},
13514 * {@link #TEXT_DIRECTION_RTL},
13515 *
13516 * @hide
13517 */
13518 public void setTextDirection(int textDirection) {
13519 if (textDirection != mTextDirection) {
13520 mTextDirection = textDirection;
13521 resetResolvedTextDirection();
13522 requestLayout();
13523 }
13524 }
13525
13526 /**
13527 * Return the resolved text direction.
13528 *
13529 * @return the resolved text direction. Return one of:
13530 *
Doug Feltcb3791202011-07-07 11:57:48 -070013531 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13532 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013533 * {@link #TEXT_DIRECTION_LTR},
13534 * {@link #TEXT_DIRECTION_RTL},
13535 *
13536 * @hide
13537 */
13538 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013539 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013540 resolveTextDirection();
13541 }
13542 return mResolvedTextDirection;
13543 }
13544
13545 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013546 * Resolve the text direction.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013547 *
13548 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013549 */
13550 protected void resolveTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013551 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
13552 mResolvedTextDirection = mTextDirection;
13553 return;
13554 }
13555 if (mParent != null && mParent instanceof ViewGroup) {
13556 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
13557 return;
13558 }
13559 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013560 }
13561
13562 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013563 * Reset resolved text direction. Will be resolved during a call to getResolvedTextDirection().
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013564 *
13565 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013566 */
13567 protected void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013568 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013569 }
13570
Chet Haaseb39f0512011-05-24 14:36:40 -070013571 //
13572 // Properties
13573 //
13574 /**
13575 * A Property wrapper around the <code>alpha</code> functionality handled by the
13576 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
13577 */
Romain Guy02ccac62011-06-24 13:20:23 -070013578 public static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013579 @Override
13580 public void setValue(View object, float value) {
13581 object.setAlpha(value);
13582 }
13583
13584 @Override
13585 public Float get(View object) {
13586 return object.getAlpha();
13587 }
13588 };
13589
13590 /**
13591 * A Property wrapper around the <code>translationX</code> functionality handled by the
13592 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
13593 */
13594 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
13595 @Override
13596 public void setValue(View object, float value) {
13597 object.setTranslationX(value);
13598 }
13599
13600 @Override
13601 public Float get(View object) {
13602 return object.getTranslationX();
13603 }
13604 };
13605
13606 /**
13607 * A Property wrapper around the <code>translationY</code> functionality handled by the
13608 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
13609 */
13610 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
13611 @Override
13612 public void setValue(View object, float value) {
13613 object.setTranslationY(value);
13614 }
13615
13616 @Override
13617 public Float get(View object) {
13618 return object.getTranslationY();
13619 }
13620 };
13621
13622 /**
13623 * A Property wrapper around the <code>x</code> functionality handled by the
13624 * {@link View#setX(float)} and {@link View#getX()} methods.
13625 */
13626 public static Property<View, Float> X = new FloatProperty<View>("x") {
13627 @Override
13628 public void setValue(View object, float value) {
13629 object.setX(value);
13630 }
13631
13632 @Override
13633 public Float get(View object) {
13634 return object.getX();
13635 }
13636 };
13637
13638 /**
13639 * A Property wrapper around the <code>y</code> functionality handled by the
13640 * {@link View#setY(float)} and {@link View#getY()} methods.
13641 */
13642 public static Property<View, Float> Y = new FloatProperty<View>("y") {
13643 @Override
13644 public void setValue(View object, float value) {
13645 object.setY(value);
13646 }
13647
13648 @Override
13649 public Float get(View object) {
13650 return object.getY();
13651 }
13652 };
13653
13654 /**
13655 * A Property wrapper around the <code>rotation</code> functionality handled by the
13656 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
13657 */
13658 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
13659 @Override
13660 public void setValue(View object, float value) {
13661 object.setRotation(value);
13662 }
13663
13664 @Override
13665 public Float get(View object) {
13666 return object.getRotation();
13667 }
13668 };
13669
13670 /**
13671 * A Property wrapper around the <code>rotationX</code> functionality handled by the
13672 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
13673 */
13674 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
13675 @Override
13676 public void setValue(View object, float value) {
13677 object.setRotationX(value);
13678 }
13679
13680 @Override
13681 public Float get(View object) {
13682 return object.getRotationX();
13683 }
13684 };
13685
13686 /**
13687 * A Property wrapper around the <code>rotationY</code> functionality handled by the
13688 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
13689 */
13690 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
13691 @Override
13692 public void setValue(View object, float value) {
13693 object.setRotationY(value);
13694 }
13695
13696 @Override
13697 public Float get(View object) {
13698 return object.getRotationY();
13699 }
13700 };
13701
13702 /**
13703 * A Property wrapper around the <code>scaleX</code> functionality handled by the
13704 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
13705 */
13706 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
13707 @Override
13708 public void setValue(View object, float value) {
13709 object.setScaleX(value);
13710 }
13711
13712 @Override
13713 public Float get(View object) {
13714 return object.getScaleX();
13715 }
13716 };
13717
13718 /**
13719 * A Property wrapper around the <code>scaleY</code> functionality handled by the
13720 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
13721 */
13722 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
13723 @Override
13724 public void setValue(View object, float value) {
13725 object.setScaleY(value);
13726 }
13727
13728 @Override
13729 public Float get(View object) {
13730 return object.getScaleY();
13731 }
13732 };
13733
Jeff Brown33bbfd22011-02-24 20:55:35 -080013734 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013735 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
13736 * Each MeasureSpec represents a requirement for either the width or the height.
13737 * A MeasureSpec is comprised of a size and a mode. There are three possible
13738 * modes:
13739 * <dl>
13740 * <dt>UNSPECIFIED</dt>
13741 * <dd>
13742 * The parent has not imposed any constraint on the child. It can be whatever size
13743 * it wants.
13744 * </dd>
13745 *
13746 * <dt>EXACTLY</dt>
13747 * <dd>
13748 * The parent has determined an exact size for the child. The child is going to be
13749 * given those bounds regardless of how big it wants to be.
13750 * </dd>
13751 *
13752 * <dt>AT_MOST</dt>
13753 * <dd>
13754 * The child can be as large as it wants up to the specified size.
13755 * </dd>
13756 * </dl>
13757 *
13758 * MeasureSpecs are implemented as ints to reduce object allocation. This class
13759 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
13760 */
13761 public static class MeasureSpec {
13762 private static final int MODE_SHIFT = 30;
13763 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
13764
13765 /**
13766 * Measure specification mode: The parent has not imposed any constraint
13767 * on the child. It can be whatever size it wants.
13768 */
13769 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
13770
13771 /**
13772 * Measure specification mode: The parent has determined an exact size
13773 * for the child. The child is going to be given those bounds regardless
13774 * of how big it wants to be.
13775 */
13776 public static final int EXACTLY = 1 << MODE_SHIFT;
13777
13778 /**
13779 * Measure specification mode: The child can be as large as it wants up
13780 * to the specified size.
13781 */
13782 public static final int AT_MOST = 2 << MODE_SHIFT;
13783
13784 /**
13785 * Creates a measure specification based on the supplied size and mode.
13786 *
13787 * The mode must always be one of the following:
13788 * <ul>
13789 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
13790 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
13791 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
13792 * </ul>
13793 *
13794 * @param size the size of the measure specification
13795 * @param mode the mode of the measure specification
13796 * @return the measure specification based on size and mode
13797 */
13798 public static int makeMeasureSpec(int size, int mode) {
13799 return size + mode;
13800 }
13801
13802 /**
13803 * Extracts the mode from the supplied measure specification.
13804 *
13805 * @param measureSpec the measure specification to extract the mode from
13806 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
13807 * {@link android.view.View.MeasureSpec#AT_MOST} or
13808 * {@link android.view.View.MeasureSpec#EXACTLY}
13809 */
13810 public static int getMode(int measureSpec) {
13811 return (measureSpec & MODE_MASK);
13812 }
13813
13814 /**
13815 * Extracts the size from the supplied measure specification.
13816 *
13817 * @param measureSpec the measure specification to extract the size from
13818 * @return the size in pixels defined in the supplied measure specification
13819 */
13820 public static int getSize(int measureSpec) {
13821 return (measureSpec & ~MODE_MASK);
13822 }
13823
13824 /**
13825 * Returns a String representation of the specified measure
13826 * specification.
13827 *
13828 * @param measureSpec the measure specification to convert to a String
13829 * @return a String with the following format: "MeasureSpec: MODE SIZE"
13830 */
13831 public static String toString(int measureSpec) {
13832 int mode = getMode(measureSpec);
13833 int size = getSize(measureSpec);
13834
13835 StringBuilder sb = new StringBuilder("MeasureSpec: ");
13836
13837 if (mode == UNSPECIFIED)
13838 sb.append("UNSPECIFIED ");
13839 else if (mode == EXACTLY)
13840 sb.append("EXACTLY ");
13841 else if (mode == AT_MOST)
13842 sb.append("AT_MOST ");
13843 else
13844 sb.append(mode).append(" ");
13845
13846 sb.append(size);
13847 return sb.toString();
13848 }
13849 }
13850
13851 class CheckForLongPress implements Runnable {
13852
13853 private int mOriginalWindowAttachCount;
13854
13855 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070013856 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013857 && mOriginalWindowAttachCount == mWindowAttachCount) {
13858 if (performLongClick()) {
13859 mHasPerformedLongPress = true;
13860 }
13861 }
13862 }
13863
13864 public void rememberWindowAttachCount() {
13865 mOriginalWindowAttachCount = mWindowAttachCount;
13866 }
13867 }
Joe Malin32736f02011-01-19 16:14:20 -080013868
Adam Powelle14579b2009-12-16 18:39:52 -080013869 private final class CheckForTap implements Runnable {
13870 public void run() {
13871 mPrivateFlags &= ~PREPRESSED;
13872 mPrivateFlags |= PRESSED;
13873 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013874 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080013875 }
13876 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013877
Adam Powella35d7682010-03-12 14:48:13 -080013878 private final class PerformClick implements Runnable {
13879 public void run() {
13880 performClick();
13881 }
13882 }
13883
Dianne Hackborn63042d62011-01-26 18:56:29 -080013884 /** @hide */
13885 public void hackTurnOffWindowResizeAnim(boolean off) {
13886 mAttachInfo.mTurnOffWindowResizeAnim = off;
13887 }
Joe Malin32736f02011-01-19 16:14:20 -080013888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013889 /**
Chet Haasea00f3862011-02-22 06:34:40 -080013890 * This method returns a ViewPropertyAnimator object, which can be used to animate
13891 * specific properties on this View.
13892 *
13893 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
13894 */
13895 public ViewPropertyAnimator animate() {
13896 if (mAnimator == null) {
13897 mAnimator = new ViewPropertyAnimator(this);
13898 }
13899 return mAnimator;
13900 }
13901
13902 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013903 * Interface definition for a callback to be invoked when a key event is
13904 * dispatched to this view. The callback will be invoked before the key
13905 * event is given to the view.
13906 */
13907 public interface OnKeyListener {
13908 /**
13909 * Called when a key is dispatched to a view. This allows listeners to
13910 * get a chance to respond before the target view.
13911 *
13912 * @param v The view the key has been dispatched to.
13913 * @param keyCode The code for the physical key that was pressed
13914 * @param event The KeyEvent object containing full information about
13915 * the event.
13916 * @return True if the listener has consumed the event, false otherwise.
13917 */
13918 boolean onKey(View v, int keyCode, KeyEvent event);
13919 }
13920
13921 /**
13922 * Interface definition for a callback to be invoked when a touch event is
13923 * dispatched to this view. The callback will be invoked before the touch
13924 * event is given to the view.
13925 */
13926 public interface OnTouchListener {
13927 /**
13928 * Called when a touch event is dispatched to a view. This allows listeners to
13929 * get a chance to respond before the target view.
13930 *
13931 * @param v The view the touch event has been dispatched to.
13932 * @param event The MotionEvent object containing full information about
13933 * the event.
13934 * @return True if the listener has consumed the event, false otherwise.
13935 */
13936 boolean onTouch(View v, MotionEvent event);
13937 }
13938
13939 /**
Jeff Brown10b62902011-06-20 16:40:37 -070013940 * Interface definition for a callback to be invoked when a hover event is
13941 * dispatched to this view. The callback will be invoked before the hover
13942 * event is given to the view.
13943 */
13944 public interface OnHoverListener {
13945 /**
13946 * Called when a hover event is dispatched to a view. This allows listeners to
13947 * get a chance to respond before the target view.
13948 *
13949 * @param v The view the hover event has been dispatched to.
13950 * @param event The MotionEvent object containing full information about
13951 * the event.
13952 * @return True if the listener has consumed the event, false otherwise.
13953 */
13954 boolean onHover(View v, MotionEvent event);
13955 }
13956
13957 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013958 * Interface definition for a callback to be invoked when a generic motion event is
13959 * dispatched to this view. The callback will be invoked before the generic motion
13960 * event is given to the view.
13961 */
13962 public interface OnGenericMotionListener {
13963 /**
13964 * Called when a generic motion event is dispatched to a view. This allows listeners to
13965 * get a chance to respond before the target view.
13966 *
13967 * @param v The view the generic motion event has been dispatched to.
13968 * @param event The MotionEvent object containing full information about
13969 * the event.
13970 * @return True if the listener has consumed the event, false otherwise.
13971 */
13972 boolean onGenericMotion(View v, MotionEvent event);
13973 }
13974
13975 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013976 * Interface definition for a callback to be invoked when a view has been clicked and held.
13977 */
13978 public interface OnLongClickListener {
13979 /**
13980 * Called when a view has been clicked and held.
13981 *
13982 * @param v The view that was clicked and held.
13983 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080013984 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013985 */
13986 boolean onLongClick(View v);
13987 }
13988
13989 /**
Chris Tate32affef2010-10-18 15:29:21 -070013990 * Interface definition for a callback to be invoked when a drag is being dispatched
13991 * to this view. The callback will be invoked before the hosting view's own
13992 * onDrag(event) method. If the listener wants to fall back to the hosting view's
13993 * onDrag(event) behavior, it should return 'false' from this callback.
13994 */
13995 public interface OnDragListener {
13996 /**
13997 * Called when a drag event is dispatched to a view. This allows listeners
13998 * to get a chance to override base View behavior.
13999 *
Joe Malin32736f02011-01-19 16:14:20 -080014000 * @param v The View that received the drag event.
14001 * @param event The {@link android.view.DragEvent} object for the drag event.
14002 * @return {@code true} if the drag event was handled successfully, or {@code false}
14003 * if the drag event was not handled. Note that {@code false} will trigger the View
14004 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070014005 */
14006 boolean onDrag(View v, DragEvent event);
14007 }
14008
14009 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014010 * Interface definition for a callback to be invoked when the focus state of
14011 * a view changed.
14012 */
14013 public interface OnFocusChangeListener {
14014 /**
14015 * Called when the focus state of a view has changed.
14016 *
14017 * @param v The view whose state has changed.
14018 * @param hasFocus The new focus state of v.
14019 */
14020 void onFocusChange(View v, boolean hasFocus);
14021 }
14022
14023 /**
14024 * Interface definition for a callback to be invoked when a view is clicked.
14025 */
14026 public interface OnClickListener {
14027 /**
14028 * Called when a view has been clicked.
14029 *
14030 * @param v The view that was clicked.
14031 */
14032 void onClick(View v);
14033 }
14034
14035 /**
14036 * Interface definition for a callback to be invoked when the context menu
14037 * for this view is being built.
14038 */
14039 public interface OnCreateContextMenuListener {
14040 /**
14041 * Called when the context menu for this view is being built. It is not
14042 * safe to hold onto the menu after this method returns.
14043 *
14044 * @param menu The context menu that is being built
14045 * @param v The view for which the context menu is being built
14046 * @param menuInfo Extra information about the item for which the
14047 * context menu should be shown. This information will vary
14048 * depending on the class of v.
14049 */
14050 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
14051 }
14052
Joe Onorato664644d2011-01-23 17:53:23 -080014053 /**
14054 * Interface definition for a callback to be invoked when the status bar changes
14055 * visibility.
14056 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070014057 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080014058 */
14059 public interface OnSystemUiVisibilityChangeListener {
14060 /**
14061 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070014062 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080014063 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040014064 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
14065 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080014066 */
14067 public void onSystemUiVisibilityChange(int visibility);
14068 }
14069
Adam Powell4afd62b2011-02-18 15:02:18 -080014070 /**
14071 * Interface definition for a callback to be invoked when this view is attached
14072 * or detached from its window.
14073 */
14074 public interface OnAttachStateChangeListener {
14075 /**
14076 * Called when the view is attached to a window.
14077 * @param v The view that was attached
14078 */
14079 public void onViewAttachedToWindow(View v);
14080 /**
14081 * Called when the view is detached from a window.
14082 * @param v The view that was detached
14083 */
14084 public void onViewDetachedFromWindow(View v);
14085 }
14086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014087 private final class UnsetPressedState implements Runnable {
14088 public void run() {
14089 setPressed(false);
14090 }
14091 }
14092
14093 /**
14094 * Base class for derived classes that want to save and restore their own
14095 * state in {@link android.view.View#onSaveInstanceState()}.
14096 */
14097 public static class BaseSavedState extends AbsSavedState {
14098 /**
14099 * Constructor used when reading from a parcel. Reads the state of the superclass.
14100 *
14101 * @param source
14102 */
14103 public BaseSavedState(Parcel source) {
14104 super(source);
14105 }
14106
14107 /**
14108 * Constructor called by derived classes when creating their SavedState objects
14109 *
14110 * @param superState The state of the superclass of this view
14111 */
14112 public BaseSavedState(Parcelable superState) {
14113 super(superState);
14114 }
14115
14116 public static final Parcelable.Creator<BaseSavedState> CREATOR =
14117 new Parcelable.Creator<BaseSavedState>() {
14118 public BaseSavedState createFromParcel(Parcel in) {
14119 return new BaseSavedState(in);
14120 }
14121
14122 public BaseSavedState[] newArray(int size) {
14123 return new BaseSavedState[size];
14124 }
14125 };
14126 }
14127
14128 /**
14129 * A set of information given to a view when it is attached to its parent
14130 * window.
14131 */
14132 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014133 interface Callbacks {
14134 void playSoundEffect(int effectId);
14135 boolean performHapticFeedback(int effectId, boolean always);
14136 }
14137
14138 /**
14139 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
14140 * to a Handler. This class contains the target (View) to invalidate and
14141 * the coordinates of the dirty rectangle.
14142 *
14143 * For performance purposes, this class also implements a pool of up to
14144 * POOL_LIMIT objects that get reused. This reduces memory allocations
14145 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014146 */
Romain Guyd928d682009-03-31 17:52:16 -070014147 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014148 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070014149 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
14150 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070014151 public InvalidateInfo newInstance() {
14152 return new InvalidateInfo();
14153 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014154
Romain Guyd928d682009-03-31 17:52:16 -070014155 public void onAcquired(InvalidateInfo element) {
14156 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014157
Romain Guyd928d682009-03-31 17:52:16 -070014158 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070014159 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070014160 }
14161 }, POOL_LIMIT)
14162 );
14163
14164 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014165 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014166
14167 View target;
14168
14169 int left;
14170 int top;
14171 int right;
14172 int bottom;
14173
Romain Guyd928d682009-03-31 17:52:16 -070014174 public void setNextPoolable(InvalidateInfo element) {
14175 mNext = element;
14176 }
14177
14178 public InvalidateInfo getNextPoolable() {
14179 return mNext;
14180 }
14181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014182 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070014183 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014184 }
14185
14186 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070014187 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014188 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014189
14190 public boolean isPooled() {
14191 return mIsPooled;
14192 }
14193
14194 public void setPooled(boolean isPooled) {
14195 mIsPooled = isPooled;
14196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014197 }
14198
14199 final IWindowSession mSession;
14200
14201 final IWindow mWindow;
14202
14203 final IBinder mWindowToken;
14204
14205 final Callbacks mRootCallbacks;
14206
Romain Guy59a12ca2011-06-09 17:48:21 -070014207 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080014208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014209 /**
14210 * The top view of the hierarchy.
14211 */
14212 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070014213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014214 IBinder mPanelParentWindowToken;
14215 Surface mSurface;
14216
Romain Guyb051e892010-09-28 19:09:36 -070014217 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014218 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070014219 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080014220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014221 /**
Romain Guy8506ab42009-06-11 17:35:47 -070014222 * Scale factor used by the compatibility mode
14223 */
14224 float mApplicationScale;
14225
14226 /**
14227 * Indicates whether the application is in compatibility mode
14228 */
14229 boolean mScalingRequired;
14230
14231 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014232 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080014233 */
14234 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080014235
Dianne Hackborn63042d62011-01-26 18:56:29 -080014236 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014237 * Left position of this view's window
14238 */
14239 int mWindowLeft;
14240
14241 /**
14242 * Top position of this view's window
14243 */
14244 int mWindowTop;
14245
14246 /**
Adam Powell26153a32010-11-08 15:22:27 -080014247 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070014248 */
Adam Powell26153a32010-11-08 15:22:27 -080014249 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070014250
14251 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014252 * For windows that are full-screen but using insets to layout inside
14253 * of the screen decorations, these are the current insets for the
14254 * content of the window.
14255 */
14256 final Rect mContentInsets = new Rect();
14257
14258 /**
14259 * For windows that are full-screen but using insets to layout inside
14260 * of the screen decorations, these are the current insets for the
14261 * actual visible parts of the window.
14262 */
14263 final Rect mVisibleInsets = new Rect();
14264
14265 /**
14266 * The internal insets given by this window. This value is
14267 * supplied by the client (through
14268 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
14269 * be given to the window manager when changed to be used in laying
14270 * out windows behind it.
14271 */
14272 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
14273 = new ViewTreeObserver.InternalInsetsInfo();
14274
14275 /**
14276 * All views in the window's hierarchy that serve as scroll containers,
14277 * used to determine if the window can be resized or must be panned
14278 * to adjust for a soft input area.
14279 */
14280 final ArrayList<View> mScrollContainers = new ArrayList<View>();
14281
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070014282 final KeyEvent.DispatcherState mKeyDispatchState
14283 = new KeyEvent.DispatcherState();
14284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014285 /**
14286 * Indicates whether the view's window currently has the focus.
14287 */
14288 boolean mHasWindowFocus;
14289
14290 /**
14291 * The current visibility of the window.
14292 */
14293 int mWindowVisibility;
14294
14295 /**
14296 * Indicates the time at which drawing started to occur.
14297 */
14298 long mDrawingTime;
14299
14300 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070014301 * Indicates whether or not ignoring the DIRTY_MASK flags.
14302 */
14303 boolean mIgnoreDirtyState;
14304
14305 /**
Romain Guy02ccac62011-06-24 13:20:23 -070014306 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
14307 * to avoid clearing that flag prematurely.
14308 */
14309 boolean mSetIgnoreDirtyState = false;
14310
14311 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014312 * Indicates whether the view's window is currently in touch mode.
14313 */
14314 boolean mInTouchMode;
14315
14316 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014317 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014318 * the next time it performs a traversal
14319 */
14320 boolean mRecomputeGlobalAttributes;
14321
14322 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014323 * Set during a traveral if any views want to keep the screen on.
14324 */
14325 boolean mKeepScreenOn;
14326
14327 /**
Joe Onorato664644d2011-01-23 17:53:23 -080014328 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
14329 */
14330 int mSystemUiVisibility;
14331
14332 /**
14333 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
14334 * attached.
14335 */
14336 boolean mHasSystemUiListeners;
14337
14338 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014339 * Set if the visibility of any views has changed.
14340 */
14341 boolean mViewVisibilityChanged;
14342
14343 /**
14344 * Set to true if a view has been scrolled.
14345 */
14346 boolean mViewScrollChanged;
14347
14348 /**
14349 * Global to the view hierarchy used as a temporary for dealing with
14350 * x/y points in the transparent region computations.
14351 */
14352 final int[] mTransparentLocation = new int[2];
14353
14354 /**
14355 * Global to the view hierarchy used as a temporary for dealing with
14356 * x/y points in the ViewGroup.invalidateChild implementation.
14357 */
14358 final int[] mInvalidateChildLocation = new int[2];
14359
Chet Haasec3aa3612010-06-17 08:50:37 -070014360
14361 /**
14362 * Global to the view hierarchy used as a temporary for dealing with
14363 * x/y location when view is transformed.
14364 */
14365 final float[] mTmpTransformLocation = new float[2];
14366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014367 /**
14368 * The view tree observer used to dispatch global events like
14369 * layout, pre-draw, touch mode change, etc.
14370 */
14371 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
14372
14373 /**
14374 * A Canvas used by the view hierarchy to perform bitmap caching.
14375 */
14376 Canvas mCanvas;
14377
14378 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014379 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014380 * handler can be used to pump events in the UI events queue.
14381 */
14382 final Handler mHandler;
14383
14384 /**
14385 * Identifier for messages requesting the view to be invalidated.
14386 * Such messages should be sent to {@link #mHandler}.
14387 */
14388 static final int INVALIDATE_MSG = 0x1;
14389
14390 /**
14391 * Identifier for messages requesting the view to invalidate a region.
14392 * Such messages should be sent to {@link #mHandler}.
14393 */
14394 static final int INVALIDATE_RECT_MSG = 0x2;
14395
14396 /**
14397 * Temporary for use in computing invalidate rectangles while
14398 * calling up the hierarchy.
14399 */
14400 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070014401
14402 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070014403 * Temporary for use in computing hit areas with transformed views
14404 */
14405 final RectF mTmpTransformRect = new RectF();
14406
14407 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070014408 * Temporary list for use in collecting focusable descendents of a view.
14409 */
14410 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
14411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014412 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014413 * The id of the window for accessibility purposes.
14414 */
14415 int mAccessibilityWindowId = View.NO_ID;
14416
14417 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014418 * Creates a new set of attachment information with the specified
14419 * events handler and thread.
14420 *
14421 * @param handler the events handler the view must use
14422 */
14423 AttachInfo(IWindowSession session, IWindow window,
14424 Handler handler, Callbacks effectPlayer) {
14425 mSession = session;
14426 mWindow = window;
14427 mWindowToken = window.asBinder();
14428 mHandler = handler;
14429 mRootCallbacks = effectPlayer;
14430 }
14431 }
14432
14433 /**
14434 * <p>ScrollabilityCache holds various fields used by a View when scrolling
14435 * is supported. This avoids keeping too many unused fields in most
14436 * instances of View.</p>
14437 */
Mike Cleronf116bf82009-09-27 19:14:12 -070014438 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080014439
Mike Cleronf116bf82009-09-27 19:14:12 -070014440 /**
14441 * Scrollbars are not visible
14442 */
14443 public static final int OFF = 0;
14444
14445 /**
14446 * Scrollbars are visible
14447 */
14448 public static final int ON = 1;
14449
14450 /**
14451 * Scrollbars are fading away
14452 */
14453 public static final int FADING = 2;
14454
14455 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080014456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014457 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070014458 public int scrollBarDefaultDelayBeforeFade;
14459 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014460
14461 public int scrollBarSize;
14462 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070014463 public float[] interpolatorValues;
14464 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014465
14466 public final Paint paint;
14467 public final Matrix matrix;
14468 public Shader shader;
14469
Mike Cleronf116bf82009-09-27 19:14:12 -070014470 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
14471
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014472 private static final float[] OPAQUE = { 255 };
14473 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080014474
Mike Cleronf116bf82009-09-27 19:14:12 -070014475 /**
14476 * When fading should start. This time moves into the future every time
14477 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
14478 */
14479 public long fadeStartTime;
14480
14481
14482 /**
14483 * The current state of the scrollbars: ON, OFF, or FADING
14484 */
14485 public int state = OFF;
14486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014487 private int mLastColor;
14488
Mike Cleronf116bf82009-09-27 19:14:12 -070014489 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014490 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
14491 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070014492 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
14493 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014494
14495 paint = new Paint();
14496 matrix = new Matrix();
14497 // use use a height of 1, and then wack the matrix each time we
14498 // actually use it.
14499 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014501 paint.setShader(shader);
14502 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070014503 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014504 }
Romain Guy8506ab42009-06-11 17:35:47 -070014505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014506 public void setFadeColor(int color) {
14507 if (color != 0 && color != mLastColor) {
14508 mLastColor = color;
14509 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070014510
Romain Guye55e1a72009-08-27 10:42:26 -070014511 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
14512 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014514 paint.setShader(shader);
14515 // Restore the default transfer mode (src_over)
14516 paint.setXfermode(null);
14517 }
14518 }
Joe Malin32736f02011-01-19 16:14:20 -080014519
Mike Cleronf116bf82009-09-27 19:14:12 -070014520 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070014521 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070014522 if (now >= fadeStartTime) {
14523
14524 // the animation fades the scrollbars out by changing
14525 // the opacity (alpha) from fully opaque to fully
14526 // transparent
14527 int nextFrame = (int) now;
14528 int framesCount = 0;
14529
14530 Interpolator interpolator = scrollBarInterpolator;
14531
14532 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014533 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070014534
14535 // End transparent
14536 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014537 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070014538
14539 state = FADING;
14540
14541 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080014542 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070014543 }
14544 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070014545 }
Mike Cleronf116bf82009-09-27 19:14:12 -070014546
Svetoslav Ganova0156172011-06-26 17:55:44 -070014547 /**
14548 * Resuable callback for sending
14549 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
14550 */
14551 private class SendViewScrolledAccessibilityEvent implements Runnable {
14552 public volatile boolean mIsPending;
14553
14554 public void run() {
14555 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
14556 mIsPending = false;
14557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014558 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070014559
14560 /**
14561 * <p>
14562 * This class represents a delegate that can be registered in a {@link View}
14563 * to enhance accessibility support via composition rather via inheritance.
14564 * It is specifically targeted to widget developers that extend basic View
14565 * classes i.e. classes in package android.view, that would like their
14566 * applications to be backwards compatible.
14567 * </p>
14568 * <p>
14569 * A scenario in which a developer would like to use an accessibility delegate
14570 * is overriding a method introduced in a later API version then the minimal API
14571 * version supported by the application. For example, the method
14572 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
14573 * in API version 4 when the accessibility APIs were first introduced. If a
14574 * developer would like his application to run on API version 4 devices (assuming
14575 * all other APIs used by the application are version 4 or lower) and take advantage
14576 * of this method, instead of overriding the method which would break the application's
14577 * backwards compatibility, he can override the corresponding method in this
14578 * delegate and register the delegate in the target View if the API version of
14579 * the system is high enough i.e. the API version is same or higher to the API
14580 * version that introduced
14581 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
14582 * </p>
14583 * <p>
14584 * Here is an example implementation:
14585 * </p>
14586 * <code><pre><p>
14587 * if (Build.VERSION.SDK_INT >= 14) {
14588 * // If the API version is equal of higher than the version in
14589 * // which onInitializeAccessibilityNodeInfo was introduced we
14590 * // register a delegate with a customized implementation.
14591 * View view = findViewById(R.id.view_id);
14592 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
14593 * public void onInitializeAccessibilityNodeInfo(View host,
14594 * AccessibilityNodeInfo info) {
14595 * // Let the default implementation populate the info.
14596 * super.onInitializeAccessibilityNodeInfo(host, info);
14597 * // Set some other information.
14598 * info.setEnabled(host.isEnabled());
14599 * }
14600 * });
14601 * }
14602 * </code></pre></p>
14603 * <p>
14604 * This delegate contains methods that correspond to the accessibility methods
14605 * in View. If a delegate has been specified the implementation in View hands
14606 * off handling to the corresponding method in this delegate. The default
14607 * implementation the delegate methods behaves exactly as the corresponding
14608 * method in View for the case of no accessibility delegate been set. Hence,
14609 * to customize the behavior of a View method, clients can override only the
14610 * corresponding delegate method without altering the behavior of the rest
14611 * accessibility related methods of the host view.
14612 * </p>
14613 */
14614 public static class AccessibilityDelegate {
14615
14616 /**
14617 * Sends an accessibility event of the given type. If accessibility is not
14618 * enabled this method has no effect.
14619 * <p>
14620 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
14621 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
14622 * been set.
14623 * </p>
14624 *
14625 * @param host The View hosting the delegate.
14626 * @param eventType The type of the event to send.
14627 *
14628 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
14629 */
14630 public void sendAccessibilityEvent(View host, int eventType) {
14631 host.sendAccessibilityEventInternal(eventType);
14632 }
14633
14634 /**
14635 * Sends an accessibility event. This method behaves exactly as
14636 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
14637 * empty {@link AccessibilityEvent} and does not perform a check whether
14638 * accessibility is enabled.
14639 * <p>
14640 * The default implementation behaves as
14641 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14642 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
14643 * the case of no accessibility delegate been set.
14644 * </p>
14645 *
14646 * @param host The View hosting the delegate.
14647 * @param event The event to send.
14648 *
14649 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14650 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14651 */
14652 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
14653 host.sendAccessibilityEventUncheckedInternal(event);
14654 }
14655
14656 /**
14657 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
14658 * to its children for adding their text content to the event.
14659 * <p>
14660 * The default implementation behaves as
14661 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14662 * View#dispatchPopulateAccessibilityEvent(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.
14668 * @return True if the event population was completed.
14669 *
14670 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14671 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14672 */
14673 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14674 return host.dispatchPopulateAccessibilityEventInternal(event);
14675 }
14676
14677 /**
14678 * Gives a chance to the host View to populate the accessibility event with its
14679 * text content.
14680 * <p>
14681 * The default implementation behaves as
14682 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
14683 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
14684 * the case of no accessibility delegate been set.
14685 * </p>
14686 *
14687 * @param host The View hosting the delegate.
14688 * @param event The accessibility event which to populate.
14689 *
14690 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
14691 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
14692 */
14693 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14694 host.onPopulateAccessibilityEventInternal(event);
14695 }
14696
14697 /**
14698 * Initializes an {@link AccessibilityEvent} with information about the
14699 * the host View which is the event source.
14700 * <p>
14701 * The default implementation behaves as
14702 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
14703 * View#onInitializeAccessibilityEvent(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 event to initialize.
14709 *
14710 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
14711 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
14712 */
14713 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
14714 host.onInitializeAccessibilityEventInternal(event);
14715 }
14716
14717 /**
14718 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
14719 * <p>
14720 * The default implementation behaves as
14721 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14722 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
14723 * the case of no accessibility delegate been set.
14724 * </p>
14725 *
14726 * @param host The View hosting the delegate.
14727 * @param info The instance to initialize.
14728 *
14729 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14730 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14731 */
14732 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
14733 host.onInitializeAccessibilityNodeInfoInternal(info);
14734 }
14735
14736 /**
14737 * Called when a child of the host View has requested sending an
14738 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
14739 * to augment the event.
14740 * <p>
14741 * The default implementation behaves as
14742 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14743 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
14744 * the case of no accessibility delegate been set.
14745 * </p>
14746 *
14747 * @param host The View hosting the delegate.
14748 * @param child The child which requests sending the event.
14749 * @param event The event to be sent.
14750 * @return True if the event should be sent
14751 *
14752 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14753 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14754 */
14755 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
14756 AccessibilityEvent event) {
14757 return host.onRequestSendAccessibilityEventInternal(child, event);
14758 }
14759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014760}