blob: ec9a2d0e99b56ce41a9a0ccb189d08c467af7c05 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Christopher Tatea53146c2010-09-07 11:57:52 -070019import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070025import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.Canvas;
Mike Cleronf116bf82009-09-27 19:14:12 -070027import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.graphics.LinearGradient;
29import android.graphics.Matrix;
30import android.graphics.Paint;
31import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070032import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.graphics.PorterDuff;
34import android.graphics.PorterDuffXfermode;
35import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070036import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.graphics.Region;
38import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.graphics.drawable.ColorDrawable;
40import android.graphics.drawable.Drawable;
41import android.os.Handler;
42import android.os.IBinder;
43import android.os.Message;
44import android.os.Parcel;
45import android.os.Parcelable;
46import android.os.RemoteException;
47import android.os.SystemClock;
Svetoslav Ganovea515ae2011-09-14 18:15:32 -070048import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070050import android.util.FloatProperty;
51import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070053import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070054import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070055import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070057import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080059import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070061import android.view.accessibility.AccessibilityEvent;
62import android.view.accessibility.AccessibilityEventSource;
63import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070064import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070066import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070067import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.inputmethod.InputConnection;
69import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.widget.ScrollBarDrawable;
71
Romain Guy1ef3fdb2011-09-09 15:30:30 -070072import static android.os.Build.VERSION_CODES.*;
73
Doug Feltcb3791202011-07-07 11:57:48 -070074import com.android.internal.R;
75import com.android.internal.util.Predicate;
76import com.android.internal.view.menu.MenuBuilder;
77
Christopher Tatea0374192010-10-05 13:06:41 -070078import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070079import java.lang.reflect.InvocationTargetException;
80import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import java.util.ArrayList;
82import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070083import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080084import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085
86/**
87 * <p>
88 * This class represents the basic building block for user interface components. A View
89 * occupies a rectangular area on the screen and is responsible for drawing and
90 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070091 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
93 * are invisible containers that hold other Views (or other ViewGroups) and define
94 * their layout properties.
95 * </p>
96 *
97 * <div class="special">
Romain Guy8506ab42009-06-11 17:35:47 -070098 * <p>For an introduction to using this class to develop your
99 * application's user interface, read the Developer Guide documentation on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics
Romain Guy8506ab42009-06-11 17:35:47 -0700101 * include:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>
103 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>
104 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a>
105 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a>
106 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a>
107 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>
108 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>
109 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>.
110 * </p>
111 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700112 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 * <a name="Using"></a>
114 * <h3>Using Views</h3>
115 * <p>
116 * All of the views in a window are arranged in a single tree. You can add views
117 * either from code or by specifying a tree of views in one or more XML layout
118 * files. There are many specialized subclasses of views that act as controls or
119 * are capable of displaying text, images, or other content.
120 * </p>
121 * <p>
122 * Once you have created a tree of views, there are typically a few types of
123 * common operations you may wish to perform:
124 * <ul>
125 * <li><strong>Set properties:</strong> for example setting the text of a
126 * {@link android.widget.TextView}. The available properties and the methods
127 * that set them will vary among the different subclasses of views. Note that
128 * properties that are known at build time can be set in the XML layout
129 * files.</li>
130 * <li><strong>Set focus:</strong> The framework will handled moving focus in
131 * response to user input. To force focus to a specific view, call
132 * {@link #requestFocus}.</li>
133 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
134 * that will be notified when something interesting happens to the view. For
135 * example, all views will let you set a listener to be notified when the view
136 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700137 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
138 * Other view subclasses offer more specialized listeners. For example, a Button
139 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700141 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 * </ul>
143 * </p>
144 * <p><em>
145 * Note: The Android framework is responsible for measuring, laying out and
146 * drawing views. You should not call methods that perform these actions on
147 * views yourself unless you are actually implementing a
148 * {@link android.view.ViewGroup}.
149 * </em></p>
150 *
151 * <a name="Lifecycle"></a>
152 * <h3>Implementing a Custom View</h3>
153 *
154 * <p>
155 * To implement a custom view, you will usually begin by providing overrides for
156 * some of the standard methods that the framework calls on all views. You do
157 * not need to override all of these methods. In fact, you can start by just
158 * overriding {@link #onDraw(android.graphics.Canvas)}.
159 * <table border="2" width="85%" align="center" cellpadding="5">
160 * <thead>
161 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
162 * </thead>
163 *
164 * <tbody>
165 * <tr>
166 * <td rowspan="2">Creation</td>
167 * <td>Constructors</td>
168 * <td>There is a form of the constructor that are called when the view
169 * is created from code and a form that is called when the view is
170 * inflated from a layout file. The second form should parse and apply
171 * any attributes defined in the layout file.
172 * </td>
173 * </tr>
174 * <tr>
175 * <td><code>{@link #onFinishInflate()}</code></td>
176 * <td>Called after a view and all of its children has been inflated
177 * from XML.</td>
178 * </tr>
179 *
180 * <tr>
181 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700182 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 * <td>Called to determine the size requirements for this view and all
184 * of its children.
185 * </td>
186 * </tr>
187 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700188 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 * <td>Called when this view should assign a size and position to all
190 * of its children.
191 * </td>
192 * </tr>
193 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700194 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 * <td>Called when the size of this view has changed.
196 * </td>
197 * </tr>
198 *
199 * <tr>
200 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700201 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 * <td>Called when the view should render its content.
203 * </td>
204 * </tr>
205 *
206 * <tr>
207 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700208 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 * <td>Called when a new key event occurs.
210 * </td>
211 * </tr>
212 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700213 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 * <td>Called when a key up event occurs.
215 * </td>
216 * </tr>
217 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700218 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 * <td>Called when a trackball motion event occurs.
220 * </td>
221 * </tr>
222 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700223 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 * <td>Called when a touch screen motion event occurs.
225 * </td>
226 * </tr>
227 *
228 * <tr>
229 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700230 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 * <td>Called when the view gains or loses focus.
232 * </td>
233 * </tr>
234 *
235 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700236 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 * <td>Called when the window containing the view gains or loses focus.
238 * </td>
239 * </tr>
240 *
241 * <tr>
242 * <td rowspan="3">Attaching</td>
243 * <td><code>{@link #onAttachedToWindow()}</code></td>
244 * <td>Called when the view is attached to a window.
245 * </td>
246 * </tr>
247 *
248 * <tr>
249 * <td><code>{@link #onDetachedFromWindow}</code></td>
250 * <td>Called when the view is detached from its window.
251 * </td>
252 * </tr>
253 *
254 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700255 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 * <td>Called when the visibility of the window containing the view
257 * has changed.
258 * </td>
259 * </tr>
260 * </tbody>
261 *
262 * </table>
263 * </p>
264 *
265 * <a name="IDs"></a>
266 * <h3>IDs</h3>
267 * Views may have an integer id associated with them. These ids are typically
268 * assigned in the layout XML files, and are used to find specific views within
269 * the view tree. A common pattern is to:
270 * <ul>
271 * <li>Define a Button in the layout file and assign it a unique ID.
272 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700273 * &lt;Button
274 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 * android:layout_width="wrap_content"
276 * android:layout_height="wrap_content"
277 * android:text="@string/my_button_text"/&gt;
278 * </pre></li>
279 * <li>From the onCreate method of an Activity, find the Button
280 * <pre class="prettyprint">
281 * Button myButton = (Button) findViewById(R.id.my_button);
282 * </pre></li>
283 * </ul>
284 * <p>
285 * View IDs need not be unique throughout the tree, but it is good practice to
286 * ensure that they are at least unique within the part of the tree you are
287 * searching.
288 * </p>
289 *
290 * <a name="Position"></a>
291 * <h3>Position</h3>
292 * <p>
293 * The geometry of a view is that of a rectangle. A view has a location,
294 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
295 * two dimensions, expressed as a width and a height. The unit for location
296 * and dimensions is the pixel.
297 * </p>
298 *
299 * <p>
300 * It is possible to retrieve the location of a view by invoking the methods
301 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
302 * coordinate of the rectangle representing the view. The latter returns the
303 * top, or Y, coordinate of the rectangle representing the view. These methods
304 * both return the location of the view relative to its parent. For instance,
305 * when getLeft() returns 20, that means the view is located 20 pixels to the
306 * right of the left edge of its direct parent.
307 * </p>
308 *
309 * <p>
310 * In addition, several convenience methods are offered to avoid unnecessary
311 * computations, namely {@link #getRight()} and {@link #getBottom()}.
312 * These methods return the coordinates of the right and bottom edges of the
313 * rectangle representing the view. For instance, calling {@link #getRight()}
314 * is similar to the following computation: <code>getLeft() + getWidth()</code>
315 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
316 * </p>
317 *
318 * <a name="SizePaddingMargins"></a>
319 * <h3>Size, padding and margins</h3>
320 * <p>
321 * The size of a view is expressed with a width and a height. A view actually
322 * possess two pairs of width and height values.
323 * </p>
324 *
325 * <p>
326 * The first pair is known as <em>measured width</em> and
327 * <em>measured height</em>. These dimensions define how big a view wants to be
328 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
329 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
330 * and {@link #getMeasuredHeight()}.
331 * </p>
332 *
333 * <p>
334 * The second pair is simply known as <em>width</em> and <em>height</em>, or
335 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
336 * dimensions define the actual size of the view on screen, at drawing time and
337 * after layout. These values may, but do not have to, be different from the
338 * measured width and height. The width and height can be obtained by calling
339 * {@link #getWidth()} and {@link #getHeight()}.
340 * </p>
341 *
342 * <p>
343 * To measure its dimensions, a view takes into account its padding. The padding
344 * is expressed in pixels for the left, top, right and bottom parts of the view.
345 * Padding can be used to offset the content of the view by a specific amount of
346 * pixels. For instance, a left padding of 2 will push the view's content by
347 * 2 pixels to the right of the left edge. Padding can be set using the
348 * {@link #setPadding(int, int, int, int)} method and queried by calling
349 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
Fabrice Di Megliod8703a92011-06-16 18:54:08 -0700350 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 * </p>
352 *
353 * <p>
354 * Even though a view can define a padding, it does not provide any support for
355 * margins. However, view groups provide such a support. Refer to
356 * {@link android.view.ViewGroup} and
357 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
358 * </p>
359 *
360 * <a name="Layout"></a>
361 * <h3>Layout</h3>
362 * <p>
363 * Layout is a two pass process: a measure pass and a layout pass. The measuring
364 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
365 * of the view tree. Each view pushes dimension specifications down the tree
366 * during the recursion. At the end of the measure pass, every view has stored
367 * its measurements. The second pass happens in
368 * {@link #layout(int,int,int,int)} and is also top-down. During
369 * this pass each parent is responsible for positioning all of its children
370 * using the sizes computed in the measure pass.
371 * </p>
372 *
373 * <p>
374 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
375 * {@link #getMeasuredHeight()} values must be set, along with those for all of
376 * that view's descendants. A view's measured width and measured height values
377 * must respect the constraints imposed by the view's parents. This guarantees
378 * that at the end of the measure pass, all parents accept all of their
379 * children's measurements. A parent view may call measure() more than once on
380 * its children. For example, the parent may measure each child once with
381 * unspecified dimensions to find out how big they want to be, then call
382 * measure() on them again with actual numbers if the sum of all the children's
383 * unconstrained sizes is too big or too small.
384 * </p>
385 *
386 * <p>
387 * The measure pass uses two classes to communicate dimensions. The
388 * {@link MeasureSpec} class is used by views to tell their parents how they
389 * want to be measured and positioned. The base LayoutParams class just
390 * describes how big the view wants to be for both width and height. For each
391 * dimension, it can specify one of:
392 * <ul>
393 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800394 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 * (minus padding)
396 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
397 * enclose its content (plus padding).
398 * </ul>
399 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
400 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
401 * an X and Y value.
402 * </p>
403 *
404 * <p>
405 * MeasureSpecs are used to push requirements down the tree from parent to
406 * child. A MeasureSpec can be in one of three modes:
407 * <ul>
408 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
409 * of a child view. For example, a LinearLayout may call measure() on its child
410 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
411 * tall the child view wants to be given a width of 240 pixels.
412 * <li>EXACTLY: This is used by the parent to impose an exact size on the
413 * child. The child must use this size, and guarantee that all of its
414 * descendants will fit within this size.
415 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
416 * child. The child must gurantee that it and all of its descendants will fit
417 * within this size.
418 * </ul>
419 * </p>
420 *
421 * <p>
422 * To intiate a layout, call {@link #requestLayout}. This method is typically
423 * called by a view on itself when it believes that is can no longer fit within
424 * its current bounds.
425 * </p>
426 *
427 * <a name="Drawing"></a>
428 * <h3>Drawing</h3>
429 * <p>
430 * Drawing is handled by walking the tree and rendering each view that
431 * intersects the the invalid region. Because the tree is traversed in-order,
432 * this means that parents will draw before (i.e., behind) their children, with
433 * siblings drawn in the order they appear in the tree.
434 * If you set a background drawable for a View, then the View will draw it for you
435 * before calling back to its <code>onDraw()</code> method.
436 * </p>
437 *
438 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700439 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 * </p>
441 *
442 * <p>
443 * To force a view to draw, call {@link #invalidate()}.
444 * </p>
445 *
446 * <a name="EventHandlingThreading"></a>
447 * <h3>Event Handling and Threading</h3>
448 * <p>
449 * The basic cycle of a view is as follows:
450 * <ol>
451 * <li>An event comes in and is dispatched to the appropriate view. The view
452 * handles the event and notifies any listeners.</li>
453 * <li>If in the course of processing the event, the view's bounds may need
454 * to be changed, the view will call {@link #requestLayout()}.</li>
455 * <li>Similarly, if in the course of processing the event the view's appearance
456 * may need to be changed, the view will call {@link #invalidate()}.</li>
457 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
458 * the framework will take care of measuring, laying out, and drawing the tree
459 * as appropriate.</li>
460 * </ol>
461 * </p>
462 *
463 * <p><em>Note: The entire view tree is single threaded. You must always be on
464 * the UI thread when calling any method on any view.</em>
465 * If you are doing work on other threads and want to update the state of a view
466 * from that thread, you should use a {@link Handler}.
467 * </p>
468 *
469 * <a name="FocusHandling"></a>
470 * <h3>Focus Handling</h3>
471 * <p>
472 * The framework will handle routine focus movement in response to user input.
473 * This includes changing the focus as views are removed or hidden, or as new
474 * views become available. Views indicate their willingness to take focus
475 * through the {@link #isFocusable} method. To change whether a view can take
476 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
477 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
478 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
479 * </p>
480 * <p>
481 * Focus movement is based on an algorithm which finds the nearest neighbor in a
482 * given direction. In rare cases, the default algorithm may not match the
483 * intended behavior of the developer. In these situations, you can provide
484 * explicit overrides by using these XML attributes in the layout file:
485 * <pre>
486 * nextFocusDown
487 * nextFocusLeft
488 * nextFocusRight
489 * nextFocusUp
490 * </pre>
491 * </p>
492 *
493 *
494 * <p>
495 * To get a particular view to take focus, call {@link #requestFocus()}.
496 * </p>
497 *
498 * <a name="TouchMode"></a>
499 * <h3>Touch Mode</h3>
500 * <p>
501 * When a user is navigating a user interface via directional keys such as a D-pad, it is
502 * necessary to give focus to actionable items such as buttons so the user can see
503 * what will take input. If the device has touch capabilities, however, and the user
504 * begins interacting with the interface by touching it, it is no longer necessary to
505 * always highlight, or give focus to, a particular view. This motivates a mode
506 * for interaction named 'touch mode'.
507 * </p>
508 * <p>
509 * For a touch capable device, once the user touches the screen, the device
510 * will enter touch mode. From this point onward, only views for which
511 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
512 * Other views that are touchable, like buttons, will not take focus when touched; they will
513 * only fire the on click listeners.
514 * </p>
515 * <p>
516 * Any time a user hits a directional key, such as a D-pad direction, the view device will
517 * exit touch mode, and find a view to take focus, so that the user may resume interacting
518 * with the user interface without touching the screen again.
519 * </p>
520 * <p>
521 * The touch mode state is maintained across {@link android.app.Activity}s. Call
522 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
523 * </p>
524 *
525 * <a name="Scrolling"></a>
526 * <h3>Scrolling</h3>
527 * <p>
528 * The framework provides basic support for views that wish to internally
529 * scroll their content. This includes keeping track of the X and Y scroll
530 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800531 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700532 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 * </p>
534 *
535 * <a name="Tags"></a>
536 * <h3>Tags</h3>
537 * <p>
538 * Unlike IDs, tags are not used to identify views. Tags are essentially an
539 * extra piece of information that can be associated with a view. They are most
540 * often used as a convenience to store data related to views in the views
541 * themselves rather than by putting them in a separate structure.
542 * </p>
543 *
544 * <a name="Animation"></a>
545 * <h3>Animation</h3>
546 * <p>
547 * You can attach an {@link Animation} object to a view using
548 * {@link #setAnimation(Animation)} or
549 * {@link #startAnimation(Animation)}. The animation can alter the scale,
550 * rotation, translation and alpha of a view over time. If the animation is
551 * attached to a view that has children, the animation will affect the entire
552 * subtree rooted by that node. When an animation is started, the framework will
553 * take care of redrawing the appropriate views until the animation completes.
554 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800555 * <p>
556 * Starting with Android 3.0, the preferred way of animating views is to use the
557 * {@link android.animation} package APIs.
558 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 *
Jeff Brown85a31762010-09-01 17:01:00 -0700560 * <a name="Security"></a>
561 * <h3>Security</h3>
562 * <p>
563 * Sometimes it is essential that an application be able to verify that an action
564 * is being performed with the full knowledge and consent of the user, such as
565 * granting a permission request, making a purchase or clicking on an advertisement.
566 * Unfortunately, a malicious application could try to spoof the user into
567 * performing these actions, unaware, by concealing the intended purpose of the view.
568 * As a remedy, the framework offers a touch filtering mechanism that can be used to
569 * improve the security of views that provide access to sensitive functionality.
570 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700571 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800572 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700573 * will discard touches that are received whenever the view's window is obscured by
574 * another visible window. As a result, the view will not receive touches whenever a
575 * toast, dialog or other window appears above the view's window.
576 * </p><p>
577 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700578 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
579 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700580 * </p>
581 *
Romain Guy171c5922011-01-06 10:04:23 -0800582 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700583 * @attr ref android.R.styleable#View_background
584 * @attr ref android.R.styleable#View_clickable
585 * @attr ref android.R.styleable#View_contentDescription
586 * @attr ref android.R.styleable#View_drawingCacheQuality
587 * @attr ref android.R.styleable#View_duplicateParentState
588 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700589 * @attr ref android.R.styleable#View_requiresFadingEdge
Romain Guyd6a463a2009-05-21 23:10:10 -0700590 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700591 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700593 * @attr ref android.R.styleable#View_isScrollContainer
594 * @attr ref android.R.styleable#View_focusable
595 * @attr ref android.R.styleable#View_focusableInTouchMode
596 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
597 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800598 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700599 * @attr ref android.R.styleable#View_longClickable
600 * @attr ref android.R.styleable#View_minHeight
601 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 * @attr ref android.R.styleable#View_nextFocusDown
603 * @attr ref android.R.styleable#View_nextFocusLeft
604 * @attr ref android.R.styleable#View_nextFocusRight
605 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700606 * @attr ref android.R.styleable#View_onClick
607 * @attr ref android.R.styleable#View_padding
608 * @attr ref android.R.styleable#View_paddingBottom
609 * @attr ref android.R.styleable#View_paddingLeft
610 * @attr ref android.R.styleable#View_paddingRight
611 * @attr ref android.R.styleable#View_paddingTop
612 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800613 * @attr ref android.R.styleable#View_rotation
614 * @attr ref android.R.styleable#View_rotationX
615 * @attr ref android.R.styleable#View_rotationY
616 * @attr ref android.R.styleable#View_scaleX
617 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 * @attr ref android.R.styleable#View_scrollX
619 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700620 * @attr ref android.R.styleable#View_scrollbarSize
621 * @attr ref android.R.styleable#View_scrollbarStyle
622 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700623 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
624 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
626 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 * @attr ref android.R.styleable#View_scrollbarThumbVertical
628 * @attr ref android.R.styleable#View_scrollbarTrackVertical
629 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
630 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700631 * @attr ref android.R.styleable#View_soundEffectsEnabled
632 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800633 * @attr ref android.R.styleable#View_transformPivotX
634 * @attr ref android.R.styleable#View_transformPivotY
635 * @attr ref android.R.styleable#View_translationX
636 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700637 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 *
639 * @see android.view.ViewGroup
640 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700641public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
642 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 private static final boolean DBG = false;
644
645 /**
646 * The logging tag used by this class with android.util.Log.
647 */
648 protected static final String VIEW_LOG_TAG = "View";
649
650 /**
651 * Used to mark a View that has no ID.
652 */
653 public static final int NO_ID = -1;
654
655 /**
656 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
657 * calling setFlags.
658 */
659 private static final int NOT_FOCUSABLE = 0x00000000;
660
661 /**
662 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
663 * setFlags.
664 */
665 private static final int FOCUSABLE = 0x00000001;
666
667 /**
668 * Mask for use with setFlags indicating bits used for focus.
669 */
670 private static final int FOCUSABLE_MASK = 0x00000001;
671
672 /**
673 * This view will adjust its padding to fit sytem windows (e.g. status bar)
674 */
675 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
676
677 /**
Scott Main812634c22011-07-27 13:22:35 -0700678 * This view is visible.
679 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
680 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 */
682 public static final int VISIBLE = 0x00000000;
683
684 /**
685 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700686 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
687 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 */
689 public static final int INVISIBLE = 0x00000004;
690
691 /**
692 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700693 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
694 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 */
696 public static final int GONE = 0x00000008;
697
698 /**
699 * Mask for use with setFlags indicating bits used for visibility.
700 * {@hide}
701 */
702 static final int VISIBILITY_MASK = 0x0000000C;
703
704 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
705
706 /**
707 * This view is enabled. Intrepretation varies by subclass.
708 * Use with ENABLED_MASK when calling setFlags.
709 * {@hide}
710 */
711 static final int ENABLED = 0x00000000;
712
713 /**
714 * This view is disabled. Intrepretation varies by subclass.
715 * Use with ENABLED_MASK when calling setFlags.
716 * {@hide}
717 */
718 static final int DISABLED = 0x00000020;
719
720 /**
721 * Mask for use with setFlags indicating bits used for indicating whether
722 * this view is enabled
723 * {@hide}
724 */
725 static final int ENABLED_MASK = 0x00000020;
726
727 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700728 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
729 * called and further optimizations will be performed. It is okay to have
730 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 * {@hide}
732 */
733 static final int WILL_NOT_DRAW = 0x00000080;
734
735 /**
736 * Mask for use with setFlags indicating bits used for indicating whether
737 * this view is will draw
738 * {@hide}
739 */
740 static final int DRAW_MASK = 0x00000080;
741
742 /**
743 * <p>This view doesn't show scrollbars.</p>
744 * {@hide}
745 */
746 static final int SCROLLBARS_NONE = 0x00000000;
747
748 /**
749 * <p>This view shows horizontal scrollbars.</p>
750 * {@hide}
751 */
752 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
753
754 /**
755 * <p>This view shows vertical scrollbars.</p>
756 * {@hide}
757 */
758 static final int SCROLLBARS_VERTICAL = 0x00000200;
759
760 /**
761 * <p>Mask for use with setFlags indicating bits used for indicating which
762 * scrollbars are enabled.</p>
763 * {@hide}
764 */
765 static final int SCROLLBARS_MASK = 0x00000300;
766
Jeff Brown85a31762010-09-01 17:01:00 -0700767 /**
768 * Indicates that the view should filter touches when its window is obscured.
769 * Refer to the class comments for more information about this security feature.
770 * {@hide}
771 */
772 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
773
774 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775
776 /**
777 * <p>This view doesn't show fading edges.</p>
778 * {@hide}
779 */
780 static final int FADING_EDGE_NONE = 0x00000000;
781
782 /**
783 * <p>This view shows horizontal fading edges.</p>
784 * {@hide}
785 */
786 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
787
788 /**
789 * <p>This view shows vertical fading edges.</p>
790 * {@hide}
791 */
792 static final int FADING_EDGE_VERTICAL = 0x00002000;
793
794 /**
795 * <p>Mask for use with setFlags indicating bits used for indicating which
796 * fading edges are enabled.</p>
797 * {@hide}
798 */
799 static final int FADING_EDGE_MASK = 0x00003000;
800
801 /**
802 * <p>Indicates this view can be clicked. When clickable, a View reacts
803 * to clicks by notifying the OnClickListener.<p>
804 * {@hide}
805 */
806 static final int CLICKABLE = 0x00004000;
807
808 /**
809 * <p>Indicates this view is caching its drawing into a bitmap.</p>
810 * {@hide}
811 */
812 static final int DRAWING_CACHE_ENABLED = 0x00008000;
813
814 /**
815 * <p>Indicates that no icicle should be saved for this view.<p>
816 * {@hide}
817 */
818 static final int SAVE_DISABLED = 0x000010000;
819
820 /**
821 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
822 * property.</p>
823 * {@hide}
824 */
825 static final int SAVE_DISABLED_MASK = 0x000010000;
826
827 /**
828 * <p>Indicates that no drawing cache should ever be created for this view.<p>
829 * {@hide}
830 */
831 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
832
833 /**
834 * <p>Indicates this view can take / keep focus when int touch mode.</p>
835 * {@hide}
836 */
837 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
838
839 /**
840 * <p>Enables low quality mode for the drawing cache.</p>
841 */
842 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
843
844 /**
845 * <p>Enables high quality mode for the drawing cache.</p>
846 */
847 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
848
849 /**
850 * <p>Enables automatic quality mode for the drawing cache.</p>
851 */
852 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
853
854 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
855 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
856 };
857
858 /**
859 * <p>Mask for use with setFlags indicating bits used for the cache
860 * quality property.</p>
861 * {@hide}
862 */
863 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
864
865 /**
866 * <p>
867 * Indicates this view can be long clicked. When long clickable, a View
868 * reacts to long clicks by notifying the OnLongClickListener or showing a
869 * context menu.
870 * </p>
871 * {@hide}
872 */
873 static final int LONG_CLICKABLE = 0x00200000;
874
875 /**
876 * <p>Indicates that this view gets its drawable states from its direct parent
877 * and ignores its original internal states.</p>
878 *
879 * @hide
880 */
881 static final int DUPLICATE_PARENT_STATE = 0x00400000;
882
883 /**
884 * The scrollbar style to display the scrollbars inside the content area,
885 * without increasing the padding. The scrollbars will be overlaid with
886 * translucency on the view's content.
887 */
888 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
889
890 /**
891 * The scrollbar style to display the scrollbars inside the padded area,
892 * increasing the padding of the view. The scrollbars will not overlap the
893 * content area of the view.
894 */
895 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
896
897 /**
898 * The scrollbar style to display the scrollbars at the edge of the view,
899 * without increasing the padding. The scrollbars will be overlaid with
900 * translucency.
901 */
902 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
903
904 /**
905 * The scrollbar style to display the scrollbars at the edge of the view,
906 * increasing the padding of the view. The scrollbars will only overlap the
907 * background, if any.
908 */
909 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
910
911 /**
912 * Mask to check if the scrollbar style is overlay or inset.
913 * {@hide}
914 */
915 static final int SCROLLBARS_INSET_MASK = 0x01000000;
916
917 /**
918 * Mask to check if the scrollbar style is inside or outside.
919 * {@hide}
920 */
921 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
922
923 /**
924 * Mask for scrollbar style.
925 * {@hide}
926 */
927 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
928
929 /**
930 * View flag indicating that the screen should remain on while the
931 * window containing this view is visible to the user. This effectively
932 * takes care of automatically setting the WindowManager's
933 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
934 */
935 public static final int KEEP_SCREEN_ON = 0x04000000;
936
937 /**
938 * View flag indicating whether this view should have sound effects enabled
939 * for events such as clicking and touching.
940 */
941 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
942
943 /**
944 * View flag indicating whether this view should have haptic feedback
945 * enabled for events such as long presses.
946 */
947 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
948
949 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700950 * <p>Indicates that the view hierarchy should stop saving state when
951 * it reaches this view. If state saving is initiated immediately at
952 * the view, it will be allowed.
953 * {@hide}
954 */
955 static final int PARENT_SAVE_DISABLED = 0x20000000;
956
957 /**
958 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
959 * {@hide}
960 */
961 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
962
963 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800964 * Horizontal direction of this view is from Left to Right.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700965 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800966 * {@hide}
967 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700968 public static final int LAYOUT_DIRECTION_LTR = 0x00000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800969
970 /**
971 * Horizontal direction of this view is from Right to Left.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700972 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800973 * {@hide}
974 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700975 public static final int LAYOUT_DIRECTION_RTL = 0x40000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800976
977 /**
978 * Horizontal direction of this view is inherited from its parent.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700979 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800980 * {@hide}
981 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700982 public static final int LAYOUT_DIRECTION_INHERIT = 0x80000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800983
984 /**
985 * Horizontal direction of this view is from deduced from the default language
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700986 * script for the locale. Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800987 * {@hide}
988 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700989 public static final int LAYOUT_DIRECTION_LOCALE = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800990
991 /**
992 * Mask for use with setFlags indicating bits used for horizontalDirection.
993 * {@hide}
994 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700995 static final int LAYOUT_DIRECTION_MASK = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800996
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700997 /*
998 * Array of horizontal direction flags for mapping attribute "horizontalDirection" to correct
999 * flag value.
1000 * {@hide}
1001 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07001002 private static final int[] LAYOUT_DIRECTION_FLAGS = {LAYOUT_DIRECTION_LTR,
1003 LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE};
Cibu Johny7632cb92010-02-22 13:01:02 -08001004
1005 /**
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001006 * Default horizontalDirection.
1007 * {@hide}
1008 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07001009 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001010
1011 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001012 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1013 * should add all focusable Views regardless if they are focusable in touch mode.
1014 */
1015 public static final int FOCUSABLES_ALL = 0x00000000;
1016
1017 /**
1018 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1019 * should add only Views focusable in touch mode.
1020 */
1021 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1022
1023 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001024 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 * item.
1026 */
1027 public static final int FOCUS_BACKWARD = 0x00000001;
1028
1029 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001030 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 * item.
1032 */
1033 public static final int FOCUS_FORWARD = 0x00000002;
1034
1035 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001036 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 */
1038 public static final int FOCUS_LEFT = 0x00000011;
1039
1040 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001041 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 */
1043 public static final int FOCUS_UP = 0x00000021;
1044
1045 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001046 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 */
1048 public static final int FOCUS_RIGHT = 0x00000042;
1049
1050 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001051 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 */
1053 public static final int FOCUS_DOWN = 0x00000082;
1054
1055 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001056 * Bits of {@link #getMeasuredWidthAndState()} and
1057 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1058 */
1059 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1060
1061 /**
1062 * Bits of {@link #getMeasuredWidthAndState()} and
1063 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1064 */
1065 public static final int MEASURED_STATE_MASK = 0xff000000;
1066
1067 /**
1068 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1069 * for functions that combine both width and height into a single int,
1070 * such as {@link #getMeasuredState()} and the childState argument of
1071 * {@link #resolveSizeAndState(int, int, int)}.
1072 */
1073 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1074
1075 /**
1076 * Bit of {@link #getMeasuredWidthAndState()} and
1077 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1078 * is smaller that the space the view would like to have.
1079 */
1080 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1081
1082 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 * Base View state sets
1084 */
1085 // Singles
1086 /**
1087 * Indicates the view has no states set. States are used with
1088 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1089 * view depending on its state.
1090 *
1091 * @see android.graphics.drawable.Drawable
1092 * @see #getDrawableState()
1093 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001094 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 /**
1096 * Indicates the view is enabled. States are used with
1097 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1098 * view depending on its state.
1099 *
1100 * @see android.graphics.drawable.Drawable
1101 * @see #getDrawableState()
1102 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001103 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 /**
1105 * Indicates the view is focused. States are used with
1106 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1107 * view depending on its state.
1108 *
1109 * @see android.graphics.drawable.Drawable
1110 * @see #getDrawableState()
1111 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001112 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 /**
1114 * Indicates the view is selected. States are used with
1115 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1116 * view depending on its state.
1117 *
1118 * @see android.graphics.drawable.Drawable
1119 * @see #getDrawableState()
1120 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001121 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 /**
1123 * Indicates the view is pressed. States are used with
1124 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1125 * view depending on its state.
1126 *
1127 * @see android.graphics.drawable.Drawable
1128 * @see #getDrawableState()
1129 * @hide
1130 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001131 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 /**
1133 * Indicates the view's window has focus. States are used with
1134 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1135 * view depending on its state.
1136 *
1137 * @see android.graphics.drawable.Drawable
1138 * @see #getDrawableState()
1139 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001140 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 // Doubles
1142 /**
1143 * Indicates the view is enabled and has the focus.
1144 *
1145 * @see #ENABLED_STATE_SET
1146 * @see #FOCUSED_STATE_SET
1147 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001148 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 /**
1150 * Indicates the view is enabled and selected.
1151 *
1152 * @see #ENABLED_STATE_SET
1153 * @see #SELECTED_STATE_SET
1154 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001155 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 /**
1157 * Indicates the view is enabled and that its window has focus.
1158 *
1159 * @see #ENABLED_STATE_SET
1160 * @see #WINDOW_FOCUSED_STATE_SET
1161 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001162 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 /**
1164 * Indicates the view is focused and selected.
1165 *
1166 * @see #FOCUSED_STATE_SET
1167 * @see #SELECTED_STATE_SET
1168 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001169 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 /**
1171 * Indicates the view has the focus and that its window has the focus.
1172 *
1173 * @see #FOCUSED_STATE_SET
1174 * @see #WINDOW_FOCUSED_STATE_SET
1175 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001176 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 /**
1178 * Indicates the view is selected and that its window has the focus.
1179 *
1180 * @see #SELECTED_STATE_SET
1181 * @see #WINDOW_FOCUSED_STATE_SET
1182 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001183 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 // Triples
1185 /**
1186 * Indicates the view is enabled, focused and selected.
1187 *
1188 * @see #ENABLED_STATE_SET
1189 * @see #FOCUSED_STATE_SET
1190 * @see #SELECTED_STATE_SET
1191 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001192 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 /**
1194 * Indicates the view is enabled, focused and its window has the focus.
1195 *
1196 * @see #ENABLED_STATE_SET
1197 * @see #FOCUSED_STATE_SET
1198 * @see #WINDOW_FOCUSED_STATE_SET
1199 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001200 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 /**
1202 * Indicates the view is enabled, selected and its window has the focus.
1203 *
1204 * @see #ENABLED_STATE_SET
1205 * @see #SELECTED_STATE_SET
1206 * @see #WINDOW_FOCUSED_STATE_SET
1207 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001208 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 /**
1210 * Indicates the view is focused, selected and its window has the focus.
1211 *
1212 * @see #FOCUSED_STATE_SET
1213 * @see #SELECTED_STATE_SET
1214 * @see #WINDOW_FOCUSED_STATE_SET
1215 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001216 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 /**
1218 * Indicates the view is enabled, focused, selected and its window
1219 * has the focus.
1220 *
1221 * @see #ENABLED_STATE_SET
1222 * @see #FOCUSED_STATE_SET
1223 * @see #SELECTED_STATE_SET
1224 * @see #WINDOW_FOCUSED_STATE_SET
1225 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001226 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 /**
1228 * Indicates the view is pressed and its window has the focus.
1229 *
1230 * @see #PRESSED_STATE_SET
1231 * @see #WINDOW_FOCUSED_STATE_SET
1232 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001233 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 /**
1235 * Indicates the view is pressed and selected.
1236 *
1237 * @see #PRESSED_STATE_SET
1238 * @see #SELECTED_STATE_SET
1239 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001240 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 /**
1242 * Indicates the view is pressed, selected and its window has the focus.
1243 *
1244 * @see #PRESSED_STATE_SET
1245 * @see #SELECTED_STATE_SET
1246 * @see #WINDOW_FOCUSED_STATE_SET
1247 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001248 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 /**
1250 * Indicates the view is pressed and focused.
1251 *
1252 * @see #PRESSED_STATE_SET
1253 * @see #FOCUSED_STATE_SET
1254 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001255 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 /**
1257 * Indicates the view is pressed, focused and its window has the focus.
1258 *
1259 * @see #PRESSED_STATE_SET
1260 * @see #FOCUSED_STATE_SET
1261 * @see #WINDOW_FOCUSED_STATE_SET
1262 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001263 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 /**
1265 * Indicates the view is pressed, focused and selected.
1266 *
1267 * @see #PRESSED_STATE_SET
1268 * @see #SELECTED_STATE_SET
1269 * @see #FOCUSED_STATE_SET
1270 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001271 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 /**
1273 * Indicates the view is pressed, focused, selected and its window has the focus.
1274 *
1275 * @see #PRESSED_STATE_SET
1276 * @see #FOCUSED_STATE_SET
1277 * @see #SELECTED_STATE_SET
1278 * @see #WINDOW_FOCUSED_STATE_SET
1279 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001280 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 /**
1282 * Indicates the view is pressed and enabled.
1283 *
1284 * @see #PRESSED_STATE_SET
1285 * @see #ENABLED_STATE_SET
1286 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001287 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 /**
1289 * Indicates the view is pressed, enabled and its window has the focus.
1290 *
1291 * @see #PRESSED_STATE_SET
1292 * @see #ENABLED_STATE_SET
1293 * @see #WINDOW_FOCUSED_STATE_SET
1294 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001295 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 /**
1297 * Indicates the view is pressed, enabled and selected.
1298 *
1299 * @see #PRESSED_STATE_SET
1300 * @see #ENABLED_STATE_SET
1301 * @see #SELECTED_STATE_SET
1302 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001303 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 /**
1305 * Indicates the view is pressed, enabled, selected and its window has the
1306 * focus.
1307 *
1308 * @see #PRESSED_STATE_SET
1309 * @see #ENABLED_STATE_SET
1310 * @see #SELECTED_STATE_SET
1311 * @see #WINDOW_FOCUSED_STATE_SET
1312 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001313 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 /**
1315 * Indicates the view is pressed, enabled and focused.
1316 *
1317 * @see #PRESSED_STATE_SET
1318 * @see #ENABLED_STATE_SET
1319 * @see #FOCUSED_STATE_SET
1320 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001321 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 /**
1323 * Indicates the view is pressed, enabled, focused and its window has the
1324 * focus.
1325 *
1326 * @see #PRESSED_STATE_SET
1327 * @see #ENABLED_STATE_SET
1328 * @see #FOCUSED_STATE_SET
1329 * @see #WINDOW_FOCUSED_STATE_SET
1330 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001331 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 /**
1333 * Indicates the view is pressed, enabled, focused and selected.
1334 *
1335 * @see #PRESSED_STATE_SET
1336 * @see #ENABLED_STATE_SET
1337 * @see #SELECTED_STATE_SET
1338 * @see #FOCUSED_STATE_SET
1339 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001340 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 /**
1342 * Indicates the view is pressed, enabled, focused, selected and its window
1343 * has the focus.
1344 *
1345 * @see #PRESSED_STATE_SET
1346 * @see #ENABLED_STATE_SET
1347 * @see #SELECTED_STATE_SET
1348 * @see #FOCUSED_STATE_SET
1349 * @see #WINDOW_FOCUSED_STATE_SET
1350 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001351 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352
1353 /**
1354 * The order here is very important to {@link #getDrawableState()}
1355 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001356 private static final int[][] VIEW_STATE_SETS;
1357
Romain Guyb051e892010-09-28 19:09:36 -07001358 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1359 static final int VIEW_STATE_SELECTED = 1 << 1;
1360 static final int VIEW_STATE_FOCUSED = 1 << 2;
1361 static final int VIEW_STATE_ENABLED = 1 << 3;
1362 static final int VIEW_STATE_PRESSED = 1 << 4;
1363 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001364 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001365 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001366 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1367 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001368
1369 static final int[] VIEW_STATE_IDS = new int[] {
1370 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1371 R.attr.state_selected, VIEW_STATE_SELECTED,
1372 R.attr.state_focused, VIEW_STATE_FOCUSED,
1373 R.attr.state_enabled, VIEW_STATE_ENABLED,
1374 R.attr.state_pressed, VIEW_STATE_PRESSED,
1375 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001376 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001377 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001378 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1379 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 };
1381
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001382 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001383 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1384 throw new IllegalStateException(
1385 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1386 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001387 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001388 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001389 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001390 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001391 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001392 orderedIds[i * 2] = viewState;
1393 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001394 }
1395 }
1396 }
Romain Guyb051e892010-09-28 19:09:36 -07001397 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1398 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1399 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001400 int numBits = Integer.bitCount(i);
1401 int[] set = new int[numBits];
1402 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001403 for (int j = 0; j < orderedIds.length; j += 2) {
1404 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001405 set[pos++] = orderedIds[j];
1406 }
1407 }
1408 VIEW_STATE_SETS[i] = set;
1409 }
1410
1411 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1412 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1413 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1414 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1416 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1417 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1418 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1419 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1421 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1422 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1423 | VIEW_STATE_FOCUSED];
1424 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1425 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1426 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1427 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1428 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1429 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1430 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1431 | VIEW_STATE_ENABLED];
1432 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1433 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1434 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1435 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1436 | VIEW_STATE_ENABLED];
1437 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1438 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1439 | VIEW_STATE_ENABLED];
1440 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1441 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1442 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1443
1444 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1445 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1447 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1449 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1451 | VIEW_STATE_PRESSED];
1452 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1453 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1454 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1455 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1456 | VIEW_STATE_PRESSED];
1457 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1459 | VIEW_STATE_PRESSED];
1460 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1462 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1463 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1464 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1465 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1467 | VIEW_STATE_PRESSED];
1468 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1470 | VIEW_STATE_PRESSED];
1471 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1472 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1473 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1474 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1475 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1476 | VIEW_STATE_PRESSED];
1477 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1478 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1479 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1480 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1481 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1482 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1483 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1484 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1485 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1486 | VIEW_STATE_PRESSED];
1487 }
1488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 * Temporary Rect currently for use in setBackground(). This will probably
1491 * be extended in the future to hold our own class with more than just
1492 * a Rect. :)
1493 */
1494 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001495
1496 /**
1497 * Map used to store views' tags.
1498 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001499 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001502 * The next available accessiiblity id.
1503 */
1504 private static int sNextAccessibilityViewId;
1505
1506 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 * The animation currently associated with this view.
1508 * @hide
1509 */
1510 protected Animation mCurrentAnimation = null;
1511
1512 /**
1513 * Width as measured during measure pass.
1514 * {@hide}
1515 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001516 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001517 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518
1519 /**
1520 * Height as measured during measure pass.
1521 * {@hide}
1522 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001523 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001524 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525
1526 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001527 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1528 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1529 * its display list. This flag, used only when hw accelerated, allows us to clear the
1530 * flag while retaining this information until it's needed (at getDisplayList() time and
1531 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1532 *
1533 * {@hide}
1534 */
1535 boolean mRecreateDisplayList = false;
1536
1537 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 * The view's identifier.
1539 * {@hide}
1540 *
1541 * @see #setId(int)
1542 * @see #getId()
1543 */
1544 @ViewDebug.ExportedProperty(resolveId = true)
1545 int mID = NO_ID;
1546
1547 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001548 * The stable ID of this view for accessibility porposes.
1549 */
1550 int mAccessibilityViewId = NO_ID;
1551
1552 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 * The view's tag.
1554 * {@hide}
1555 *
1556 * @see #setTag(Object)
1557 * @see #getTag()
1558 */
1559 protected Object mTag;
1560
1561 // for mPrivateFlags:
1562 /** {@hide} */
1563 static final int WANTS_FOCUS = 0x00000001;
1564 /** {@hide} */
1565 static final int FOCUSED = 0x00000002;
1566 /** {@hide} */
1567 static final int SELECTED = 0x00000004;
1568 /** {@hide} */
1569 static final int IS_ROOT_NAMESPACE = 0x00000008;
1570 /** {@hide} */
1571 static final int HAS_BOUNDS = 0x00000010;
1572 /** {@hide} */
1573 static final int DRAWN = 0x00000020;
1574 /**
1575 * When this flag is set, this view is running an animation on behalf of its
1576 * children and should therefore not cancel invalidate requests, even if they
1577 * lie outside of this view's bounds.
1578 *
1579 * {@hide}
1580 */
1581 static final int DRAW_ANIMATION = 0x00000040;
1582 /** {@hide} */
1583 static final int SKIP_DRAW = 0x00000080;
1584 /** {@hide} */
1585 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1586 /** {@hide} */
1587 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1588 /** {@hide} */
1589 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1590 /** {@hide} */
1591 static final int MEASURED_DIMENSION_SET = 0x00000800;
1592 /** {@hide} */
1593 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001594 /** {@hide} */
1595 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596
1597 private static final int PRESSED = 0x00004000;
1598
1599 /** {@hide} */
1600 static final int DRAWING_CACHE_VALID = 0x00008000;
1601 /**
1602 * Flag used to indicate that this view should be drawn once more (and only once
1603 * more) after its animation has completed.
1604 * {@hide}
1605 */
1606 static final int ANIMATION_STARTED = 0x00010000;
1607
1608 private static final int SAVE_STATE_CALLED = 0x00020000;
1609
1610 /**
1611 * Indicates that the View returned true when onSetAlpha() was called and that
1612 * the alpha must be restored.
1613 * {@hide}
1614 */
1615 static final int ALPHA_SET = 0x00040000;
1616
1617 /**
1618 * Set by {@link #setScrollContainer(boolean)}.
1619 */
1620 static final int SCROLL_CONTAINER = 0x00080000;
1621
1622 /**
1623 * Set by {@link #setScrollContainer(boolean)}.
1624 */
1625 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1626
1627 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001628 * View flag indicating whether this view was invalidated (fully or partially.)
1629 *
1630 * @hide
1631 */
1632 static final int DIRTY = 0x00200000;
1633
1634 /**
1635 * View flag indicating whether this view was invalidated by an opaque
1636 * invalidate request.
1637 *
1638 * @hide
1639 */
1640 static final int DIRTY_OPAQUE = 0x00400000;
1641
1642 /**
1643 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1644 *
1645 * @hide
1646 */
1647 static final int DIRTY_MASK = 0x00600000;
1648
1649 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001650 * Indicates whether the background is opaque.
1651 *
1652 * @hide
1653 */
1654 static final int OPAQUE_BACKGROUND = 0x00800000;
1655
1656 /**
1657 * Indicates whether the scrollbars are opaque.
1658 *
1659 * @hide
1660 */
1661 static final int OPAQUE_SCROLLBARS = 0x01000000;
1662
1663 /**
1664 * Indicates whether the view is opaque.
1665 *
1666 * @hide
1667 */
1668 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001669
Adam Powelle14579b2009-12-16 18:39:52 -08001670 /**
1671 * Indicates a prepressed state;
1672 * the short time between ACTION_DOWN and recognizing
1673 * a 'real' press. Prepressed is used to recognize quick taps
1674 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001675 *
Adam Powelle14579b2009-12-16 18:39:52 -08001676 * @hide
1677 */
1678 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001679
Adam Powellc9fbaab2010-02-16 17:16:19 -08001680 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001681 * Indicates whether the view is temporarily detached.
1682 *
1683 * @hide
1684 */
1685 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001686
Adam Powell8568c3a2010-04-19 14:26:11 -07001687 /**
1688 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001689 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001690 * @hide
1691 */
1692 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001693
1694 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001695 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1696 * @hide
1697 */
1698 private static final int HOVERED = 0x10000000;
1699
1700 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001701 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1702 * for transform operations
1703 *
1704 * @hide
1705 */
Adam Powellf37df072010-09-17 16:22:49 -07001706 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001707
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001708 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001709 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001710
Chet Haasefd2b0022010-08-06 13:08:56 -07001711 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001712 * Indicates that this view was specifically invalidated, not just dirtied because some
1713 * child view was invalidated. The flag is used to determine when we need to recreate
1714 * a view's display list (as opposed to just returning a reference to its existing
1715 * display list).
1716 *
1717 * @hide
1718 */
1719 static final int INVALIDATED = 0x80000000;
1720
Christopher Tate3d4bf172011-03-28 16:16:46 -07001721 /* Masks for mPrivateFlags2 */
1722
1723 /**
1724 * Indicates that this view has reported that it can accept the current drag's content.
1725 * Cleared when the drag operation concludes.
1726 * @hide
1727 */
1728 static final int DRAG_CAN_ACCEPT = 0x00000001;
1729
1730 /**
1731 * Indicates that this view is currently directly under the drag location in a
1732 * drag-and-drop operation involving content that it can accept. Cleared when
1733 * the drag exits the view, or when the drag operation concludes.
1734 * @hide
1735 */
1736 static final int DRAG_HOVERED = 0x00000002;
1737
Cibu Johny86666632010-02-22 13:01:02 -08001738 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001739 * Indicates whether the view layout direction has been resolved and drawn to the
1740 * right-to-left direction.
Cibu Johny86666632010-02-22 13:01:02 -08001741 *
1742 * @hide
1743 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001744 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004;
1745
1746 /**
1747 * Indicates whether the view layout direction has been resolved.
1748 *
1749 * @hide
1750 */
1751 static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008;
1752
Cibu Johny86666632010-02-22 13:01:02 -08001753
Christopher Tate3d4bf172011-03-28 16:16:46 -07001754 /* End of masks for mPrivateFlags2 */
1755
1756 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1757
Chet Haasedaf98e92011-01-10 14:10:36 -08001758 /**
Adam Powell637d3372010-08-25 14:37:03 -07001759 * Always allow a user to over-scroll this view, provided it is a
1760 * view that can scroll.
1761 *
1762 * @see #getOverScrollMode()
1763 * @see #setOverScrollMode(int)
1764 */
1765 public static final int OVER_SCROLL_ALWAYS = 0;
1766
1767 /**
1768 * Allow a user to over-scroll this view only if the content is large
1769 * enough to meaningfully scroll, provided it is a view that can scroll.
1770 *
1771 * @see #getOverScrollMode()
1772 * @see #setOverScrollMode(int)
1773 */
1774 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1775
1776 /**
1777 * Never allow a user to over-scroll this view.
1778 *
1779 * @see #getOverScrollMode()
1780 * @see #setOverScrollMode(int)
1781 */
1782 public static final int OVER_SCROLL_NEVER = 2;
1783
1784 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001785 * View has requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08001786 *
Joe Malin32736f02011-01-19 16:14:20 -08001787 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001788 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001789 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08001790
1791 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001792 * View has requested the system UI to enter an unobtrusive "low profile" mode.
1793 *
1794 * This is for use in games, book readers, video players, or any other "immersive" application
1795 * where the usual system chrome is deemed too distracting.
1796 *
1797 * In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08001798 *
Joe Malin32736f02011-01-19 16:14:20 -08001799 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001800 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001801 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
1802
1803 /**
1804 * View has requested that the system navigation be temporarily hidden.
1805 *
1806 * This is an even less obtrusive state than that called for by
1807 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
1808 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
1809 * those to disappear. This is useful (in conjunction with the
1810 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
1811 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
1812 * window flags) for displaying content using every last pixel on the display.
1813 *
1814 * There is a limitation: because navigation controls are so important, the least user
1815 * interaction will cause them to reappear immediately.
1816 *
1817 * @see #setSystemUiVisibility(int)
1818 */
1819 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
1820
1821 /**
1822 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
1823 */
1824 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
1825
1826 /**
1827 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
1828 */
1829 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08001830
1831 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001832 * @hide
1833 *
1834 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1835 * out of the public fields to keep the undefined bits out of the developer's way.
1836 *
1837 * Flag to make the status bar not expandable. Unless you also
1838 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1839 */
1840 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1841
1842 /**
1843 * @hide
1844 *
1845 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1846 * out of the public fields to keep the undefined bits out of the developer's way.
1847 *
1848 * Flag to hide notification icons and scrolling ticker text.
1849 */
1850 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1851
1852 /**
1853 * @hide
1854 *
1855 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1856 * out of the public fields to keep the undefined bits out of the developer's way.
1857 *
1858 * Flag to disable incoming notification alerts. This will not block
1859 * icons, but it will block sound, vibrating and other visual or aural notifications.
1860 */
1861 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1862
1863 /**
1864 * @hide
1865 *
1866 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1867 * out of the public fields to keep the undefined bits out of the developer's way.
1868 *
1869 * Flag to hide only the scrolling ticker. Note that
1870 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1871 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1872 */
1873 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1874
1875 /**
1876 * @hide
1877 *
1878 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1879 * out of the public fields to keep the undefined bits out of the developer's way.
1880 *
1881 * Flag to hide the center system info area.
1882 */
1883 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1884
1885 /**
1886 * @hide
1887 *
1888 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1889 * out of the public fields to keep the undefined bits out of the developer's way.
1890 *
1891 * Flag to hide only the navigation buttons. Don't use this
1892 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001893 *
1894 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001895 */
1896 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1897
1898 /**
1899 * @hide
1900 *
1901 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1902 * out of the public fields to keep the undefined bits out of the developer's way.
1903 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001904 * Flag to hide only the back button. Don't use this
1905 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1906 */
1907 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1908
1909 /**
1910 * @hide
1911 *
1912 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1913 * out of the public fields to keep the undefined bits out of the developer's way.
1914 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001915 * Flag to hide only the clock. You might use this if your activity has
1916 * its own clock making the status bar's clock redundant.
1917 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001918 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1919
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001920 /**
1921 * @hide
1922 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001923 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001924
1925 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001926 * Find views that render the specified text.
1927 *
1928 * @see #findViewsWithText(ArrayList, CharSequence, int)
1929 */
1930 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
1931
1932 /**
1933 * Find find views that contain the specified content description.
1934 *
1935 * @see #findViewsWithText(ArrayList, CharSequence, int)
1936 */
1937 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
1938
1939 /**
Adam Powell637d3372010-08-25 14:37:03 -07001940 * Controls the over-scroll mode for this view.
1941 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1942 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1943 * and {@link #OVER_SCROLL_NEVER}.
1944 */
1945 private int mOverScrollMode;
1946
1947 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 * The parent this view is attached to.
1949 * {@hide}
1950 *
1951 * @see #getParent()
1952 */
1953 protected ViewParent mParent;
1954
1955 /**
1956 * {@hide}
1957 */
1958 AttachInfo mAttachInfo;
1959
1960 /**
1961 * {@hide}
1962 */
Romain Guy809a7f62009-05-14 15:44:42 -07001963 @ViewDebug.ExportedProperty(flagMapping = {
1964 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1965 name = "FORCE_LAYOUT"),
1966 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1967 name = "LAYOUT_REQUIRED"),
1968 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001969 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001970 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1971 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1972 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1973 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1974 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001976 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977
1978 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001979 * This view's request for the visibility of the status bar.
1980 * @hide
1981 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001982 @ViewDebug.ExportedProperty(flagMapping = {
1983 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
1984 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
1985 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
1986 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
1987 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
1988 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
1989 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
1990 equals = SYSTEM_UI_FLAG_VISIBLE,
1991 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
1992 })
Joe Onorato664644d2011-01-23 17:53:23 -08001993 int mSystemUiVisibility;
1994
1995 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 * Count of how many windows this view has been attached to.
1997 */
1998 int mWindowAttachCount;
1999
2000 /**
2001 * The layout parameters associated with this view and used by the parent
2002 * {@link android.view.ViewGroup} to determine how this view should be
2003 * laid out.
2004 * {@hide}
2005 */
2006 protected ViewGroup.LayoutParams mLayoutParams;
2007
2008 /**
2009 * The view flags hold various views states.
2010 * {@hide}
2011 */
2012 @ViewDebug.ExportedProperty
2013 int mViewFlags;
2014
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002015 static class TransformationInfo {
2016 /**
2017 * The transform matrix for the View. This transform is calculated internally
2018 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2019 * is used by default. Do *not* use this variable directly; instead call
2020 * getMatrix(), which will automatically recalculate the matrix if necessary
2021 * to get the correct matrix based on the latest rotation and scale properties.
2022 */
2023 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002024
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002025 /**
2026 * The transform matrix for the View. This transform is calculated internally
2027 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2028 * is used by default. Do *not* use this variable directly; instead call
2029 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2030 * to get the correct matrix based on the latest rotation and scale properties.
2031 */
2032 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002033
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002034 /**
2035 * An internal variable that tracks whether we need to recalculate the
2036 * transform matrix, based on whether the rotation or scaleX/Y properties
2037 * have changed since the matrix was last calculated.
2038 */
2039 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002040
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002041 /**
2042 * An internal variable that tracks whether we need to recalculate the
2043 * transform matrix, based on whether the rotation or scaleX/Y properties
2044 * have changed since the matrix was last calculated.
2045 */
2046 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002047
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002048 /**
2049 * A variable that tracks whether we need to recalculate the
2050 * transform matrix, based on whether the rotation or scaleX/Y properties
2051 * have changed since the matrix was last calculated. This variable
2052 * is only valid after a call to updateMatrix() or to a function that
2053 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2054 */
2055 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002056
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002057 /**
2058 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2059 */
2060 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002061
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002062 /**
2063 * This matrix is used when computing the matrix for 3D rotations.
2064 */
2065 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002066
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002067 /**
2068 * These prev values are used to recalculate a centered pivot point when necessary. The
2069 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2070 * set), so thes values are only used then as well.
2071 */
2072 private int mPrevWidth = -1;
2073 private int mPrevHeight = -1;
2074
2075 /**
2076 * The degrees rotation around the vertical axis through the pivot point.
2077 */
2078 @ViewDebug.ExportedProperty
2079 float mRotationY = 0f;
2080
2081 /**
2082 * The degrees rotation around the horizontal axis through the pivot point.
2083 */
2084 @ViewDebug.ExportedProperty
2085 float mRotationX = 0f;
2086
2087 /**
2088 * The degrees rotation around the pivot point.
2089 */
2090 @ViewDebug.ExportedProperty
2091 float mRotation = 0f;
2092
2093 /**
2094 * The amount of translation of the object away from its left property (post-layout).
2095 */
2096 @ViewDebug.ExportedProperty
2097 float mTranslationX = 0f;
2098
2099 /**
2100 * The amount of translation of the object away from its top property (post-layout).
2101 */
2102 @ViewDebug.ExportedProperty
2103 float mTranslationY = 0f;
2104
2105 /**
2106 * The amount of scale in the x direction around the pivot point. A
2107 * value of 1 means no scaling is applied.
2108 */
2109 @ViewDebug.ExportedProperty
2110 float mScaleX = 1f;
2111
2112 /**
2113 * The amount of scale in the y direction around the pivot point. A
2114 * value of 1 means no scaling is applied.
2115 */
2116 @ViewDebug.ExportedProperty
2117 float mScaleY = 1f;
2118
2119 /**
2120 * The amount of scale in the x direction around the pivot point. A
2121 * value of 1 means no scaling is applied.
2122 */
2123 @ViewDebug.ExportedProperty
2124 float mPivotX = 0f;
2125
2126 /**
2127 * The amount of scale in the y direction around the pivot point. A
2128 * value of 1 means no scaling is applied.
2129 */
2130 @ViewDebug.ExportedProperty
2131 float mPivotY = 0f;
2132
2133 /**
2134 * The opacity of the View. This is a value from 0 to 1, where 0 means
2135 * completely transparent and 1 means completely opaque.
2136 */
2137 @ViewDebug.ExportedProperty
2138 float mAlpha = 1f;
2139 }
2140
2141 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002142
Joe Malin32736f02011-01-19 16:14:20 -08002143 private boolean mLastIsOpaque;
2144
Chet Haasefd2b0022010-08-06 13:08:56 -07002145 /**
2146 * Convenience value to check for float values that are close enough to zero to be considered
2147 * zero.
2148 */
Romain Guy2542d192010-08-18 11:47:12 -07002149 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002150
2151 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 * The distance in pixels from the left edge of this view's parent
2153 * to the left edge of this view.
2154 * {@hide}
2155 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002156 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 protected int mLeft;
2158 /**
2159 * The distance in pixels from the left edge of this view's parent
2160 * to the right edge of this view.
2161 * {@hide}
2162 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002163 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 protected int mRight;
2165 /**
2166 * The distance in pixels from the top edge of this view's parent
2167 * to the top edge of this view.
2168 * {@hide}
2169 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002170 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 protected int mTop;
2172 /**
2173 * The distance in pixels from the top edge of this view's parent
2174 * to the bottom edge of this view.
2175 * {@hide}
2176 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002177 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 protected int mBottom;
2179
2180 /**
2181 * The offset, in pixels, by which the content of this view is scrolled
2182 * horizontally.
2183 * {@hide}
2184 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002185 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 protected int mScrollX;
2187 /**
2188 * The offset, in pixels, by which the content of this view is scrolled
2189 * vertically.
2190 * {@hide}
2191 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002192 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 protected int mScrollY;
2194
2195 /**
2196 * The left padding in pixels, that is the distance in pixels between the
2197 * left edge of this view and the left edge of its content.
2198 * {@hide}
2199 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002200 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 protected int mPaddingLeft;
2202 /**
2203 * The right padding in pixels, that is the distance in pixels between the
2204 * right edge of this view and the right edge of its content.
2205 * {@hide}
2206 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002207 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 protected int mPaddingRight;
2209 /**
2210 * The top padding in pixels, that is the distance in pixels between the
2211 * top edge of this view and the top edge of its content.
2212 * {@hide}
2213 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002214 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 protected int mPaddingTop;
2216 /**
2217 * The bottom padding in pixels, that is the distance in pixels between the
2218 * bottom edge of this view and the bottom edge of its content.
2219 * {@hide}
2220 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002221 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 protected int mPaddingBottom;
2223
2224 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002225 * Briefly describes the view and is primarily used for accessibility support.
2226 */
2227 private CharSequence mContentDescription;
2228
2229 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002231 *
2232 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002234 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002235 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236
2237 /**
2238 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002239 *
2240 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002242 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002243 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002245 /**
Adam Powell20232d02010-12-08 21:08:53 -08002246 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002247 *
2248 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002249 */
2250 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002251 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002252
2253 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002254 * Cache if the user padding is relative.
2255 *
2256 */
2257 @ViewDebug.ExportedProperty(category = "padding")
2258 boolean mUserPaddingRelative;
2259
2260 /**
2261 * Cache the paddingStart set by the user to append to the scrollbar's size.
2262 *
2263 */
2264 @ViewDebug.ExportedProperty(category = "padding")
2265 int mUserPaddingStart;
2266
2267 /**
2268 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2269 *
2270 */
2271 @ViewDebug.ExportedProperty(category = "padding")
2272 int mUserPaddingEnd;
2273
2274 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002275 * @hide
2276 */
2277 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2278 /**
2279 * @hide
2280 */
2281 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 private Drawable mBGDrawable;
2284
2285 private int mBackgroundResource;
2286 private boolean mBackgroundSizeChanged;
2287
2288 /**
2289 * Listener used to dispatch focus change events.
2290 * This field should be made private, so it is hidden from the SDK.
2291 * {@hide}
2292 */
2293 protected OnFocusChangeListener mOnFocusChangeListener;
2294
2295 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002296 * Listeners for layout change events.
2297 */
2298 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2299
2300 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002301 * Listeners for attach events.
2302 */
2303 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2304
2305 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306 * Listener used to dispatch click events.
2307 * This field should be made private, so it is hidden from the SDK.
2308 * {@hide}
2309 */
2310 protected OnClickListener mOnClickListener;
2311
2312 /**
2313 * Listener used to dispatch long click events.
2314 * This field should be made private, so it is hidden from the SDK.
2315 * {@hide}
2316 */
2317 protected OnLongClickListener mOnLongClickListener;
2318
2319 /**
2320 * Listener used to build the context menu.
2321 * This field should be made private, so it is hidden from the SDK.
2322 * {@hide}
2323 */
2324 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2325
2326 private OnKeyListener mOnKeyListener;
2327
2328 private OnTouchListener mOnTouchListener;
2329
Jeff Brown10b62902011-06-20 16:40:37 -07002330 private OnHoverListener mOnHoverListener;
2331
Jeff Brown33bbfd22011-02-24 20:55:35 -08002332 private OnGenericMotionListener mOnGenericMotionListener;
2333
Chris Tate32affef2010-10-18 15:29:21 -07002334 private OnDragListener mOnDragListener;
2335
Joe Onorato664644d2011-01-23 17:53:23 -08002336 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 /**
2339 * The application environment this view lives in.
2340 * This field should be made private, so it is hidden from the SDK.
2341 * {@hide}
2342 */
2343 protected Context mContext;
2344
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002345 private final Resources mResources;
2346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 private ScrollabilityCache mScrollCache;
2348
2349 private int[] mDrawableState = null;
2350
Romain Guy0211a0a2011-02-14 16:34:59 -08002351 /**
2352 * Set to true when drawing cache is enabled and cannot be created.
2353 *
2354 * @hide
2355 */
2356 public boolean mCachingFailed;
2357
Romain Guy02890fd2010-08-06 17:58:44 -07002358 private Bitmap mDrawingCache;
2359 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002360 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002361 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362
2363 /**
2364 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2365 * the user may specify which view to go to next.
2366 */
2367 private int mNextFocusLeftId = View.NO_ID;
2368
2369 /**
2370 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2371 * the user may specify which view to go to next.
2372 */
2373 private int mNextFocusRightId = View.NO_ID;
2374
2375 /**
2376 * When this view has focus and the next focus is {@link #FOCUS_UP},
2377 * the user may specify which view to go to next.
2378 */
2379 private int mNextFocusUpId = View.NO_ID;
2380
2381 /**
2382 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2383 * the user may specify which view to go to next.
2384 */
2385 private int mNextFocusDownId = View.NO_ID;
2386
Jeff Brown4e6319b2010-12-13 10:36:51 -08002387 /**
2388 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2389 * the user may specify which view to go to next.
2390 */
2391 int mNextFocusForwardId = View.NO_ID;
2392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002394 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002395 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002396 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 private UnsetPressedState mUnsetPressedState;
2399
2400 /**
2401 * Whether the long press's action has been invoked. The tap's action is invoked on the
2402 * up event while a long press is invoked as soon as the long press duration is reached, so
2403 * a long press could be performed before the tap is checked, in which case the tap's action
2404 * should not be invoked.
2405 */
2406 private boolean mHasPerformedLongPress;
2407
2408 /**
2409 * The minimum height of the view. We'll try our best to have the height
2410 * of this view to at least this amount.
2411 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002412 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 private int mMinHeight;
2414
2415 /**
2416 * The minimum width of the view. We'll try our best to have the width
2417 * of this view to at least this amount.
2418 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002419 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 private int mMinWidth;
2421
2422 /**
2423 * The delegate to handle touch events that are physically in this view
2424 * but should be handled by another view.
2425 */
2426 private TouchDelegate mTouchDelegate = null;
2427
2428 /**
2429 * Solid color to use as a background when creating the drawing cache. Enables
2430 * the cache to use 16 bit bitmaps instead of 32 bit.
2431 */
2432 private int mDrawingCacheBackgroundColor = 0;
2433
2434 /**
2435 * Special tree observer used when mAttachInfo is null.
2436 */
2437 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002438
Adam Powelle14579b2009-12-16 18:39:52 -08002439 /**
2440 * Cache the touch slop from the context that created the view.
2441 */
2442 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002445 * Object that handles automatic animation of view properties.
2446 */
2447 private ViewPropertyAnimator mAnimator = null;
2448
2449 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002450 * Flag indicating that a drag can cross window boundaries. When
2451 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2452 * with this flag set, all visible applications will be able to participate
2453 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002454 *
2455 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002456 */
2457 public static final int DRAG_FLAG_GLOBAL = 1;
2458
2459 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002460 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2461 */
2462 private float mVerticalScrollFactor;
2463
2464 /**
Adam Powell20232d02010-12-08 21:08:53 -08002465 * Position of the vertical scroll bar.
2466 */
2467 private int mVerticalScrollbarPosition;
2468
2469 /**
2470 * Position the scroll bar at the default position as determined by the system.
2471 */
2472 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2473
2474 /**
2475 * Position the scroll bar along the left edge.
2476 */
2477 public static final int SCROLLBAR_POSITION_LEFT = 1;
2478
2479 /**
2480 * Position the scroll bar along the right edge.
2481 */
2482 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2483
2484 /**
Romain Guy171c5922011-01-06 10:04:23 -08002485 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002486 *
2487 * @see #getLayerType()
2488 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002489 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002490 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002491 */
2492 public static final int LAYER_TYPE_NONE = 0;
2493
2494 /**
2495 * <p>Indicates that the view has a software layer. A software layer is backed
2496 * by a bitmap and causes the view to be rendered using Android's software
2497 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002498 *
Romain Guy171c5922011-01-06 10:04:23 -08002499 * <p>Software layers have various usages:</p>
2500 * <p>When the application is not using hardware acceleration, a software layer
2501 * is useful to apply a specific color filter and/or blending mode and/or
2502 * translucency to a view and all its children.</p>
2503 * <p>When the application is using hardware acceleration, a software layer
2504 * is useful to render drawing primitives not supported by the hardware
2505 * accelerated pipeline. It can also be used to cache a complex view tree
2506 * into a texture and reduce the complexity of drawing operations. For instance,
2507 * when animating a complex view tree with a translation, a software layer can
2508 * be used to render the view tree only once.</p>
2509 * <p>Software layers should be avoided when the affected view tree updates
2510 * often. Every update will require to re-render the software layer, which can
2511 * potentially be slow (particularly when hardware acceleration is turned on
2512 * since the layer will have to be uploaded into a hardware texture after every
2513 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002514 *
2515 * @see #getLayerType()
2516 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002517 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002518 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002519 */
2520 public static final int LAYER_TYPE_SOFTWARE = 1;
2521
2522 /**
2523 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2524 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2525 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2526 * rendering pipeline, but only if hardware acceleration is turned on for the
2527 * view hierarchy. When hardware acceleration is turned off, hardware layers
2528 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002529 *
Romain Guy171c5922011-01-06 10:04:23 -08002530 * <p>A hardware layer is useful to apply a specific color filter and/or
2531 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002532 * <p>A hardware layer can be used to cache a complex view tree into a
2533 * texture and reduce the complexity of drawing operations. For instance,
2534 * when animating a complex view tree with a translation, a hardware layer can
2535 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002536 * <p>A hardware layer can also be used to increase the rendering quality when
2537 * rotation transformations are applied on a view. It can also be used to
2538 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002539 *
2540 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002541 * @see #setLayerType(int, android.graphics.Paint)
2542 * @see #LAYER_TYPE_NONE
2543 * @see #LAYER_TYPE_SOFTWARE
2544 */
2545 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002546
Romain Guy3aaff3a2011-01-12 14:18:47 -08002547 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2548 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2549 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2550 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2551 })
Romain Guy171c5922011-01-06 10:04:23 -08002552 int mLayerType = LAYER_TYPE_NONE;
2553 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002554 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002555
2556 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002557 * Set to true when the view is sending hover accessibility events because it
2558 * is the innermost hovered view.
2559 */
2560 private boolean mSendingHoverAccessibilityEvents;
2561
2562 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002563 * Delegate for injecting accessiblity functionality.
2564 */
2565 AccessibilityDelegate mAccessibilityDelegate;
2566
2567 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002568 * Text direction is inherited thru {@link ViewGroup}
2569 * @hide
2570 */
2571 public static final int TEXT_DIRECTION_INHERIT = 0;
2572
2573 /**
2574 * Text direction is using "first strong algorithm". The first strong directional character
2575 * determines the paragraph direction. If there is no strong directional character, the
Doug Feltcb3791202011-07-07 11:57:48 -07002576 * paragraph direction is the view's resolved ayout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002577 *
2578 * @hide
2579 */
2580 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2581
2582 /**
2583 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2584 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002585 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002586 *
2587 * @hide
2588 */
2589 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2590
2591 /**
2592 * Text direction is forced to LTR.
2593 *
2594 * @hide
2595 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002596 public static final int TEXT_DIRECTION_LTR = 3;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002597
2598 /**
2599 * Text direction is forced to RTL.
2600 *
2601 * @hide
2602 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002603 public static final int TEXT_DIRECTION_RTL = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002604
2605 /**
2606 * Default text direction is inherited
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07002607 *
2608 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002609 */
2610 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2611
2612 /**
2613 * The text direction that has been defined by {@link #setTextDirection(int)}.
2614 *
2615 * {@hide}
2616 */
2617 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002618 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2619 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2620 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2621 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2622 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2623 })
Doug Feltcb3791202011-07-07 11:57:48 -07002624 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002625
2626 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002627 * The resolved text direction. This needs resolution if the value is
2628 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if that is
2629 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2630 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002631 *
2632 * {@hide}
2633 */
2634 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002635 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2636 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2637 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002638 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2639 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2640 })
Doug Feltcb3791202011-07-07 11:57:48 -07002641 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002642
2643 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002644 * Consistency verifier for debugging purposes.
2645 * @hide
2646 */
2647 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2648 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2649 new InputEventConsistencyVerifier(this, 0) : null;
2650
2651 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 * Simple constructor to use when creating a view from code.
2653 *
2654 * @param context The Context the view is running in, through which it can
2655 * access the current theme, resources, etc.
2656 */
2657 public View(Context context) {
2658 mContext = context;
2659 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002660 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002661 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002662 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002663 mUserPaddingStart = -1;
2664 mUserPaddingEnd = -1;
2665 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 }
2667
2668 /**
2669 * Constructor that is called when inflating a view from XML. This is called
2670 * when a view is being constructed from an XML file, supplying attributes
2671 * that were specified in the XML file. This version uses a default style of
2672 * 0, so the only attribute values applied are those in the Context's Theme
2673 * and the given AttributeSet.
2674 *
2675 * <p>
2676 * The method onFinishInflate() will be called after all children have been
2677 * added.
2678 *
2679 * @param context The Context the view is running in, through which it can
2680 * access the current theme, resources, etc.
2681 * @param attrs The attributes of the XML tag that is inflating the view.
2682 * @see #View(Context, AttributeSet, int)
2683 */
2684 public View(Context context, AttributeSet attrs) {
2685 this(context, attrs, 0);
2686 }
2687
2688 /**
2689 * Perform inflation from XML and apply a class-specific base style. This
2690 * constructor of View allows subclasses to use their own base style when
2691 * they are inflating. For example, a Button class's constructor would call
2692 * this version of the super class constructor and supply
2693 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2694 * the theme's button style to modify all of the base view attributes (in
2695 * particular its background) as well as the Button class's attributes.
2696 *
2697 * @param context The Context the view is running in, through which it can
2698 * access the current theme, resources, etc.
2699 * @param attrs The attributes of the XML tag that is inflating the view.
2700 * @param defStyle The default style to apply to this view. If 0, no style
2701 * will be applied (beyond what is included in the theme). This may
2702 * either be an attribute resource, whose value will be retrieved
2703 * from the current theme, or an explicit style resource.
2704 * @see #View(Context, AttributeSet)
2705 */
2706 public View(Context context, AttributeSet attrs, int defStyle) {
2707 this(context);
2708
2709 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2710 defStyle, 0);
2711
2712 Drawable background = null;
2713
2714 int leftPadding = -1;
2715 int topPadding = -1;
2716 int rightPadding = -1;
2717 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002718 int startPadding = -1;
2719 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720
2721 int padding = -1;
2722
2723 int viewFlagValues = 0;
2724 int viewFlagMasks = 0;
2725
2726 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 int x = 0;
2729 int y = 0;
2730
Chet Haase73066682010-11-29 15:55:32 -08002731 float tx = 0;
2732 float ty = 0;
2733 float rotation = 0;
2734 float rotationX = 0;
2735 float rotationY = 0;
2736 float sx = 1f;
2737 float sy = 1f;
2738 boolean transformSet = false;
2739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2741
Adam Powell637d3372010-08-25 14:37:03 -07002742 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 final int N = a.getIndexCount();
2744 for (int i = 0; i < N; i++) {
2745 int attr = a.getIndex(i);
2746 switch (attr) {
2747 case com.android.internal.R.styleable.View_background:
2748 background = a.getDrawable(attr);
2749 break;
2750 case com.android.internal.R.styleable.View_padding:
2751 padding = a.getDimensionPixelSize(attr, -1);
2752 break;
2753 case com.android.internal.R.styleable.View_paddingLeft:
2754 leftPadding = a.getDimensionPixelSize(attr, -1);
2755 break;
2756 case com.android.internal.R.styleable.View_paddingTop:
2757 topPadding = a.getDimensionPixelSize(attr, -1);
2758 break;
2759 case com.android.internal.R.styleable.View_paddingRight:
2760 rightPadding = a.getDimensionPixelSize(attr, -1);
2761 break;
2762 case com.android.internal.R.styleable.View_paddingBottom:
2763 bottomPadding = a.getDimensionPixelSize(attr, -1);
2764 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002765 case com.android.internal.R.styleable.View_paddingStart:
2766 startPadding = a.getDimensionPixelSize(attr, -1);
2767 break;
2768 case com.android.internal.R.styleable.View_paddingEnd:
2769 endPadding = a.getDimensionPixelSize(attr, -1);
2770 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 case com.android.internal.R.styleable.View_scrollX:
2772 x = a.getDimensionPixelOffset(attr, 0);
2773 break;
2774 case com.android.internal.R.styleable.View_scrollY:
2775 y = a.getDimensionPixelOffset(attr, 0);
2776 break;
Chet Haase73066682010-11-29 15:55:32 -08002777 case com.android.internal.R.styleable.View_alpha:
2778 setAlpha(a.getFloat(attr, 1f));
2779 break;
2780 case com.android.internal.R.styleable.View_transformPivotX:
2781 setPivotX(a.getDimensionPixelOffset(attr, 0));
2782 break;
2783 case com.android.internal.R.styleable.View_transformPivotY:
2784 setPivotY(a.getDimensionPixelOffset(attr, 0));
2785 break;
2786 case com.android.internal.R.styleable.View_translationX:
2787 tx = a.getDimensionPixelOffset(attr, 0);
2788 transformSet = true;
2789 break;
2790 case com.android.internal.R.styleable.View_translationY:
2791 ty = a.getDimensionPixelOffset(attr, 0);
2792 transformSet = true;
2793 break;
2794 case com.android.internal.R.styleable.View_rotation:
2795 rotation = a.getFloat(attr, 0);
2796 transformSet = true;
2797 break;
2798 case com.android.internal.R.styleable.View_rotationX:
2799 rotationX = a.getFloat(attr, 0);
2800 transformSet = true;
2801 break;
2802 case com.android.internal.R.styleable.View_rotationY:
2803 rotationY = a.getFloat(attr, 0);
2804 transformSet = true;
2805 break;
2806 case com.android.internal.R.styleable.View_scaleX:
2807 sx = a.getFloat(attr, 1f);
2808 transformSet = true;
2809 break;
2810 case com.android.internal.R.styleable.View_scaleY:
2811 sy = a.getFloat(attr, 1f);
2812 transformSet = true;
2813 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 case com.android.internal.R.styleable.View_id:
2815 mID = a.getResourceId(attr, NO_ID);
2816 break;
2817 case com.android.internal.R.styleable.View_tag:
2818 mTag = a.getText(attr);
2819 break;
2820 case com.android.internal.R.styleable.View_fitsSystemWindows:
2821 if (a.getBoolean(attr, false)) {
2822 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2823 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2824 }
2825 break;
2826 case com.android.internal.R.styleable.View_focusable:
2827 if (a.getBoolean(attr, false)) {
2828 viewFlagValues |= FOCUSABLE;
2829 viewFlagMasks |= FOCUSABLE_MASK;
2830 }
2831 break;
2832 case com.android.internal.R.styleable.View_focusableInTouchMode:
2833 if (a.getBoolean(attr, false)) {
2834 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2835 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2836 }
2837 break;
2838 case com.android.internal.R.styleable.View_clickable:
2839 if (a.getBoolean(attr, false)) {
2840 viewFlagValues |= CLICKABLE;
2841 viewFlagMasks |= CLICKABLE;
2842 }
2843 break;
2844 case com.android.internal.R.styleable.View_longClickable:
2845 if (a.getBoolean(attr, false)) {
2846 viewFlagValues |= LONG_CLICKABLE;
2847 viewFlagMasks |= LONG_CLICKABLE;
2848 }
2849 break;
2850 case com.android.internal.R.styleable.View_saveEnabled:
2851 if (!a.getBoolean(attr, true)) {
2852 viewFlagValues |= SAVE_DISABLED;
2853 viewFlagMasks |= SAVE_DISABLED_MASK;
2854 }
2855 break;
2856 case com.android.internal.R.styleable.View_duplicateParentState:
2857 if (a.getBoolean(attr, false)) {
2858 viewFlagValues |= DUPLICATE_PARENT_STATE;
2859 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2860 }
2861 break;
2862 case com.android.internal.R.styleable.View_visibility:
2863 final int visibility = a.getInt(attr, 0);
2864 if (visibility != 0) {
2865 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2866 viewFlagMasks |= VISIBILITY_MASK;
2867 }
2868 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002869 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002870 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002871 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002872 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002873 final int layoutDirection = a.getInt(attr, -1);
2874 if (layoutDirection != -1) {
2875 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002876 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002877 // Set to default (LAYOUT_DIRECTION_INHERIT)
2878 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002879 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002880 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002881 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 case com.android.internal.R.styleable.View_drawingCacheQuality:
2883 final int cacheQuality = a.getInt(attr, 0);
2884 if (cacheQuality != 0) {
2885 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2886 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2887 }
2888 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002889 case com.android.internal.R.styleable.View_contentDescription:
2890 mContentDescription = a.getString(attr);
2891 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2893 if (!a.getBoolean(attr, true)) {
2894 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2895 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2896 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002897 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2899 if (!a.getBoolean(attr, true)) {
2900 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2901 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2902 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002903 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 case R.styleable.View_scrollbars:
2905 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2906 if (scrollbars != SCROLLBARS_NONE) {
2907 viewFlagValues |= scrollbars;
2908 viewFlagMasks |= SCROLLBARS_MASK;
2909 initializeScrollbars(a);
2910 }
2911 break;
2912 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07002913 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
2914 // Ignore the attribute starting with ICS
2915 break;
2916 }
2917 // With builds < ICS, fall through and apply fading edges
2918 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2920 if (fadingEdge != FADING_EDGE_NONE) {
2921 viewFlagValues |= fadingEdge;
2922 viewFlagMasks |= FADING_EDGE_MASK;
2923 initializeFadingEdge(a);
2924 }
2925 break;
2926 case R.styleable.View_scrollbarStyle:
2927 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2928 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2929 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2930 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2931 }
2932 break;
2933 case R.styleable.View_isScrollContainer:
2934 setScrollContainer = true;
2935 if (a.getBoolean(attr, false)) {
2936 setScrollContainer(true);
2937 }
2938 break;
2939 case com.android.internal.R.styleable.View_keepScreenOn:
2940 if (a.getBoolean(attr, false)) {
2941 viewFlagValues |= KEEP_SCREEN_ON;
2942 viewFlagMasks |= KEEP_SCREEN_ON;
2943 }
2944 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002945 case R.styleable.View_filterTouchesWhenObscured:
2946 if (a.getBoolean(attr, false)) {
2947 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2948 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2949 }
2950 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 case R.styleable.View_nextFocusLeft:
2952 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2953 break;
2954 case R.styleable.View_nextFocusRight:
2955 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2956 break;
2957 case R.styleable.View_nextFocusUp:
2958 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2959 break;
2960 case R.styleable.View_nextFocusDown:
2961 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2962 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002963 case R.styleable.View_nextFocusForward:
2964 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2965 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 case R.styleable.View_minWidth:
2967 mMinWidth = a.getDimensionPixelSize(attr, 0);
2968 break;
2969 case R.styleable.View_minHeight:
2970 mMinHeight = a.getDimensionPixelSize(attr, 0);
2971 break;
Romain Guy9a817362009-05-01 10:57:14 -07002972 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002973 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002974 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002975 + "be used within a restricted context");
2976 }
2977
Romain Guy9a817362009-05-01 10:57:14 -07002978 final String handlerName = a.getString(attr);
2979 if (handlerName != null) {
2980 setOnClickListener(new OnClickListener() {
2981 private Method mHandler;
2982
2983 public void onClick(View v) {
2984 if (mHandler == null) {
2985 try {
2986 mHandler = getContext().getClass().getMethod(handlerName,
2987 View.class);
2988 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002989 int id = getId();
2990 String idText = id == NO_ID ? "" : " with id '"
2991 + getContext().getResources().getResourceEntryName(
2992 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002993 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002994 handlerName + "(View) in the activity "
2995 + getContext().getClass() + " for onClick handler"
2996 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002997 }
2998 }
2999
3000 try {
3001 mHandler.invoke(getContext(), View.this);
3002 } catch (IllegalAccessException e) {
3003 throw new IllegalStateException("Could not execute non "
3004 + "public method of the activity", e);
3005 } catch (InvocationTargetException e) {
3006 throw new IllegalStateException("Could not execute "
3007 + "method of the activity", e);
3008 }
3009 }
3010 });
3011 }
3012 break;
Adam Powell637d3372010-08-25 14:37:03 -07003013 case R.styleable.View_overScrollMode:
3014 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3015 break;
Adam Powell20232d02010-12-08 21:08:53 -08003016 case R.styleable.View_verticalScrollbarPosition:
3017 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3018 break;
Romain Guy171c5922011-01-06 10:04:23 -08003019 case R.styleable.View_layerType:
3020 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3021 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003022 case R.styleable.View_textDirection:
3023 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
3024 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 }
3026 }
3027
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003028 a.recycle();
3029
Adam Powell637d3372010-08-25 14:37:03 -07003030 setOverScrollMode(overScrollMode);
3031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 if (background != null) {
3033 setBackgroundDrawable(background);
3034 }
3035
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003036 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
3037
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003038 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3039 // layout direction). Those cached values will be used later during padding resolution.
3040 mUserPaddingStart = startPadding;
3041 mUserPaddingEnd = endPadding;
3042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003043 if (padding >= 0) {
3044 leftPadding = padding;
3045 topPadding = padding;
3046 rightPadding = padding;
3047 bottomPadding = padding;
3048 }
3049
3050 // If the user specified the padding (either with android:padding or
3051 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3052 // use the default padding or the padding from the background drawable
3053 // (stored at this point in mPadding*)
3054 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3055 topPadding >= 0 ? topPadding : mPaddingTop,
3056 rightPadding >= 0 ? rightPadding : mPaddingRight,
3057 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3058
3059 if (viewFlagMasks != 0) {
3060 setFlags(viewFlagValues, viewFlagMasks);
3061 }
3062
3063 // Needs to be called after mViewFlags is set
3064 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3065 recomputePadding();
3066 }
3067
3068 if (x != 0 || y != 0) {
3069 scrollTo(x, y);
3070 }
3071
Chet Haase73066682010-11-29 15:55:32 -08003072 if (transformSet) {
3073 setTranslationX(tx);
3074 setTranslationY(ty);
3075 setRotation(rotation);
3076 setRotationX(rotationX);
3077 setRotationY(rotationY);
3078 setScaleX(sx);
3079 setScaleY(sy);
3080 }
3081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3083 setScrollContainer(true);
3084 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003085
3086 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 }
3088
3089 /**
3090 * Non-public constructor for use in testing
3091 */
3092 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003093 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003094 }
3095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003096 /**
3097 * <p>
3098 * Initializes the fading edges from a given set of styled attributes. This
3099 * method should be called by subclasses that need fading edges and when an
3100 * instance of these subclasses is created programmatically rather than
3101 * being inflated from XML. This method is automatically called when the XML
3102 * is inflated.
3103 * </p>
3104 *
3105 * @param a the styled attributes set to initialize the fading edges from
3106 */
3107 protected void initializeFadingEdge(TypedArray a) {
3108 initScrollCache();
3109
3110 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3111 R.styleable.View_fadingEdgeLength,
3112 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3113 }
3114
3115 /**
3116 * Returns the size of the vertical faded edges used to indicate that more
3117 * content in this view is visible.
3118 *
3119 * @return The size in pixels of the vertical faded edge or 0 if vertical
3120 * faded edges are not enabled for this view.
3121 * @attr ref android.R.styleable#View_fadingEdgeLength
3122 */
3123 public int getVerticalFadingEdgeLength() {
3124 if (isVerticalFadingEdgeEnabled()) {
3125 ScrollabilityCache cache = mScrollCache;
3126 if (cache != null) {
3127 return cache.fadingEdgeLength;
3128 }
3129 }
3130 return 0;
3131 }
3132
3133 /**
3134 * Set the size of the faded edge used to indicate that more content in this
3135 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003136 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3137 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3138 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 *
3140 * @param length The size in pixels of the faded edge used to indicate that more
3141 * content in this view is visible.
3142 */
3143 public void setFadingEdgeLength(int length) {
3144 initScrollCache();
3145 mScrollCache.fadingEdgeLength = length;
3146 }
3147
3148 /**
3149 * Returns the size of the horizontal faded edges used to indicate that more
3150 * content in this view is visible.
3151 *
3152 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3153 * faded edges are not enabled for this view.
3154 * @attr ref android.R.styleable#View_fadingEdgeLength
3155 */
3156 public int getHorizontalFadingEdgeLength() {
3157 if (isHorizontalFadingEdgeEnabled()) {
3158 ScrollabilityCache cache = mScrollCache;
3159 if (cache != null) {
3160 return cache.fadingEdgeLength;
3161 }
3162 }
3163 return 0;
3164 }
3165
3166 /**
3167 * Returns the width of the vertical scrollbar.
3168 *
3169 * @return The width in pixels of the vertical scrollbar or 0 if there
3170 * is no vertical scrollbar.
3171 */
3172 public int getVerticalScrollbarWidth() {
3173 ScrollabilityCache cache = mScrollCache;
3174 if (cache != null) {
3175 ScrollBarDrawable scrollBar = cache.scrollBar;
3176 if (scrollBar != null) {
3177 int size = scrollBar.getSize(true);
3178 if (size <= 0) {
3179 size = cache.scrollBarSize;
3180 }
3181 return size;
3182 }
3183 return 0;
3184 }
3185 return 0;
3186 }
3187
3188 /**
3189 * Returns the height of the horizontal scrollbar.
3190 *
3191 * @return The height in pixels of the horizontal scrollbar or 0 if
3192 * there is no horizontal scrollbar.
3193 */
3194 protected int getHorizontalScrollbarHeight() {
3195 ScrollabilityCache cache = mScrollCache;
3196 if (cache != null) {
3197 ScrollBarDrawable scrollBar = cache.scrollBar;
3198 if (scrollBar != null) {
3199 int size = scrollBar.getSize(false);
3200 if (size <= 0) {
3201 size = cache.scrollBarSize;
3202 }
3203 return size;
3204 }
3205 return 0;
3206 }
3207 return 0;
3208 }
3209
3210 /**
3211 * <p>
3212 * Initializes the scrollbars from a given set of styled attributes. This
3213 * method should be called by subclasses that need scrollbars and when an
3214 * instance of these subclasses is created programmatically rather than
3215 * being inflated from XML. This method is automatically called when the XML
3216 * is inflated.
3217 * </p>
3218 *
3219 * @param a the styled attributes set to initialize the scrollbars from
3220 */
3221 protected void initializeScrollbars(TypedArray a) {
3222 initScrollCache();
3223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003225
Mike Cleronf116bf82009-09-27 19:14:12 -07003226 if (scrollabilityCache.scrollBar == null) {
3227 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3228 }
Joe Malin32736f02011-01-19 16:14:20 -08003229
Romain Guy8bda2482010-03-02 11:42:11 -08003230 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231
Mike Cleronf116bf82009-09-27 19:14:12 -07003232 if (!fadeScrollbars) {
3233 scrollabilityCache.state = ScrollabilityCache.ON;
3234 }
3235 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003236
3237
Mike Cleronf116bf82009-09-27 19:14:12 -07003238 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3239 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3240 .getScrollBarFadeDuration());
3241 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3242 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3243 ViewConfiguration.getScrollDefaultDelay());
3244
Joe Malin32736f02011-01-19 16:14:20 -08003245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3247 com.android.internal.R.styleable.View_scrollbarSize,
3248 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3249
3250 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3251 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3252
3253 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3254 if (thumb != null) {
3255 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3256 }
3257
3258 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3259 false);
3260 if (alwaysDraw) {
3261 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3262 }
3263
3264 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3265 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3266
3267 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3268 if (thumb != null) {
3269 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3270 }
3271
3272 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3273 false);
3274 if (alwaysDraw) {
3275 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3276 }
3277
3278 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003279 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003280 }
3281
3282 /**
3283 * <p>
3284 * Initalizes the scrollability cache if necessary.
3285 * </p>
3286 */
3287 private void initScrollCache() {
3288 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003289 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003290 }
3291 }
3292
3293 /**
Adam Powell20232d02010-12-08 21:08:53 -08003294 * Set the position of the vertical scroll bar. Should be one of
3295 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3296 * {@link #SCROLLBAR_POSITION_RIGHT}.
3297 *
3298 * @param position Where the vertical scroll bar should be positioned.
3299 */
3300 public void setVerticalScrollbarPosition(int position) {
3301 if (mVerticalScrollbarPosition != position) {
3302 mVerticalScrollbarPosition = position;
3303 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003304 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003305 }
3306 }
3307
3308 /**
3309 * @return The position where the vertical scroll bar will show, if applicable.
3310 * @see #setVerticalScrollbarPosition(int)
3311 */
3312 public int getVerticalScrollbarPosition() {
3313 return mVerticalScrollbarPosition;
3314 }
3315
3316 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 * Register a callback to be invoked when focus of this view changed.
3318 *
3319 * @param l The callback that will run.
3320 */
3321 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3322 mOnFocusChangeListener = l;
3323 }
3324
3325 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003326 * Add a listener that will be called when the bounds of the view change due to
3327 * layout processing.
3328 *
3329 * @param listener The listener that will be called when layout bounds change.
3330 */
3331 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3332 if (mOnLayoutChangeListeners == null) {
3333 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3334 }
3335 mOnLayoutChangeListeners.add(listener);
3336 }
3337
3338 /**
3339 * Remove a listener for layout changes.
3340 *
3341 * @param listener The listener for layout bounds change.
3342 */
3343 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3344 if (mOnLayoutChangeListeners == null) {
3345 return;
3346 }
3347 mOnLayoutChangeListeners.remove(listener);
3348 }
3349
3350 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003351 * Add a listener for attach state changes.
3352 *
3353 * This listener will be called whenever this view is attached or detached
3354 * from a window. Remove the listener using
3355 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3356 *
3357 * @param listener Listener to attach
3358 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3359 */
3360 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3361 if (mOnAttachStateChangeListeners == null) {
3362 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3363 }
3364 mOnAttachStateChangeListeners.add(listener);
3365 }
3366
3367 /**
3368 * Remove a listener for attach state changes. The listener will receive no further
3369 * notification of window attach/detach events.
3370 *
3371 * @param listener Listener to remove
3372 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3373 */
3374 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3375 if (mOnAttachStateChangeListeners == null) {
3376 return;
3377 }
3378 mOnAttachStateChangeListeners.remove(listener);
3379 }
3380
3381 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003382 * Returns the focus-change callback registered for this view.
3383 *
3384 * @return The callback, or null if one is not registered.
3385 */
3386 public OnFocusChangeListener getOnFocusChangeListener() {
3387 return mOnFocusChangeListener;
3388 }
3389
3390 /**
3391 * Register a callback to be invoked when this view is clicked. If this view is not
3392 * clickable, it becomes clickable.
3393 *
3394 * @param l The callback that will run
3395 *
3396 * @see #setClickable(boolean)
3397 */
3398 public void setOnClickListener(OnClickListener l) {
3399 if (!isClickable()) {
3400 setClickable(true);
3401 }
3402 mOnClickListener = l;
3403 }
3404
3405 /**
3406 * Register a callback to be invoked when this view is clicked and held. If this view is not
3407 * long clickable, it becomes long clickable.
3408 *
3409 * @param l The callback that will run
3410 *
3411 * @see #setLongClickable(boolean)
3412 */
3413 public void setOnLongClickListener(OnLongClickListener l) {
3414 if (!isLongClickable()) {
3415 setLongClickable(true);
3416 }
3417 mOnLongClickListener = l;
3418 }
3419
3420 /**
3421 * Register a callback to be invoked when the context menu for this view is
3422 * being built. If this view is not long clickable, it becomes long clickable.
3423 *
3424 * @param l The callback that will run
3425 *
3426 */
3427 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3428 if (!isLongClickable()) {
3429 setLongClickable(true);
3430 }
3431 mOnCreateContextMenuListener = l;
3432 }
3433
3434 /**
3435 * Call this view's OnClickListener, if it is defined.
3436 *
3437 * @return True there was an assigned OnClickListener that was called, false
3438 * otherwise is returned.
3439 */
3440 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003441 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003443 if (mOnClickListener != null) {
3444 playSoundEffect(SoundEffectConstants.CLICK);
3445 mOnClickListener.onClick(this);
3446 return true;
3447 }
3448
3449 return false;
3450 }
3451
3452 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003453 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3454 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003455 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003456 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003457 */
3458 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003459 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 boolean handled = false;
3462 if (mOnLongClickListener != null) {
3463 handled = mOnLongClickListener.onLongClick(View.this);
3464 }
3465 if (!handled) {
3466 handled = showContextMenu();
3467 }
3468 if (handled) {
3469 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3470 }
3471 return handled;
3472 }
3473
3474 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003475 * Performs button-related actions during a touch down event.
3476 *
3477 * @param event The event.
3478 * @return True if the down was consumed.
3479 *
3480 * @hide
3481 */
3482 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3483 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3484 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3485 return true;
3486 }
3487 }
3488 return false;
3489 }
3490
3491 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003492 * Bring up the context menu for this view.
3493 *
3494 * @return Whether a context menu was displayed.
3495 */
3496 public boolean showContextMenu() {
3497 return getParent().showContextMenuForChild(this);
3498 }
3499
3500 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003501 * Bring up the context menu for this view, referring to the item under the specified point.
3502 *
3503 * @param x The referenced x coordinate.
3504 * @param y The referenced y coordinate.
3505 * @param metaState The keyboard modifiers that were pressed.
3506 * @return Whether a context menu was displayed.
3507 *
3508 * @hide
3509 */
3510 public boolean showContextMenu(float x, float y, int metaState) {
3511 return showContextMenu();
3512 }
3513
3514 /**
Adam Powell6e346362010-07-23 10:18:23 -07003515 * Start an action mode.
3516 *
3517 * @param callback Callback that will control the lifecycle of the action mode
3518 * @return The new action mode if it is started, null otherwise
3519 *
3520 * @see ActionMode
3521 */
3522 public ActionMode startActionMode(ActionMode.Callback callback) {
3523 return getParent().startActionModeForChild(this, callback);
3524 }
3525
3526 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527 * Register a callback to be invoked when a key is pressed in this view.
3528 * @param l the key listener to attach to this view
3529 */
3530 public void setOnKeyListener(OnKeyListener l) {
3531 mOnKeyListener = l;
3532 }
3533
3534 /**
3535 * Register a callback to be invoked when a touch event is sent to this view.
3536 * @param l the touch listener to attach to this view
3537 */
3538 public void setOnTouchListener(OnTouchListener l) {
3539 mOnTouchListener = l;
3540 }
3541
3542 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003543 * Register a callback to be invoked when a generic motion event is sent to this view.
3544 * @param l the generic motion listener to attach to this view
3545 */
3546 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3547 mOnGenericMotionListener = l;
3548 }
3549
3550 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003551 * Register a callback to be invoked when a hover event is sent to this view.
3552 * @param l the hover listener to attach to this view
3553 */
3554 public void setOnHoverListener(OnHoverListener l) {
3555 mOnHoverListener = l;
3556 }
3557
3558 /**
Joe Malin32736f02011-01-19 16:14:20 -08003559 * Register a drag event listener callback object for this View. The parameter is
3560 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3561 * View, the system calls the
3562 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3563 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003564 */
3565 public void setOnDragListener(OnDragListener l) {
3566 mOnDragListener = l;
3567 }
3568
3569 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003570 * Give this view focus. This will cause
3571 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003572 *
3573 * Note: this does not check whether this {@link View} should get focus, it just
3574 * gives it focus no matter what. It should only be called internally by framework
3575 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3576 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003577 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3578 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003579 * focus moved when requestFocus() is called. It may not always
3580 * apply, in which case use the default View.FOCUS_DOWN.
3581 * @param previouslyFocusedRect The rectangle of the view that had focus
3582 * prior in this View's coordinate system.
3583 */
3584 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3585 if (DBG) {
3586 System.out.println(this + " requestFocus()");
3587 }
3588
3589 if ((mPrivateFlags & FOCUSED) == 0) {
3590 mPrivateFlags |= FOCUSED;
3591
3592 if (mParent != null) {
3593 mParent.requestChildFocus(this, this);
3594 }
3595
3596 onFocusChanged(true, direction, previouslyFocusedRect);
3597 refreshDrawableState();
3598 }
3599 }
3600
3601 /**
3602 * Request that a rectangle of this view be visible on the screen,
3603 * scrolling if necessary just enough.
3604 *
3605 * <p>A View should call this if it maintains some notion of which part
3606 * of its content is interesting. For example, a text editing view
3607 * should call this when its cursor moves.
3608 *
3609 * @param rectangle The rectangle.
3610 * @return Whether any parent scrolled.
3611 */
3612 public boolean requestRectangleOnScreen(Rect rectangle) {
3613 return requestRectangleOnScreen(rectangle, false);
3614 }
3615
3616 /**
3617 * Request that a rectangle of this view be visible on the screen,
3618 * scrolling if necessary just enough.
3619 *
3620 * <p>A View should call this if it maintains some notion of which part
3621 * of its content is interesting. For example, a text editing view
3622 * should call this when its cursor moves.
3623 *
3624 * <p>When <code>immediate</code> is set to true, scrolling will not be
3625 * animated.
3626 *
3627 * @param rectangle The rectangle.
3628 * @param immediate True to forbid animated scrolling, false otherwise
3629 * @return Whether any parent scrolled.
3630 */
3631 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3632 View child = this;
3633 ViewParent parent = mParent;
3634 boolean scrolled = false;
3635 while (parent != null) {
3636 scrolled |= parent.requestChildRectangleOnScreen(child,
3637 rectangle, immediate);
3638
3639 // offset rect so next call has the rectangle in the
3640 // coordinate system of its direct child.
3641 rectangle.offset(child.getLeft(), child.getTop());
3642 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3643
3644 if (!(parent instanceof View)) {
3645 break;
3646 }
Romain Guy8506ab42009-06-11 17:35:47 -07003647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003648 child = (View) parent;
3649 parent = child.getParent();
3650 }
3651 return scrolled;
3652 }
3653
3654 /**
3655 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003656 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003657 */
3658 public void clearFocus() {
3659 if (DBG) {
3660 System.out.println(this + " clearFocus()");
3661 }
3662
3663 if ((mPrivateFlags & FOCUSED) != 0) {
3664 mPrivateFlags &= ~FOCUSED;
3665
3666 if (mParent != null) {
3667 mParent.clearChildFocus(this);
3668 }
3669
3670 onFocusChanged(false, 0, null);
3671 refreshDrawableState();
3672 }
3673 }
3674
3675 /**
3676 * Called to clear the focus of a view that is about to be removed.
3677 * Doesn't call clearChildFocus, which prevents this view from taking
3678 * focus again before it has been removed from the parent
3679 */
3680 void clearFocusForRemoval() {
3681 if ((mPrivateFlags & FOCUSED) != 0) {
3682 mPrivateFlags &= ~FOCUSED;
3683
3684 onFocusChanged(false, 0, null);
3685 refreshDrawableState();
3686 }
3687 }
3688
3689 /**
3690 * Called internally by the view system when a new view is getting focus.
3691 * This is what clears the old focus.
3692 */
3693 void unFocus() {
3694 if (DBG) {
3695 System.out.println(this + " unFocus()");
3696 }
3697
3698 if ((mPrivateFlags & FOCUSED) != 0) {
3699 mPrivateFlags &= ~FOCUSED;
3700
3701 onFocusChanged(false, 0, null);
3702 refreshDrawableState();
3703 }
3704 }
3705
3706 /**
3707 * Returns true if this view has focus iteself, or is the ancestor of the
3708 * view that has focus.
3709 *
3710 * @return True if this view has or contains focus, false otherwise.
3711 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003712 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003713 public boolean hasFocus() {
3714 return (mPrivateFlags & FOCUSED) != 0;
3715 }
3716
3717 /**
3718 * Returns true if this view is focusable or if it contains a reachable View
3719 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3720 * is a View whose parents do not block descendants focus.
3721 *
3722 * Only {@link #VISIBLE} views are considered focusable.
3723 *
3724 * @return True if the view is focusable or if the view contains a focusable
3725 * View, false otherwise.
3726 *
3727 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3728 */
3729 public boolean hasFocusable() {
3730 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3731 }
3732
3733 /**
3734 * Called by the view system when the focus state of this view changes.
3735 * When the focus change event is caused by directional navigation, direction
3736 * and previouslyFocusedRect provide insight into where the focus is coming from.
3737 * When overriding, be sure to call up through to the super class so that
3738 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003739 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 * @param gainFocus True if the View has focus; false otherwise.
3741 * @param direction The direction focus has moved when requestFocus()
3742 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003743 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3744 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3745 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003746 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3747 * system, of the previously focused view. If applicable, this will be
3748 * passed in as finer grained information about where the focus is coming
3749 * from (in addition to direction). Will be <code>null</code> otherwise.
3750 */
3751 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003752 if (gainFocus) {
3753 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3754 }
3755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003756 InputMethodManager imm = InputMethodManager.peekInstance();
3757 if (!gainFocus) {
3758 if (isPressed()) {
3759 setPressed(false);
3760 }
3761 if (imm != null && mAttachInfo != null
3762 && mAttachInfo.mHasWindowFocus) {
3763 imm.focusOut(this);
3764 }
Romain Guya2431d02009-04-30 16:30:00 -07003765 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003766 } else if (imm != null && mAttachInfo != null
3767 && mAttachInfo.mHasWindowFocus) {
3768 imm.focusIn(this);
3769 }
Romain Guy8506ab42009-06-11 17:35:47 -07003770
Romain Guy0fd89bf2011-01-26 15:41:30 -08003771 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003772 if (mOnFocusChangeListener != null) {
3773 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3774 }
Joe Malin32736f02011-01-19 16:14:20 -08003775
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003776 if (mAttachInfo != null) {
3777 mAttachInfo.mKeyDispatchState.reset(this);
3778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003779 }
3780
3781 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003782 * Sends an accessibility event of the given type. If accessiiblity is
3783 * not enabled this method has no effect. The default implementation calls
3784 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3785 * to populate information about the event source (this View), then calls
3786 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3787 * populate the text content of the event source including its descendants,
3788 * and last calls
3789 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3790 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003791 * <p>
3792 * If an {@link AccessibilityDelegate} has been specified via calling
3793 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3794 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
3795 * responsible for handling this call.
3796 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003797 *
3798 * @param eventType The type of the event to send.
3799 *
3800 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3801 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3802 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003803 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07003804 */
3805 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003806 if (mAccessibilityDelegate != null) {
3807 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
3808 } else {
3809 sendAccessibilityEventInternal(eventType);
3810 }
3811 }
3812
3813 /**
3814 * @see #sendAccessibilityEvent(int)
3815 *
3816 * Note: Called from the default {@link AccessibilityDelegate}.
3817 */
3818 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003819 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3820 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3821 }
3822 }
3823
3824 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003825 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3826 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003827 * perform a check whether accessibility is enabled.
3828 * <p>
3829 * If an {@link AccessibilityDelegate} has been specified via calling
3830 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3831 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
3832 * is responsible for handling this call.
3833 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003834 *
3835 * @param event The event to send.
3836 *
3837 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003838 */
3839 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003840 if (mAccessibilityDelegate != null) {
3841 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
3842 } else {
3843 sendAccessibilityEventUncheckedInternal(event);
3844 }
3845 }
3846
3847 /**
3848 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
3849 *
3850 * Note: Called from the default {@link AccessibilityDelegate}.
3851 */
3852 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003853 if (!isShown()) {
3854 return;
3855 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003856 onInitializeAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003857 dispatchPopulateAccessibilityEvent(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003858 // In the beginning we called #isShown(), so we know that getParent() is not null.
3859 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003860 }
3861
3862 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003863 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3864 * to its children for adding their text content to the event. Note that the
3865 * event text is populated in a separate dispatch path since we add to the
3866 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003867 * A typical implementation will call
3868 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3869 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3870 * on each child. Override this method if custom population of the event text
3871 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003872 * <p>
3873 * If an {@link AccessibilityDelegate} has been specified via calling
3874 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3875 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
3876 * is responsible for handling this call.
3877 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07003878 *
3879 * @param event The event.
3880 *
3881 * @return True if the event population was completed.
3882 */
3883 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003884 if (mAccessibilityDelegate != null) {
3885 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
3886 } else {
3887 return dispatchPopulateAccessibilityEventInternal(event);
3888 }
3889 }
3890
3891 /**
3892 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3893 *
3894 * Note: Called from the default {@link AccessibilityDelegate}.
3895 */
3896 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003897 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003898 return false;
3899 }
3900
3901 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003902 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003903 * giving a chance to this View to populate the accessibility event with its
3904 * text content. While the implementation is free to modify other event
3905 * attributes this should be performed in
3906 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
3907 * <p>
3908 * Example: Adding formatted date string to an accessibility event in addition
3909 * to the text added by the super implementation.
3910 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003911 * public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3912 * super.onPopulateAccessibilityEvent(event);
3913 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
3914 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
3915 * mCurrentDate.getTimeInMillis(), flags);
3916 * event.getText().add(selectedDateUtterance);
3917 * }
3918 * </code></pre></p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003919 * <p>
3920 * If an {@link AccessibilityDelegate} has been specified via calling
3921 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3922 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
3923 * is responsible for handling this call.
3924 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003925 *
3926 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003927 *
3928 * @see #sendAccessibilityEvent(int)
3929 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003930 */
3931 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003932 if (mAccessibilityDelegate != null) {
3933 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
3934 } else {
3935 onPopulateAccessibilityEventInternal(event);
3936 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003937 }
3938
3939 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003940 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
3941 *
3942 * Note: Called from the default {@link AccessibilityDelegate}.
3943 */
3944 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
3945
3946 }
3947
3948 /**
3949 * Initializes an {@link AccessibilityEvent} with information about
3950 * this View which is the event source. In other words, the source of
3951 * an accessibility event is the view whose state change triggered firing
3952 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003953 * <p>
3954 * Example: Setting the password property of an event in addition
3955 * to properties set by the super implementation.
3956 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003957 * public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
3958 * super.onInitializeAccessibilityEvent(event);
3959 * event.setPassword(true);
3960 * }
3961 * </code></pre></p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003962 * <p>
3963 * If an {@link AccessibilityDelegate} has been specified via calling
3964 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3965 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
3966 * is responsible for handling this call.
3967 * </p>
3968 *
3969 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003970 *
3971 * @see #sendAccessibilityEvent(int)
3972 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3973 */
3974 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003975 if (mAccessibilityDelegate != null) {
3976 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
3977 } else {
3978 onInitializeAccessibilityEventInternal(event);
3979 }
3980 }
3981
3982 /**
3983 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3984 *
3985 * Note: Called from the default {@link AccessibilityDelegate}.
3986 */
3987 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003988 event.setSource(this);
Svetoslav Ganov30401322011-05-12 18:53:45 -07003989 event.setClassName(getClass().getName());
3990 event.setPackageName(getContext().getPackageName());
3991 event.setEnabled(isEnabled());
3992 event.setContentDescription(mContentDescription);
3993
Svetoslav Ganova0156172011-06-26 17:55:44 -07003994 final int eventType = event.getEventType();
3995 switch (eventType) {
3996 case AccessibilityEvent.TYPE_VIEW_FOCUSED: {
3997 if (mAttachInfo != null) {
3998 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3999 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4000 FOCUSABLES_ALL);
4001 event.setItemCount(focusablesTempList.size());
4002 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4003 focusablesTempList.clear();
4004 }
4005 } break;
4006 case AccessibilityEvent.TYPE_VIEW_SCROLLED: {
4007 event.setScrollX(mScrollX);
4008 event.setScrollY(mScrollY);
4009 event.setItemCount(getHeight());
4010 } break;
Svetoslav Ganov30401322011-05-12 18:53:45 -07004011 }
4012 }
4013
4014 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004015 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4016 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4017 * This method is responsible for obtaining an accessibility node info from a
4018 * pool of reusable instances and calling
4019 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4020 * initialize the former.
4021 * <p>
4022 * Note: The client is responsible for recycling the obtained instance by calling
4023 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4024 * </p>
4025 * @return A populated {@link AccessibilityNodeInfo}.
4026 *
4027 * @see AccessibilityNodeInfo
4028 */
4029 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
4030 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4031 onInitializeAccessibilityNodeInfo(info);
4032 return info;
4033 }
4034
4035 /**
4036 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4037 * The base implementation sets:
4038 * <ul>
4039 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004040 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4041 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004042 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4043 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4044 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4045 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4046 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4047 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4048 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4049 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4050 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4051 * </ul>
4052 * <p>
4053 * Subclasses should override this method, call the super implementation,
4054 * and set additional attributes.
4055 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004056 * <p>
4057 * If an {@link AccessibilityDelegate} has been specified via calling
4058 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4059 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4060 * is responsible for handling this call.
4061 * </p>
4062 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004063 * @param info The instance to initialize.
4064 */
4065 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004066 if (mAccessibilityDelegate != null) {
4067 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4068 } else {
4069 onInitializeAccessibilityNodeInfoInternal(info);
4070 }
4071 }
4072
4073 /**
4074 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4075 *
4076 * Note: Called from the default {@link AccessibilityDelegate}.
4077 */
4078 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004079 Rect bounds = mAttachInfo.mTmpInvalRect;
4080 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004081 info.setBoundsInParent(bounds);
4082
4083 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4084 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004085 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004086 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4087 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004088
4089 ViewParent parent = getParent();
4090 if (parent instanceof View) {
4091 View parentView = (View) parent;
4092 info.setParent(parentView);
4093 }
4094
4095 info.setPackageName(mContext.getPackageName());
4096 info.setClassName(getClass().getName());
4097 info.setContentDescription(getContentDescription());
4098
4099 info.setEnabled(isEnabled());
4100 info.setClickable(isClickable());
4101 info.setFocusable(isFocusable());
4102 info.setFocused(isFocused());
4103 info.setSelected(isSelected());
4104 info.setLongClickable(isLongClickable());
4105
4106 // TODO: These make sense only if we are in an AdapterView but all
4107 // views can be selected. Maybe from accessiiblity perspective
4108 // we should report as selectable view in an AdapterView.
4109 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4110 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4111
4112 if (isFocusable()) {
4113 if (isFocused()) {
4114 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4115 } else {
4116 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4117 }
4118 }
4119 }
4120
4121 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004122 * Sets a delegate for implementing accessibility support via compositon as
4123 * opposed to inheritance. The delegate's primary use is for implementing
4124 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4125 *
4126 * @param delegate The delegate instance.
4127 *
4128 * @see AccessibilityDelegate
4129 */
4130 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4131 mAccessibilityDelegate = delegate;
4132 }
4133
4134 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004135 * Gets the unique identifier of this view on the screen for accessibility purposes.
4136 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4137 *
4138 * @return The view accessibility id.
4139 *
4140 * @hide
4141 */
4142 public int getAccessibilityViewId() {
4143 if (mAccessibilityViewId == NO_ID) {
4144 mAccessibilityViewId = sNextAccessibilityViewId++;
4145 }
4146 return mAccessibilityViewId;
4147 }
4148
4149 /**
4150 * Gets the unique identifier of the window in which this View reseides.
4151 *
4152 * @return The window accessibility id.
4153 *
4154 * @hide
4155 */
4156 public int getAccessibilityWindowId() {
4157 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4158 }
4159
4160 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004161 * Gets the {@link View} description. It briefly describes the view and is
4162 * primarily used for accessibility support. Set this property to enable
4163 * better accessibility support for your application. This is especially
4164 * true for views that do not have textual representation (For example,
4165 * ImageButton).
4166 *
4167 * @return The content descriptiopn.
4168 *
4169 * @attr ref android.R.styleable#View_contentDescription
4170 */
4171 public CharSequence getContentDescription() {
4172 return mContentDescription;
4173 }
4174
4175 /**
4176 * Sets the {@link View} description. It briefly describes the view and is
4177 * primarily used for accessibility support. Set this property to enable
4178 * better accessibility support for your application. This is especially
4179 * true for views that do not have textual representation (For example,
4180 * ImageButton).
4181 *
4182 * @param contentDescription The content description.
4183 *
4184 * @attr ref android.R.styleable#View_contentDescription
4185 */
4186 public void setContentDescription(CharSequence contentDescription) {
4187 mContentDescription = contentDescription;
4188 }
4189
4190 /**
Romain Guya2431d02009-04-30 16:30:00 -07004191 * Invoked whenever this view loses focus, either by losing window focus or by losing
4192 * focus within its window. This method can be used to clear any state tied to the
4193 * focus. For instance, if a button is held pressed with the trackball and the window
4194 * loses focus, this method can be used to cancel the press.
4195 *
4196 * Subclasses of View overriding this method should always call super.onFocusLost().
4197 *
4198 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004199 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004200 *
4201 * @hide pending API council approval
4202 */
4203 protected void onFocusLost() {
4204 resetPressedState();
4205 }
4206
4207 private void resetPressedState() {
4208 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4209 return;
4210 }
4211
4212 if (isPressed()) {
4213 setPressed(false);
4214
4215 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004216 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004217 }
4218 }
4219 }
4220
4221 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004222 * Returns true if this view has focus
4223 *
4224 * @return True if this view has focus, false otherwise.
4225 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004226 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 public boolean isFocused() {
4228 return (mPrivateFlags & FOCUSED) != 0;
4229 }
4230
4231 /**
4232 * Find the view in the hierarchy rooted at this view that currently has
4233 * focus.
4234 *
4235 * @return The view that currently has focus, or null if no focused view can
4236 * be found.
4237 */
4238 public View findFocus() {
4239 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4240 }
4241
4242 /**
4243 * Change whether this view is one of the set of scrollable containers in
4244 * its window. This will be used to determine whether the window can
4245 * resize or must pan when a soft input area is open -- scrollable
4246 * containers allow the window to use resize mode since the container
4247 * will appropriately shrink.
4248 */
4249 public void setScrollContainer(boolean isScrollContainer) {
4250 if (isScrollContainer) {
4251 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4252 mAttachInfo.mScrollContainers.add(this);
4253 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4254 }
4255 mPrivateFlags |= SCROLL_CONTAINER;
4256 } else {
4257 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4258 mAttachInfo.mScrollContainers.remove(this);
4259 }
4260 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4261 }
4262 }
4263
4264 /**
4265 * Returns the quality of the drawing cache.
4266 *
4267 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4268 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4269 *
4270 * @see #setDrawingCacheQuality(int)
4271 * @see #setDrawingCacheEnabled(boolean)
4272 * @see #isDrawingCacheEnabled()
4273 *
4274 * @attr ref android.R.styleable#View_drawingCacheQuality
4275 */
4276 public int getDrawingCacheQuality() {
4277 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4278 }
4279
4280 /**
4281 * Set the drawing cache quality of this view. This value is used only when the
4282 * drawing cache is enabled
4283 *
4284 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4285 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4286 *
4287 * @see #getDrawingCacheQuality()
4288 * @see #setDrawingCacheEnabled(boolean)
4289 * @see #isDrawingCacheEnabled()
4290 *
4291 * @attr ref android.R.styleable#View_drawingCacheQuality
4292 */
4293 public void setDrawingCacheQuality(int quality) {
4294 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4295 }
4296
4297 /**
4298 * Returns whether the screen should remain on, corresponding to the current
4299 * value of {@link #KEEP_SCREEN_ON}.
4300 *
4301 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4302 *
4303 * @see #setKeepScreenOn(boolean)
4304 *
4305 * @attr ref android.R.styleable#View_keepScreenOn
4306 */
4307 public boolean getKeepScreenOn() {
4308 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4309 }
4310
4311 /**
4312 * Controls whether the screen should remain on, modifying the
4313 * value of {@link #KEEP_SCREEN_ON}.
4314 *
4315 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4316 *
4317 * @see #getKeepScreenOn()
4318 *
4319 * @attr ref android.R.styleable#View_keepScreenOn
4320 */
4321 public void setKeepScreenOn(boolean keepScreenOn) {
4322 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4323 }
4324
4325 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004326 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4327 * @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 -08004328 *
4329 * @attr ref android.R.styleable#View_nextFocusLeft
4330 */
4331 public int getNextFocusLeftId() {
4332 return mNextFocusLeftId;
4333 }
4334
4335 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004336 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4337 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4338 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004339 *
4340 * @attr ref android.R.styleable#View_nextFocusLeft
4341 */
4342 public void setNextFocusLeftId(int nextFocusLeftId) {
4343 mNextFocusLeftId = nextFocusLeftId;
4344 }
4345
4346 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004347 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4348 * @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 -08004349 *
4350 * @attr ref android.R.styleable#View_nextFocusRight
4351 */
4352 public int getNextFocusRightId() {
4353 return mNextFocusRightId;
4354 }
4355
4356 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004357 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4358 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4359 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004360 *
4361 * @attr ref android.R.styleable#View_nextFocusRight
4362 */
4363 public void setNextFocusRightId(int nextFocusRightId) {
4364 mNextFocusRightId = nextFocusRightId;
4365 }
4366
4367 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004368 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4369 * @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 -08004370 *
4371 * @attr ref android.R.styleable#View_nextFocusUp
4372 */
4373 public int getNextFocusUpId() {
4374 return mNextFocusUpId;
4375 }
4376
4377 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004378 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4379 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4380 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004381 *
4382 * @attr ref android.R.styleable#View_nextFocusUp
4383 */
4384 public void setNextFocusUpId(int nextFocusUpId) {
4385 mNextFocusUpId = nextFocusUpId;
4386 }
4387
4388 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004389 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4390 * @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 -08004391 *
4392 * @attr ref android.R.styleable#View_nextFocusDown
4393 */
4394 public int getNextFocusDownId() {
4395 return mNextFocusDownId;
4396 }
4397
4398 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004399 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4400 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4401 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004402 *
4403 * @attr ref android.R.styleable#View_nextFocusDown
4404 */
4405 public void setNextFocusDownId(int nextFocusDownId) {
4406 mNextFocusDownId = nextFocusDownId;
4407 }
4408
4409 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004410 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4411 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4412 *
4413 * @attr ref android.R.styleable#View_nextFocusForward
4414 */
4415 public int getNextFocusForwardId() {
4416 return mNextFocusForwardId;
4417 }
4418
4419 /**
4420 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4421 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4422 * decide automatically.
4423 *
4424 * @attr ref android.R.styleable#View_nextFocusForward
4425 */
4426 public void setNextFocusForwardId(int nextFocusForwardId) {
4427 mNextFocusForwardId = nextFocusForwardId;
4428 }
4429
4430 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004431 * Returns the visibility of this view and all of its ancestors
4432 *
4433 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4434 */
4435 public boolean isShown() {
4436 View current = this;
4437 //noinspection ConstantConditions
4438 do {
4439 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4440 return false;
4441 }
4442 ViewParent parent = current.mParent;
4443 if (parent == null) {
4444 return false; // We are not attached to the view root
4445 }
4446 if (!(parent instanceof View)) {
4447 return true;
4448 }
4449 current = (View) parent;
4450 } while (current != null);
4451
4452 return false;
4453 }
4454
4455 /**
4456 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4457 * is set
4458 *
4459 * @param insets Insets for system windows
4460 *
4461 * @return True if this view applied the insets, false otherwise
4462 */
4463 protected boolean fitSystemWindows(Rect insets) {
4464 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4465 mPaddingLeft = insets.left;
4466 mPaddingTop = insets.top;
4467 mPaddingRight = insets.right;
4468 mPaddingBottom = insets.bottom;
4469 requestLayout();
4470 return true;
4471 }
4472 return false;
4473 }
4474
4475 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004476 * Set whether or not this view should account for system screen decorations
4477 * such as the status bar and inset its content. This allows this view to be
4478 * positioned in absolute screen coordinates and remain visible to the user.
4479 *
4480 * <p>This should only be used by top-level window decor views.
4481 *
4482 * @param fitSystemWindows true to inset content for system screen decorations, false for
4483 * default behavior.
4484 *
4485 * @attr ref android.R.styleable#View_fitsSystemWindows
4486 */
4487 public void setFitsSystemWindows(boolean fitSystemWindows) {
4488 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4489 }
4490
4491 /**
4492 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4493 * will account for system screen decorations such as the status bar and inset its
4494 * content. This allows the view to be positioned in absolute screen coordinates
4495 * and remain visible to the user.
4496 *
4497 * @return true if this view will adjust its content bounds for system screen decorations.
4498 *
4499 * @attr ref android.R.styleable#View_fitsSystemWindows
4500 */
4501 public boolean fitsSystemWindows() {
4502 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4503 }
4504
4505 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004506 * Returns the visibility status for this view.
4507 *
4508 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4509 * @attr ref android.R.styleable#View_visibility
4510 */
4511 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004512 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4513 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4514 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004515 })
4516 public int getVisibility() {
4517 return mViewFlags & VISIBILITY_MASK;
4518 }
4519
4520 /**
4521 * Set the enabled state of this view.
4522 *
4523 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4524 * @attr ref android.R.styleable#View_visibility
4525 */
4526 @RemotableViewMethod
4527 public void setVisibility(int visibility) {
4528 setFlags(visibility, VISIBILITY_MASK);
4529 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4530 }
4531
4532 /**
4533 * Returns the enabled status for this view. The interpretation of the
4534 * enabled state varies by subclass.
4535 *
4536 * @return True if this view is enabled, false otherwise.
4537 */
4538 @ViewDebug.ExportedProperty
4539 public boolean isEnabled() {
4540 return (mViewFlags & ENABLED_MASK) == ENABLED;
4541 }
4542
4543 /**
4544 * Set the enabled state of this view. The interpretation of the enabled
4545 * state varies by subclass.
4546 *
4547 * @param enabled True if this view is enabled, false otherwise.
4548 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004549 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004550 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004551 if (enabled == isEnabled()) return;
4552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004553 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4554
4555 /*
4556 * The View most likely has to change its appearance, so refresh
4557 * the drawable state.
4558 */
4559 refreshDrawableState();
4560
4561 // Invalidate too, since the default behavior for views is to be
4562 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004563 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004564 }
4565
4566 /**
4567 * Set whether this view can receive the focus.
4568 *
4569 * Setting this to false will also ensure that this view is not focusable
4570 * in touch mode.
4571 *
4572 * @param focusable If true, this view can receive the focus.
4573 *
4574 * @see #setFocusableInTouchMode(boolean)
4575 * @attr ref android.R.styleable#View_focusable
4576 */
4577 public void setFocusable(boolean focusable) {
4578 if (!focusable) {
4579 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4580 }
4581 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4582 }
4583
4584 /**
4585 * Set whether this view can receive focus while in touch mode.
4586 *
4587 * Setting this to true will also ensure that this view is focusable.
4588 *
4589 * @param focusableInTouchMode If true, this view can receive the focus while
4590 * in touch mode.
4591 *
4592 * @see #setFocusable(boolean)
4593 * @attr ref android.R.styleable#View_focusableInTouchMode
4594 */
4595 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4596 // Focusable in touch mode should always be set before the focusable flag
4597 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4598 // which, in touch mode, will not successfully request focus on this view
4599 // because the focusable in touch mode flag is not set
4600 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4601 if (focusableInTouchMode) {
4602 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4603 }
4604 }
4605
4606 /**
4607 * Set whether this view should have sound effects enabled for events such as
4608 * clicking and touching.
4609 *
4610 * <p>You may wish to disable sound effects for a view if you already play sounds,
4611 * for instance, a dial key that plays dtmf tones.
4612 *
4613 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4614 * @see #isSoundEffectsEnabled()
4615 * @see #playSoundEffect(int)
4616 * @attr ref android.R.styleable#View_soundEffectsEnabled
4617 */
4618 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4619 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4620 }
4621
4622 /**
4623 * @return whether this view should have sound effects enabled for events such as
4624 * clicking and touching.
4625 *
4626 * @see #setSoundEffectsEnabled(boolean)
4627 * @see #playSoundEffect(int)
4628 * @attr ref android.R.styleable#View_soundEffectsEnabled
4629 */
4630 @ViewDebug.ExportedProperty
4631 public boolean isSoundEffectsEnabled() {
4632 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4633 }
4634
4635 /**
4636 * Set whether this view should have haptic feedback for events such as
4637 * long presses.
4638 *
4639 * <p>You may wish to disable haptic feedback if your view already controls
4640 * its own haptic feedback.
4641 *
4642 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4643 * @see #isHapticFeedbackEnabled()
4644 * @see #performHapticFeedback(int)
4645 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4646 */
4647 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4648 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4649 }
4650
4651 /**
4652 * @return whether this view should have haptic feedback enabled for events
4653 * long presses.
4654 *
4655 * @see #setHapticFeedbackEnabled(boolean)
4656 * @see #performHapticFeedback(int)
4657 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4658 */
4659 @ViewDebug.ExportedProperty
4660 public boolean isHapticFeedbackEnabled() {
4661 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4662 }
4663
4664 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004665 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004666 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004667 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4668 * {@link #LAYOUT_DIRECTION_RTL},
4669 * {@link #LAYOUT_DIRECTION_INHERIT} or
4670 * {@link #LAYOUT_DIRECTION_LOCALE}.
4671 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004672 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004673 * @hide
4674 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004675 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004676 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4677 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4678 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4679 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004680 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004681 public int getLayoutDirection() {
4682 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004683 }
4684
4685 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004686 * Set the layout direction for this view. This will propagate a reset of layout direction
4687 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004688 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004689 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4690 * {@link #LAYOUT_DIRECTION_RTL},
4691 * {@link #LAYOUT_DIRECTION_INHERIT} or
4692 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004693 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004694 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004695 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004696 * @hide
4697 */
4698 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004699 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004700 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004701 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004702 // Setting the flag will also request a layout.
4703 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4704 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004705 }
4706
4707 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004708 * Returns the resolved layout direction for this view.
4709 *
4710 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4711 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4712 *
4713 * @hide
4714 */
4715 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4716 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4717 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4718 })
4719 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004720 resolveLayoutDirectionIfNeeded();
4721 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004722 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4723 }
4724
4725 /**
4726 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4727 * layout attribute and/or the inherited value from the parent.</p>
4728 *
4729 * @return true if the layout is right-to-left.
4730 *
4731 * @hide
4732 */
4733 @ViewDebug.ExportedProperty(category = "layout")
4734 public boolean isLayoutRtl() {
4735 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4736 }
4737
4738 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004739 * If this view doesn't do any drawing on its own, set this flag to
4740 * allow further optimizations. By default, this flag is not set on
4741 * View, but could be set on some View subclasses such as ViewGroup.
4742 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004743 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4744 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004745 *
4746 * @param willNotDraw whether or not this View draw on its own
4747 */
4748 public void setWillNotDraw(boolean willNotDraw) {
4749 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4750 }
4751
4752 /**
4753 * Returns whether or not this View draws on its own.
4754 *
4755 * @return true if this view has nothing to draw, false otherwise
4756 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004757 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004758 public boolean willNotDraw() {
4759 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4760 }
4761
4762 /**
4763 * When a View's drawing cache is enabled, drawing is redirected to an
4764 * offscreen bitmap. Some views, like an ImageView, must be able to
4765 * bypass this mechanism if they already draw a single bitmap, to avoid
4766 * unnecessary usage of the memory.
4767 *
4768 * @param willNotCacheDrawing true if this view does not cache its
4769 * drawing, false otherwise
4770 */
4771 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4772 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4773 }
4774
4775 /**
4776 * Returns whether or not this View can cache its drawing or not.
4777 *
4778 * @return true if this view does not cache its drawing, false otherwise
4779 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004780 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004781 public boolean willNotCacheDrawing() {
4782 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4783 }
4784
4785 /**
4786 * Indicates whether this view reacts to click events or not.
4787 *
4788 * @return true if the view is clickable, false otherwise
4789 *
4790 * @see #setClickable(boolean)
4791 * @attr ref android.R.styleable#View_clickable
4792 */
4793 @ViewDebug.ExportedProperty
4794 public boolean isClickable() {
4795 return (mViewFlags & CLICKABLE) == CLICKABLE;
4796 }
4797
4798 /**
4799 * Enables or disables click events for this view. When a view
4800 * is clickable it will change its state to "pressed" on every click.
4801 * Subclasses should set the view clickable to visually react to
4802 * user's clicks.
4803 *
4804 * @param clickable true to make the view clickable, false otherwise
4805 *
4806 * @see #isClickable()
4807 * @attr ref android.R.styleable#View_clickable
4808 */
4809 public void setClickable(boolean clickable) {
4810 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4811 }
4812
4813 /**
4814 * Indicates whether this view reacts to long click events or not.
4815 *
4816 * @return true if the view is long clickable, false otherwise
4817 *
4818 * @see #setLongClickable(boolean)
4819 * @attr ref android.R.styleable#View_longClickable
4820 */
4821 public boolean isLongClickable() {
4822 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4823 }
4824
4825 /**
4826 * Enables or disables long click events for this view. When a view is long
4827 * clickable it reacts to the user holding down the button for a longer
4828 * duration than a tap. This event can either launch the listener or a
4829 * context menu.
4830 *
4831 * @param longClickable true to make the view long clickable, false otherwise
4832 * @see #isLongClickable()
4833 * @attr ref android.R.styleable#View_longClickable
4834 */
4835 public void setLongClickable(boolean longClickable) {
4836 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4837 }
4838
4839 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004840 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004841 *
4842 * @see #isClickable()
4843 * @see #setClickable(boolean)
4844 *
4845 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4846 * the View's internal state from a previously set "pressed" state.
4847 */
4848 public void setPressed(boolean pressed) {
4849 if (pressed) {
4850 mPrivateFlags |= PRESSED;
4851 } else {
4852 mPrivateFlags &= ~PRESSED;
4853 }
4854 refreshDrawableState();
4855 dispatchSetPressed(pressed);
4856 }
4857
4858 /**
4859 * Dispatch setPressed to all of this View's children.
4860 *
4861 * @see #setPressed(boolean)
4862 *
4863 * @param pressed The new pressed state
4864 */
4865 protected void dispatchSetPressed(boolean pressed) {
4866 }
4867
4868 /**
4869 * Indicates whether the view is currently in pressed state. Unless
4870 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4871 * the pressed state.
4872 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004873 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004874 * @see #isClickable()
4875 * @see #setClickable(boolean)
4876 *
4877 * @return true if the view is currently pressed, false otherwise
4878 */
4879 public boolean isPressed() {
4880 return (mPrivateFlags & PRESSED) == PRESSED;
4881 }
4882
4883 /**
4884 * Indicates whether this view will save its state (that is,
4885 * whether its {@link #onSaveInstanceState} method will be called).
4886 *
4887 * @return Returns true if the view state saving is enabled, else false.
4888 *
4889 * @see #setSaveEnabled(boolean)
4890 * @attr ref android.R.styleable#View_saveEnabled
4891 */
4892 public boolean isSaveEnabled() {
4893 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4894 }
4895
4896 /**
4897 * Controls whether the saving of this view's state is
4898 * enabled (that is, whether its {@link #onSaveInstanceState} method
4899 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004900 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004901 * for its state to be saved. This flag can only disable the
4902 * saving of this view; any child views may still have their state saved.
4903 *
4904 * @param enabled Set to false to <em>disable</em> state saving, or true
4905 * (the default) to allow it.
4906 *
4907 * @see #isSaveEnabled()
4908 * @see #setId(int)
4909 * @see #onSaveInstanceState()
4910 * @attr ref android.R.styleable#View_saveEnabled
4911 */
4912 public void setSaveEnabled(boolean enabled) {
4913 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4914 }
4915
Jeff Brown85a31762010-09-01 17:01:00 -07004916 /**
4917 * Gets whether the framework should discard touches when the view's
4918 * window is obscured by another visible window.
4919 * Refer to the {@link View} security documentation for more details.
4920 *
4921 * @return True if touch filtering is enabled.
4922 *
4923 * @see #setFilterTouchesWhenObscured(boolean)
4924 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4925 */
4926 @ViewDebug.ExportedProperty
4927 public boolean getFilterTouchesWhenObscured() {
4928 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4929 }
4930
4931 /**
4932 * Sets whether the framework should discard touches when the view's
4933 * window is obscured by another visible window.
4934 * Refer to the {@link View} security documentation for more details.
4935 *
4936 * @param enabled True if touch filtering should be enabled.
4937 *
4938 * @see #getFilterTouchesWhenObscured
4939 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4940 */
4941 public void setFilterTouchesWhenObscured(boolean enabled) {
4942 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4943 FILTER_TOUCHES_WHEN_OBSCURED);
4944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004945
4946 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004947 * Indicates whether the entire hierarchy under this view will save its
4948 * state when a state saving traversal occurs from its parent. The default
4949 * is true; if false, these views will not be saved unless
4950 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4951 *
4952 * @return Returns true if the view state saving from parent is enabled, else false.
4953 *
4954 * @see #setSaveFromParentEnabled(boolean)
4955 */
4956 public boolean isSaveFromParentEnabled() {
4957 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4958 }
4959
4960 /**
4961 * Controls whether the entire hierarchy under this view will save its
4962 * state when a state saving traversal occurs from its parent. The default
4963 * is true; if false, these views will not be saved unless
4964 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4965 *
4966 * @param enabled Set to false to <em>disable</em> state saving, or true
4967 * (the default) to allow it.
4968 *
4969 * @see #isSaveFromParentEnabled()
4970 * @see #setId(int)
4971 * @see #onSaveInstanceState()
4972 */
4973 public void setSaveFromParentEnabled(boolean enabled) {
4974 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4975 }
4976
4977
4978 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004979 * Returns whether this View is able to take focus.
4980 *
4981 * @return True if this view can take focus, or false otherwise.
4982 * @attr ref android.R.styleable#View_focusable
4983 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004984 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004985 public final boolean isFocusable() {
4986 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4987 }
4988
4989 /**
4990 * When a view is focusable, it may not want to take focus when in touch mode.
4991 * For example, a button would like focus when the user is navigating via a D-pad
4992 * so that the user can click on it, but once the user starts touching the screen,
4993 * the button shouldn't take focus
4994 * @return Whether the view is focusable in touch mode.
4995 * @attr ref android.R.styleable#View_focusableInTouchMode
4996 */
4997 @ViewDebug.ExportedProperty
4998 public final boolean isFocusableInTouchMode() {
4999 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5000 }
5001
5002 /**
5003 * Find the nearest view in the specified direction that can take focus.
5004 * This does not actually give focus to that view.
5005 *
5006 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5007 *
5008 * @return The nearest focusable in the specified direction, or null if none
5009 * can be found.
5010 */
5011 public View focusSearch(int direction) {
5012 if (mParent != null) {
5013 return mParent.focusSearch(this, direction);
5014 } else {
5015 return null;
5016 }
5017 }
5018
5019 /**
5020 * This method is the last chance for the focused view and its ancestors to
5021 * respond to an arrow key. This is called when the focused view did not
5022 * consume the key internally, nor could the view system find a new view in
5023 * the requested direction to give focus to.
5024 *
5025 * @param focused The currently focused view.
5026 * @param direction The direction focus wants to move. One of FOCUS_UP,
5027 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5028 * @return True if the this view consumed this unhandled move.
5029 */
5030 public boolean dispatchUnhandledMove(View focused, int direction) {
5031 return false;
5032 }
5033
5034 /**
5035 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005036 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005037 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005038 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5039 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005040 * @return The user specified next view, or null if there is none.
5041 */
5042 View findUserSetNextFocus(View root, int direction) {
5043 switch (direction) {
5044 case FOCUS_LEFT:
5045 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005046 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005047 case FOCUS_RIGHT:
5048 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005049 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005050 case FOCUS_UP:
5051 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005052 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005053 case FOCUS_DOWN:
5054 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005055 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005056 case FOCUS_FORWARD:
5057 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005058 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005059 case FOCUS_BACKWARD: {
5060 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005061 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005062 @Override
5063 public boolean apply(View t) {
5064 return t.mNextFocusForwardId == id;
5065 }
5066 });
5067 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005068 }
5069 return null;
5070 }
5071
Jeff Brown4dfbec22011-08-15 14:55:37 -07005072 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5073 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5074 @Override
5075 public boolean apply(View t) {
5076 return t.mID == childViewId;
5077 }
5078 });
5079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005080 if (result == null) {
5081 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5082 + "by user for id " + childViewId);
5083 }
5084 return result;
5085 }
5086
5087 /**
5088 * Find and return all focusable views that are descendants of this view,
5089 * possibly including this view if it is focusable itself.
5090 *
5091 * @param direction The direction of the focus
5092 * @return A list of focusable views
5093 */
5094 public ArrayList<View> getFocusables(int direction) {
5095 ArrayList<View> result = new ArrayList<View>(24);
5096 addFocusables(result, direction);
5097 return result;
5098 }
5099
5100 /**
5101 * Add any focusable views that are descendants of this view (possibly
5102 * including this view if it is focusable itself) to views. If we are in touch mode,
5103 * only add views that are also focusable in touch mode.
5104 *
5105 * @param views Focusable views found so far
5106 * @param direction The direction of the focus
5107 */
5108 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005109 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005111
svetoslavganov75986cf2009-05-14 22:28:01 -07005112 /**
5113 * Adds any focusable views that are descendants of this view (possibly
5114 * including this view if it is focusable itself) to views. This method
5115 * adds all focusable views regardless if we are in touch mode or
5116 * only views focusable in touch mode if we are in touch mode depending on
5117 * the focusable mode paramater.
5118 *
5119 * @param views Focusable views found so far or null if all we are interested is
5120 * the number of focusables.
5121 * @param direction The direction of the focus.
5122 * @param focusableMode The type of focusables to be added.
5123 *
5124 * @see #FOCUSABLES_ALL
5125 * @see #FOCUSABLES_TOUCH_MODE
5126 */
5127 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5128 if (!isFocusable()) {
5129 return;
5130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005131
svetoslavganov75986cf2009-05-14 22:28:01 -07005132 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5133 isInTouchMode() && !isFocusableInTouchMode()) {
5134 return;
5135 }
5136
5137 if (views != null) {
5138 views.add(this);
5139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005140 }
5141
5142 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005143 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005144 * The search is performed by either the text that the View renders or the content
5145 * description that describes the view for accessibility purposes and the view does
5146 * not render or both. Clients can specify how the search is to be performed via
5147 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5148 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005149 *
5150 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005151 * @param searched The text to match against.
5152 *
5153 * @see #FIND_VIEWS_WITH_TEXT
5154 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5155 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005156 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005157 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
5158 if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0 && !TextUtils.isEmpty(searched)
5159 && !TextUtils.isEmpty(mContentDescription)) {
5160 String searchedLowerCase = searched.toString().toLowerCase();
5161 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5162 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5163 outViews.add(this);
5164 }
5165 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005166 }
5167
5168 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005169 * Find and return all touchable views that are descendants of this view,
5170 * possibly including this view if it is touchable itself.
5171 *
5172 * @return A list of touchable views
5173 */
5174 public ArrayList<View> getTouchables() {
5175 ArrayList<View> result = new ArrayList<View>();
5176 addTouchables(result);
5177 return result;
5178 }
5179
5180 /**
5181 * Add any touchable views that are descendants of this view (possibly
5182 * including this view if it is touchable itself) to views.
5183 *
5184 * @param views Touchable views found so far
5185 */
5186 public void addTouchables(ArrayList<View> views) {
5187 final int viewFlags = mViewFlags;
5188
5189 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5190 && (viewFlags & ENABLED_MASK) == ENABLED) {
5191 views.add(this);
5192 }
5193 }
5194
5195 /**
5196 * Call this to try to give focus to a specific view or to one of its
5197 * descendants.
5198 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005199 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5200 * false), or if it is focusable and it is not focusable in touch mode
5201 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005202 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005203 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005204 * have focus, and you want your parent to look for the next one.
5205 *
5206 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5207 * {@link #FOCUS_DOWN} and <code>null</code>.
5208 *
5209 * @return Whether this view or one of its descendants actually took focus.
5210 */
5211 public final boolean requestFocus() {
5212 return requestFocus(View.FOCUS_DOWN);
5213 }
5214
5215
5216 /**
5217 * Call this to try to give focus to a specific view or to one of its
5218 * descendants and give it a hint about what direction focus is heading.
5219 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005220 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5221 * false), or if it is focusable and it is not focusable in touch mode
5222 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005223 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005224 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225 * have focus, and you want your parent to look for the next one.
5226 *
5227 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5228 * <code>null</code> set for the previously focused rectangle.
5229 *
5230 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5231 * @return Whether this view or one of its descendants actually took focus.
5232 */
5233 public final boolean requestFocus(int direction) {
5234 return requestFocus(direction, null);
5235 }
5236
5237 /**
5238 * Call this to try to give focus to a specific view or to one of its descendants
5239 * and give it hints about the direction and a specific rectangle that the focus
5240 * is coming from. The rectangle can help give larger views a finer grained hint
5241 * about where focus is coming from, and therefore, where to show selection, or
5242 * forward focus change internally.
5243 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005244 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5245 * false), or if it is focusable and it is not focusable in touch mode
5246 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005247 *
5248 * A View will not take focus if it is not visible.
5249 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005250 * A View will not take focus if one of its parents has
5251 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5252 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005253 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005254 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005255 * have focus, and you want your parent to look for the next one.
5256 *
5257 * You may wish to override this method if your custom {@link View} has an internal
5258 * {@link View} that it wishes to forward the request to.
5259 *
5260 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5261 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5262 * to give a finer grained hint about where focus is coming from. May be null
5263 * if there is no hint.
5264 * @return Whether this view or one of its descendants actually took focus.
5265 */
5266 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5267 // need to be focusable
5268 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5269 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5270 return false;
5271 }
5272
5273 // need to be focusable in touch mode if in touch mode
5274 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005275 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5276 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 }
5278
5279 // need to not have any parents blocking us
5280 if (hasAncestorThatBlocksDescendantFocus()) {
5281 return false;
5282 }
5283
5284 handleFocusGainInternal(direction, previouslyFocusedRect);
5285 return true;
5286 }
5287
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005288 /** Gets the ViewAncestor, or null if not attached. */
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005289 /*package*/ ViewRootImpl getViewRootImpl() {
Christopher Tate2c095f32010-10-04 14:13:40 -07005290 View root = getRootView();
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005291 return root != null ? (ViewRootImpl)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07005292 }
5293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005294 /**
5295 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5296 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5297 * touch mode to request focus when they are touched.
5298 *
5299 * @return Whether this view or one of its descendants actually took focus.
5300 *
5301 * @see #isInTouchMode()
5302 *
5303 */
5304 public final boolean requestFocusFromTouch() {
5305 // Leave touch mode if we need to
5306 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005307 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005308 if (viewRoot != null) {
5309 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005310 }
5311 }
5312 return requestFocus(View.FOCUS_DOWN);
5313 }
5314
5315 /**
5316 * @return Whether any ancestor of this view blocks descendant focus.
5317 */
5318 private boolean hasAncestorThatBlocksDescendantFocus() {
5319 ViewParent ancestor = mParent;
5320 while (ancestor instanceof ViewGroup) {
5321 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5322 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5323 return true;
5324 } else {
5325 ancestor = vgAncestor.getParent();
5326 }
5327 }
5328 return false;
5329 }
5330
5331 /**
Romain Guya440b002010-02-24 15:57:54 -08005332 * @hide
5333 */
5334 public void dispatchStartTemporaryDetach() {
5335 onStartTemporaryDetach();
5336 }
5337
5338 /**
5339 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005340 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5341 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005342 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005343 */
5344 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005345 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005346 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005347 }
5348
5349 /**
5350 * @hide
5351 */
5352 public void dispatchFinishTemporaryDetach() {
5353 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005354 }
Romain Guy8506ab42009-06-11 17:35:47 -07005355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005356 /**
5357 * Called after {@link #onStartTemporaryDetach} when the container is done
5358 * changing the view.
5359 */
5360 public void onFinishTemporaryDetach() {
5361 }
Romain Guy8506ab42009-06-11 17:35:47 -07005362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005364 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5365 * for this view's window. Returns null if the view is not currently attached
5366 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005367 * just use the standard high-level event callbacks like
5368 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005369 */
5370 public KeyEvent.DispatcherState getKeyDispatcherState() {
5371 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5372 }
Joe Malin32736f02011-01-19 16:14:20 -08005373
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005374 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 * Dispatch a key event before it is processed by any input method
5376 * associated with the view hierarchy. This can be used to intercept
5377 * key events in special situations before the IME consumes them; a
5378 * typical example would be handling the BACK key to update the application's
5379 * UI instead of allowing the IME to see it and close itself.
5380 *
5381 * @param event The key event to be dispatched.
5382 * @return True if the event was handled, false otherwise.
5383 */
5384 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5385 return onKeyPreIme(event.getKeyCode(), event);
5386 }
5387
5388 /**
5389 * Dispatch a key event to the next view on the focus path. This path runs
5390 * from the top of the view tree down to the currently focused view. If this
5391 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5392 * the next node down the focus path. This method also fires any key
5393 * listeners.
5394 *
5395 * @param event The key event to be dispatched.
5396 * @return True if the event was handled, false otherwise.
5397 */
5398 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005399 if (mInputEventConsistencyVerifier != null) {
5400 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005402
Jeff Brown21bc5c92011-02-28 18:27:14 -08005403 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005404 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005405 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5406 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
5407 return true;
5408 }
5409
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005410 if (event.dispatch(this, mAttachInfo != null
5411 ? mAttachInfo.mKeyDispatchState : null, this)) {
5412 return true;
5413 }
5414
5415 if (mInputEventConsistencyVerifier != null) {
5416 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5417 }
5418 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005419 }
5420
5421 /**
5422 * Dispatches a key shortcut event.
5423 *
5424 * @param event The key event to be dispatched.
5425 * @return True if the event was handled by the view, false otherwise.
5426 */
5427 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5428 return onKeyShortcut(event.getKeyCode(), event);
5429 }
5430
5431 /**
5432 * Pass the touch screen motion event down to the target view, or this
5433 * view if it is the target.
5434 *
5435 * @param event The motion event to be dispatched.
5436 * @return True if the event was handled by the view, false otherwise.
5437 */
5438 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005439 if (mInputEventConsistencyVerifier != null) {
5440 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5441 }
5442
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005443 if (onFilterTouchEventForSecurity(event)) {
5444 //noinspection SimplifiableIfStatement
5445 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
5446 mOnTouchListener.onTouch(this, event)) {
5447 return true;
5448 }
5449
5450 if (onTouchEvent(event)) {
5451 return true;
5452 }
Jeff Brown85a31762010-09-01 17:01:00 -07005453 }
5454
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005455 if (mInputEventConsistencyVerifier != null) {
5456 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005457 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005458 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005459 }
5460
5461 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005462 * Filter the touch event to apply security policies.
5463 *
5464 * @param event The motion event to be filtered.
5465 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005466 *
Jeff Brown85a31762010-09-01 17:01:00 -07005467 * @see #getFilterTouchesWhenObscured
5468 */
5469 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005470 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005471 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5472 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5473 // Window is obscured, drop this touch.
5474 return false;
5475 }
5476 return true;
5477 }
5478
5479 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005480 * Pass a trackball motion event down to the focused view.
5481 *
5482 * @param event The motion event to be dispatched.
5483 * @return True if the event was handled by the view, false otherwise.
5484 */
5485 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005486 if (mInputEventConsistencyVerifier != null) {
5487 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5488 }
5489
Romain Guy02ccac62011-06-24 13:20:23 -07005490 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005491 }
5492
5493 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005494 * Dispatch a generic motion event.
5495 * <p>
5496 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5497 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005498 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005499 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005500 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005501 *
5502 * @param event The motion event to be dispatched.
5503 * @return True if the event was handled by the view, false otherwise.
5504 */
5505 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005506 if (mInputEventConsistencyVerifier != null) {
5507 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5508 }
5509
Jeff Browna032cc02011-03-07 16:56:21 -08005510 final int source = event.getSource();
5511 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5512 final int action = event.getAction();
5513 if (action == MotionEvent.ACTION_HOVER_ENTER
5514 || action == MotionEvent.ACTION_HOVER_MOVE
5515 || action == MotionEvent.ACTION_HOVER_EXIT) {
5516 if (dispatchHoverEvent(event)) {
5517 return true;
5518 }
5519 } else if (dispatchGenericPointerEvent(event)) {
5520 return true;
5521 }
5522 } else if (dispatchGenericFocusedEvent(event)) {
5523 return true;
5524 }
5525
Jeff Brown10b62902011-06-20 16:40:37 -07005526 if (dispatchGenericMotionEventInternal(event)) {
5527 return true;
5528 }
5529
5530 if (mInputEventConsistencyVerifier != null) {
5531 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5532 }
5533 return false;
5534 }
5535
5536 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005537 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08005538 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5539 && mOnGenericMotionListener.onGenericMotion(this, event)) {
5540 return true;
5541 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005542
5543 if (onGenericMotionEvent(event)) {
5544 return true;
5545 }
5546
5547 if (mInputEventConsistencyVerifier != null) {
5548 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5549 }
5550 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005551 }
5552
5553 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005554 * Dispatch a hover event.
5555 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005556 * Do not call this method directly.
5557 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005558 * </p>
5559 *
5560 * @param event The motion event to be dispatched.
5561 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005562 */
5563 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005564 //noinspection SimplifiableIfStatement
Jeff Brown10b62902011-06-20 16:40:37 -07005565 if (mOnHoverListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5566 && mOnHoverListener.onHover(this, event)) {
5567 return true;
5568 }
5569
Jeff Browna032cc02011-03-07 16:56:21 -08005570 return onHoverEvent(event);
5571 }
5572
5573 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005574 * Returns true if the view has a child to which it has recently sent
5575 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5576 * it does not have a hovered child, then it must be the innermost hovered view.
5577 * @hide
5578 */
5579 protected boolean hasHoveredChild() {
5580 return false;
5581 }
5582
5583 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005584 * Dispatch a generic motion event to the view under the first pointer.
5585 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005586 * Do not call this method directly.
5587 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005588 * </p>
5589 *
5590 * @param event The motion event to be dispatched.
5591 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005592 */
5593 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5594 return false;
5595 }
5596
5597 /**
5598 * Dispatch a generic motion event to the currently focused view.
5599 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005600 * Do not call this method directly.
5601 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005602 * </p>
5603 *
5604 * @param event The motion event to be dispatched.
5605 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005606 */
5607 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5608 return false;
5609 }
5610
5611 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005612 * Dispatch a pointer event.
5613 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005614 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5615 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5616 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005617 * and should not be expected to handle other pointing device features.
5618 * </p>
5619 *
5620 * @param event The motion event to be dispatched.
5621 * @return True if the event was handled by the view, false otherwise.
5622 * @hide
5623 */
5624 public final boolean dispatchPointerEvent(MotionEvent event) {
5625 if (event.isTouchEvent()) {
5626 return dispatchTouchEvent(event);
5627 } else {
5628 return dispatchGenericMotionEvent(event);
5629 }
5630 }
5631
5632 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005633 * Called when the window containing this view gains or loses window focus.
5634 * ViewGroups should override to route to their children.
5635 *
5636 * @param hasFocus True if the window containing this view now has focus,
5637 * false otherwise.
5638 */
5639 public void dispatchWindowFocusChanged(boolean hasFocus) {
5640 onWindowFocusChanged(hasFocus);
5641 }
5642
5643 /**
5644 * Called when the window containing this view gains or loses focus. Note
5645 * that this is separate from view focus: to receive key events, both
5646 * your view and its window must have focus. If a window is displayed
5647 * on top of yours that takes input focus, then your own window will lose
5648 * focus but the view focus will remain unchanged.
5649 *
5650 * @param hasWindowFocus True if the window containing this view now has
5651 * focus, false otherwise.
5652 */
5653 public void onWindowFocusChanged(boolean hasWindowFocus) {
5654 InputMethodManager imm = InputMethodManager.peekInstance();
5655 if (!hasWindowFocus) {
5656 if (isPressed()) {
5657 setPressed(false);
5658 }
5659 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5660 imm.focusOut(this);
5661 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005662 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005663 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005664 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005665 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5666 imm.focusIn(this);
5667 }
5668 refreshDrawableState();
5669 }
5670
5671 /**
5672 * Returns true if this view is in a window that currently has window focus.
5673 * Note that this is not the same as the view itself having focus.
5674 *
5675 * @return True if this view is in a window that currently has window focus.
5676 */
5677 public boolean hasWindowFocus() {
5678 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5679 }
5680
5681 /**
Adam Powell326d8082009-12-09 15:10:07 -08005682 * Dispatch a view visibility change down the view hierarchy.
5683 * ViewGroups should override to route to their children.
5684 * @param changedView The view whose visibility changed. Could be 'this' or
5685 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005686 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5687 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005688 */
5689 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5690 onVisibilityChanged(changedView, visibility);
5691 }
5692
5693 /**
5694 * Called when the visibility of the view or an ancestor of the view is changed.
5695 * @param changedView The view whose visibility changed. Could be 'this' or
5696 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005697 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5698 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005699 */
5700 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005701 if (visibility == VISIBLE) {
5702 if (mAttachInfo != null) {
5703 initialAwakenScrollBars();
5704 } else {
5705 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5706 }
5707 }
Adam Powell326d8082009-12-09 15:10:07 -08005708 }
5709
5710 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005711 * Dispatch a hint about whether this view is displayed. For instance, when
5712 * a View moves out of the screen, it might receives a display hint indicating
5713 * the view is not displayed. Applications should not <em>rely</em> on this hint
5714 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005715 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005716 * @param hint A hint about whether or not this view is displayed:
5717 * {@link #VISIBLE} or {@link #INVISIBLE}.
5718 */
5719 public void dispatchDisplayHint(int hint) {
5720 onDisplayHint(hint);
5721 }
5722
5723 /**
5724 * Gives this view a hint about whether is displayed or not. For instance, when
5725 * a View moves out of the screen, it might receives a display hint indicating
5726 * the view is not displayed. Applications should not <em>rely</em> on this hint
5727 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005728 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005729 * @param hint A hint about whether or not this view is displayed:
5730 * {@link #VISIBLE} or {@link #INVISIBLE}.
5731 */
5732 protected void onDisplayHint(int hint) {
5733 }
5734
5735 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005736 * Dispatch a window visibility change down the view hierarchy.
5737 * ViewGroups should override to route to their children.
5738 *
5739 * @param visibility The new visibility of the window.
5740 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005741 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005742 */
5743 public void dispatchWindowVisibilityChanged(int visibility) {
5744 onWindowVisibilityChanged(visibility);
5745 }
5746
5747 /**
5748 * Called when the window containing has change its visibility
5749 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5750 * that this tells you whether or not your window is being made visible
5751 * to the window manager; this does <em>not</em> tell you whether or not
5752 * your window is obscured by other windows on the screen, even if it
5753 * is itself visible.
5754 *
5755 * @param visibility The new visibility of the window.
5756 */
5757 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005758 if (visibility == VISIBLE) {
5759 initialAwakenScrollBars();
5760 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005761 }
5762
5763 /**
5764 * Returns the current visibility of the window this view is attached to
5765 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5766 *
5767 * @return Returns the current visibility of the view's window.
5768 */
5769 public int getWindowVisibility() {
5770 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5771 }
5772
5773 /**
5774 * Retrieve the overall visible display size in which the window this view is
5775 * attached to has been positioned in. This takes into account screen
5776 * decorations above the window, for both cases where the window itself
5777 * is being position inside of them or the window is being placed under
5778 * then and covered insets are used for the window to position its content
5779 * inside. In effect, this tells you the available area where content can
5780 * be placed and remain visible to users.
5781 *
5782 * <p>This function requires an IPC back to the window manager to retrieve
5783 * the requested information, so should not be used in performance critical
5784 * code like drawing.
5785 *
5786 * @param outRect Filled in with the visible display frame. If the view
5787 * is not attached to a window, this is simply the raw display size.
5788 */
5789 public void getWindowVisibleDisplayFrame(Rect outRect) {
5790 if (mAttachInfo != null) {
5791 try {
5792 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5793 } catch (RemoteException e) {
5794 return;
5795 }
5796 // XXX This is really broken, and probably all needs to be done
5797 // in the window manager, and we need to know more about whether
5798 // we want the area behind or in front of the IME.
5799 final Rect insets = mAttachInfo.mVisibleInsets;
5800 outRect.left += insets.left;
5801 outRect.top += insets.top;
5802 outRect.right -= insets.right;
5803 outRect.bottom -= insets.bottom;
5804 return;
5805 }
5806 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005807 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005808 }
5809
5810 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005811 * Dispatch a notification about a resource configuration change down
5812 * the view hierarchy.
5813 * ViewGroups should override to route to their children.
5814 *
5815 * @param newConfig The new resource configuration.
5816 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005817 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005818 */
5819 public void dispatchConfigurationChanged(Configuration newConfig) {
5820 onConfigurationChanged(newConfig);
5821 }
5822
5823 /**
5824 * Called when the current configuration of the resources being used
5825 * by the application have changed. You can use this to decide when
5826 * to reload resources that can changed based on orientation and other
5827 * configuration characterstics. You only need to use this if you are
5828 * not relying on the normal {@link android.app.Activity} mechanism of
5829 * recreating the activity instance upon a configuration change.
5830 *
5831 * @param newConfig The new resource configuration.
5832 */
5833 protected void onConfigurationChanged(Configuration newConfig) {
5834 }
5835
5836 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005837 * Private function to aggregate all per-view attributes in to the view
5838 * root.
5839 */
5840 void dispatchCollectViewAttributes(int visibility) {
5841 performCollectViewAttributes(visibility);
5842 }
5843
5844 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005845 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005846 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5847 mAttachInfo.mKeepScreenOn = true;
5848 }
5849 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5850 if (mOnSystemUiVisibilityChangeListener != null) {
5851 mAttachInfo.mHasSystemUiListeners = true;
5852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005853 }
5854 }
5855
5856 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005857 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005858 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005859 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5860 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005861 ai.mRecomputeGlobalAttributes = true;
5862 }
5863 }
5864 }
5865
5866 /**
5867 * Returns whether the device is currently in touch mode. Touch mode is entered
5868 * once the user begins interacting with the device by touch, and affects various
5869 * things like whether focus is always visible to the user.
5870 *
5871 * @return Whether the device is in touch mode.
5872 */
5873 @ViewDebug.ExportedProperty
5874 public boolean isInTouchMode() {
5875 if (mAttachInfo != null) {
5876 return mAttachInfo.mInTouchMode;
5877 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005878 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005879 }
5880 }
5881
5882 /**
5883 * Returns the context the view is running in, through which it can
5884 * access the current theme, resources, etc.
5885 *
5886 * @return The view's Context.
5887 */
5888 @ViewDebug.CapturedViewProperty
5889 public final Context getContext() {
5890 return mContext;
5891 }
5892
5893 /**
5894 * Handle a key event before it is processed by any input method
5895 * associated with the view hierarchy. This can be used to intercept
5896 * key events in special situations before the IME consumes them; a
5897 * typical example would be handling the BACK key to update the application's
5898 * UI instead of allowing the IME to see it and close itself.
5899 *
5900 * @param keyCode The value in event.getKeyCode().
5901 * @param event Description of the key event.
5902 * @return If you handled the event, return true. If you want to allow the
5903 * event to be handled by the next receiver, return false.
5904 */
5905 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5906 return false;
5907 }
5908
5909 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005910 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5911 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005912 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5913 * is released, if the view is enabled and clickable.
5914 *
5915 * @param keyCode A key code that represents the button pressed, from
5916 * {@link android.view.KeyEvent}.
5917 * @param event The KeyEvent object that defines the button action.
5918 */
5919 public boolean onKeyDown(int keyCode, KeyEvent event) {
5920 boolean result = false;
5921
5922 switch (keyCode) {
5923 case KeyEvent.KEYCODE_DPAD_CENTER:
5924 case KeyEvent.KEYCODE_ENTER: {
5925 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5926 return true;
5927 }
5928 // Long clickable items don't necessarily have to be clickable
5929 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5930 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5931 (event.getRepeatCount() == 0)) {
5932 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005933 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005934 return true;
5935 }
5936 break;
5937 }
5938 }
5939 return result;
5940 }
5941
5942 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005943 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5944 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5945 * the event).
5946 */
5947 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5948 return false;
5949 }
5950
5951 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005952 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5953 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005954 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5955 * {@link KeyEvent#KEYCODE_ENTER} is released.
5956 *
5957 * @param keyCode A key code that represents the button pressed, from
5958 * {@link android.view.KeyEvent}.
5959 * @param event The KeyEvent object that defines the button action.
5960 */
5961 public boolean onKeyUp(int keyCode, KeyEvent event) {
5962 boolean result = false;
5963
5964 switch (keyCode) {
5965 case KeyEvent.KEYCODE_DPAD_CENTER:
5966 case KeyEvent.KEYCODE_ENTER: {
5967 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5968 return true;
5969 }
5970 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5971 setPressed(false);
5972
5973 if (!mHasPerformedLongPress) {
5974 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005975 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005976
5977 result = performClick();
5978 }
5979 }
5980 break;
5981 }
5982 }
5983 return result;
5984 }
5985
5986 /**
5987 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5988 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5989 * the event).
5990 *
5991 * @param keyCode A key code that represents the button pressed, from
5992 * {@link android.view.KeyEvent}.
5993 * @param repeatCount The number of times the action was made.
5994 * @param event The KeyEvent object that defines the button action.
5995 */
5996 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5997 return false;
5998 }
5999
6000 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006001 * Called on the focused view when a key shortcut event is not handled.
6002 * Override this method to implement local key shortcuts for the View.
6003 * Key shortcuts can also be implemented by setting the
6004 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006005 *
6006 * @param keyCode The value in event.getKeyCode().
6007 * @param event Description of the key event.
6008 * @return If you handled the event, return true. If you want to allow the
6009 * event to be handled by the next receiver, return false.
6010 */
6011 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6012 return false;
6013 }
6014
6015 /**
6016 * Check whether the called view is a text editor, in which case it
6017 * would make sense to automatically display a soft input window for
6018 * it. Subclasses should override this if they implement
6019 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006020 * a call on that method would return a non-null InputConnection, and
6021 * they are really a first-class editor that the user would normally
6022 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006023 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006024 * <p>The default implementation always returns false. This does
6025 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6026 * will not be called or the user can not otherwise perform edits on your
6027 * view; it is just a hint to the system that this is not the primary
6028 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006029 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006030 * @return Returns true if this view is a text editor, else false.
6031 */
6032 public boolean onCheckIsTextEditor() {
6033 return false;
6034 }
Romain Guy8506ab42009-06-11 17:35:47 -07006035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006036 /**
6037 * Create a new InputConnection for an InputMethod to interact
6038 * with the view. The default implementation returns null, since it doesn't
6039 * support input methods. You can override this to implement such support.
6040 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006041 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006042 * <p>When implementing this, you probably also want to implement
6043 * {@link #onCheckIsTextEditor()} to indicate you will return a
6044 * non-null InputConnection.
6045 *
6046 * @param outAttrs Fill in with attribute information about the connection.
6047 */
6048 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6049 return null;
6050 }
6051
6052 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006053 * Called by the {@link android.view.inputmethod.InputMethodManager}
6054 * when a view who is not the current
6055 * input connection target is trying to make a call on the manager. The
6056 * default implementation returns false; you can override this to return
6057 * true for certain views if you are performing InputConnection proxying
6058 * to them.
6059 * @param view The View that is making the InputMethodManager call.
6060 * @return Return true to allow the call, false to reject.
6061 */
6062 public boolean checkInputConnectionProxy(View view) {
6063 return false;
6064 }
Romain Guy8506ab42009-06-11 17:35:47 -07006065
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006066 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006067 * Show the context menu for this view. It is not safe to hold on to the
6068 * menu after returning from this method.
6069 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006070 * You should normally not overload this method. Overload
6071 * {@link #onCreateContextMenu(ContextMenu)} or define an
6072 * {@link OnCreateContextMenuListener} to add items to the context menu.
6073 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006074 * @param menu The context menu to populate
6075 */
6076 public void createContextMenu(ContextMenu menu) {
6077 ContextMenuInfo menuInfo = getContextMenuInfo();
6078
6079 // Sets the current menu info so all items added to menu will have
6080 // my extra info set.
6081 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6082
6083 onCreateContextMenu(menu);
6084 if (mOnCreateContextMenuListener != null) {
6085 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
6086 }
6087
6088 // Clear the extra information so subsequent items that aren't mine don't
6089 // have my extra info.
6090 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6091
6092 if (mParent != null) {
6093 mParent.createContextMenu(menu);
6094 }
6095 }
6096
6097 /**
6098 * Views should implement this if they have extra information to associate
6099 * with the context menu. The return result is supplied as a parameter to
6100 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6101 * callback.
6102 *
6103 * @return Extra information about the item for which the context menu
6104 * should be shown. This information will vary across different
6105 * subclasses of View.
6106 */
6107 protected ContextMenuInfo getContextMenuInfo() {
6108 return null;
6109 }
6110
6111 /**
6112 * Views should implement this if the view itself is going to add items to
6113 * the context menu.
6114 *
6115 * @param menu the context menu to populate
6116 */
6117 protected void onCreateContextMenu(ContextMenu menu) {
6118 }
6119
6120 /**
6121 * Implement this method to handle trackball motion events. The
6122 * <em>relative</em> movement of the trackball since the last event
6123 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6124 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6125 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6126 * they will often be fractional values, representing the more fine-grained
6127 * movement information available from a trackball).
6128 *
6129 * @param event The motion event.
6130 * @return True if the event was handled, false otherwise.
6131 */
6132 public boolean onTrackballEvent(MotionEvent event) {
6133 return false;
6134 }
6135
6136 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006137 * Implement this method to handle generic motion events.
6138 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006139 * Generic motion events describe joystick movements, mouse hovers, track pad
6140 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006141 * {@link MotionEvent#getSource() source} of the motion event specifies
6142 * the class of input that was received. Implementations of this method
6143 * must examine the bits in the source before processing the event.
6144 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006145 * </p><p>
6146 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6147 * are delivered to the view under the pointer. All other generic motion events are
6148 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006149 * </p>
6150 * <code>
6151 * public boolean onGenericMotionEvent(MotionEvent event) {
6152 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006153 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6154 * // process the joystick movement...
6155 * return true;
6156 * }
6157 * }
6158 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6159 * switch (event.getAction()) {
6160 * case MotionEvent.ACTION_HOVER_MOVE:
6161 * // process the mouse hover movement...
6162 * return true;
6163 * case MotionEvent.ACTION_SCROLL:
6164 * // process the scroll wheel movement...
6165 * return true;
6166 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006167 * }
6168 * return super.onGenericMotionEvent(event);
6169 * }
6170 * </code>
6171 *
6172 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006173 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006174 */
6175 public boolean onGenericMotionEvent(MotionEvent event) {
6176 return false;
6177 }
6178
6179 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006180 * Implement this method to handle hover events.
6181 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006182 * This method is called whenever a pointer is hovering into, over, or out of the
6183 * bounds of a view and the view is not currently being touched.
6184 * Hover events are represented as pointer events with action
6185 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6186 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6187 * </p>
6188 * <ul>
6189 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6190 * when the pointer enters the bounds of the view.</li>
6191 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6192 * when the pointer has already entered the bounds of the view and has moved.</li>
6193 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6194 * when the pointer has exited the bounds of the view or when the pointer is
6195 * about to go down due to a button click, tap, or similar user action that
6196 * causes the view to be touched.</li>
6197 * </ul>
6198 * <p>
6199 * The view should implement this method to return true to indicate that it is
6200 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006201 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006202 * The default implementation calls {@link #setHovered} to update the hovered state
6203 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006204 * is enabled and is clickable. The default implementation also sends hover
6205 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006206 * </p>
6207 *
6208 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006209 * @return True if the view handled the hover event.
6210 *
6211 * @see #isHovered
6212 * @see #setHovered
6213 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006214 */
6215 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006216 // The root view may receive hover (or touch) events that are outside the bounds of
6217 // the window. This code ensures that we only send accessibility events for
6218 // hovers that are actually within the bounds of the root view.
6219 final int action = event.getAction();
6220 if (!mSendingHoverAccessibilityEvents) {
6221 if ((action == MotionEvent.ACTION_HOVER_ENTER
6222 || action == MotionEvent.ACTION_HOVER_MOVE)
6223 && !hasHoveredChild()
6224 && pointInView(event.getX(), event.getY())) {
6225 mSendingHoverAccessibilityEvents = true;
6226 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6227 }
6228 } else {
6229 if (action == MotionEvent.ACTION_HOVER_EXIT
6230 || (action == MotionEvent.ACTION_HOVER_MOVE
6231 && !pointInView(event.getX(), event.getY()))) {
6232 mSendingHoverAccessibilityEvents = false;
6233 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6234 }
Jeff Browna1b24182011-07-28 13:38:24 -07006235 }
6236
Jeff Brown87b7f802011-06-21 18:35:45 -07006237 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006238 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006239 case MotionEvent.ACTION_HOVER_ENTER:
6240 setHovered(true);
6241 break;
6242 case MotionEvent.ACTION_HOVER_EXIT:
6243 setHovered(false);
6244 break;
6245 }
Jeff Browna1b24182011-07-28 13:38:24 -07006246
6247 // Dispatch the event to onGenericMotionEvent before returning true.
6248 // This is to provide compatibility with existing applications that
6249 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6250 // break because of the new default handling for hoverable views
6251 // in onHoverEvent.
6252 // Note that onGenericMotionEvent will be called by default when
6253 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6254 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006255 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006256 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006257 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006258 }
6259
6260 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006261 * Returns true if the view should handle {@link #onHoverEvent}
6262 * by calling {@link #setHovered} to change its hovered state.
6263 *
6264 * @return True if the view is hoverable.
6265 */
6266 private boolean isHoverable() {
6267 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006268 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006269 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6270 return false;
6271 }
6272
6273 return (viewFlags & CLICKABLE) == CLICKABLE
6274 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6275 }
6276
6277 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006278 * Returns true if the view is currently hovered.
6279 *
6280 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006281 *
6282 * @see #setHovered
6283 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006284 */
Jeff Brown10b62902011-06-20 16:40:37 -07006285 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006286 public boolean isHovered() {
6287 return (mPrivateFlags & HOVERED) != 0;
6288 }
6289
6290 /**
6291 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006292 * <p>
6293 * Calling this method also changes the drawable state of the view. This
6294 * enables the view to react to hover by using different drawable resources
6295 * to change its appearance.
6296 * </p><p>
6297 * The {@link #onHoverChanged} method is called when the hovered state changes.
6298 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006299 *
6300 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006301 *
6302 * @see #isHovered
6303 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006304 */
6305 public void setHovered(boolean hovered) {
6306 if (hovered) {
6307 if ((mPrivateFlags & HOVERED) == 0) {
6308 mPrivateFlags |= HOVERED;
6309 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006310 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006311 }
6312 } else {
6313 if ((mPrivateFlags & HOVERED) != 0) {
6314 mPrivateFlags &= ~HOVERED;
6315 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006316 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006317 }
6318 }
6319 }
6320
6321 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006322 * Implement this method to handle hover state changes.
6323 * <p>
6324 * This method is called whenever the hover state changes as a result of a
6325 * call to {@link #setHovered}.
6326 * </p>
6327 *
6328 * @param hovered The current hover state, as returned by {@link #isHovered}.
6329 *
6330 * @see #isHovered
6331 * @see #setHovered
6332 */
6333 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006334 }
6335
6336 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006337 * Implement this method to handle touch screen motion events.
6338 *
6339 * @param event The motion event.
6340 * @return True if the event was handled, false otherwise.
6341 */
6342 public boolean onTouchEvent(MotionEvent event) {
6343 final int viewFlags = mViewFlags;
6344
6345 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006346 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
6347 mPrivateFlags &= ~PRESSED;
6348 refreshDrawableState();
6349 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006350 // A disabled view that is clickable still consumes the touch
6351 // events, it just doesn't respond to them.
6352 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6353 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6354 }
6355
6356 if (mTouchDelegate != null) {
6357 if (mTouchDelegate.onTouchEvent(event)) {
6358 return true;
6359 }
6360 }
6361
6362 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6363 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6364 switch (event.getAction()) {
6365 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006366 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6367 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006368 // take focus if we don't have it already and we should in
6369 // touch mode.
6370 boolean focusTaken = false;
6371 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6372 focusTaken = requestFocus();
6373 }
6374
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006375 if (prepressed) {
6376 // The button is being released before we actually
6377 // showed it as pressed. Make it show the pressed
6378 // state now (before scheduling the click) to ensure
6379 // the user sees it.
6380 mPrivateFlags |= PRESSED;
6381 refreshDrawableState();
6382 }
Joe Malin32736f02011-01-19 16:14:20 -08006383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006384 if (!mHasPerformedLongPress) {
6385 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006386 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006387
6388 // Only perform take click actions if we were in the pressed state
6389 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006390 // Use a Runnable and post this rather than calling
6391 // performClick directly. This lets other visual state
6392 // of the view update before click actions start.
6393 if (mPerformClick == null) {
6394 mPerformClick = new PerformClick();
6395 }
6396 if (!post(mPerformClick)) {
6397 performClick();
6398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006399 }
6400 }
6401
6402 if (mUnsetPressedState == null) {
6403 mUnsetPressedState = new UnsetPressedState();
6404 }
6405
Adam Powelle14579b2009-12-16 18:39:52 -08006406 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006407 postDelayed(mUnsetPressedState,
6408 ViewConfiguration.getPressedStateDuration());
6409 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006410 // If the post failed, unpress right now
6411 mUnsetPressedState.run();
6412 }
Adam Powelle14579b2009-12-16 18:39:52 -08006413 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006414 }
6415 break;
6416
6417 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006418 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006419
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006420 if (performButtonActionOnTouchDown(event)) {
6421 break;
6422 }
6423
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006424 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006425 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006426
6427 // For views inside a scrolling container, delay the pressed feedback for
6428 // a short period in case this is a scroll.
6429 if (isInScrollingContainer) {
6430 mPrivateFlags |= PREPRESSED;
6431 if (mPendingCheckForTap == null) {
6432 mPendingCheckForTap = new CheckForTap();
6433 }
6434 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6435 } else {
6436 // Not inside a scrolling container, so show the feedback right away
6437 mPrivateFlags |= PRESSED;
6438 refreshDrawableState();
6439 checkForLongClick(0);
6440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006441 break;
6442
6443 case MotionEvent.ACTION_CANCEL:
6444 mPrivateFlags &= ~PRESSED;
6445 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08006446 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006447 break;
6448
6449 case MotionEvent.ACTION_MOVE:
6450 final int x = (int) event.getX();
6451 final int y = (int) event.getY();
6452
6453 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006454 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006455 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006456 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006457 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006458 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006459 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006460
6461 // Need to switch from pressed to not pressed
6462 mPrivateFlags &= ~PRESSED;
6463 refreshDrawableState();
6464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006465 }
6466 break;
6467 }
6468 return true;
6469 }
6470
6471 return false;
6472 }
6473
6474 /**
Adam Powell10298662011-08-14 18:26:30 -07006475 * @hide
6476 */
6477 public boolean isInScrollingContainer() {
6478 ViewParent p = getParent();
6479 while (p != null && p instanceof ViewGroup) {
6480 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6481 return true;
6482 }
6483 p = p.getParent();
6484 }
6485 return false;
6486 }
6487
6488 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006489 * Remove the longpress detection timer.
6490 */
6491 private void removeLongPressCallback() {
6492 if (mPendingCheckForLongPress != null) {
6493 removeCallbacks(mPendingCheckForLongPress);
6494 }
6495 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006496
6497 /**
6498 * Remove the pending click action
6499 */
6500 private void removePerformClickCallback() {
6501 if (mPerformClick != null) {
6502 removeCallbacks(mPerformClick);
6503 }
6504 }
6505
Adam Powelle14579b2009-12-16 18:39:52 -08006506 /**
Romain Guya440b002010-02-24 15:57:54 -08006507 * Remove the prepress detection timer.
6508 */
6509 private void removeUnsetPressCallback() {
6510 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6511 setPressed(false);
6512 removeCallbacks(mUnsetPressedState);
6513 }
6514 }
6515
6516 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006517 * Remove the tap detection timer.
6518 */
6519 private void removeTapCallback() {
6520 if (mPendingCheckForTap != null) {
6521 mPrivateFlags &= ~PREPRESSED;
6522 removeCallbacks(mPendingCheckForTap);
6523 }
6524 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006525
6526 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006527 * Cancels a pending long press. Your subclass can use this if you
6528 * want the context menu to come up if the user presses and holds
6529 * at the same place, but you don't want it to come up if they press
6530 * and then move around enough to cause scrolling.
6531 */
6532 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006533 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006534
6535 /*
6536 * The prepressed state handled by the tap callback is a display
6537 * construct, but the tap callback will post a long press callback
6538 * less its own timeout. Remove it here.
6539 */
6540 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006541 }
6542
6543 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006544 * Remove the pending callback for sending a
6545 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6546 */
6547 private void removeSendViewScrolledAccessibilityEventCallback() {
6548 if (mSendViewScrolledAccessibilityEvent != null) {
6549 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6550 }
6551 }
6552
6553 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006554 * Sets the TouchDelegate for this View.
6555 */
6556 public void setTouchDelegate(TouchDelegate delegate) {
6557 mTouchDelegate = delegate;
6558 }
6559
6560 /**
6561 * Gets the TouchDelegate for this View.
6562 */
6563 public TouchDelegate getTouchDelegate() {
6564 return mTouchDelegate;
6565 }
6566
6567 /**
6568 * Set flags controlling behavior of this view.
6569 *
6570 * @param flags Constant indicating the value which should be set
6571 * @param mask Constant indicating the bit range that should be changed
6572 */
6573 void setFlags(int flags, int mask) {
6574 int old = mViewFlags;
6575 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6576
6577 int changed = mViewFlags ^ old;
6578 if (changed == 0) {
6579 return;
6580 }
6581 int privateFlags = mPrivateFlags;
6582
6583 /* Check if the FOCUSABLE bit has changed */
6584 if (((changed & FOCUSABLE_MASK) != 0) &&
6585 ((privateFlags & HAS_BOUNDS) !=0)) {
6586 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6587 && ((privateFlags & FOCUSED) != 0)) {
6588 /* Give up focus if we are no longer focusable */
6589 clearFocus();
6590 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6591 && ((privateFlags & FOCUSED) == 0)) {
6592 /*
6593 * Tell the view system that we are now available to take focus
6594 * if no one else already has it.
6595 */
6596 if (mParent != null) mParent.focusableViewAvailable(this);
6597 }
6598 }
6599
6600 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6601 if ((changed & VISIBILITY_MASK) != 0) {
6602 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006603 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006604 * it was not visible. Marking it drawn ensures that the invalidation will
6605 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006606 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006607 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006608 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006609
6610 needGlobalAttributesUpdate(true);
6611
6612 // a view becoming visible is worth notifying the parent
6613 // about in case nothing has focus. even if this specific view
6614 // isn't focusable, it may contain something that is, so let
6615 // the root view try to give this focus if nothing else does.
6616 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6617 mParent.focusableViewAvailable(this);
6618 }
6619 }
6620 }
6621
6622 /* Check if the GONE bit has changed */
6623 if ((changed & GONE) != 0) {
6624 needGlobalAttributesUpdate(false);
6625 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006626
Romain Guyecd80ee2009-12-03 17:13:02 -08006627 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6628 if (hasFocus()) clearFocus();
6629 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006630 if (mParent instanceof View) {
6631 // GONE views noop invalidation, so invalidate the parent
6632 ((View) mParent).invalidate(true);
6633 }
6634 // Mark the view drawn to ensure that it gets invalidated properly the next
6635 // time it is visible and gets invalidated
6636 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006637 }
6638 if (mAttachInfo != null) {
6639 mAttachInfo.mViewVisibilityChanged = true;
6640 }
6641 }
6642
6643 /* Check if the VISIBLE bit has changed */
6644 if ((changed & INVISIBLE) != 0) {
6645 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006646 /*
6647 * If this view is becoming invisible, set the DRAWN flag so that
6648 * the next invalidate() will not be skipped.
6649 */
6650 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006651
6652 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6653 // root view becoming invisible shouldn't clear focus
6654 if (getRootView() != this) {
6655 clearFocus();
6656 }
6657 }
6658 if (mAttachInfo != null) {
6659 mAttachInfo.mViewVisibilityChanged = true;
6660 }
6661 }
6662
Adam Powell326d8082009-12-09 15:10:07 -08006663 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006664 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006665 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6666 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006667 } else if (mParent != null) {
6668 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006669 }
Adam Powell326d8082009-12-09 15:10:07 -08006670 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6671 }
6672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006673 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6674 destroyDrawingCache();
6675 }
6676
6677 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6678 destroyDrawingCache();
6679 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006680 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006681 }
6682
6683 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6684 destroyDrawingCache();
6685 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6686 }
6687
6688 if ((changed & DRAW_MASK) != 0) {
6689 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6690 if (mBGDrawable != null) {
6691 mPrivateFlags &= ~SKIP_DRAW;
6692 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6693 } else {
6694 mPrivateFlags |= SKIP_DRAW;
6695 }
6696 } else {
6697 mPrivateFlags &= ~SKIP_DRAW;
6698 }
6699 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006700 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006701 }
6702
6703 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006704 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006705 mParent.recomputeViewAttributes(this);
6706 }
6707 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006708
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006709 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006710 requestLayout();
6711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006712 }
6713
6714 /**
6715 * Change the view's z order in the tree, so it's on top of other sibling
6716 * views
6717 */
6718 public void bringToFront() {
6719 if (mParent != null) {
6720 mParent.bringChildToFront(this);
6721 }
6722 }
6723
6724 /**
6725 * This is called in response to an internal scroll in this view (i.e., the
6726 * view scrolled its own contents). This is typically as a result of
6727 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6728 * called.
6729 *
6730 * @param l Current horizontal scroll origin.
6731 * @param t Current vertical scroll origin.
6732 * @param oldl Previous horizontal scroll origin.
6733 * @param oldt Previous vertical scroll origin.
6734 */
6735 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006736 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6737 postSendViewScrolledAccessibilityEventCallback();
6738 }
6739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006740 mBackgroundSizeChanged = true;
6741
6742 final AttachInfo ai = mAttachInfo;
6743 if (ai != null) {
6744 ai.mViewScrollChanged = true;
6745 }
6746 }
6747
6748 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006749 * Interface definition for a callback to be invoked when the layout bounds of a view
6750 * changes due to layout processing.
6751 */
6752 public interface OnLayoutChangeListener {
6753 /**
6754 * Called when the focus state of a view has changed.
6755 *
6756 * @param v The view whose state has changed.
6757 * @param left The new value of the view's left property.
6758 * @param top The new value of the view's top property.
6759 * @param right The new value of the view's right property.
6760 * @param bottom The new value of the view's bottom property.
6761 * @param oldLeft The previous value of the view's left property.
6762 * @param oldTop The previous value of the view's top property.
6763 * @param oldRight The previous value of the view's right property.
6764 * @param oldBottom The previous value of the view's bottom property.
6765 */
6766 void onLayoutChange(View v, int left, int top, int right, int bottom,
6767 int oldLeft, int oldTop, int oldRight, int oldBottom);
6768 }
6769
6770 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006771 * This is called during layout when the size of this view has changed. If
6772 * you were just added to the view hierarchy, you're called with the old
6773 * values of 0.
6774 *
6775 * @param w Current width of this view.
6776 * @param h Current height of this view.
6777 * @param oldw Old width of this view.
6778 * @param oldh Old height of this view.
6779 */
6780 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6781 }
6782
6783 /**
6784 * Called by draw to draw the child views. This may be overridden
6785 * by derived classes to gain control just before its children are drawn
6786 * (but after its own view has been drawn).
6787 * @param canvas the canvas on which to draw the view
6788 */
6789 protected void dispatchDraw(Canvas canvas) {
6790 }
6791
6792 /**
6793 * Gets the parent of this view. Note that the parent is a
6794 * ViewParent and not necessarily a View.
6795 *
6796 * @return Parent of this view.
6797 */
6798 public final ViewParent getParent() {
6799 return mParent;
6800 }
6801
6802 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006803 * Set the horizontal scrolled position of your view. This will cause a call to
6804 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6805 * invalidated.
6806 * @param value the x position to scroll to
6807 */
6808 public void setScrollX(int value) {
6809 scrollTo(value, mScrollY);
6810 }
6811
6812 /**
6813 * Set the vertical scrolled position of your view. This will cause a call to
6814 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6815 * invalidated.
6816 * @param value the y position to scroll to
6817 */
6818 public void setScrollY(int value) {
6819 scrollTo(mScrollX, value);
6820 }
6821
6822 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006823 * Return the scrolled left position of this view. This is the left edge of
6824 * the displayed part of your view. You do not need to draw any pixels
6825 * farther left, since those are outside of the frame of your view on
6826 * screen.
6827 *
6828 * @return The left edge of the displayed part of your view, in pixels.
6829 */
6830 public final int getScrollX() {
6831 return mScrollX;
6832 }
6833
6834 /**
6835 * Return the scrolled top position of this view. This is the top edge of
6836 * the displayed part of your view. You do not need to draw any pixels above
6837 * it, since those are outside of the frame of your view on screen.
6838 *
6839 * @return The top edge of the displayed part of your view, in pixels.
6840 */
6841 public final int getScrollY() {
6842 return mScrollY;
6843 }
6844
6845 /**
6846 * Return the width of the your view.
6847 *
6848 * @return The width of your view, in pixels.
6849 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006850 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006851 public final int getWidth() {
6852 return mRight - mLeft;
6853 }
6854
6855 /**
6856 * Return the height of your view.
6857 *
6858 * @return The height of your view, in pixels.
6859 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006860 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006861 public final int getHeight() {
6862 return mBottom - mTop;
6863 }
6864
6865 /**
6866 * Return the visible drawing bounds of your view. Fills in the output
6867 * rectangle with the values from getScrollX(), getScrollY(),
6868 * getWidth(), and getHeight().
6869 *
6870 * @param outRect The (scrolled) drawing bounds of the view.
6871 */
6872 public void getDrawingRect(Rect outRect) {
6873 outRect.left = mScrollX;
6874 outRect.top = mScrollY;
6875 outRect.right = mScrollX + (mRight - mLeft);
6876 outRect.bottom = mScrollY + (mBottom - mTop);
6877 }
6878
6879 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006880 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6881 * raw width component (that is the result is masked by
6882 * {@link #MEASURED_SIZE_MASK}).
6883 *
6884 * @return The raw measured width of this view.
6885 */
6886 public final int getMeasuredWidth() {
6887 return mMeasuredWidth & MEASURED_SIZE_MASK;
6888 }
6889
6890 /**
6891 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006892 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006893 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006894 * This should be used during measurement and layout calculations only. Use
6895 * {@link #getWidth()} to see how wide a view is after layout.
6896 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006897 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006898 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006899 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006900 return mMeasuredWidth;
6901 }
6902
6903 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006904 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6905 * raw width component (that is the result is masked by
6906 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006907 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006908 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006909 */
6910 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006911 return mMeasuredHeight & MEASURED_SIZE_MASK;
6912 }
6913
6914 /**
6915 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006916 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006917 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6918 * This should be used during measurement and layout calculations only. Use
6919 * {@link #getHeight()} to see how wide a view is after layout.
6920 *
6921 * @return The measured width of this view as a bit mask.
6922 */
6923 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006924 return mMeasuredHeight;
6925 }
6926
6927 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006928 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6929 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6930 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6931 * and the height component is at the shifted bits
6932 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6933 */
6934 public final int getMeasuredState() {
6935 return (mMeasuredWidth&MEASURED_STATE_MASK)
6936 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6937 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6938 }
6939
6940 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006941 * The transform matrix of this view, which is calculated based on the current
6942 * roation, scale, and pivot properties.
6943 *
6944 * @see #getRotation()
6945 * @see #getScaleX()
6946 * @see #getScaleY()
6947 * @see #getPivotX()
6948 * @see #getPivotY()
6949 * @return The current transform matrix for the view
6950 */
6951 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07006952 if (mTransformationInfo != null) {
6953 updateMatrix();
6954 return mTransformationInfo.mMatrix;
6955 }
6956 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07006957 }
6958
6959 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006960 * Utility function to determine if the value is far enough away from zero to be
6961 * considered non-zero.
6962 * @param value A floating point value to check for zero-ness
6963 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6964 */
6965 private static boolean nonzero(float value) {
6966 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6967 }
6968
6969 /**
Jeff Brown86671742010-09-30 20:00:15 -07006970 * Returns true if the transform matrix is the identity matrix.
6971 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006972 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006973 * @return True if the transform matrix is the identity matrix, false otherwise.
6974 */
Jeff Brown86671742010-09-30 20:00:15 -07006975 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07006976 if (mTransformationInfo != null) {
6977 updateMatrix();
6978 return mTransformationInfo.mMatrixIsIdentity;
6979 }
6980 return true;
6981 }
6982
6983 void ensureTransformationInfo() {
6984 if (mTransformationInfo == null) {
6985 mTransformationInfo = new TransformationInfo();
6986 }
Jeff Brown86671742010-09-30 20:00:15 -07006987 }
6988
6989 /**
6990 * Recomputes the transform matrix if necessary.
6991 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006992 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07006993 final TransformationInfo info = mTransformationInfo;
6994 if (info == null) {
6995 return;
6996 }
6997 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006998 // transform-related properties have changed since the last time someone
6999 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007000
7001 // Figure out if we need to update the pivot point
7002 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007003 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7004 info.mPrevWidth = mRight - mLeft;
7005 info.mPrevHeight = mBottom - mTop;
7006 info.mPivotX = info.mPrevWidth / 2f;
7007 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007008 }
7009 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007010 info.mMatrix.reset();
7011 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7012 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7013 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7014 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007015 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007016 if (info.mCamera == null) {
7017 info.mCamera = new Camera();
7018 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007019 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007020 info.mCamera.save();
7021 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7022 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7023 info.mCamera.getMatrix(info.matrix3D);
7024 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7025 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7026 info.mPivotY + info.mTranslationY);
7027 info.mMatrix.postConcat(info.matrix3D);
7028 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007029 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007030 info.mMatrixDirty = false;
7031 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7032 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007033 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007034 }
7035
7036 /**
7037 * Utility method to retrieve the inverse of the current mMatrix property.
7038 * We cache the matrix to avoid recalculating it when transform properties
7039 * have not changed.
7040 *
7041 * @return The inverse of the current matrix of this view.
7042 */
Jeff Brown86671742010-09-30 20:00:15 -07007043 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007044 final TransformationInfo info = mTransformationInfo;
7045 if (info != null) {
7046 updateMatrix();
7047 if (info.mInverseMatrixDirty) {
7048 if (info.mInverseMatrix == null) {
7049 info.mInverseMatrix = new Matrix();
7050 }
7051 info.mMatrix.invert(info.mInverseMatrix);
7052 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007053 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007054 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007055 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007056 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007057 }
7058
7059 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007060 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7061 * views are drawn) from the camera to this view. The camera's distance
7062 * affects 3D transformations, for instance rotations around the X and Y
7063 * axis. If the rotationX or rotationY properties are changed and this view is
7064 * large (more than half the size of the screen), it is recommended to always
7065 * use a camera distance that's greater than the height (X axis rotation) or
7066 * the width (Y axis rotation) of this view.</p>
7067 *
7068 * <p>The distance of the camera from the view plane can have an affect on the
7069 * perspective distortion of the view when it is rotated around the x or y axis.
7070 * For example, a large distance will result in a large viewing angle, and there
7071 * will not be much perspective distortion of the view as it rotates. A short
7072 * distance may cause much more perspective distortion upon rotation, and can
7073 * also result in some drawing artifacts if the rotated view ends up partially
7074 * behind the camera (which is why the recommendation is to use a distance at
7075 * least as far as the size of the view, if the view is to be rotated.)</p>
7076 *
7077 * <p>The distance is expressed in "depth pixels." The default distance depends
7078 * on the screen density. For instance, on a medium density display, the
7079 * default distance is 1280. On a high density display, the default distance
7080 * is 1920.</p>
7081 *
7082 * <p>If you want to specify a distance that leads to visually consistent
7083 * results across various densities, use the following formula:</p>
7084 * <pre>
7085 * float scale = context.getResources().getDisplayMetrics().density;
7086 * view.setCameraDistance(distance * scale);
7087 * </pre>
7088 *
7089 * <p>The density scale factor of a high density display is 1.5,
7090 * and 1920 = 1280 * 1.5.</p>
7091 *
7092 * @param distance The distance in "depth pixels", if negative the opposite
7093 * value is used
7094 *
7095 * @see #setRotationX(float)
7096 * @see #setRotationY(float)
7097 */
7098 public void setCameraDistance(float distance) {
7099 invalidateParentCaches();
7100 invalidate(false);
7101
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007102 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007103 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007104 final TransformationInfo info = mTransformationInfo;
7105 if (info.mCamera == null) {
7106 info.mCamera = new Camera();
7107 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007108 }
7109
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007110 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7111 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007112
7113 invalidate(false);
7114 }
7115
7116 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007117 * The degrees that the view is rotated around the pivot point.
7118 *
Romain Guya5364ee2011-02-24 14:46:04 -08007119 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007120 * @see #getPivotX()
7121 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007122 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007123 * @return The degrees of rotation.
7124 */
7125 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007126 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007127 }
7128
7129 /**
Chet Haase897247b2010-09-09 14:54:47 -07007130 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7131 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007132 *
7133 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007134 *
7135 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007136 * @see #getPivotX()
7137 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007138 * @see #setRotationX(float)
7139 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007140 *
7141 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007142 */
7143 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007144 ensureTransformationInfo();
7145 final TransformationInfo info = mTransformationInfo;
7146 if (info.mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007147 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007148 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007149 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007150 info.mRotation = rotation;
7151 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007152 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007153 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007154 }
7155 }
7156
7157 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007158 * The degrees that the view is rotated around the vertical axis through the pivot point.
7159 *
7160 * @see #getPivotX()
7161 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007162 * @see #setRotationY(float)
7163 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007164 * @return The degrees of Y rotation.
7165 */
7166 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007167 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007168 }
7169
7170 /**
Chet Haase897247b2010-09-09 14:54:47 -07007171 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7172 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7173 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007174 *
7175 * When rotating large views, it is recommended to adjust the camera distance
7176 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007177 *
7178 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007179 *
7180 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007181 * @see #getPivotX()
7182 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007183 * @see #setRotation(float)
7184 * @see #setRotationX(float)
7185 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007186 *
7187 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007188 */
7189 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007190 ensureTransformationInfo();
7191 final TransformationInfo info = mTransformationInfo;
7192 if (info.mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007193 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007194 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007195 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007196 info.mRotationY = rotationY;
7197 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007198 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007199 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007200 }
7201 }
7202
7203 /**
7204 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7205 *
7206 * @see #getPivotX()
7207 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007208 * @see #setRotationX(float)
7209 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007210 * @return The degrees of X rotation.
7211 */
7212 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007213 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007214 }
7215
7216 /**
Chet Haase897247b2010-09-09 14:54:47 -07007217 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7218 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7219 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007220 *
7221 * When rotating large views, it is recommended to adjust the camera distance
7222 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007223 *
7224 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007225 *
7226 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007227 * @see #getPivotX()
7228 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007229 * @see #setRotation(float)
7230 * @see #setRotationY(float)
7231 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007232 *
7233 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007234 */
7235 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007236 ensureTransformationInfo();
7237 final TransformationInfo info = mTransformationInfo;
7238 if (info.mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007239 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007240 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007241 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007242 info.mRotationX = rotationX;
7243 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007244 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007245 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007246 }
7247 }
7248
7249 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007250 * The amount that the view is scaled in x around the pivot point, as a proportion of
7251 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7252 *
Joe Onorato93162322010-09-16 15:42:01 -04007253 * <p>By default, this is 1.0f.
7254 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007255 * @see #getPivotX()
7256 * @see #getPivotY()
7257 * @return The scaling factor.
7258 */
7259 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007260 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007261 }
7262
7263 /**
7264 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7265 * the view's unscaled width. A value of 1 means that no scaling is applied.
7266 *
7267 * @param scaleX The scaling factor.
7268 * @see #getPivotX()
7269 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007270 *
7271 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007272 */
7273 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007274 ensureTransformationInfo();
7275 final TransformationInfo info = mTransformationInfo;
7276 if (info.mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007277 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007278 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007279 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007280 info.mScaleX = scaleX;
7281 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007282 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007283 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007284 }
7285 }
7286
7287 /**
7288 * The amount that the view is scaled in y around the pivot point, as a proportion of
7289 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7290 *
Joe Onorato93162322010-09-16 15:42:01 -04007291 * <p>By default, this is 1.0f.
7292 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007293 * @see #getPivotX()
7294 * @see #getPivotY()
7295 * @return The scaling factor.
7296 */
7297 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007298 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007299 }
7300
7301 /**
7302 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7303 * the view's unscaled width. A value of 1 means that no scaling is applied.
7304 *
7305 * @param scaleY The scaling factor.
7306 * @see #getPivotX()
7307 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007308 *
7309 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007310 */
7311 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007312 ensureTransformationInfo();
7313 final TransformationInfo info = mTransformationInfo;
7314 if (info.mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007315 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007316 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007317 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007318 info.mScaleY = scaleY;
7319 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007320 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007321 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007322 }
7323 }
7324
7325 /**
7326 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7327 * and {@link #setScaleX(float) scaled}.
7328 *
7329 * @see #getRotation()
7330 * @see #getScaleX()
7331 * @see #getScaleY()
7332 * @see #getPivotY()
7333 * @return The x location of the pivot point.
7334 */
7335 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007336 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007337 }
7338
7339 /**
7340 * Sets the x location of the point around which the view is
7341 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007342 * By default, the pivot point is centered on the object.
7343 * Setting this property disables this behavior and causes the view to use only the
7344 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007345 *
7346 * @param pivotX The x location of the pivot point.
7347 * @see #getRotation()
7348 * @see #getScaleX()
7349 * @see #getScaleY()
7350 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007351 *
7352 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007353 */
7354 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007355 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007356 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007357 final TransformationInfo info = mTransformationInfo;
7358 if (info.mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007359 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007360 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007361 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007362 info.mPivotX = pivotX;
7363 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007364 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007365 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007366 }
7367 }
7368
7369 /**
7370 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7371 * and {@link #setScaleY(float) scaled}.
7372 *
7373 * @see #getRotation()
7374 * @see #getScaleX()
7375 * @see #getScaleY()
7376 * @see #getPivotY()
7377 * @return The y location of the pivot point.
7378 */
7379 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007380 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007381 }
7382
7383 /**
7384 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007385 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7386 * Setting this property disables this behavior and causes the view to use only the
7387 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007388 *
7389 * @param pivotY The y location of the pivot point.
7390 * @see #getRotation()
7391 * @see #getScaleX()
7392 * @see #getScaleY()
7393 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007394 *
7395 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007396 */
7397 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007398 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007399 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007400 final TransformationInfo info = mTransformationInfo;
7401 if (info.mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007402 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007403 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007404 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007405 info.mPivotY = pivotY;
7406 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007407 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007408 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007409 }
7410 }
7411
7412 /**
7413 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7414 * completely transparent and 1 means the view is completely opaque.
7415 *
Joe Onorato93162322010-09-16 15:42:01 -04007416 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007417 * @return The opacity of the view.
7418 */
7419 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007420 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007421 }
7422
7423 /**
Romain Guy171c5922011-01-06 10:04:23 -08007424 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7425 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007426 *
Romain Guy171c5922011-01-06 10:04:23 -08007427 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7428 * responsible for applying the opacity itself. Otherwise, calling this method is
7429 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007430 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007431 *
7432 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007433 *
Joe Malin32736f02011-01-19 16:14:20 -08007434 * @see #setLayerType(int, android.graphics.Paint)
7435 *
Chet Haase73066682010-11-29 15:55:32 -08007436 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007437 */
7438 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007439 ensureTransformationInfo();
7440 mTransformationInfo.mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007441 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07007442 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07007443 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007444 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007445 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07007446 } else {
Romain Guya3496a92010-10-12 11:53:24 -07007447 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007448 invalidate(false);
7449 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007450 }
7451
7452 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007453 * Faster version of setAlpha() which performs the same steps except there are
7454 * no calls to invalidate(). The caller of this function should perform proper invalidation
7455 * on the parent and this object. The return value indicates whether the subclass handles
7456 * alpha (the return value for onSetAlpha()).
7457 *
7458 * @param alpha The new value for the alpha property
7459 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
7460 */
7461 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007462 ensureTransformationInfo();
7463 mTransformationInfo.mAlpha = alpha;
Chet Haasea00f3862011-02-22 06:34:40 -08007464 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7465 if (subclassHandlesAlpha) {
7466 mPrivateFlags |= ALPHA_SET;
7467 } else {
7468 mPrivateFlags &= ~ALPHA_SET;
7469 }
7470 return subclassHandlesAlpha;
7471 }
7472
7473 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007474 * Top position of this view relative to its parent.
7475 *
7476 * @return The top of this view, in pixels.
7477 */
7478 @ViewDebug.CapturedViewProperty
7479 public final int getTop() {
7480 return mTop;
7481 }
7482
7483 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007484 * Sets the top position of this view relative to its parent. This method is meant to be called
7485 * by the layout system and should not generally be called otherwise, because the property
7486 * may be changed at any time by the layout.
7487 *
7488 * @param top The top of this view, in pixels.
7489 */
7490 public final void setTop(int top) {
7491 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007492 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007493 final boolean matrixIsIdentity = mTransformationInfo == null
7494 || mTransformationInfo.mMatrixIsIdentity;
7495 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007496 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007497 int minTop;
7498 int yLoc;
7499 if (top < mTop) {
7500 minTop = top;
7501 yLoc = top - mTop;
7502 } else {
7503 minTop = mTop;
7504 yLoc = 0;
7505 }
Chet Haasee9140a72011-02-16 16:23:29 -08007506 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007507 }
7508 } else {
7509 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007510 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007511 }
7512
Chet Haaseed032702010-10-01 14:05:54 -07007513 int width = mRight - mLeft;
7514 int oldHeight = mBottom - mTop;
7515
Chet Haase21cd1382010-09-01 17:42:29 -07007516 mTop = top;
7517
Chet Haaseed032702010-10-01 14:05:54 -07007518 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7519
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007520 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007521 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7522 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007523 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007524 }
Chet Haase21cd1382010-09-01 17:42:29 -07007525 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007526 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007527 }
Chet Haase55dbb652010-12-21 20:15:08 -08007528 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007529 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007530 }
7531 }
7532
7533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007534 * Bottom position of this view relative to its parent.
7535 *
7536 * @return The bottom of this view, in pixels.
7537 */
7538 @ViewDebug.CapturedViewProperty
7539 public final int getBottom() {
7540 return mBottom;
7541 }
7542
7543 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007544 * True if this view has changed since the last time being drawn.
7545 *
7546 * @return The dirty state of this view.
7547 */
7548 public boolean isDirty() {
7549 return (mPrivateFlags & DIRTY_MASK) != 0;
7550 }
7551
7552 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007553 * Sets the bottom position of this view relative to its parent. This method is meant to be
7554 * called by the layout system and should not generally be called otherwise, because the
7555 * property may be changed at any time by the layout.
7556 *
7557 * @param bottom The bottom of this view, in pixels.
7558 */
7559 public final void setBottom(int bottom) {
7560 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007561 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007562 final boolean matrixIsIdentity = mTransformationInfo == null
7563 || mTransformationInfo.mMatrixIsIdentity;
7564 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007565 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007566 int maxBottom;
7567 if (bottom < mBottom) {
7568 maxBottom = mBottom;
7569 } else {
7570 maxBottom = bottom;
7571 }
Chet Haasee9140a72011-02-16 16:23:29 -08007572 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007573 }
7574 } else {
7575 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007576 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007577 }
7578
Chet Haaseed032702010-10-01 14:05:54 -07007579 int width = mRight - mLeft;
7580 int oldHeight = mBottom - mTop;
7581
Chet Haase21cd1382010-09-01 17:42:29 -07007582 mBottom = bottom;
7583
Chet Haaseed032702010-10-01 14:05:54 -07007584 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7585
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007586 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007587 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7588 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007589 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007590 }
Chet Haase21cd1382010-09-01 17:42:29 -07007591 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007592 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007593 }
Chet Haase55dbb652010-12-21 20:15:08 -08007594 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007595 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007596 }
7597 }
7598
7599 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007600 * Left position of this view relative to its parent.
7601 *
7602 * @return The left edge of this view, in pixels.
7603 */
7604 @ViewDebug.CapturedViewProperty
7605 public final int getLeft() {
7606 return mLeft;
7607 }
7608
7609 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007610 * Sets the left position of this view relative to its parent. This method is meant to be called
7611 * by the layout system and should not generally be called otherwise, because the property
7612 * may be changed at any time by the layout.
7613 *
7614 * @param left The bottom of this view, in pixels.
7615 */
7616 public final void setLeft(int left) {
7617 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007618 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007619 final boolean matrixIsIdentity = mTransformationInfo == null
7620 || mTransformationInfo.mMatrixIsIdentity;
7621 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007622 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007623 int minLeft;
7624 int xLoc;
7625 if (left < mLeft) {
7626 minLeft = left;
7627 xLoc = left - mLeft;
7628 } else {
7629 minLeft = mLeft;
7630 xLoc = 0;
7631 }
Chet Haasee9140a72011-02-16 16:23:29 -08007632 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007633 }
7634 } else {
7635 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007636 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007637 }
7638
Chet Haaseed032702010-10-01 14:05:54 -07007639 int oldWidth = mRight - mLeft;
7640 int height = mBottom - mTop;
7641
Chet Haase21cd1382010-09-01 17:42:29 -07007642 mLeft = left;
7643
Chet Haaseed032702010-10-01 14:05:54 -07007644 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7645
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007646 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007647 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7648 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007649 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007650 }
Chet Haase21cd1382010-09-01 17:42:29 -07007651 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007652 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007653 }
Chet Haase55dbb652010-12-21 20:15:08 -08007654 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007655 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007656 }
7657 }
7658
7659 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007660 * Right position of this view relative to its parent.
7661 *
7662 * @return The right edge of this view, in pixels.
7663 */
7664 @ViewDebug.CapturedViewProperty
7665 public final int getRight() {
7666 return mRight;
7667 }
7668
7669 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007670 * Sets the right position of this view relative to its parent. This method is meant to be called
7671 * by the layout system and should not generally be called otherwise, because the property
7672 * may be changed at any time by the layout.
7673 *
7674 * @param right The bottom of this view, in pixels.
7675 */
7676 public final void setRight(int right) {
7677 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007678 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007679 final boolean matrixIsIdentity = mTransformationInfo == null
7680 || mTransformationInfo.mMatrixIsIdentity;
7681 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007682 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007683 int maxRight;
7684 if (right < mRight) {
7685 maxRight = mRight;
7686 } else {
7687 maxRight = right;
7688 }
Chet Haasee9140a72011-02-16 16:23:29 -08007689 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007690 }
7691 } else {
7692 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007693 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007694 }
7695
Chet Haaseed032702010-10-01 14:05:54 -07007696 int oldWidth = mRight - mLeft;
7697 int height = mBottom - mTop;
7698
Chet Haase21cd1382010-09-01 17:42:29 -07007699 mRight = right;
7700
Chet Haaseed032702010-10-01 14:05:54 -07007701 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7702
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007703 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007704 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7705 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007706 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007707 }
Chet Haase21cd1382010-09-01 17:42:29 -07007708 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007709 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007710 }
Chet Haase55dbb652010-12-21 20:15:08 -08007711 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007712 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007713 }
7714 }
7715
7716 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007717 * The visual x position of this view, in pixels. This is equivalent to the
7718 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007719 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007720 *
Chet Haasedf030d22010-07-30 17:22:38 -07007721 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007722 */
Chet Haasedf030d22010-07-30 17:22:38 -07007723 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007724 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007725 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007726
Chet Haasedf030d22010-07-30 17:22:38 -07007727 /**
7728 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7729 * {@link #setTranslationX(float) translationX} property to be the difference between
7730 * the x value passed in and the current {@link #getLeft() left} property.
7731 *
7732 * @param x The visual x position of this view, in pixels.
7733 */
7734 public void setX(float x) {
7735 setTranslationX(x - mLeft);
7736 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007737
Chet Haasedf030d22010-07-30 17:22:38 -07007738 /**
7739 * The visual y position of this view, in pixels. This is equivalent to the
7740 * {@link #setTranslationY(float) translationY} property plus the current
7741 * {@link #getTop() top} property.
7742 *
7743 * @return The visual y position of this view, in pixels.
7744 */
7745 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007746 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007747 }
7748
7749 /**
7750 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7751 * {@link #setTranslationY(float) translationY} property to be the difference between
7752 * the y value passed in and the current {@link #getTop() top} property.
7753 *
7754 * @param y The visual y position of this view, in pixels.
7755 */
7756 public void setY(float y) {
7757 setTranslationY(y - mTop);
7758 }
7759
7760
7761 /**
7762 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7763 * This position is post-layout, in addition to wherever the object's
7764 * layout placed it.
7765 *
7766 * @return The horizontal position of this view relative to its left position, in pixels.
7767 */
7768 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007769 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07007770 }
7771
7772 /**
7773 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7774 * This effectively positions the object post-layout, in addition to wherever the object's
7775 * layout placed it.
7776 *
7777 * @param translationX The horizontal position of this view relative to its left position,
7778 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007779 *
7780 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007781 */
7782 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007783 ensureTransformationInfo();
7784 final TransformationInfo info = mTransformationInfo;
7785 if (info.mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007786 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007787 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007788 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007789 info.mTranslationX = translationX;
7790 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07007791 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007792 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007793 }
7794 }
7795
7796 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007797 * The horizontal location of this view relative to its {@link #getTop() top} position.
7798 * This position is post-layout, in addition to wherever the object's
7799 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007800 *
Chet Haasedf030d22010-07-30 17:22:38 -07007801 * @return The vertical position of this view relative to its top position,
7802 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007803 */
Chet Haasedf030d22010-07-30 17:22:38 -07007804 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007805 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007806 }
7807
7808 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007809 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7810 * This effectively positions the object post-layout, in addition to wherever the object's
7811 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007812 *
Chet Haasedf030d22010-07-30 17:22:38 -07007813 * @param translationY The vertical position of this view relative to its top position,
7814 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007815 *
7816 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007817 */
Chet Haasedf030d22010-07-30 17:22:38 -07007818 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007819 ensureTransformationInfo();
7820 final TransformationInfo info = mTransformationInfo;
7821 if (info.mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007822 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007823 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007824 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007825 info.mTranslationY = translationY;
7826 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07007827 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007828 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007829 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007830 }
7831
7832 /**
Romain Guyda489792011-02-03 01:05:15 -08007833 * @hide
7834 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007835 public void setFastTranslationX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007836 ensureTransformationInfo();
7837 final TransformationInfo info = mTransformationInfo;
7838 info.mTranslationX = x;
7839 info.mMatrixDirty = true;
Michael Jurkadece29f2011-02-03 01:41:49 -08007840 }
7841
7842 /**
7843 * @hide
7844 */
7845 public void setFastTranslationY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007846 ensureTransformationInfo();
7847 final TransformationInfo info = mTransformationInfo;
7848 info.mTranslationY = y;
7849 info.mMatrixDirty = true;
Michael Jurkadece29f2011-02-03 01:41:49 -08007850 }
7851
7852 /**
7853 * @hide
7854 */
Romain Guyda489792011-02-03 01:05:15 -08007855 public void setFastX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007856 ensureTransformationInfo();
7857 final TransformationInfo info = mTransformationInfo;
7858 info.mTranslationX = x - mLeft;
7859 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007860 }
7861
7862 /**
7863 * @hide
7864 */
7865 public void setFastY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007866 ensureTransformationInfo();
7867 final TransformationInfo info = mTransformationInfo;
7868 info.mTranslationY = y - mTop;
7869 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007870 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007871
Romain Guyda489792011-02-03 01:05:15 -08007872 /**
7873 * @hide
7874 */
7875 public void setFastScaleX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007876 ensureTransformationInfo();
7877 final TransformationInfo info = mTransformationInfo;
7878 info.mScaleX = x;
7879 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007880 }
7881
7882 /**
7883 * @hide
7884 */
7885 public void setFastScaleY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007886 ensureTransformationInfo();
7887 final TransformationInfo info = mTransformationInfo;
7888 info.mScaleY = y;
7889 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007890 }
7891
7892 /**
7893 * @hide
7894 */
7895 public void setFastAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007896 ensureTransformationInfo();
7897 mTransformationInfo.mAlpha = alpha;
Romain Guyda489792011-02-03 01:05:15 -08007898 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007899
Romain Guyda489792011-02-03 01:05:15 -08007900 /**
7901 * @hide
7902 */
7903 public void setFastRotationY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007904 ensureTransformationInfo();
7905 final TransformationInfo info = mTransformationInfo;
7906 info.mRotationY = y;
7907 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007908 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007909
Romain Guyda489792011-02-03 01:05:15 -08007910 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007911 * Hit rectangle in parent's coordinates
7912 *
7913 * @param outRect The hit rectangle of the view.
7914 */
7915 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07007916 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007917 final TransformationInfo info = mTransformationInfo;
7918 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007919 outRect.set(mLeft, mTop, mRight, mBottom);
7920 } else {
7921 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007922 tmpRect.set(-info.mPivotX, -info.mPivotY,
7923 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
7924 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07007925 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
7926 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07007927 }
7928 }
7929
7930 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007931 * Determines whether the given point, in local coordinates is inside the view.
7932 */
7933 /*package*/ final boolean pointInView(float localX, float localY) {
7934 return localX >= 0 && localX < (mRight - mLeft)
7935 && localY >= 0 && localY < (mBottom - mTop);
7936 }
7937
7938 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007939 * Utility method to determine whether the given point, in local coordinates,
7940 * is inside the view, where the area of the view is expanded by the slop factor.
7941 * This method is called while processing touch-move events to determine if the event
7942 * is still within the view.
7943 */
7944 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007945 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007946 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007947 }
7948
7949 /**
7950 * When a view has focus and the user navigates away from it, the next view is searched for
7951 * starting from the rectangle filled in by this method.
7952 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07007953 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
7954 * of the view. However, if your view maintains some idea of internal selection,
7955 * such as a cursor, or a selected row or column, you should override this method and
7956 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007957 *
7958 * @param r The rectangle to fill in, in this view's coordinates.
7959 */
7960 public void getFocusedRect(Rect r) {
7961 getDrawingRect(r);
7962 }
7963
7964 /**
7965 * If some part of this view is not clipped by any of its parents, then
7966 * return that area in r in global (root) coordinates. To convert r to local
7967 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
7968 * -globalOffset.y)) If the view is completely clipped or translated out,
7969 * return false.
7970 *
7971 * @param r If true is returned, r holds the global coordinates of the
7972 * visible portion of this view.
7973 * @param globalOffset If true is returned, globalOffset holds the dx,dy
7974 * between this view and its root. globalOffet may be null.
7975 * @return true if r is non-empty (i.e. part of the view is visible at the
7976 * root level.
7977 */
7978 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7979 int width = mRight - mLeft;
7980 int height = mBottom - mTop;
7981 if (width > 0 && height > 0) {
7982 r.set(0, 0, width, height);
7983 if (globalOffset != null) {
7984 globalOffset.set(-mScrollX, -mScrollY);
7985 }
7986 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7987 }
7988 return false;
7989 }
7990
7991 public final boolean getGlobalVisibleRect(Rect r) {
7992 return getGlobalVisibleRect(r, null);
7993 }
7994
7995 public final boolean getLocalVisibleRect(Rect r) {
7996 Point offset = new Point();
7997 if (getGlobalVisibleRect(r, offset)) {
7998 r.offset(-offset.x, -offset.y); // make r local
7999 return true;
8000 }
8001 return false;
8002 }
8003
8004 /**
8005 * Offset this view's vertical location by the specified number of pixels.
8006 *
8007 * @param offset the number of pixels to offset the view by
8008 */
8009 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008010 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008011 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008012 final boolean matrixIsIdentity = mTransformationInfo == null
8013 || mTransformationInfo.mMatrixIsIdentity;
8014 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008015 final ViewParent p = mParent;
8016 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008017 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008018 int minTop;
8019 int maxBottom;
8020 int yLoc;
8021 if (offset < 0) {
8022 minTop = mTop + offset;
8023 maxBottom = mBottom;
8024 yLoc = offset;
8025 } else {
8026 minTop = mTop;
8027 maxBottom = mBottom + offset;
8028 yLoc = 0;
8029 }
8030 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8031 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008032 }
8033 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008034 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008035 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008036
Chet Haasec3aa3612010-06-17 08:50:37 -07008037 mTop += offset;
8038 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008039
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008040 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008041 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008042 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008043 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008044 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008045 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008046 }
8047
8048 /**
8049 * Offset this view's horizontal location by the specified amount of pixels.
8050 *
8051 * @param offset the numer of pixels to offset the view by
8052 */
8053 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008054 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008055 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008056 final boolean matrixIsIdentity = mTransformationInfo == null
8057 || mTransformationInfo.mMatrixIsIdentity;
8058 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008059 final ViewParent p = mParent;
8060 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008061 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008062 int minLeft;
8063 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008064 if (offset < 0) {
8065 minLeft = mLeft + offset;
8066 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008067 } else {
8068 minLeft = mLeft;
8069 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008070 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008071 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008072 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008073 }
8074 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008075 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008076 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008077
Chet Haasec3aa3612010-06-17 08:50:37 -07008078 mLeft += offset;
8079 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008080
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008081 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008082 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008083 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008084 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008085 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008086 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008087 }
8088
8089 /**
8090 * Get the LayoutParams associated with this view. All views should have
8091 * layout parameters. These supply parameters to the <i>parent</i> of this
8092 * view specifying how it should be arranged. There are many subclasses of
8093 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8094 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008095 *
8096 * This method may return null if this View is not attached to a parent
8097 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8098 * was not invoked successfully. When a View is attached to a parent
8099 * ViewGroup, this method must not return null.
8100 *
8101 * @return The LayoutParams associated with this view, or null if no
8102 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008103 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008104 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008105 public ViewGroup.LayoutParams getLayoutParams() {
8106 return mLayoutParams;
8107 }
8108
8109 /**
8110 * Set the layout parameters associated with this view. These supply
8111 * parameters to the <i>parent</i> of this view specifying how it should be
8112 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8113 * correspond to the different subclasses of ViewGroup that are responsible
8114 * for arranging their children.
8115 *
Romain Guy01c174b2011-02-22 11:51:06 -08008116 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008117 */
8118 public void setLayoutParams(ViewGroup.LayoutParams params) {
8119 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008120 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008121 }
8122 mLayoutParams = params;
8123 requestLayout();
8124 }
8125
8126 /**
8127 * Set the scrolled position of your view. This will cause a call to
8128 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8129 * invalidated.
8130 * @param x the x position to scroll to
8131 * @param y the y position to scroll to
8132 */
8133 public void scrollTo(int x, int y) {
8134 if (mScrollX != x || mScrollY != y) {
8135 int oldX = mScrollX;
8136 int oldY = mScrollY;
8137 mScrollX = x;
8138 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008139 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008140 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008141 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008142 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008144 }
8145 }
8146
8147 /**
8148 * Move the scrolled position of your view. This will cause a call to
8149 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8150 * invalidated.
8151 * @param x the amount of pixels to scroll by horizontally
8152 * @param y the amount of pixels to scroll by vertically
8153 */
8154 public void scrollBy(int x, int y) {
8155 scrollTo(mScrollX + x, mScrollY + y);
8156 }
8157
8158 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008159 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8160 * animation to fade the scrollbars out after a default delay. If a subclass
8161 * provides animated scrolling, the start delay should equal the duration
8162 * of the scrolling animation.</p>
8163 *
8164 * <p>The animation starts only if at least one of the scrollbars is
8165 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8166 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8167 * this method returns true, and false otherwise. If the animation is
8168 * started, this method calls {@link #invalidate()}; in that case the
8169 * caller should not call {@link #invalidate()}.</p>
8170 *
8171 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008172 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008173 *
8174 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8175 * and {@link #scrollTo(int, int)}.</p>
8176 *
8177 * @return true if the animation is played, false otherwise
8178 *
8179 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008180 * @see #scrollBy(int, int)
8181 * @see #scrollTo(int, int)
8182 * @see #isHorizontalScrollBarEnabled()
8183 * @see #isVerticalScrollBarEnabled()
8184 * @see #setHorizontalScrollBarEnabled(boolean)
8185 * @see #setVerticalScrollBarEnabled(boolean)
8186 */
8187 protected boolean awakenScrollBars() {
8188 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008189 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008190 }
8191
8192 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008193 * Trigger the scrollbars to draw.
8194 * This method differs from awakenScrollBars() only in its default duration.
8195 * initialAwakenScrollBars() will show the scroll bars for longer than
8196 * usual to give the user more of a chance to notice them.
8197 *
8198 * @return true if the animation is played, false otherwise.
8199 */
8200 private boolean initialAwakenScrollBars() {
8201 return mScrollCache != null &&
8202 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8203 }
8204
8205 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008206 * <p>
8207 * Trigger the scrollbars to draw. When invoked this method starts an
8208 * animation to fade the scrollbars out after a fixed delay. If a subclass
8209 * provides animated scrolling, the start delay should equal the duration of
8210 * the scrolling animation.
8211 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008212 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008213 * <p>
8214 * The animation starts only if at least one of the scrollbars is enabled,
8215 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8216 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8217 * this method returns true, and false otherwise. If the animation is
8218 * started, this method calls {@link #invalidate()}; in that case the caller
8219 * should not call {@link #invalidate()}.
8220 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008221 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008222 * <p>
8223 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008224 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008225 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008226 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008227 * @param startDelay the delay, in milliseconds, after which the animation
8228 * should start; when the delay is 0, the animation starts
8229 * immediately
8230 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008231 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008232 * @see #scrollBy(int, int)
8233 * @see #scrollTo(int, int)
8234 * @see #isHorizontalScrollBarEnabled()
8235 * @see #isVerticalScrollBarEnabled()
8236 * @see #setHorizontalScrollBarEnabled(boolean)
8237 * @see #setVerticalScrollBarEnabled(boolean)
8238 */
8239 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008240 return awakenScrollBars(startDelay, true);
8241 }
Joe Malin32736f02011-01-19 16:14:20 -08008242
Mike Cleron290947b2009-09-29 18:34:32 -07008243 /**
8244 * <p>
8245 * Trigger the scrollbars to draw. When invoked this method starts an
8246 * animation to fade the scrollbars out after a fixed delay. If a subclass
8247 * provides animated scrolling, the start delay should equal the duration of
8248 * the scrolling animation.
8249 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008250 *
Mike Cleron290947b2009-09-29 18:34:32 -07008251 * <p>
8252 * The animation starts only if at least one of the scrollbars is enabled,
8253 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8254 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8255 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008256 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008257 * is set to true; in that case the caller
8258 * should not call {@link #invalidate()}.
8259 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008260 *
Mike Cleron290947b2009-09-29 18:34:32 -07008261 * <p>
8262 * This method should be invoked everytime a subclass directly updates the
8263 * scroll parameters.
8264 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008265 *
Mike Cleron290947b2009-09-29 18:34:32 -07008266 * @param startDelay the delay, in milliseconds, after which the animation
8267 * should start; when the delay is 0, the animation starts
8268 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008269 *
Mike Cleron290947b2009-09-29 18:34:32 -07008270 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008271 *
Mike Cleron290947b2009-09-29 18:34:32 -07008272 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008273 *
Mike Cleron290947b2009-09-29 18:34:32 -07008274 * @see #scrollBy(int, int)
8275 * @see #scrollTo(int, int)
8276 * @see #isHorizontalScrollBarEnabled()
8277 * @see #isVerticalScrollBarEnabled()
8278 * @see #setHorizontalScrollBarEnabled(boolean)
8279 * @see #setVerticalScrollBarEnabled(boolean)
8280 */
8281 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008282 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008283
Mike Cleronf116bf82009-09-27 19:14:12 -07008284 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8285 return false;
8286 }
8287
8288 if (scrollCache.scrollBar == null) {
8289 scrollCache.scrollBar = new ScrollBarDrawable();
8290 }
8291
8292 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8293
Mike Cleron290947b2009-09-29 18:34:32 -07008294 if (invalidate) {
8295 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008296 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008297 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008298
8299 if (scrollCache.state == ScrollabilityCache.OFF) {
8300 // FIXME: this is copied from WindowManagerService.
8301 // We should get this value from the system when it
8302 // is possible to do so.
8303 final int KEY_REPEAT_FIRST_DELAY = 750;
8304 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8305 }
8306
8307 // Tell mScrollCache when we should start fading. This may
8308 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008309 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008310 scrollCache.fadeStartTime = fadeStartTime;
8311 scrollCache.state = ScrollabilityCache.ON;
8312
8313 // Schedule our fader to run, unscheduling any old ones first
8314 if (mAttachInfo != null) {
8315 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8316 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8317 }
8318
8319 return true;
8320 }
8321
8322 return false;
8323 }
8324
8325 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008326 * Do not invalidate views which are not visible and which are not running an animation. They
8327 * will not get drawn and they should not set dirty flags as if they will be drawn
8328 */
8329 private boolean skipInvalidate() {
8330 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8331 (!(mParent instanceof ViewGroup) ||
8332 !((ViewGroup) mParent).isViewTransitioning(this));
8333 }
8334 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008335 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008336 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8337 * in the future. This must be called from a UI thread. To call from a non-UI
8338 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008339 *
8340 * WARNING: This method is destructive to dirty.
8341 * @param dirty the rectangle representing the bounds of the dirty region
8342 */
8343 public void invalidate(Rect dirty) {
8344 if (ViewDebug.TRACE_HIERARCHY) {
8345 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8346 }
8347
Chet Haaseaceafe62011-08-26 15:44:33 -07008348 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008349 return;
8350 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008351 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008352 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8353 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008354 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008355 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008356 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008357 final ViewParent p = mParent;
8358 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008359 //noinspection PointlessBooleanExpression,ConstantConditions
8360 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8361 if (p != null && ai != null && ai.mHardwareAccelerated) {
8362 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008363 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008364 p.invalidateChild(this, null);
8365 return;
8366 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008368 if (p != null && ai != null) {
8369 final int scrollX = mScrollX;
8370 final int scrollY = mScrollY;
8371 final Rect r = ai.mTmpInvalRect;
8372 r.set(dirty.left - scrollX, dirty.top - scrollY,
8373 dirty.right - scrollX, dirty.bottom - scrollY);
8374 mParent.invalidateChild(this, r);
8375 }
8376 }
8377 }
8378
8379 /**
8380 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
8381 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008382 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8383 * will be called at some point in the future. This must be called from
8384 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008385 * @param l the left position of the dirty region
8386 * @param t the top position of the dirty region
8387 * @param r the right position of the dirty region
8388 * @param b the bottom position of the dirty region
8389 */
8390 public void invalidate(int l, int t, int r, int b) {
8391 if (ViewDebug.TRACE_HIERARCHY) {
8392 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8393 }
8394
Chet Haaseaceafe62011-08-26 15:44:33 -07008395 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008396 return;
8397 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008398 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008399 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8400 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008401 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008402 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008403 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008404 final ViewParent p = mParent;
8405 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008406 //noinspection PointlessBooleanExpression,ConstantConditions
8407 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8408 if (p != null && ai != null && ai.mHardwareAccelerated) {
8409 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008410 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008411 p.invalidateChild(this, null);
8412 return;
8413 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008415 if (p != null && ai != null && l < r && t < b) {
8416 final int scrollX = mScrollX;
8417 final int scrollY = mScrollY;
8418 final Rect tmpr = ai.mTmpInvalRect;
8419 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8420 p.invalidateChild(this, tmpr);
8421 }
8422 }
8423 }
8424
8425 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008426 * Invalidate the whole view. If the view is visible,
8427 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8428 * the future. This must be called from a UI thread. To call from a non-UI thread,
8429 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008430 */
8431 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008432 invalidate(true);
8433 }
Joe Malin32736f02011-01-19 16:14:20 -08008434
Chet Haaseed032702010-10-01 14:05:54 -07008435 /**
8436 * This is where the invalidate() work actually happens. A full invalidate()
8437 * causes the drawing cache to be invalidated, but this function can be called with
8438 * invalidateCache set to false to skip that invalidation step for cases that do not
8439 * need it (for example, a component that remains at the same dimensions with the same
8440 * content).
8441 *
8442 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8443 * well. This is usually true for a full invalidate, but may be set to false if the
8444 * View's contents or dimensions have not changed.
8445 */
Romain Guy849d0a32011-02-01 17:20:48 -08008446 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008447 if (ViewDebug.TRACE_HIERARCHY) {
8448 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8449 }
8450
Chet Haaseaceafe62011-08-26 15:44:33 -07008451 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008452 return;
8453 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008454 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008455 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008456 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8457 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008458 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008459 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008460 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008461 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008462 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008464 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008465 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008466 //noinspection PointlessBooleanExpression,ConstantConditions
8467 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8468 if (p != null && ai != null && ai.mHardwareAccelerated) {
8469 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008470 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008471 p.invalidateChild(this, null);
8472 return;
8473 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008474 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008476 if (p != null && ai != null) {
8477 final Rect r = ai.mTmpInvalRect;
8478 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8479 // Don't call invalidate -- we don't want to internally scroll
8480 // our own bounds
8481 p.invalidateChild(this, r);
8482 }
8483 }
8484 }
8485
8486 /**
Romain Guyda489792011-02-03 01:05:15 -08008487 * @hide
8488 */
8489 public void fastInvalidate() {
Chet Haaseaceafe62011-08-26 15:44:33 -07008490 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008491 return;
8492 }
Romain Guyda489792011-02-03 01:05:15 -08008493 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
8494 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8495 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
8496 if (mParent instanceof View) {
8497 ((View) mParent).mPrivateFlags |= INVALIDATED;
8498 }
8499 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008500 mPrivateFlags |= DIRTY;
Romain Guyda489792011-02-03 01:05:15 -08008501 mPrivateFlags |= INVALIDATED;
8502 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07008503 if (mParent != null && mAttachInfo != null) {
8504 if (mAttachInfo.mHardwareAccelerated) {
8505 mParent.invalidateChild(this, null);
8506 } else {
8507 final Rect r = mAttachInfo.mTmpInvalRect;
8508 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8509 // Don't call invalidate -- we don't want to internally scroll
8510 // our own bounds
8511 mParent.invalidateChild(this, r);
8512 }
Romain Guyda489792011-02-03 01:05:15 -08008513 }
8514 }
8515 }
8516
8517 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008518 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008519 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8520 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008521 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8522 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008523 *
8524 * @hide
8525 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008526 protected void invalidateParentCaches() {
8527 if (mParent instanceof View) {
8528 ((View) mParent).mPrivateFlags |= INVALIDATED;
8529 }
8530 }
Joe Malin32736f02011-01-19 16:14:20 -08008531
Romain Guy0fd89bf2011-01-26 15:41:30 -08008532 /**
8533 * Used to indicate that the parent of this view should be invalidated. This functionality
8534 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8535 * which is necessary when various parent-managed properties of the view change, such as
8536 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8537 * an invalidation event to the parent.
8538 *
8539 * @hide
8540 */
8541 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008542 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008543 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008544 }
8545 }
8546
8547 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008548 * Indicates whether this View is opaque. An opaque View guarantees that it will
8549 * draw all the pixels overlapping its bounds using a fully opaque color.
8550 *
8551 * Subclasses of View should override this method whenever possible to indicate
8552 * whether an instance is opaque. Opaque Views are treated in a special way by
8553 * the View hierarchy, possibly allowing it to perform optimizations during
8554 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008555 *
Romain Guy24443ea2009-05-11 11:56:30 -07008556 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008557 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008558 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008559 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008560 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008561 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8562 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008563 }
8564
Adam Powell20232d02010-12-08 21:08:53 -08008565 /**
8566 * @hide
8567 */
8568 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008569 // Opaque if:
8570 // - Has a background
8571 // - Background is opaque
8572 // - Doesn't have scrollbars or scrollbars are inside overlay
8573
8574 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8575 mPrivateFlags |= OPAQUE_BACKGROUND;
8576 } else {
8577 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8578 }
8579
8580 final int flags = mViewFlags;
8581 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8582 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8583 mPrivateFlags |= OPAQUE_SCROLLBARS;
8584 } else {
8585 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8586 }
8587 }
8588
8589 /**
8590 * @hide
8591 */
8592 protected boolean hasOpaqueScrollbars() {
8593 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008594 }
8595
8596 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008597 * @return A handler associated with the thread running the View. This
8598 * handler can be used to pump events in the UI events queue.
8599 */
8600 public Handler getHandler() {
8601 if (mAttachInfo != null) {
8602 return mAttachInfo.mHandler;
8603 }
8604 return null;
8605 }
8606
8607 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008608 * <p>Causes the Runnable to be added to the message queue.
8609 * The runnable will be run on the user interface thread.</p>
8610 *
8611 * <p>This method can be invoked from outside of the UI thread
8612 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008613 *
8614 * @param action The Runnable that will be executed.
8615 *
8616 * @return Returns true if the Runnable was successfully placed in to the
8617 * message queue. Returns false on failure, usually because the
8618 * looper processing the message queue is exiting.
8619 */
8620 public boolean post(Runnable action) {
8621 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008622 AttachInfo attachInfo = mAttachInfo;
8623 if (attachInfo != null) {
8624 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008625 } else {
8626 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008627 ViewRootImpl.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008628 return true;
8629 }
8630
8631 return handler.post(action);
8632 }
8633
8634 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008635 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008636 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008637 * The runnable will be run on the user interface thread.</p>
8638 *
8639 * <p>This method can be invoked from outside of the UI thread
8640 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008641 *
8642 * @param action The Runnable that will be executed.
8643 * @param delayMillis The delay (in milliseconds) until the Runnable
8644 * will be executed.
8645 *
8646 * @return true if the Runnable was successfully placed in to the
8647 * message queue. Returns false on failure, usually because the
8648 * looper processing the message queue is exiting. Note that a
8649 * result of true does not mean the Runnable will be processed --
8650 * if the looper is quit before the delivery time of the message
8651 * occurs then the message will be dropped.
8652 */
8653 public boolean postDelayed(Runnable action, long delayMillis) {
8654 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008655 AttachInfo attachInfo = mAttachInfo;
8656 if (attachInfo != null) {
8657 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008658 } else {
8659 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008660 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008661 return true;
8662 }
8663
8664 return handler.postDelayed(action, delayMillis);
8665 }
8666
8667 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008668 * <p>Removes the specified Runnable from the message queue.</p>
8669 *
8670 * <p>This method can be invoked from outside of the UI thread
8671 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008672 *
8673 * @param action The Runnable to remove from the message handling queue
8674 *
8675 * @return true if this view could ask the Handler to remove the Runnable,
8676 * false otherwise. When the returned value is true, the Runnable
8677 * may or may not have been actually removed from the message queue
8678 * (for instance, if the Runnable was not in the queue already.)
8679 */
8680 public boolean removeCallbacks(Runnable action) {
8681 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008682 AttachInfo attachInfo = mAttachInfo;
8683 if (attachInfo != null) {
8684 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008685 } else {
8686 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008687 ViewRootImpl.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008688 return true;
8689 }
8690
8691 handler.removeCallbacks(action);
8692 return true;
8693 }
8694
8695 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008696 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
8697 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008698 *
Romain Guye63a4f32011-08-11 11:33:31 -07008699 * <p>This method can be invoked from outside of the UI thread
8700 * only when this View is attached to a window.</p>
8701 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008702 * @see #invalidate()
8703 */
8704 public void postInvalidate() {
8705 postInvalidateDelayed(0);
8706 }
8707
8708 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008709 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8710 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
8711 *
8712 * <p>This method can be invoked from outside of the UI thread
8713 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008714 *
8715 * @param left The left coordinate of the rectangle to invalidate.
8716 * @param top The top coordinate of the rectangle to invalidate.
8717 * @param right The right coordinate of the rectangle to invalidate.
8718 * @param bottom The bottom coordinate of the rectangle to invalidate.
8719 *
8720 * @see #invalidate(int, int, int, int)
8721 * @see #invalidate(Rect)
8722 */
8723 public void postInvalidate(int left, int top, int right, int bottom) {
8724 postInvalidateDelayed(0, left, top, right, bottom);
8725 }
8726
8727 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008728 * <p>Cause an invalidate to happen on a subsequent cycle through the event
8729 * loop. Waits for the specified amount of time.</p>
8730 *
8731 * <p>This method can be invoked from outside of the UI thread
8732 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008733 *
8734 * @param delayMilliseconds the duration in milliseconds to delay the
8735 * invalidation by
8736 */
8737 public void postInvalidateDelayed(long delayMilliseconds) {
8738 // We try only with the AttachInfo because there's no point in invalidating
8739 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008740 AttachInfo attachInfo = mAttachInfo;
8741 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008742 Message msg = Message.obtain();
8743 msg.what = AttachInfo.INVALIDATE_MSG;
8744 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008745 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008746 }
8747 }
8748
8749 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008750 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8751 * through the event loop. Waits for the specified amount of time.</p>
8752 *
8753 * <p>This method can be invoked from outside of the UI thread
8754 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008755 *
8756 * @param delayMilliseconds the duration in milliseconds to delay the
8757 * invalidation by
8758 * @param left The left coordinate of the rectangle to invalidate.
8759 * @param top The top coordinate of the rectangle to invalidate.
8760 * @param right The right coordinate of the rectangle to invalidate.
8761 * @param bottom The bottom coordinate of the rectangle to invalidate.
8762 */
8763 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8764 int right, int bottom) {
8765
8766 // We try only with the AttachInfo because there's no point in invalidating
8767 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008768 AttachInfo attachInfo = mAttachInfo;
8769 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008770 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8771 info.target = this;
8772 info.left = left;
8773 info.top = top;
8774 info.right = right;
8775 info.bottom = bottom;
8776
8777 final Message msg = Message.obtain();
8778 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8779 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008780 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008781 }
8782 }
8783
8784 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008785 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8786 * This event is sent at most once every
8787 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8788 */
8789 private void postSendViewScrolledAccessibilityEventCallback() {
8790 if (mSendViewScrolledAccessibilityEvent == null) {
8791 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8792 }
8793 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8794 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8795 postDelayed(mSendViewScrolledAccessibilityEvent,
8796 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8797 }
8798 }
8799
8800 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008801 * Called by a parent to request that a child update its values for mScrollX
8802 * and mScrollY if necessary. This will typically be done if the child is
8803 * animating a scroll using a {@link android.widget.Scroller Scroller}
8804 * object.
8805 */
8806 public void computeScroll() {
8807 }
8808
8809 /**
8810 * <p>Indicate whether the horizontal edges are faded when the view is
8811 * scrolled horizontally.</p>
8812 *
8813 * @return true if the horizontal edges should are faded on scroll, false
8814 * otherwise
8815 *
8816 * @see #setHorizontalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008817 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008818 */
8819 public boolean isHorizontalFadingEdgeEnabled() {
8820 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8821 }
8822
8823 /**
8824 * <p>Define whether the horizontal edges should be faded when this view
8825 * is scrolled horizontally.</p>
8826 *
8827 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8828 * be faded when the view is scrolled
8829 * horizontally
8830 *
8831 * @see #isHorizontalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008832 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008833 */
8834 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8835 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8836 if (horizontalFadingEdgeEnabled) {
8837 initScrollCache();
8838 }
8839
8840 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8841 }
8842 }
8843
8844 /**
8845 * <p>Indicate whether the vertical edges are faded when the view is
8846 * scrolled horizontally.</p>
8847 *
8848 * @return true if the vertical edges should are faded on scroll, false
8849 * otherwise
8850 *
8851 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008852 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008853 */
8854 public boolean isVerticalFadingEdgeEnabled() {
8855 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8856 }
8857
8858 /**
8859 * <p>Define whether the vertical edges should be faded when this view
8860 * is scrolled vertically.</p>
8861 *
8862 * @param verticalFadingEdgeEnabled true if the vertical edges should
8863 * be faded when the view is scrolled
8864 * vertically
8865 *
8866 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008867 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008868 */
8869 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8870 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8871 if (verticalFadingEdgeEnabled) {
8872 initScrollCache();
8873 }
8874
8875 mViewFlags ^= FADING_EDGE_VERTICAL;
8876 }
8877 }
8878
8879 /**
8880 * Returns the strength, or intensity, of the top faded edge. The strength is
8881 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8882 * returns 0.0 or 1.0 but no value in between.
8883 *
8884 * Subclasses should override this method to provide a smoother fade transition
8885 * when scrolling occurs.
8886 *
8887 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8888 */
8889 protected float getTopFadingEdgeStrength() {
8890 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8891 }
8892
8893 /**
8894 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8895 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8896 * returns 0.0 or 1.0 but no value in between.
8897 *
8898 * Subclasses should override this method to provide a smoother fade transition
8899 * when scrolling occurs.
8900 *
8901 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8902 */
8903 protected float getBottomFadingEdgeStrength() {
8904 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8905 computeVerticalScrollRange() ? 1.0f : 0.0f;
8906 }
8907
8908 /**
8909 * Returns the strength, or intensity, of the left faded edge. The strength is
8910 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8911 * returns 0.0 or 1.0 but no value in between.
8912 *
8913 * Subclasses should override this method to provide a smoother fade transition
8914 * when scrolling occurs.
8915 *
8916 * @return the intensity of the left fade as a float between 0.0f and 1.0f
8917 */
8918 protected float getLeftFadingEdgeStrength() {
8919 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
8920 }
8921
8922 /**
8923 * Returns the strength, or intensity, of the right faded edge. The strength is
8924 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8925 * returns 0.0 or 1.0 but no value in between.
8926 *
8927 * Subclasses should override this method to provide a smoother fade transition
8928 * when scrolling occurs.
8929 *
8930 * @return the intensity of the right fade as a float between 0.0f and 1.0f
8931 */
8932 protected float getRightFadingEdgeStrength() {
8933 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
8934 computeHorizontalScrollRange() ? 1.0f : 0.0f;
8935 }
8936
8937 /**
8938 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
8939 * scrollbar is not drawn by default.</p>
8940 *
8941 * @return true if the horizontal scrollbar should be painted, false
8942 * otherwise
8943 *
8944 * @see #setHorizontalScrollBarEnabled(boolean)
8945 */
8946 public boolean isHorizontalScrollBarEnabled() {
8947 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8948 }
8949
8950 /**
8951 * <p>Define whether the horizontal scrollbar should be drawn or not. The
8952 * scrollbar is not drawn by default.</p>
8953 *
8954 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
8955 * be painted
8956 *
8957 * @see #isHorizontalScrollBarEnabled()
8958 */
8959 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
8960 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
8961 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008962 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008963 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008964 }
8965 }
8966
8967 /**
8968 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
8969 * scrollbar is not drawn by default.</p>
8970 *
8971 * @return true if the vertical scrollbar should be painted, false
8972 * otherwise
8973 *
8974 * @see #setVerticalScrollBarEnabled(boolean)
8975 */
8976 public boolean isVerticalScrollBarEnabled() {
8977 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
8978 }
8979
8980 /**
8981 * <p>Define whether the vertical scrollbar should be drawn or not. The
8982 * scrollbar is not drawn by default.</p>
8983 *
8984 * @param verticalScrollBarEnabled true if the vertical scrollbar should
8985 * be painted
8986 *
8987 * @see #isVerticalScrollBarEnabled()
8988 */
8989 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
8990 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
8991 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008992 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008993 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008994 }
8995 }
8996
Adam Powell20232d02010-12-08 21:08:53 -08008997 /**
8998 * @hide
8999 */
9000 protected void recomputePadding() {
9001 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009002 }
Joe Malin32736f02011-01-19 16:14:20 -08009003
Mike Cleronfe81d382009-09-28 14:22:16 -07009004 /**
9005 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009006 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009007 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009008 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009009 */
9010 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9011 initScrollCache();
9012 final ScrollabilityCache scrollabilityCache = mScrollCache;
9013 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009014 if (fadeScrollbars) {
9015 scrollabilityCache.state = ScrollabilityCache.OFF;
9016 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009017 scrollabilityCache.state = ScrollabilityCache.ON;
9018 }
9019 }
Joe Malin32736f02011-01-19 16:14:20 -08009020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009021 /**
Joe Malin32736f02011-01-19 16:14:20 -08009022 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009023 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009024 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009025 * @return true if scrollbar fading is enabled
9026 */
9027 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009028 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009029 }
Joe Malin32736f02011-01-19 16:14:20 -08009030
Mike Cleron52f0a642009-09-28 18:21:37 -07009031 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009032 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9033 * inset. When inset, they add to the padding of the view. And the scrollbars
9034 * can be drawn inside the padding area or on the edge of the view. For example,
9035 * if a view has a background drawable and you want to draw the scrollbars
9036 * inside the padding specified by the drawable, you can use
9037 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9038 * appear at the edge of the view, ignoring the padding, then you can use
9039 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9040 * @param style the style of the scrollbars. Should be one of
9041 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9042 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9043 * @see #SCROLLBARS_INSIDE_OVERLAY
9044 * @see #SCROLLBARS_INSIDE_INSET
9045 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9046 * @see #SCROLLBARS_OUTSIDE_INSET
9047 */
9048 public void setScrollBarStyle(int style) {
9049 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9050 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009051 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009052 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009053 }
9054 }
9055
9056 /**
9057 * <p>Returns the current scrollbar style.</p>
9058 * @return the current scrollbar style
9059 * @see #SCROLLBARS_INSIDE_OVERLAY
9060 * @see #SCROLLBARS_INSIDE_INSET
9061 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9062 * @see #SCROLLBARS_OUTSIDE_INSET
9063 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009064 @ViewDebug.ExportedProperty(mapping = {
9065 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9066 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9067 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9068 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9069 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009070 public int getScrollBarStyle() {
9071 return mViewFlags & SCROLLBARS_STYLE_MASK;
9072 }
9073
9074 /**
9075 * <p>Compute the horizontal range that the horizontal scrollbar
9076 * represents.</p>
9077 *
9078 * <p>The range is expressed in arbitrary units that must be the same as the
9079 * units used by {@link #computeHorizontalScrollExtent()} and
9080 * {@link #computeHorizontalScrollOffset()}.</p>
9081 *
9082 * <p>The default range is the drawing width of this view.</p>
9083 *
9084 * @return the total horizontal range represented by the horizontal
9085 * scrollbar
9086 *
9087 * @see #computeHorizontalScrollExtent()
9088 * @see #computeHorizontalScrollOffset()
9089 * @see android.widget.ScrollBarDrawable
9090 */
9091 protected int computeHorizontalScrollRange() {
9092 return getWidth();
9093 }
9094
9095 /**
9096 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9097 * within the horizontal range. This value is used to compute the position
9098 * of the thumb within the scrollbar's track.</p>
9099 *
9100 * <p>The range is expressed in arbitrary units that must be the same as the
9101 * units used by {@link #computeHorizontalScrollRange()} and
9102 * {@link #computeHorizontalScrollExtent()}.</p>
9103 *
9104 * <p>The default offset is the scroll offset of this view.</p>
9105 *
9106 * @return the horizontal offset of the scrollbar's thumb
9107 *
9108 * @see #computeHorizontalScrollRange()
9109 * @see #computeHorizontalScrollExtent()
9110 * @see android.widget.ScrollBarDrawable
9111 */
9112 protected int computeHorizontalScrollOffset() {
9113 return mScrollX;
9114 }
9115
9116 /**
9117 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9118 * within the horizontal range. This value is used to compute the length
9119 * of the thumb within the scrollbar's track.</p>
9120 *
9121 * <p>The range is expressed in arbitrary units that must be the same as the
9122 * units used by {@link #computeHorizontalScrollRange()} and
9123 * {@link #computeHorizontalScrollOffset()}.</p>
9124 *
9125 * <p>The default extent is the drawing width of this view.</p>
9126 *
9127 * @return the horizontal extent of the scrollbar's thumb
9128 *
9129 * @see #computeHorizontalScrollRange()
9130 * @see #computeHorizontalScrollOffset()
9131 * @see android.widget.ScrollBarDrawable
9132 */
9133 protected int computeHorizontalScrollExtent() {
9134 return getWidth();
9135 }
9136
9137 /**
9138 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9139 *
9140 * <p>The range is expressed in arbitrary units that must be the same as the
9141 * units used by {@link #computeVerticalScrollExtent()} and
9142 * {@link #computeVerticalScrollOffset()}.</p>
9143 *
9144 * @return the total vertical range represented by the vertical scrollbar
9145 *
9146 * <p>The default range is the drawing height of this view.</p>
9147 *
9148 * @see #computeVerticalScrollExtent()
9149 * @see #computeVerticalScrollOffset()
9150 * @see android.widget.ScrollBarDrawable
9151 */
9152 protected int computeVerticalScrollRange() {
9153 return getHeight();
9154 }
9155
9156 /**
9157 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9158 * within the horizontal range. This value is used to compute the position
9159 * of the thumb within the scrollbar's track.</p>
9160 *
9161 * <p>The range is expressed in arbitrary units that must be the same as the
9162 * units used by {@link #computeVerticalScrollRange()} and
9163 * {@link #computeVerticalScrollExtent()}.</p>
9164 *
9165 * <p>The default offset is the scroll offset of this view.</p>
9166 *
9167 * @return the vertical offset of the scrollbar's thumb
9168 *
9169 * @see #computeVerticalScrollRange()
9170 * @see #computeVerticalScrollExtent()
9171 * @see android.widget.ScrollBarDrawable
9172 */
9173 protected int computeVerticalScrollOffset() {
9174 return mScrollY;
9175 }
9176
9177 /**
9178 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9179 * within the vertical range. This value is used to compute the length
9180 * of the thumb within the scrollbar's track.</p>
9181 *
9182 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009183 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009184 * {@link #computeVerticalScrollOffset()}.</p>
9185 *
9186 * <p>The default extent is the drawing height of this view.</p>
9187 *
9188 * @return the vertical extent of the scrollbar's thumb
9189 *
9190 * @see #computeVerticalScrollRange()
9191 * @see #computeVerticalScrollOffset()
9192 * @see android.widget.ScrollBarDrawable
9193 */
9194 protected int computeVerticalScrollExtent() {
9195 return getHeight();
9196 }
9197
9198 /**
Adam Powell69159442011-06-13 17:53:06 -07009199 * Check if this view can be scrolled horizontally in a certain direction.
9200 *
9201 * @param direction Negative to check scrolling left, positive to check scrolling right.
9202 * @return true if this view can be scrolled in the specified direction, false otherwise.
9203 */
9204 public boolean canScrollHorizontally(int direction) {
9205 final int offset = computeHorizontalScrollOffset();
9206 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9207 if (range == 0) return false;
9208 if (direction < 0) {
9209 return offset > 0;
9210 } else {
9211 return offset < range - 1;
9212 }
9213 }
9214
9215 /**
9216 * Check if this view can be scrolled vertically in a certain direction.
9217 *
9218 * @param direction Negative to check scrolling up, positive to check scrolling down.
9219 * @return true if this view can be scrolled in the specified direction, false otherwise.
9220 */
9221 public boolean canScrollVertically(int direction) {
9222 final int offset = computeVerticalScrollOffset();
9223 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9224 if (range == 0) return false;
9225 if (direction < 0) {
9226 return offset > 0;
9227 } else {
9228 return offset < range - 1;
9229 }
9230 }
9231
9232 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009233 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9234 * scrollbars are painted only if they have been awakened first.</p>
9235 *
9236 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009237 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009238 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009239 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009240 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009241 // scrollbars are drawn only when the animation is running
9242 final ScrollabilityCache cache = mScrollCache;
9243 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009244
Mike Cleronf116bf82009-09-27 19:14:12 -07009245 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009246
Mike Cleronf116bf82009-09-27 19:14:12 -07009247 if (state == ScrollabilityCache.OFF) {
9248 return;
9249 }
Joe Malin32736f02011-01-19 16:14:20 -08009250
Mike Cleronf116bf82009-09-27 19:14:12 -07009251 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009252
Mike Cleronf116bf82009-09-27 19:14:12 -07009253 if (state == ScrollabilityCache.FADING) {
9254 // We're fading -- get our fade interpolation
9255 if (cache.interpolatorValues == null) {
9256 cache.interpolatorValues = new float[1];
9257 }
Joe Malin32736f02011-01-19 16:14:20 -08009258
Mike Cleronf116bf82009-09-27 19:14:12 -07009259 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009260
Mike Cleronf116bf82009-09-27 19:14:12 -07009261 // Stops the animation if we're done
9262 if (cache.scrollBarInterpolator.timeToValues(values) ==
9263 Interpolator.Result.FREEZE_END) {
9264 cache.state = ScrollabilityCache.OFF;
9265 } else {
9266 cache.scrollBar.setAlpha(Math.round(values[0]));
9267 }
Joe Malin32736f02011-01-19 16:14:20 -08009268
9269 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009270 // drawing. We only want this when we're fading so that
9271 // we prevent excessive redraws
9272 invalidate = true;
9273 } else {
9274 // We're just on -- but we may have been fading before so
9275 // reset alpha
9276 cache.scrollBar.setAlpha(255);
9277 }
9278
Joe Malin32736f02011-01-19 16:14:20 -08009279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009280 final int viewFlags = mViewFlags;
9281
9282 final boolean drawHorizontalScrollBar =
9283 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9284 final boolean drawVerticalScrollBar =
9285 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9286 && !isVerticalScrollBarHidden();
9287
9288 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9289 final int width = mRight - mLeft;
9290 final int height = mBottom - mTop;
9291
9292 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009293
Mike Reede8853fc2009-09-04 14:01:48 -04009294 final int scrollX = mScrollX;
9295 final int scrollY = mScrollY;
9296 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9297
Mike Cleronf116bf82009-09-27 19:14:12 -07009298 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009300 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009301 int size = scrollBar.getSize(false);
9302 if (size <= 0) {
9303 size = cache.scrollBarSize;
9304 }
9305
Mike Cleronf116bf82009-09-27 19:14:12 -07009306 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009307 computeHorizontalScrollOffset(),
9308 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009309 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009310 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009311 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009312 left = scrollX + (mPaddingLeft & inside);
9313 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9314 bottom = top + size;
9315 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9316 if (invalidate) {
9317 invalidate(left, top, right, bottom);
9318 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009319 }
9320
9321 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009322 int size = scrollBar.getSize(true);
9323 if (size <= 0) {
9324 size = cache.scrollBarSize;
9325 }
9326
Mike Reede8853fc2009-09-04 14:01:48 -04009327 scrollBar.setParameters(computeVerticalScrollRange(),
9328 computeVerticalScrollOffset(),
9329 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009330 switch (mVerticalScrollbarPosition) {
9331 default:
9332 case SCROLLBAR_POSITION_DEFAULT:
9333 case SCROLLBAR_POSITION_RIGHT:
9334 left = scrollX + width - size - (mUserPaddingRight & inside);
9335 break;
9336 case SCROLLBAR_POSITION_LEFT:
9337 left = scrollX + (mUserPaddingLeft & inside);
9338 break;
9339 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009340 top = scrollY + (mPaddingTop & inside);
9341 right = left + size;
9342 bottom = scrollY + height - (mUserPaddingBottom & inside);
9343 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9344 if (invalidate) {
9345 invalidate(left, top, right, bottom);
9346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009347 }
9348 }
9349 }
9350 }
Romain Guy8506ab42009-06-11 17:35:47 -07009351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009352 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009353 * 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 -08009354 * FastScroller is visible.
9355 * @return whether to temporarily hide the vertical scrollbar
9356 * @hide
9357 */
9358 protected boolean isVerticalScrollBarHidden() {
9359 return false;
9360 }
9361
9362 /**
9363 * <p>Draw the horizontal scrollbar if
9364 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9365 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009366 * @param canvas the canvas on which to draw the scrollbar
9367 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009368 *
9369 * @see #isHorizontalScrollBarEnabled()
9370 * @see #computeHorizontalScrollRange()
9371 * @see #computeHorizontalScrollExtent()
9372 * @see #computeHorizontalScrollOffset()
9373 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009374 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009375 */
Romain Guy8fb95422010-08-17 18:38:51 -07009376 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9377 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009378 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009379 scrollBar.draw(canvas);
9380 }
Mike Reede8853fc2009-09-04 14:01:48 -04009381
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009382 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009383 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9384 * returns true.</p>
9385 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009386 * @param canvas the canvas on which to draw the scrollbar
9387 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009388 *
9389 * @see #isVerticalScrollBarEnabled()
9390 * @see #computeVerticalScrollRange()
9391 * @see #computeVerticalScrollExtent()
9392 * @see #computeVerticalScrollOffset()
9393 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009394 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009395 */
Romain Guy8fb95422010-08-17 18:38:51 -07009396 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9397 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009398 scrollBar.setBounds(l, t, r, b);
9399 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009400 }
9401
9402 /**
9403 * Implement this to do your drawing.
9404 *
9405 * @param canvas the canvas on which the background will be drawn
9406 */
9407 protected void onDraw(Canvas canvas) {
9408 }
9409
9410 /*
9411 * Caller is responsible for calling requestLayout if necessary.
9412 * (This allows addViewInLayout to not request a new layout.)
9413 */
9414 void assignParent(ViewParent parent) {
9415 if (mParent == null) {
9416 mParent = parent;
9417 } else if (parent == null) {
9418 mParent = null;
9419 } else {
9420 throw new RuntimeException("view " + this + " being added, but"
9421 + " it already has a parent");
9422 }
9423 }
9424
9425 /**
9426 * This is called when the view is attached to a window. At this point it
9427 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009428 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9429 * however it may be called any time before the first onDraw -- including
9430 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009431 *
9432 * @see #onDetachedFromWindow()
9433 */
9434 protected void onAttachedToWindow() {
9435 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9436 mParent.requestTransparentRegion(this);
9437 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009438 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9439 initialAwakenScrollBars();
9440 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9441 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009442 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009443 // Order is important here: LayoutDirection MUST be resolved before Padding
9444 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009445 resolveLayoutDirectionIfNeeded();
9446 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009447 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009448 if (isFocused()) {
9449 InputMethodManager imm = InputMethodManager.peekInstance();
9450 imm.focusIn(this);
9451 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009452 }
Cibu Johny86666632010-02-22 13:01:02 -08009453
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009454 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009455 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9456 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009457 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009458 private void resolveLayoutDirectionIfNeeded() {
9459 // Do not resolve if it is not needed
9460 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9461
9462 // Clear any previous layout direction resolution
9463 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9464
Fabrice Di Meglio4b60c302011-08-17 16:56:55 -07009465 // Reset also TextDirection as a change into LayoutDirection may impact the selected
9466 // TextDirectionHeuristic
9467 resetResolvedTextDirection();
9468
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009469 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009470 switch (getLayoutDirection()) {
9471 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009472 // We cannot do the resolution if there is no parent
9473 if (mParent == null) return;
9474
Cibu Johny86666632010-02-22 13:01:02 -08009475 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009476 if (mParent instanceof ViewGroup) {
9477 ViewGroup viewGroup = ((ViewGroup) mParent);
9478
9479 // Check if the parent view group can resolve
9480 if (! viewGroup.canResolveLayoutDirection()) {
9481 return;
9482 }
9483 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9484 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9485 }
Cibu Johny86666632010-02-22 13:01:02 -08009486 }
9487 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009488 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009489 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009490 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009491 case LAYOUT_DIRECTION_LOCALE:
9492 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009493 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009494 }
9495 break;
9496 default:
9497 // Nothing to do, LTR by default
9498 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009499
9500 // Set to resolved
9501 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
9502 }
9503
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009504 /**
9505 * @hide
9506 */
9507 protected void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009508 // If the user specified the absolute padding (either with android:padding or
9509 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9510 // use the default padding or the padding from the background drawable
9511 // (stored at this point in mPadding*)
9512 switch (getResolvedLayoutDirection()) {
9513 case LAYOUT_DIRECTION_RTL:
9514 // Start user padding override Right user padding. Otherwise, if Right user
9515 // padding is not defined, use the default Right padding. If Right user padding
9516 // is defined, just use it.
9517 if (mUserPaddingStart >= 0) {
9518 mUserPaddingRight = mUserPaddingStart;
9519 } else if (mUserPaddingRight < 0) {
9520 mUserPaddingRight = mPaddingRight;
9521 }
9522 if (mUserPaddingEnd >= 0) {
9523 mUserPaddingLeft = mUserPaddingEnd;
9524 } else if (mUserPaddingLeft < 0) {
9525 mUserPaddingLeft = mPaddingLeft;
9526 }
9527 break;
9528 case LAYOUT_DIRECTION_LTR:
9529 default:
9530 // Start user padding override Left user padding. Otherwise, if Left user
9531 // padding is not defined, use the default left padding. If Left user padding
9532 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009533 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009534 mUserPaddingLeft = mUserPaddingStart;
9535 } else if (mUserPaddingLeft < 0) {
9536 mUserPaddingLeft = mPaddingLeft;
9537 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009538 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009539 mUserPaddingRight = mUserPaddingEnd;
9540 } else if (mUserPaddingRight < 0) {
9541 mUserPaddingRight = mPaddingRight;
9542 }
9543 }
9544
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009545 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9546
9547 recomputePadding();
9548 }
9549
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009550 /**
9551 * Return true if layout direction resolution can be done
9552 *
9553 * @hide
9554 */
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009555 protected boolean canResolveLayoutDirection() {
9556 switch (getLayoutDirection()) {
9557 case LAYOUT_DIRECTION_INHERIT:
9558 return (mParent != null);
9559 default:
9560 return true;
9561 }
9562 }
9563
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009564 /**
Doug Feltc0ccf0c2011-06-23 16:13:18 -07009565 * Reset the resolved layout direction.
9566 *
9567 * Subclasses need to override this method to clear cached information that depends on the
9568 * resolved layout direction, or to inform child views that inherit their layout direction.
9569 * Overrides must also call the superclass implementation at the start of their implementation.
9570 *
9571 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009572 */
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009573 protected void resetResolvedLayoutDirection() {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07009574 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009575 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009576 }
9577
9578 /**
9579 * Check if a Locale is corresponding to a RTL script.
9580 *
9581 * @param locale Locale to check
9582 * @return true if a Locale is corresponding to a RTL script.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009583 *
9584 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009585 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009586 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07009587 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
9588 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009589 }
9590
9591 /**
9592 * This is called when the view is detached from a window. At this point it
9593 * no longer has a surface for drawing.
9594 *
9595 * @see #onAttachedToWindow()
9596 */
9597 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009598 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009599
Romain Guya440b002010-02-24 15:57:54 -08009600 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009601 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009602 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009603 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009605 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009606
Romain Guy6d7475d2011-07-27 16:28:21 -07009607 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009608
Chet Haasedaf98e92011-01-10 14:10:36 -08009609 if (mDisplayList != null) {
9610 mDisplayList.invalidate();
9611 }
9612
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009613 if (mAttachInfo != null) {
9614 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009615 }
9616
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009617 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009618
9619 resetResolvedLayoutDirection();
9620 resetResolvedTextDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009621 }
9622
9623 /**
9624 * @return The number of times this view has been attached to a window
9625 */
9626 protected int getWindowAttachCount() {
9627 return mWindowAttachCount;
9628 }
9629
9630 /**
9631 * Retrieve a unique token identifying the window this view is attached to.
9632 * @return Return the window's token for use in
9633 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9634 */
9635 public IBinder getWindowToken() {
9636 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9637 }
9638
9639 /**
9640 * Retrieve a unique token identifying the top-level "real" window of
9641 * the window that this view is attached to. That is, this is like
9642 * {@link #getWindowToken}, except if the window this view in is a panel
9643 * window (attached to another containing window), then the token of
9644 * the containing window is returned instead.
9645 *
9646 * @return Returns the associated window token, either
9647 * {@link #getWindowToken()} or the containing window's token.
9648 */
9649 public IBinder getApplicationWindowToken() {
9650 AttachInfo ai = mAttachInfo;
9651 if (ai != null) {
9652 IBinder appWindowToken = ai.mPanelParentWindowToken;
9653 if (appWindowToken == null) {
9654 appWindowToken = ai.mWindowToken;
9655 }
9656 return appWindowToken;
9657 }
9658 return null;
9659 }
9660
9661 /**
9662 * Retrieve private session object this view hierarchy is using to
9663 * communicate with the window manager.
9664 * @return the session object to communicate with the window manager
9665 */
9666 /*package*/ IWindowSession getWindowSession() {
9667 return mAttachInfo != null ? mAttachInfo.mSession : null;
9668 }
9669
9670 /**
9671 * @param info the {@link android.view.View.AttachInfo} to associated with
9672 * this view
9673 */
9674 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9675 //System.out.println("Attached! " + this);
9676 mAttachInfo = info;
9677 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009678 // We will need to evaluate the drawable state at least once.
9679 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009680 if (mFloatingTreeObserver != null) {
9681 info.mTreeObserver.merge(mFloatingTreeObserver);
9682 mFloatingTreeObserver = null;
9683 }
9684 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9685 mAttachInfo.mScrollContainers.add(this);
9686 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9687 }
9688 performCollectViewAttributes(visibility);
9689 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009690
9691 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9692 mOnAttachStateChangeListeners;
9693 if (listeners != null && listeners.size() > 0) {
9694 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9695 // perform the dispatching. The iterator is a safe guard against listeners that
9696 // could mutate the list by calling the various add/remove methods. This prevents
9697 // the array from being modified while we iterate it.
9698 for (OnAttachStateChangeListener listener : listeners) {
9699 listener.onViewAttachedToWindow(this);
9700 }
9701 }
9702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009703 int vis = info.mWindowVisibility;
9704 if (vis != GONE) {
9705 onWindowVisibilityChanged(vis);
9706 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009707 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9708 // If nobody has evaluated the drawable state yet, then do it now.
9709 refreshDrawableState();
9710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009711 }
9712
9713 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009714 AttachInfo info = mAttachInfo;
9715 if (info != null) {
9716 int vis = info.mWindowVisibility;
9717 if (vis != GONE) {
9718 onWindowVisibilityChanged(GONE);
9719 }
9720 }
9721
9722 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009723
Adam Powell4afd62b2011-02-18 15:02:18 -08009724 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9725 mOnAttachStateChangeListeners;
9726 if (listeners != null && listeners.size() > 0) {
9727 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9728 // perform the dispatching. The iterator is a safe guard against listeners that
9729 // could mutate the list by calling the various add/remove methods. This prevents
9730 // the array from being modified while we iterate it.
9731 for (OnAttachStateChangeListener listener : listeners) {
9732 listener.onViewDetachedFromWindow(this);
9733 }
9734 }
9735
Romain Guy01d5edc2011-01-28 11:28:53 -08009736 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009737 mAttachInfo.mScrollContainers.remove(this);
9738 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9739 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009741 mAttachInfo = null;
9742 }
9743
9744 /**
9745 * Store this view hierarchy's frozen state into the given container.
9746 *
9747 * @param container The SparseArray in which to save the view's state.
9748 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009749 * @see #restoreHierarchyState(android.util.SparseArray)
9750 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9751 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009752 */
9753 public void saveHierarchyState(SparseArray<Parcelable> container) {
9754 dispatchSaveInstanceState(container);
9755 }
9756
9757 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009758 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9759 * this view and its children. May be overridden to modify how freezing happens to a
9760 * 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 -08009761 *
9762 * @param container The SparseArray in which to save the view's state.
9763 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009764 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9765 * @see #saveHierarchyState(android.util.SparseArray)
9766 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009767 */
9768 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9769 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9770 mPrivateFlags &= ~SAVE_STATE_CALLED;
9771 Parcelable state = onSaveInstanceState();
9772 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9773 throw new IllegalStateException(
9774 "Derived class did not call super.onSaveInstanceState()");
9775 }
9776 if (state != null) {
9777 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9778 // + ": " + state);
9779 container.put(mID, state);
9780 }
9781 }
9782 }
9783
9784 /**
9785 * Hook allowing a view to generate a representation of its internal state
9786 * that can later be used to create a new instance with that same state.
9787 * This state should only contain information that is not persistent or can
9788 * not be reconstructed later. For example, you will never store your
9789 * current position on screen because that will be computed again when a
9790 * new instance of the view is placed in its view hierarchy.
9791 * <p>
9792 * Some examples of things you may store here: the current cursor position
9793 * in a text view (but usually not the text itself since that is stored in a
9794 * content provider or other persistent storage), the currently selected
9795 * item in a list view.
9796 *
9797 * @return Returns a Parcelable object containing the view's current dynamic
9798 * state, or null if there is nothing interesting to save. The
9799 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009800 * @see #onRestoreInstanceState(android.os.Parcelable)
9801 * @see #saveHierarchyState(android.util.SparseArray)
9802 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009803 * @see #setSaveEnabled(boolean)
9804 */
9805 protected Parcelable onSaveInstanceState() {
9806 mPrivateFlags |= SAVE_STATE_CALLED;
9807 return BaseSavedState.EMPTY_STATE;
9808 }
9809
9810 /**
9811 * Restore this view hierarchy's frozen state from the given container.
9812 *
9813 * @param container The SparseArray which holds previously frozen states.
9814 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009815 * @see #saveHierarchyState(android.util.SparseArray)
9816 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9817 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009818 */
9819 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9820 dispatchRestoreInstanceState(container);
9821 }
9822
9823 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009824 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9825 * state for this view and its children. May be overridden to modify how restoring
9826 * happens to a view's children; for example, some views may want to not store state
9827 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009828 *
9829 * @param container The SparseArray which holds previously saved state.
9830 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009831 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9832 * @see #restoreHierarchyState(android.util.SparseArray)
9833 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009834 */
9835 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9836 if (mID != NO_ID) {
9837 Parcelable state = container.get(mID);
9838 if (state != null) {
9839 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9840 // + ": " + state);
9841 mPrivateFlags &= ~SAVE_STATE_CALLED;
9842 onRestoreInstanceState(state);
9843 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9844 throw new IllegalStateException(
9845 "Derived class did not call super.onRestoreInstanceState()");
9846 }
9847 }
9848 }
9849 }
9850
9851 /**
9852 * Hook allowing a view to re-apply a representation of its internal state that had previously
9853 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9854 * null state.
9855 *
9856 * @param state The frozen state that had previously been returned by
9857 * {@link #onSaveInstanceState}.
9858 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009859 * @see #onSaveInstanceState()
9860 * @see #restoreHierarchyState(android.util.SparseArray)
9861 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009862 */
9863 protected void onRestoreInstanceState(Parcelable state) {
9864 mPrivateFlags |= SAVE_STATE_CALLED;
9865 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009866 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9867 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009868 + "when two views of different type have the same id in the same hierarchy. "
9869 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009870 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009871 }
9872 }
9873
9874 /**
9875 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9876 *
9877 * @return the drawing start time in milliseconds
9878 */
9879 public long getDrawingTime() {
9880 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9881 }
9882
9883 /**
9884 * <p>Enables or disables the duplication of the parent's state into this view. When
9885 * duplication is enabled, this view gets its drawable state from its parent rather
9886 * than from its own internal properties.</p>
9887 *
9888 * <p>Note: in the current implementation, setting this property to true after the
9889 * view was added to a ViewGroup might have no effect at all. This property should
9890 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
9891 *
9892 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
9893 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009894 *
Gilles Debunnefb817032011-01-13 13:52:49 -08009895 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
9896 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009897 *
9898 * @param enabled True to enable duplication of the parent's drawable state, false
9899 * to disable it.
9900 *
9901 * @see #getDrawableState()
9902 * @see #isDuplicateParentStateEnabled()
9903 */
9904 public void setDuplicateParentStateEnabled(boolean enabled) {
9905 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
9906 }
9907
9908 /**
9909 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
9910 *
9911 * @return True if this view's drawable state is duplicated from the parent,
9912 * false otherwise
9913 *
9914 * @see #getDrawableState()
9915 * @see #setDuplicateParentStateEnabled(boolean)
9916 */
9917 public boolean isDuplicateParentStateEnabled() {
9918 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
9919 }
9920
9921 /**
Romain Guy171c5922011-01-06 10:04:23 -08009922 * <p>Specifies the type of layer backing this view. The layer can be
9923 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
9924 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009925 *
Romain Guy171c5922011-01-06 10:04:23 -08009926 * <p>A layer is associated with an optional {@link android.graphics.Paint}
9927 * instance that controls how the layer is composed on screen. The following
9928 * properties of the paint are taken into account when composing the layer:</p>
9929 * <ul>
9930 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
9931 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
9932 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
9933 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08009934 *
Romain Guy171c5922011-01-06 10:04:23 -08009935 * <p>If this view has an alpha value set to < 1.0 by calling
9936 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
9937 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
9938 * equivalent to setting a hardware layer on this view and providing a paint with
9939 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08009940 *
Romain Guy171c5922011-01-06 10:04:23 -08009941 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
9942 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
9943 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009944 *
Romain Guy171c5922011-01-06 10:04:23 -08009945 * @param layerType The ype of layer to use with this view, must be one of
9946 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9947 * {@link #LAYER_TYPE_HARDWARE}
9948 * @param paint The paint used to compose the layer. This argument is optional
9949 * and can be null. It is ignored when the layer type is
9950 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08009951 *
9952 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08009953 * @see #LAYER_TYPE_NONE
9954 * @see #LAYER_TYPE_SOFTWARE
9955 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08009956 * @see #setAlpha(float)
9957 *
Romain Guy171c5922011-01-06 10:04:23 -08009958 * @attr ref android.R.styleable#View_layerType
9959 */
9960 public void setLayerType(int layerType, Paint paint) {
9961 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08009962 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08009963 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
9964 }
Chet Haasedaf98e92011-01-10 14:10:36 -08009965
Romain Guyd6cd5722011-01-17 14:42:41 -08009966 if (layerType == mLayerType) {
9967 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
9968 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009969 invalidateParentCaches();
9970 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08009971 }
9972 return;
9973 }
Romain Guy171c5922011-01-06 10:04:23 -08009974
9975 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08009976 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -07009977 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -07009978 destroyLayer();
Chet Haase6f33e812011-05-17 12:42:19 -07009979 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -08009980 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -07009981 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009982 break;
Romain Guy6c319ca2011-01-11 14:29:25 -08009983 default:
9984 break;
Romain Guy171c5922011-01-06 10:04:23 -08009985 }
9986
9987 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08009988 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
9989 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
9990 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08009991
Romain Guy0fd89bf2011-01-26 15:41:30 -08009992 invalidateParentCaches();
9993 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08009994 }
9995
9996 /**
9997 * Indicates what type of layer is currently associated with this view. By default
9998 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
9999 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10000 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010001 *
Romain Guy171c5922011-01-06 10:04:23 -080010002 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10003 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010004 *
10005 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010006 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010007 * @see #LAYER_TYPE_NONE
10008 * @see #LAYER_TYPE_SOFTWARE
10009 * @see #LAYER_TYPE_HARDWARE
10010 */
10011 public int getLayerType() {
10012 return mLayerType;
10013 }
Joe Malin32736f02011-01-19 16:14:20 -080010014
Romain Guy6c319ca2011-01-11 14:29:25 -080010015 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010016 * Forces this view's layer to be created and this view to be rendered
10017 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10018 * invoking this method will have no effect.
10019 *
10020 * This method can for instance be used to render a view into its layer before
10021 * starting an animation. If this view is complex, rendering into the layer
10022 * before starting the animation will avoid skipping frames.
10023 *
10024 * @throws IllegalStateException If this view is not attached to a window
10025 *
10026 * @see #setLayerType(int, android.graphics.Paint)
10027 */
10028 public void buildLayer() {
10029 if (mLayerType == LAYER_TYPE_NONE) return;
10030
10031 if (mAttachInfo == null) {
10032 throw new IllegalStateException("This view must be attached to a window first");
10033 }
10034
10035 switch (mLayerType) {
10036 case LAYER_TYPE_HARDWARE:
10037 getHardwareLayer();
10038 break;
10039 case LAYER_TYPE_SOFTWARE:
10040 buildDrawingCache(true);
10041 break;
10042 }
10043 }
10044
10045 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010046 * <p>Returns a hardware layer that can be used to draw this view again
10047 * without executing its draw method.</p>
10048 *
10049 * @return A HardwareLayer ready to render, or null if an error occurred.
10050 */
Romain Guy5e7f7662011-01-24 22:35:56 -080010051 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010052 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10053 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010054 return null;
10055 }
10056
10057 final int width = mRight - mLeft;
10058 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010059
Romain Guy6c319ca2011-01-11 14:29:25 -080010060 if (width == 0 || height == 0) {
10061 return null;
10062 }
10063
10064 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10065 if (mHardwareLayer == null) {
10066 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10067 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -080010068 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010069 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10070 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -080010071 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010072 }
10073
Romain Guy59a12ca2011-06-09 17:48:21 -070010074 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -080010075 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
10076 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010077 try {
10078 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080010079 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -080010080 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010081
Chet Haase88172fe2011-03-07 17:36:33 -080010082 final int restoreCount = canvas.save();
10083
Romain Guy6c319ca2011-01-11 14:29:25 -080010084 computeScroll();
10085 canvas.translate(-mScrollX, -mScrollY);
10086
Romain Guy6c319ca2011-01-11 14:29:25 -080010087 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -080010088
Romain Guy6c319ca2011-01-11 14:29:25 -080010089 // Fast path for layouts with no backgrounds
10090 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10091 mPrivateFlags &= ~DIRTY_MASK;
10092 dispatchDraw(canvas);
10093 } else {
10094 draw(canvas);
10095 }
Joe Malin32736f02011-01-19 16:14:20 -080010096
Chet Haase88172fe2011-03-07 17:36:33 -080010097 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -080010098 } finally {
10099 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -080010100 mHardwareLayer.end(currentCanvas);
10101 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010102 }
10103 }
10104
10105 return mHardwareLayer;
10106 }
Romain Guy171c5922011-01-06 10:04:23 -080010107
Romain Guy65b345f2011-07-27 18:51:50 -070010108 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -070010109 if (mHardwareLayer != null) {
10110 mHardwareLayer.destroy();
10111 mHardwareLayer = null;
Romain Guy65b345f2011-07-27 18:51:50 -070010112 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010113 }
Romain Guy65b345f2011-07-27 18:51:50 -070010114 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010115 }
10116
Romain Guy171c5922011-01-06 10:04:23 -080010117 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010118 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10119 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10120 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10121 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10122 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10123 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010124 *
Romain Guy171c5922011-01-06 10:04:23 -080010125 * <p>Enabling the drawing cache is similar to
10126 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010127 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10128 * drawing cache has no effect on rendering because the system uses a different mechanism
10129 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10130 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10131 * for information on how to enable software and hardware layers.</p>
10132 *
10133 * <p>This API can be used to manually generate
10134 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10135 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010136 *
10137 * @param enabled true to enable the drawing cache, false otherwise
10138 *
10139 * @see #isDrawingCacheEnabled()
10140 * @see #getDrawingCache()
10141 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010142 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010143 */
10144 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010145 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010146 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10147 }
10148
10149 /**
10150 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10151 *
10152 * @return true if the drawing cache is enabled
10153 *
10154 * @see #setDrawingCacheEnabled(boolean)
10155 * @see #getDrawingCache()
10156 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010157 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010158 public boolean isDrawingCacheEnabled() {
10159 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10160 }
10161
10162 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010163 * Debugging utility which recursively outputs the dirty state of a view and its
10164 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010165 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010166 * @hide
10167 */
Romain Guy676b1732011-02-14 14:45:33 -080010168 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010169 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10170 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10171 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10172 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10173 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10174 if (clear) {
10175 mPrivateFlags &= clearMask;
10176 }
10177 if (this instanceof ViewGroup) {
10178 ViewGroup parent = (ViewGroup) this;
10179 final int count = parent.getChildCount();
10180 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010181 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010182 child.outputDirtyFlags(indent + " ", clear, clearMask);
10183 }
10184 }
10185 }
10186
10187 /**
10188 * This method is used by ViewGroup to cause its children to restore or recreate their
10189 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10190 * to recreate its own display list, which would happen if it went through the normal
10191 * draw/dispatchDraw mechanisms.
10192 *
10193 * @hide
10194 */
10195 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010196
10197 /**
10198 * A view that is not attached or hardware accelerated cannot create a display list.
10199 * This method checks these conditions and returns the appropriate result.
10200 *
10201 * @return true if view has the ability to create a display list, false otherwise.
10202 *
10203 * @hide
10204 */
10205 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010206 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010207 }
Joe Malin32736f02011-01-19 16:14:20 -080010208
Chet Haasedaf98e92011-01-10 14:10:36 -080010209 /**
Romain Guyb051e892010-09-28 19:09:36 -070010210 * <p>Returns a display list that can be used to draw this view again
10211 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010212 *
Romain Guyb051e892010-09-28 19:09:36 -070010213 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010214 *
10215 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010216 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010217 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -080010218 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -070010219 return null;
10220 }
10221
Chet Haasedaf98e92011-01-10 14:10:36 -080010222 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10223 mDisplayList == null || !mDisplayList.isValid() ||
10224 mRecreateDisplayList)) {
10225 // Don't need to recreate the display list, just need to tell our
10226 // children to restore/recreate theirs
10227 if (mDisplayList != null && mDisplayList.isValid() &&
10228 !mRecreateDisplayList) {
10229 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10230 mPrivateFlags &= ~DIRTY_MASK;
10231 dispatchGetDisplayList();
10232
10233 return mDisplayList;
10234 }
10235
10236 // If we got here, we're recreating it. Mark it as such to ensure that
10237 // we copy in child display lists into ours in drawChild()
10238 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -080010239 if (mDisplayList == null) {
Jeff Brown162a0212011-07-21 17:02:54 -070010240 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList();
Chet Haasedaf98e92011-01-10 14:10:36 -080010241 // If we're creating a new display list, make sure our parent gets invalidated
10242 // since they will need to recreate their display list to account for this
10243 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -080010244 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -080010245 }
Romain Guyb051e892010-09-28 19:09:36 -070010246
10247 final HardwareCanvas canvas = mDisplayList.start();
Romain Guye080af32011-09-08 15:03:39 -070010248 int restoreCount = 0;
Romain Guyb051e892010-09-28 19:09:36 -070010249 try {
10250 int width = mRight - mLeft;
10251 int height = mBottom - mTop;
10252
10253 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -080010254 // The dirty rect should always be null for a display list
10255 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -070010256
Chet Haasedaf98e92011-01-10 14:10:36 -080010257 computeScroll();
Romain Guye080af32011-09-08 15:03:39 -070010258
10259 restoreCount = canvas.save();
Chet Haasedaf98e92011-01-10 14:10:36 -080010260 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010261 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -070010262 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -080010263
Romain Guyb051e892010-09-28 19:09:36 -070010264 // Fast path for layouts with no backgrounds
10265 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -070010266 dispatchDraw(canvas);
10267 } else {
10268 draw(canvas);
10269 }
Romain Guyb051e892010-09-28 19:09:36 -070010270 } finally {
Romain Guye080af32011-09-08 15:03:39 -070010271 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -070010272 canvas.onPostDraw();
10273
10274 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -070010275 }
Chet Haase77785f92011-01-25 23:22:09 -080010276 } else {
10277 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10278 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -070010279 }
10280
10281 return mDisplayList;
10282 }
10283
10284 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010285 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010286 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010287 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010288 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010289 * @see #getDrawingCache(boolean)
10290 */
10291 public Bitmap getDrawingCache() {
10292 return getDrawingCache(false);
10293 }
10294
10295 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010296 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10297 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10298 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10299 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10300 * request the drawing cache by calling this method and draw it on screen if the
10301 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010302 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010303 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10304 * this method will create a bitmap of the same size as this view. Because this bitmap
10305 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10306 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10307 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10308 * size than the view. This implies that your application must be able to handle this
10309 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010310 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010311 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10312 * the current density of the screen when the application is in compatibility
10313 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010314 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010315 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010316 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010317 * @see #setDrawingCacheEnabled(boolean)
10318 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010319 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010320 * @see #destroyDrawingCache()
10321 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010322 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010323 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10324 return null;
10325 }
10326 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010327 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010328 }
Romain Guy02890fd2010-08-06 17:58:44 -070010329 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010330 }
10331
10332 /**
10333 * <p>Frees the resources used by the drawing cache. If you call
10334 * {@link #buildDrawingCache()} manually without calling
10335 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10336 * should cleanup the cache with this method afterwards.</p>
10337 *
10338 * @see #setDrawingCacheEnabled(boolean)
10339 * @see #buildDrawingCache()
10340 * @see #getDrawingCache()
10341 */
10342 public void destroyDrawingCache() {
10343 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010344 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010345 mDrawingCache = null;
10346 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010347 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010348 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010349 mUnscaledDrawingCache = null;
10350 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010351 }
10352
10353 /**
10354 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070010355 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010356 * view will always be drawn on top of a solid color.
10357 *
10358 * @param color The background color to use for the drawing cache's bitmap
10359 *
10360 * @see #setDrawingCacheEnabled(boolean)
10361 * @see #buildDrawingCache()
10362 * @see #getDrawingCache()
10363 */
10364 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010365 if (color != mDrawingCacheBackgroundColor) {
10366 mDrawingCacheBackgroundColor = color;
10367 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10368 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010369 }
10370
10371 /**
10372 * @see #setDrawingCacheBackgroundColor(int)
10373 *
10374 * @return The background color to used for the drawing cache's bitmap
10375 */
10376 public int getDrawingCacheBackgroundColor() {
10377 return mDrawingCacheBackgroundColor;
10378 }
10379
10380 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010381 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010382 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010383 * @see #buildDrawingCache(boolean)
10384 */
10385 public void buildDrawingCache() {
10386 buildDrawingCache(false);
10387 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010388
Romain Guyfbd8f692009-06-26 14:51:58 -070010389 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010390 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10391 *
10392 * <p>If you call {@link #buildDrawingCache()} manually without calling
10393 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10394 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010395 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010396 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10397 * this method will create a bitmap of the same size as this view. Because this bitmap
10398 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10399 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10400 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10401 * size than the view. This implies that your application must be able to handle this
10402 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010403 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010404 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10405 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010406 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010407 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010408 *
10409 * @see #getDrawingCache()
10410 * @see #destroyDrawingCache()
10411 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010412 public void buildDrawingCache(boolean autoScale) {
10413 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010414 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010415 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010416
10417 if (ViewDebug.TRACE_HIERARCHY) {
10418 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010420
Romain Guy8506ab42009-06-11 17:35:47 -070010421 int width = mRight - mLeft;
10422 int height = mBottom - mTop;
10423
10424 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010425 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010426
Romain Guye1123222009-06-29 14:24:56 -070010427 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010428 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10429 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010431
10432 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010433 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010434 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010435
10436 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010437 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010438 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010439 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10440 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010441 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010442 return;
10443 }
10444
10445 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010446 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010447
10448 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010449 Bitmap.Config quality;
10450 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010451 // Never pick ARGB_4444 because it looks awful
10452 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010453 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10454 case DRAWING_CACHE_QUALITY_AUTO:
10455 quality = Bitmap.Config.ARGB_8888;
10456 break;
10457 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010458 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010459 break;
10460 case DRAWING_CACHE_QUALITY_HIGH:
10461 quality = Bitmap.Config.ARGB_8888;
10462 break;
10463 default:
10464 quality = Bitmap.Config.ARGB_8888;
10465 break;
10466 }
10467 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010468 // Optimization for translucent windows
10469 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010470 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010471 }
10472
10473 // Try to cleanup memory
10474 if (bitmap != null) bitmap.recycle();
10475
10476 try {
10477 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010478 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010479 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010480 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010481 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010482 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010483 }
Adam Powell26153a32010-11-08 15:22:27 -080010484 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010485 } catch (OutOfMemoryError e) {
10486 // If there is not enough memory to create the bitmap cache, just
10487 // ignore the issue as bitmap caches are not required to draw the
10488 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010489 if (autoScale) {
10490 mDrawingCache = null;
10491 } else {
10492 mUnscaledDrawingCache = null;
10493 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010494 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010495 return;
10496 }
10497
10498 clear = drawingCacheBackgroundColor != 0;
10499 }
10500
10501 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010502 if (attachInfo != null) {
10503 canvas = attachInfo.mCanvas;
10504 if (canvas == null) {
10505 canvas = new Canvas();
10506 }
10507 canvas.setBitmap(bitmap);
10508 // Temporarily clobber the cached Canvas in case one of our children
10509 // is also using a drawing cache. Without this, the children would
10510 // steal the canvas by attaching their own bitmap to it and bad, bad
10511 // thing would happen (invisible views, corrupted drawings, etc.)
10512 attachInfo.mCanvas = null;
10513 } else {
10514 // This case should hopefully never or seldom happen
10515 canvas = new Canvas(bitmap);
10516 }
10517
10518 if (clear) {
10519 bitmap.eraseColor(drawingCacheBackgroundColor);
10520 }
10521
10522 computeScroll();
10523 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010524
Romain Guye1123222009-06-29 14:24:56 -070010525 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010526 final float scale = attachInfo.mApplicationScale;
10527 canvas.scale(scale, scale);
10528 }
Joe Malin32736f02011-01-19 16:14:20 -080010529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010530 canvas.translate(-mScrollX, -mScrollY);
10531
Romain Guy5bcdff42009-05-14 21:27:18 -070010532 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010533 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10534 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010535 mPrivateFlags |= DRAWING_CACHE_VALID;
10536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010537
10538 // Fast path for layouts with no backgrounds
10539 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10540 if (ViewDebug.TRACE_HIERARCHY) {
10541 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10542 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010543 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010544 dispatchDraw(canvas);
10545 } else {
10546 draw(canvas);
10547 }
10548
10549 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010550 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010551
10552 if (attachInfo != null) {
10553 // Restore the cached Canvas for our siblings
10554 attachInfo.mCanvas = canvas;
10555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010556 }
10557 }
10558
10559 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010560 * Create a snapshot of the view into a bitmap. We should probably make
10561 * some form of this public, but should think about the API.
10562 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010563 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010564 int width = mRight - mLeft;
10565 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010566
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010567 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010568 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010569 width = (int) ((width * scale) + 0.5f);
10570 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010571
Romain Guy8c11e312009-09-14 15:15:30 -070010572 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010573 if (bitmap == null) {
10574 throw new OutOfMemoryError();
10575 }
10576
Romain Guyc529d8d2011-09-06 15:01:39 -070010577 Resources resources = getResources();
10578 if (resources != null) {
10579 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
10580 }
Joe Malin32736f02011-01-19 16:14:20 -080010581
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010582 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010583 if (attachInfo != null) {
10584 canvas = attachInfo.mCanvas;
10585 if (canvas == null) {
10586 canvas = new Canvas();
10587 }
10588 canvas.setBitmap(bitmap);
10589 // Temporarily clobber the cached Canvas in case one of our children
10590 // is also using a drawing cache. Without this, the children would
10591 // steal the canvas by attaching their own bitmap to it and bad, bad
10592 // things would happen (invisible views, corrupted drawings, etc.)
10593 attachInfo.mCanvas = null;
10594 } else {
10595 // This case should hopefully never or seldom happen
10596 canvas = new Canvas(bitmap);
10597 }
10598
Romain Guy5bcdff42009-05-14 21:27:18 -070010599 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010600 bitmap.eraseColor(backgroundColor);
10601 }
10602
10603 computeScroll();
10604 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010605 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010606 canvas.translate(-mScrollX, -mScrollY);
10607
Romain Guy5bcdff42009-05-14 21:27:18 -070010608 // Temporarily remove the dirty mask
10609 int flags = mPrivateFlags;
10610 mPrivateFlags &= ~DIRTY_MASK;
10611
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010612 // Fast path for layouts with no backgrounds
10613 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10614 dispatchDraw(canvas);
10615 } else {
10616 draw(canvas);
10617 }
10618
Romain Guy5bcdff42009-05-14 21:27:18 -070010619 mPrivateFlags = flags;
10620
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010621 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010622 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010623
10624 if (attachInfo != null) {
10625 // Restore the cached Canvas for our siblings
10626 attachInfo.mCanvas = canvas;
10627 }
Romain Guy8506ab42009-06-11 17:35:47 -070010628
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010629 return bitmap;
10630 }
10631
10632 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010633 * Indicates whether this View is currently in edit mode. A View is usually
10634 * in edit mode when displayed within a developer tool. For instance, if
10635 * this View is being drawn by a visual user interface builder, this method
10636 * should return true.
10637 *
10638 * Subclasses should check the return value of this method to provide
10639 * different behaviors if their normal behavior might interfere with the
10640 * host environment. For instance: the class spawns a thread in its
10641 * constructor, the drawing code relies on device-specific features, etc.
10642 *
10643 * This method is usually checked in the drawing code of custom widgets.
10644 *
10645 * @return True if this View is in edit mode, false otherwise.
10646 */
10647 public boolean isInEditMode() {
10648 return false;
10649 }
10650
10651 /**
10652 * If the View draws content inside its padding and enables fading edges,
10653 * it needs to support padding offsets. Padding offsets are added to the
10654 * fading edges to extend the length of the fade so that it covers pixels
10655 * drawn inside the padding.
10656 *
10657 * Subclasses of this class should override this method if they need
10658 * to draw content inside the padding.
10659 *
10660 * @return True if padding offset must be applied, false otherwise.
10661 *
10662 * @see #getLeftPaddingOffset()
10663 * @see #getRightPaddingOffset()
10664 * @see #getTopPaddingOffset()
10665 * @see #getBottomPaddingOffset()
10666 *
10667 * @since CURRENT
10668 */
10669 protected boolean isPaddingOffsetRequired() {
10670 return false;
10671 }
10672
10673 /**
10674 * Amount by which to extend the left fading region. Called only when
10675 * {@link #isPaddingOffsetRequired()} returns true.
10676 *
10677 * @return The left padding offset in pixels.
10678 *
10679 * @see #isPaddingOffsetRequired()
10680 *
10681 * @since CURRENT
10682 */
10683 protected int getLeftPaddingOffset() {
10684 return 0;
10685 }
10686
10687 /**
10688 * Amount by which to extend the right fading region. Called only when
10689 * {@link #isPaddingOffsetRequired()} returns true.
10690 *
10691 * @return The right padding offset in pixels.
10692 *
10693 * @see #isPaddingOffsetRequired()
10694 *
10695 * @since CURRENT
10696 */
10697 protected int getRightPaddingOffset() {
10698 return 0;
10699 }
10700
10701 /**
10702 * Amount by which to extend the top fading region. Called only when
10703 * {@link #isPaddingOffsetRequired()} returns true.
10704 *
10705 * @return The top padding offset in pixels.
10706 *
10707 * @see #isPaddingOffsetRequired()
10708 *
10709 * @since CURRENT
10710 */
10711 protected int getTopPaddingOffset() {
10712 return 0;
10713 }
10714
10715 /**
10716 * Amount by which to extend the bottom fading region. Called only when
10717 * {@link #isPaddingOffsetRequired()} returns true.
10718 *
10719 * @return The bottom padding offset in pixels.
10720 *
10721 * @see #isPaddingOffsetRequired()
10722 *
10723 * @since CURRENT
10724 */
10725 protected int getBottomPaddingOffset() {
10726 return 0;
10727 }
10728
10729 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070010730 * @hide
10731 * @param offsetRequired
10732 */
10733 protected int getFadeTop(boolean offsetRequired) {
10734 int top = mPaddingTop;
10735 if (offsetRequired) top += getTopPaddingOffset();
10736 return top;
10737 }
10738
10739 /**
10740 * @hide
10741 * @param offsetRequired
10742 */
10743 protected int getFadeHeight(boolean offsetRequired) {
10744 int padding = mPaddingTop;
10745 if (offsetRequired) padding += getTopPaddingOffset();
10746 return mBottom - mTop - mPaddingBottom - padding;
10747 }
10748
10749 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010750 * <p>Indicates whether this view is attached to an hardware accelerated
10751 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010752 *
Romain Guy2bffd262010-09-12 17:40:02 -070010753 * <p>Even if this method returns true, it does not mean that every call
10754 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10755 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10756 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10757 * window is hardware accelerated,
10758 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10759 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010760 *
Romain Guy2bffd262010-09-12 17:40:02 -070010761 * @return True if the view is attached to a window and the window is
10762 * hardware accelerated; false in any other case.
10763 */
10764 public boolean isHardwareAccelerated() {
10765 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10766 }
Joe Malin32736f02011-01-19 16:14:20 -080010767
Romain Guy2bffd262010-09-12 17:40:02 -070010768 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010769 * Manually render this view (and all of its children) to the given Canvas.
10770 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010771 * called. When implementing a view, implement
10772 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
10773 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010774 *
10775 * @param canvas The Canvas to which the View is rendered.
10776 */
10777 public void draw(Canvas canvas) {
10778 if (ViewDebug.TRACE_HIERARCHY) {
10779 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10780 }
10781
Romain Guy5bcdff42009-05-14 21:27:18 -070010782 final int privateFlags = mPrivateFlags;
10783 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
10784 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
10785 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070010786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010787 /*
10788 * Draw traversal performs several drawing steps which must be executed
10789 * in the appropriate order:
10790 *
10791 * 1. Draw the background
10792 * 2. If necessary, save the canvas' layers to prepare for fading
10793 * 3. Draw view's content
10794 * 4. Draw children
10795 * 5. If necessary, draw the fading edges and restore layers
10796 * 6. Draw decorations (scrollbars for instance)
10797 */
10798
10799 // Step 1, draw the background, if needed
10800 int saveCount;
10801
Romain Guy24443ea2009-05-11 11:56:30 -070010802 if (!dirtyOpaque) {
10803 final Drawable background = mBGDrawable;
10804 if (background != null) {
10805 final int scrollX = mScrollX;
10806 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010807
Romain Guy24443ea2009-05-11 11:56:30 -070010808 if (mBackgroundSizeChanged) {
10809 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
10810 mBackgroundSizeChanged = false;
10811 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010812
Romain Guy24443ea2009-05-11 11:56:30 -070010813 if ((scrollX | scrollY) == 0) {
10814 background.draw(canvas);
10815 } else {
10816 canvas.translate(scrollX, scrollY);
10817 background.draw(canvas);
10818 canvas.translate(-scrollX, -scrollY);
10819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010820 }
10821 }
10822
10823 // skip step 2 & 5 if possible (common case)
10824 final int viewFlags = mViewFlags;
10825 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
10826 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
10827 if (!verticalEdges && !horizontalEdges) {
10828 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010829 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010830
10831 // Step 4, draw the children
10832 dispatchDraw(canvas);
10833
10834 // Step 6, draw decorations (scrollbars)
10835 onDrawScrollBars(canvas);
10836
10837 // we're done...
10838 return;
10839 }
10840
10841 /*
10842 * Here we do the full fledged routine...
10843 * (this is an uncommon case where speed matters less,
10844 * this is why we repeat some of the tests that have been
10845 * done above)
10846 */
10847
10848 boolean drawTop = false;
10849 boolean drawBottom = false;
10850 boolean drawLeft = false;
10851 boolean drawRight = false;
10852
10853 float topFadeStrength = 0.0f;
10854 float bottomFadeStrength = 0.0f;
10855 float leftFadeStrength = 0.0f;
10856 float rightFadeStrength = 0.0f;
10857
10858 // Step 2, save the canvas' layers
10859 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010860
10861 final boolean offsetRequired = isPaddingOffsetRequired();
10862 if (offsetRequired) {
10863 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010864 }
10865
10866 int left = mScrollX + paddingLeft;
10867 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070010868 int top = mScrollY + getFadeTop(offsetRequired);
10869 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010870
10871 if (offsetRequired) {
10872 right += getRightPaddingOffset();
10873 bottom += getBottomPaddingOffset();
10874 }
10875
10876 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010877 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
10878 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010879
10880 // clip the fade length if top and bottom fades overlap
10881 // overlapping fades produce odd-looking artifacts
10882 if (verticalEdges && (top + length > bottom - length)) {
10883 length = (bottom - top) / 2;
10884 }
10885
10886 // also clip horizontal fades if necessary
10887 if (horizontalEdges && (left + length > right - length)) {
10888 length = (right - left) / 2;
10889 }
10890
10891 if (verticalEdges) {
10892 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010893 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010894 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010895 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010896 }
10897
10898 if (horizontalEdges) {
10899 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010900 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010901 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010902 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010903 }
10904
10905 saveCount = canvas.getSaveCount();
10906
10907 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070010908 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010909 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
10910
10911 if (drawTop) {
10912 canvas.saveLayer(left, top, right, top + length, null, flags);
10913 }
10914
10915 if (drawBottom) {
10916 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
10917 }
10918
10919 if (drawLeft) {
10920 canvas.saveLayer(left, top, left + length, bottom, null, flags);
10921 }
10922
10923 if (drawRight) {
10924 canvas.saveLayer(right - length, top, right, bottom, null, flags);
10925 }
10926 } else {
10927 scrollabilityCache.setFadeColor(solidColor);
10928 }
10929
10930 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010931 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010932
10933 // Step 4, draw the children
10934 dispatchDraw(canvas);
10935
10936 // Step 5, draw the fade effect and restore layers
10937 final Paint p = scrollabilityCache.paint;
10938 final Matrix matrix = scrollabilityCache.matrix;
10939 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010940
10941 if (drawTop) {
10942 matrix.setScale(1, fadeHeight * topFadeStrength);
10943 matrix.postTranslate(left, top);
10944 fade.setLocalMatrix(matrix);
10945 canvas.drawRect(left, top, right, top + length, p);
10946 }
10947
10948 if (drawBottom) {
10949 matrix.setScale(1, fadeHeight * bottomFadeStrength);
10950 matrix.postRotate(180);
10951 matrix.postTranslate(left, bottom);
10952 fade.setLocalMatrix(matrix);
10953 canvas.drawRect(left, bottom - length, right, bottom, p);
10954 }
10955
10956 if (drawLeft) {
10957 matrix.setScale(1, fadeHeight * leftFadeStrength);
10958 matrix.postRotate(-90);
10959 matrix.postTranslate(left, top);
10960 fade.setLocalMatrix(matrix);
10961 canvas.drawRect(left, top, left + length, bottom, p);
10962 }
10963
10964 if (drawRight) {
10965 matrix.setScale(1, fadeHeight * rightFadeStrength);
10966 matrix.postRotate(90);
10967 matrix.postTranslate(right, top);
10968 fade.setLocalMatrix(matrix);
10969 canvas.drawRect(right - length, top, right, bottom, p);
10970 }
10971
10972 canvas.restoreToCount(saveCount);
10973
10974 // Step 6, draw decorations (scrollbars)
10975 onDrawScrollBars(canvas);
10976 }
10977
10978 /**
10979 * Override this if your view is known to always be drawn on top of a solid color background,
10980 * and needs to draw fading edges. Returning a non-zero color enables the view system to
10981 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
10982 * should be set to 0xFF.
10983 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010984 * @see #setVerticalFadingEdgeEnabled(boolean)
10985 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010986 *
10987 * @return The known solid color background for this view, or 0 if the color may vary
10988 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010989 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010990 public int getSolidColor() {
10991 return 0;
10992 }
10993
10994 /**
10995 * Build a human readable string representation of the specified view flags.
10996 *
10997 * @param flags the view flags to convert to a string
10998 * @return a String representing the supplied flags
10999 */
11000 private static String printFlags(int flags) {
11001 String output = "";
11002 int numFlags = 0;
11003 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
11004 output += "TAKES_FOCUS";
11005 numFlags++;
11006 }
11007
11008 switch (flags & VISIBILITY_MASK) {
11009 case INVISIBLE:
11010 if (numFlags > 0) {
11011 output += " ";
11012 }
11013 output += "INVISIBLE";
11014 // USELESS HERE numFlags++;
11015 break;
11016 case GONE:
11017 if (numFlags > 0) {
11018 output += " ";
11019 }
11020 output += "GONE";
11021 // USELESS HERE numFlags++;
11022 break;
11023 default:
11024 break;
11025 }
11026 return output;
11027 }
11028
11029 /**
11030 * Build a human readable string representation of the specified private
11031 * view flags.
11032 *
11033 * @param privateFlags the private view flags to convert to a string
11034 * @return a String representing the supplied flags
11035 */
11036 private static String printPrivateFlags(int privateFlags) {
11037 String output = "";
11038 int numFlags = 0;
11039
11040 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
11041 output += "WANTS_FOCUS";
11042 numFlags++;
11043 }
11044
11045 if ((privateFlags & FOCUSED) == FOCUSED) {
11046 if (numFlags > 0) {
11047 output += " ";
11048 }
11049 output += "FOCUSED";
11050 numFlags++;
11051 }
11052
11053 if ((privateFlags & SELECTED) == SELECTED) {
11054 if (numFlags > 0) {
11055 output += " ";
11056 }
11057 output += "SELECTED";
11058 numFlags++;
11059 }
11060
11061 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
11062 if (numFlags > 0) {
11063 output += " ";
11064 }
11065 output += "IS_ROOT_NAMESPACE";
11066 numFlags++;
11067 }
11068
11069 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
11070 if (numFlags > 0) {
11071 output += " ";
11072 }
11073 output += "HAS_BOUNDS";
11074 numFlags++;
11075 }
11076
11077 if ((privateFlags & DRAWN) == DRAWN) {
11078 if (numFlags > 0) {
11079 output += " ";
11080 }
11081 output += "DRAWN";
11082 // USELESS HERE numFlags++;
11083 }
11084 return output;
11085 }
11086
11087 /**
11088 * <p>Indicates whether or not this view's layout will be requested during
11089 * the next hierarchy layout pass.</p>
11090 *
11091 * @return true if the layout will be forced during next layout pass
11092 */
11093 public boolean isLayoutRequested() {
11094 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
11095 }
11096
11097 /**
11098 * Assign a size and position to a view and all of its
11099 * descendants
11100 *
11101 * <p>This is the second phase of the layout mechanism.
11102 * (The first is measuring). In this phase, each parent calls
11103 * layout on all of its children to position them.
11104 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080011105 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011106 *
Chet Haase9c087442011-01-12 16:20:16 -080011107 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011108 * Derived classes with children should override
11109 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080011110 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011111 *
11112 * @param l Left position, relative to parent
11113 * @param t Top position, relative to parent
11114 * @param r Right position, relative to parent
11115 * @param b Bottom position, relative to parent
11116 */
Romain Guy5429e1d2010-09-07 12:38:00 -070011117 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080011118 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070011119 int oldL = mLeft;
11120 int oldT = mTop;
11121 int oldB = mBottom;
11122 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011123 boolean changed = setFrame(l, t, r, b);
11124 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
11125 if (ViewDebug.TRACE_HIERARCHY) {
11126 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
11127 }
11128
11129 onLayout(changed, l, t, r, b);
11130 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070011131
11132 if (mOnLayoutChangeListeners != null) {
11133 ArrayList<OnLayoutChangeListener> listenersCopy =
11134 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
11135 int numListeners = listenersCopy.size();
11136 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070011137 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070011138 }
11139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011140 }
11141 mPrivateFlags &= ~FORCE_LAYOUT;
11142 }
11143
11144 /**
11145 * Called from layout when this view should
11146 * assign a size and position to each of its children.
11147 *
11148 * Derived classes with children should override
11149 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070011150 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011151 * @param changed This is a new size or position for this view
11152 * @param left Left position, relative to parent
11153 * @param top Top position, relative to parent
11154 * @param right Right position, relative to parent
11155 * @param bottom Bottom position, relative to parent
11156 */
11157 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
11158 }
11159
11160 /**
11161 * Assign a size and position to this view.
11162 *
11163 * This is called from layout.
11164 *
11165 * @param left Left position, relative to parent
11166 * @param top Top position, relative to parent
11167 * @param right Right position, relative to parent
11168 * @param bottom Bottom position, relative to parent
11169 * @return true if the new size and position are different than the
11170 * previous ones
11171 * {@hide}
11172 */
11173 protected boolean setFrame(int left, int top, int right, int bottom) {
11174 boolean changed = false;
11175
11176 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070011177 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011178 + right + "," + bottom + ")");
11179 }
11180
11181 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
11182 changed = true;
11183
11184 // Remember our drawn bit
11185 int drawn = mPrivateFlags & DRAWN;
11186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011187 int oldWidth = mRight - mLeft;
11188 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070011189 int newWidth = right - left;
11190 int newHeight = bottom - top;
11191 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
11192
11193 // Invalidate our old position
11194 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011195
11196 mLeft = left;
11197 mTop = top;
11198 mRight = right;
11199 mBottom = bottom;
11200
11201 mPrivateFlags |= HAS_BOUNDS;
11202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011203
Chet Haase75755e22011-07-18 17:48:25 -070011204 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011205 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
11206 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070011207 if (mTransformationInfo != null) {
11208 mTransformationInfo.mMatrixDirty = true;
11209 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011211 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
11212 }
11213
11214 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
11215 // If we are visible, force the DRAWN bit to on so that
11216 // this invalidate will go through (at least to our parent).
11217 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011218 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011219 // the DRAWN bit.
11220 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070011221 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080011222 // parent display list may need to be recreated based on a change in the bounds
11223 // of any child
11224 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011225 }
11226
11227 // Reset drawn bit to original value (invalidate turns it off)
11228 mPrivateFlags |= drawn;
11229
11230 mBackgroundSizeChanged = true;
11231 }
11232 return changed;
11233 }
11234
11235 /**
11236 * Finalize inflating a view from XML. This is called as the last phase
11237 * of inflation, after all child views have been added.
11238 *
11239 * <p>Even if the subclass overrides onFinishInflate, they should always be
11240 * sure to call the super method, so that we get called.
11241 */
11242 protected void onFinishInflate() {
11243 }
11244
11245 /**
11246 * Returns the resources associated with this view.
11247 *
11248 * @return Resources object.
11249 */
11250 public Resources getResources() {
11251 return mResources;
11252 }
11253
11254 /**
11255 * Invalidates the specified Drawable.
11256 *
11257 * @param drawable the drawable to invalidate
11258 */
11259 public void invalidateDrawable(Drawable drawable) {
11260 if (verifyDrawable(drawable)) {
11261 final Rect dirty = drawable.getBounds();
11262 final int scrollX = mScrollX;
11263 final int scrollY = mScrollY;
11264
11265 invalidate(dirty.left + scrollX, dirty.top + scrollY,
11266 dirty.right + scrollX, dirty.bottom + scrollY);
11267 }
11268 }
11269
11270 /**
11271 * Schedules an action on a drawable to occur at a specified time.
11272 *
11273 * @param who the recipient of the action
11274 * @param what the action to run on the drawable
11275 * @param when the time at which the action must occur. Uses the
11276 * {@link SystemClock#uptimeMillis} timebase.
11277 */
11278 public void scheduleDrawable(Drawable who, Runnable what, long when) {
11279 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
11280 mAttachInfo.mHandler.postAtTime(what, who, when);
11281 }
11282 }
11283
11284 /**
11285 * Cancels a scheduled action on a drawable.
11286 *
11287 * @param who the recipient of the action
11288 * @param what the action to cancel
11289 */
11290 public void unscheduleDrawable(Drawable who, Runnable what) {
11291 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
11292 mAttachInfo.mHandler.removeCallbacks(what, who);
11293 }
11294 }
11295
11296 /**
11297 * Unschedule any events associated with the given Drawable. This can be
11298 * used when selecting a new Drawable into a view, so that the previous
11299 * one is completely unscheduled.
11300 *
11301 * @param who The Drawable to unschedule.
11302 *
11303 * @see #drawableStateChanged
11304 */
11305 public void unscheduleDrawable(Drawable who) {
11306 if (mAttachInfo != null) {
11307 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
11308 }
11309 }
11310
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011311 /**
11312 * Return the layout direction of a given Drawable.
11313 *
11314 * @param who the Drawable to query
11315 *
11316 * @hide
11317 */
11318 public int getResolvedLayoutDirection(Drawable who) {
11319 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070011320 }
11321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011322 /**
11323 * If your view subclass is displaying its own Drawable objects, it should
11324 * override this function and return true for any Drawable it is
11325 * displaying. This allows animations for those drawables to be
11326 * scheduled.
11327 *
11328 * <p>Be sure to call through to the super class when overriding this
11329 * function.
11330 *
11331 * @param who The Drawable to verify. Return true if it is one you are
11332 * displaying, else return the result of calling through to the
11333 * super class.
11334 *
11335 * @return boolean If true than the Drawable is being displayed in the
11336 * view; else false and it is not allowed to animate.
11337 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011338 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
11339 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011340 */
11341 protected boolean verifyDrawable(Drawable who) {
11342 return who == mBGDrawable;
11343 }
11344
11345 /**
11346 * This function is called whenever the state of the view changes in such
11347 * a way that it impacts the state of drawables being shown.
11348 *
11349 * <p>Be sure to call through to the superclass when overriding this
11350 * function.
11351 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011352 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011353 */
11354 protected void drawableStateChanged() {
11355 Drawable d = mBGDrawable;
11356 if (d != null && d.isStateful()) {
11357 d.setState(getDrawableState());
11358 }
11359 }
11360
11361 /**
11362 * Call this to force a view to update its drawable state. This will cause
11363 * drawableStateChanged to be called on this view. Views that are interested
11364 * in the new state should call getDrawableState.
11365 *
11366 * @see #drawableStateChanged
11367 * @see #getDrawableState
11368 */
11369 public void refreshDrawableState() {
11370 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
11371 drawableStateChanged();
11372
11373 ViewParent parent = mParent;
11374 if (parent != null) {
11375 parent.childDrawableStateChanged(this);
11376 }
11377 }
11378
11379 /**
11380 * Return an array of resource IDs of the drawable states representing the
11381 * current state of the view.
11382 *
11383 * @return The current drawable state
11384 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011385 * @see Drawable#setState(int[])
11386 * @see #drawableStateChanged()
11387 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011388 */
11389 public final int[] getDrawableState() {
11390 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
11391 return mDrawableState;
11392 } else {
11393 mDrawableState = onCreateDrawableState(0);
11394 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
11395 return mDrawableState;
11396 }
11397 }
11398
11399 /**
11400 * Generate the new {@link android.graphics.drawable.Drawable} state for
11401 * this view. This is called by the view
11402 * system when the cached Drawable state is determined to be invalid. To
11403 * retrieve the current state, you should use {@link #getDrawableState}.
11404 *
11405 * @param extraSpace if non-zero, this is the number of extra entries you
11406 * would like in the returned array in which you can place your own
11407 * states.
11408 *
11409 * @return Returns an array holding the current {@link Drawable} state of
11410 * the view.
11411 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011412 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011413 */
11414 protected int[] onCreateDrawableState(int extraSpace) {
11415 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
11416 mParent instanceof View) {
11417 return ((View) mParent).onCreateDrawableState(extraSpace);
11418 }
11419
11420 int[] drawableState;
11421
11422 int privateFlags = mPrivateFlags;
11423
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011424 int viewStateIndex = 0;
11425 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
11426 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
11427 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070011428 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011429 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
11430 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070011431 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
11432 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011433 // This is set if HW acceleration is requested, even if the current
11434 // process doesn't allow it. This is just to allow app preview
11435 // windows to better match their app.
11436 viewStateIndex |= VIEW_STATE_ACCELERATED;
11437 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070011438 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011439
Christopher Tate3d4bf172011-03-28 16:16:46 -070011440 final int privateFlags2 = mPrivateFlags2;
11441 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
11442 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
11443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011444 drawableState = VIEW_STATE_SETS[viewStateIndex];
11445
11446 //noinspection ConstantIfStatement
11447 if (false) {
11448 Log.i("View", "drawableStateIndex=" + viewStateIndex);
11449 Log.i("View", toString()
11450 + " pressed=" + ((privateFlags & PRESSED) != 0)
11451 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
11452 + " fo=" + hasFocus()
11453 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011454 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011455 + ": " + Arrays.toString(drawableState));
11456 }
11457
11458 if (extraSpace == 0) {
11459 return drawableState;
11460 }
11461
11462 final int[] fullState;
11463 if (drawableState != null) {
11464 fullState = new int[drawableState.length + extraSpace];
11465 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
11466 } else {
11467 fullState = new int[extraSpace];
11468 }
11469
11470 return fullState;
11471 }
11472
11473 /**
11474 * Merge your own state values in <var>additionalState</var> into the base
11475 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011476 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011477 *
11478 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011479 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011480 * own additional state values.
11481 *
11482 * @param additionalState The additional state values you would like
11483 * added to <var>baseState</var>; this array is not modified.
11484 *
11485 * @return As a convenience, the <var>baseState</var> array you originally
11486 * passed into the function is returned.
11487 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011488 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011489 */
11490 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
11491 final int N = baseState.length;
11492 int i = N - 1;
11493 while (i >= 0 && baseState[i] == 0) {
11494 i--;
11495 }
11496 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
11497 return baseState;
11498 }
11499
11500 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070011501 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
11502 * on all Drawable objects associated with this view.
11503 */
11504 public void jumpDrawablesToCurrentState() {
11505 if (mBGDrawable != null) {
11506 mBGDrawable.jumpToCurrentState();
11507 }
11508 }
11509
11510 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011511 * Sets the background color for this view.
11512 * @param color the color of the background
11513 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011514 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011515 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070011516 if (mBGDrawable instanceof ColorDrawable) {
11517 ((ColorDrawable) mBGDrawable).setColor(color);
11518 } else {
11519 setBackgroundDrawable(new ColorDrawable(color));
11520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011521 }
11522
11523 /**
11524 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070011525 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011526 * @param resid The identifier of the resource.
11527 * @attr ref android.R.styleable#View_background
11528 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011529 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011530 public void setBackgroundResource(int resid) {
11531 if (resid != 0 && resid == mBackgroundResource) {
11532 return;
11533 }
11534
11535 Drawable d= null;
11536 if (resid != 0) {
11537 d = mResources.getDrawable(resid);
11538 }
11539 setBackgroundDrawable(d);
11540
11541 mBackgroundResource = resid;
11542 }
11543
11544 /**
11545 * Set the background to a given Drawable, or remove the background. If the
11546 * background has padding, this View's padding is set to the background's
11547 * padding. However, when a background is removed, this View's padding isn't
11548 * touched. If setting the padding is desired, please use
11549 * {@link #setPadding(int, int, int, int)}.
11550 *
11551 * @param d The Drawable to use as the background, or null to remove the
11552 * background
11553 */
11554 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070011555 if (d == mBGDrawable) {
11556 return;
11557 }
11558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011559 boolean requestLayout = false;
11560
11561 mBackgroundResource = 0;
11562
11563 /*
11564 * Regardless of whether we're setting a new background or not, we want
11565 * to clear the previous drawable.
11566 */
11567 if (mBGDrawable != null) {
11568 mBGDrawable.setCallback(null);
11569 unscheduleDrawable(mBGDrawable);
11570 }
11571
11572 if (d != null) {
11573 Rect padding = sThreadLocal.get();
11574 if (padding == null) {
11575 padding = new Rect();
11576 sThreadLocal.set(padding);
11577 }
11578 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011579 switch (d.getResolvedLayoutDirectionSelf()) {
11580 case LAYOUT_DIRECTION_RTL:
11581 setPadding(padding.right, padding.top, padding.left, padding.bottom);
11582 break;
11583 case LAYOUT_DIRECTION_LTR:
11584 default:
11585 setPadding(padding.left, padding.top, padding.right, padding.bottom);
11586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011587 }
11588
11589 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
11590 // if it has a different minimum size, we should layout again
11591 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
11592 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
11593 requestLayout = true;
11594 }
11595
11596 d.setCallback(this);
11597 if (d.isStateful()) {
11598 d.setState(getDrawableState());
11599 }
11600 d.setVisible(getVisibility() == VISIBLE, false);
11601 mBGDrawable = d;
11602
11603 if ((mPrivateFlags & SKIP_DRAW) != 0) {
11604 mPrivateFlags &= ~SKIP_DRAW;
11605 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
11606 requestLayout = true;
11607 }
11608 } else {
11609 /* Remove the background */
11610 mBGDrawable = null;
11611
11612 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
11613 /*
11614 * This view ONLY drew the background before and we're removing
11615 * the background, so now it won't draw anything
11616 * (hence we SKIP_DRAW)
11617 */
11618 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
11619 mPrivateFlags |= SKIP_DRAW;
11620 }
11621
11622 /*
11623 * When the background is set, we try to apply its padding to this
11624 * View. When the background is removed, we don't touch this View's
11625 * padding. This is noted in the Javadocs. Hence, we don't need to
11626 * requestLayout(), the invalidate() below is sufficient.
11627 */
11628
11629 // The old background's minimum size could have affected this
11630 // View's layout, so let's requestLayout
11631 requestLayout = true;
11632 }
11633
Romain Guy8f1344f52009-05-15 16:03:59 -070011634 computeOpaqueFlags();
11635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011636 if (requestLayout) {
11637 requestLayout();
11638 }
11639
11640 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011641 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011642 }
11643
11644 /**
11645 * Gets the background drawable
11646 * @return The drawable used as the background for this view, if any.
11647 */
11648 public Drawable getBackground() {
11649 return mBGDrawable;
11650 }
11651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011652 /**
11653 * Sets the padding. The view may add on the space required to display
11654 * the scrollbars, depending on the style and visibility of the scrollbars.
11655 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
11656 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
11657 * from the values set in this call.
11658 *
11659 * @attr ref android.R.styleable#View_padding
11660 * @attr ref android.R.styleable#View_paddingBottom
11661 * @attr ref android.R.styleable#View_paddingLeft
11662 * @attr ref android.R.styleable#View_paddingRight
11663 * @attr ref android.R.styleable#View_paddingTop
11664 * @param left the left padding in pixels
11665 * @param top the top padding in pixels
11666 * @param right the right padding in pixels
11667 * @param bottom the bottom padding in pixels
11668 */
11669 public void setPadding(int left, int top, int right, int bottom) {
11670 boolean changed = false;
11671
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011672 mUserPaddingRelative = false;
11673
Adam Powell20232d02010-12-08 21:08:53 -080011674 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011675 mUserPaddingRight = right;
11676 mUserPaddingBottom = bottom;
11677
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011678 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070011679
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011680 // Common case is there are no scroll bars.
11681 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011682 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080011683 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011684 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080011685 switch (mVerticalScrollbarPosition) {
11686 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011687 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11688 left += offset;
11689 } else {
11690 right += offset;
11691 }
11692 break;
Adam Powell20232d02010-12-08 21:08:53 -080011693 case SCROLLBAR_POSITION_RIGHT:
11694 right += offset;
11695 break;
11696 case SCROLLBAR_POSITION_LEFT:
11697 left += offset;
11698 break;
11699 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011700 }
Adam Powell20232d02010-12-08 21:08:53 -080011701 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011702 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
11703 ? 0 : getHorizontalScrollbarHeight();
11704 }
11705 }
Romain Guy8506ab42009-06-11 17:35:47 -070011706
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011707 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011708 changed = true;
11709 mPaddingLeft = left;
11710 }
11711 if (mPaddingTop != top) {
11712 changed = true;
11713 mPaddingTop = top;
11714 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011715 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011716 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011717 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011718 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011719 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011720 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011721 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011722 }
11723
11724 if (changed) {
11725 requestLayout();
11726 }
11727 }
11728
11729 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011730 * Sets the relative padding. The view may add on the space required to display
11731 * the scrollbars, depending on the style and visibility of the scrollbars.
11732 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
11733 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
11734 * from the values set in this call.
11735 *
11736 * @attr ref android.R.styleable#View_padding
11737 * @attr ref android.R.styleable#View_paddingBottom
11738 * @attr ref android.R.styleable#View_paddingStart
11739 * @attr ref android.R.styleable#View_paddingEnd
11740 * @attr ref android.R.styleable#View_paddingTop
11741 * @param start the start padding in pixels
11742 * @param top the top padding in pixels
11743 * @param end the end padding in pixels
11744 * @param bottom the bottom padding in pixels
11745 *
11746 * @hide
11747 */
11748 public void setPaddingRelative(int start, int top, int end, int bottom) {
11749 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070011750
11751 mUserPaddingStart = start;
11752 mUserPaddingEnd = end;
11753
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011754 switch(getResolvedLayoutDirection()) {
11755 case LAYOUT_DIRECTION_RTL:
11756 setPadding(end, top, start, bottom);
11757 break;
11758 case LAYOUT_DIRECTION_LTR:
11759 default:
11760 setPadding(start, top, end, bottom);
11761 }
11762 }
11763
11764 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011765 * Returns the top padding of this view.
11766 *
11767 * @return the top padding in pixels
11768 */
11769 public int getPaddingTop() {
11770 return mPaddingTop;
11771 }
11772
11773 /**
11774 * Returns the bottom padding of this view. If there are inset and enabled
11775 * scrollbars, this value may include the space required to display the
11776 * scrollbars as well.
11777 *
11778 * @return the bottom padding in pixels
11779 */
11780 public int getPaddingBottom() {
11781 return mPaddingBottom;
11782 }
11783
11784 /**
11785 * Returns the left 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 left padding in pixels
11790 */
11791 public int getPaddingLeft() {
11792 return mPaddingLeft;
11793 }
11794
11795 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011796 * Returns the start 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 start padding in pixels
11801 *
11802 * @hide
11803 */
11804 public int getPaddingStart() {
11805 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11806 mPaddingRight : mPaddingLeft;
11807 }
11808
11809 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011810 * Returns the right padding of this view. If there are inset and enabled
11811 * scrollbars, this value may include the space required to display the
11812 * scrollbars as well.
11813 *
11814 * @return the right padding in pixels
11815 */
11816 public int getPaddingRight() {
11817 return mPaddingRight;
11818 }
11819
11820 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011821 * Returns the end padding of this view. If there are inset and enabled
11822 * scrollbars, this value may include the space required to display the
11823 * scrollbars as well.
11824 *
11825 * @return the end padding in pixels
11826 *
11827 * @hide
11828 */
11829 public int getPaddingEnd() {
11830 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11831 mPaddingLeft : mPaddingRight;
11832 }
11833
11834 /**
11835 * Return if the padding as been set thru relative values
11836 * {@link #setPaddingRelative(int, int, int, int)} or thru
11837 * @attr ref android.R.styleable#View_paddingStart or
11838 * @attr ref android.R.styleable#View_paddingEnd
11839 *
11840 * @return true if the padding is relative or false if it is not.
11841 *
11842 * @hide
11843 */
11844 public boolean isPaddingRelative() {
11845 return mUserPaddingRelative;
11846 }
11847
11848 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011849 * Changes the selection state of this view. A view can be selected or not.
11850 * Note that selection is not the same as focus. Views are typically
11851 * selected in the context of an AdapterView like ListView or GridView;
11852 * the selected view is the view that is highlighted.
11853 *
11854 * @param selected true if the view must be selected, false otherwise
11855 */
11856 public void setSelected(boolean selected) {
11857 if (((mPrivateFlags & SELECTED) != 0) != selected) {
11858 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070011859 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080011860 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011861 refreshDrawableState();
11862 dispatchSetSelected(selected);
11863 }
11864 }
11865
11866 /**
11867 * Dispatch setSelected to all of this View's children.
11868 *
11869 * @see #setSelected(boolean)
11870 *
11871 * @param selected The new selected state
11872 */
11873 protected void dispatchSetSelected(boolean selected) {
11874 }
11875
11876 /**
11877 * Indicates the selection state of this view.
11878 *
11879 * @return true if the view is selected, false otherwise
11880 */
11881 @ViewDebug.ExportedProperty
11882 public boolean isSelected() {
11883 return (mPrivateFlags & SELECTED) != 0;
11884 }
11885
11886 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011887 * Changes the activated state of this view. A view can be activated or not.
11888 * Note that activation is not the same as selection. Selection is
11889 * a transient property, representing the view (hierarchy) the user is
11890 * currently interacting with. Activation is a longer-term state that the
11891 * user can move views in and out of. For example, in a list view with
11892 * single or multiple selection enabled, the views in the current selection
11893 * set are activated. (Um, yeah, we are deeply sorry about the terminology
11894 * here.) The activated state is propagated down to children of the view it
11895 * is set on.
11896 *
11897 * @param activated true if the view must be activated, false otherwise
11898 */
11899 public void setActivated(boolean activated) {
11900 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
11901 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011902 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011903 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070011904 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011905 }
11906 }
11907
11908 /**
11909 * Dispatch setActivated to all of this View's children.
11910 *
11911 * @see #setActivated(boolean)
11912 *
11913 * @param activated The new activated state
11914 */
11915 protected void dispatchSetActivated(boolean activated) {
11916 }
11917
11918 /**
11919 * Indicates the activation state of this view.
11920 *
11921 * @return true if the view is activated, false otherwise
11922 */
11923 @ViewDebug.ExportedProperty
11924 public boolean isActivated() {
11925 return (mPrivateFlags & ACTIVATED) != 0;
11926 }
11927
11928 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011929 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
11930 * observer can be used to get notifications when global events, like
11931 * layout, happen.
11932 *
11933 * The returned ViewTreeObserver observer is not guaranteed to remain
11934 * valid for the lifetime of this View. If the caller of this method keeps
11935 * a long-lived reference to ViewTreeObserver, it should always check for
11936 * the return value of {@link ViewTreeObserver#isAlive()}.
11937 *
11938 * @return The ViewTreeObserver for this view's hierarchy.
11939 */
11940 public ViewTreeObserver getViewTreeObserver() {
11941 if (mAttachInfo != null) {
11942 return mAttachInfo.mTreeObserver;
11943 }
11944 if (mFloatingTreeObserver == null) {
11945 mFloatingTreeObserver = new ViewTreeObserver();
11946 }
11947 return mFloatingTreeObserver;
11948 }
11949
11950 /**
11951 * <p>Finds the topmost view in the current view hierarchy.</p>
11952 *
11953 * @return the topmost view containing this view
11954 */
11955 public View getRootView() {
11956 if (mAttachInfo != null) {
11957 final View v = mAttachInfo.mRootView;
11958 if (v != null) {
11959 return v;
11960 }
11961 }
Romain Guy8506ab42009-06-11 17:35:47 -070011962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011963 View parent = this;
11964
11965 while (parent.mParent != null && parent.mParent instanceof View) {
11966 parent = (View) parent.mParent;
11967 }
11968
11969 return parent;
11970 }
11971
11972 /**
11973 * <p>Computes the coordinates of this view on the screen. The argument
11974 * must be an array of two integers. After the method returns, the array
11975 * contains the x and y location in that order.</p>
11976 *
11977 * @param location an array of two integers in which to hold the coordinates
11978 */
11979 public void getLocationOnScreen(int[] location) {
11980 getLocationInWindow(location);
11981
11982 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070011983 if (info != null) {
11984 location[0] += info.mWindowLeft;
11985 location[1] += info.mWindowTop;
11986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011987 }
11988
11989 /**
11990 * <p>Computes the coordinates of this view in its window. The argument
11991 * must be an array of two integers. After the method returns, the array
11992 * contains the x and y location in that order.</p>
11993 *
11994 * @param location an array of two integers in which to hold the coordinates
11995 */
11996 public void getLocationInWindow(int[] location) {
11997 if (location == null || location.length < 2) {
11998 throw new IllegalArgumentException("location must be an array of "
11999 + "two integers");
12000 }
12001
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012002 location[0] = mLeft;
12003 location[1] = mTop;
12004 if (mTransformationInfo != null) {
12005 location[0] += (int) (mTransformationInfo.mTranslationX + 0.5f);
12006 location[1] += (int) (mTransformationInfo.mTranslationY + 0.5f);
12007 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012008
12009 ViewParent viewParent = mParent;
12010 while (viewParent instanceof View) {
12011 final View view = (View)viewParent;
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012012 location[0] += view.mLeft - view.mScrollX;
12013 location[1] += view.mTop - view.mScrollY;
12014 if (view.mTransformationInfo != null) {
12015 location[0] += (int) (view.mTransformationInfo.mTranslationX + 0.5f);
12016 location[1] += (int) (view.mTransformationInfo.mTranslationY + 0.5f);
12017 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012018 viewParent = view.mParent;
12019 }
Romain Guy8506ab42009-06-11 17:35:47 -070012020
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012021 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012022 // *cough*
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012023 final ViewRootImpl vr = (ViewRootImpl)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012024 location[1] -= vr.mCurScrollY;
12025 }
12026 }
12027
12028 /**
12029 * {@hide}
12030 * @param id the id of the view to be found
12031 * @return the view of the specified id, null if cannot be found
12032 */
12033 protected View findViewTraversal(int id) {
12034 if (id == mID) {
12035 return this;
12036 }
12037 return null;
12038 }
12039
12040 /**
12041 * {@hide}
12042 * @param tag the tag of the view to be found
12043 * @return the view of specified tag, null if cannot be found
12044 */
12045 protected View findViewWithTagTraversal(Object tag) {
12046 if (tag != null && tag.equals(mTag)) {
12047 return this;
12048 }
12049 return null;
12050 }
12051
12052 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012053 * {@hide}
12054 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070012055 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080012056 * @return The first view that matches the predicate or null.
12057 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070012058 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080012059 if (predicate.apply(this)) {
12060 return this;
12061 }
12062 return null;
12063 }
12064
12065 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012066 * Look for a child view with the given id. If this view has the given
12067 * id, return this view.
12068 *
12069 * @param id The id to search for.
12070 * @return The view that has the given id in the hierarchy or null
12071 */
12072 public final View findViewById(int id) {
12073 if (id < 0) {
12074 return null;
12075 }
12076 return findViewTraversal(id);
12077 }
12078
12079 /**
12080 * Look for a child view with the given tag. If this view has the given
12081 * tag, return this view.
12082 *
12083 * @param tag The tag to search for, using "tag.equals(getTag())".
12084 * @return The View that has the given tag in the hierarchy or null
12085 */
12086 public final View findViewWithTag(Object tag) {
12087 if (tag == null) {
12088 return null;
12089 }
12090 return findViewWithTagTraversal(tag);
12091 }
12092
12093 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012094 * {@hide}
12095 * Look for a child view that matches the specified predicate.
12096 * If this view matches the predicate, return this view.
12097 *
12098 * @param predicate The predicate to evaluate.
12099 * @return The first view that matches the predicate or null.
12100 */
12101 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070012102 return findViewByPredicateTraversal(predicate, null);
12103 }
12104
12105 /**
12106 * {@hide}
12107 * Look for a child view that matches the specified predicate,
12108 * starting with the specified view and its descendents and then
12109 * recusively searching the ancestors and siblings of that view
12110 * until this view is reached.
12111 *
12112 * This method is useful in cases where the predicate does not match
12113 * a single unique view (perhaps multiple views use the same id)
12114 * and we are trying to find the view that is "closest" in scope to the
12115 * starting view.
12116 *
12117 * @param start The view to start from.
12118 * @param predicate The predicate to evaluate.
12119 * @return The first view that matches the predicate or null.
12120 */
12121 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
12122 View childToSkip = null;
12123 for (;;) {
12124 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
12125 if (view != null || start == this) {
12126 return view;
12127 }
12128
12129 ViewParent parent = start.getParent();
12130 if (parent == null || !(parent instanceof View)) {
12131 return null;
12132 }
12133
12134 childToSkip = start;
12135 start = (View) parent;
12136 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080012137 }
12138
12139 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012140 * Sets the identifier for this view. The identifier does not have to be
12141 * unique in this view's hierarchy. The identifier should be a positive
12142 * number.
12143 *
12144 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070012145 * @see #getId()
12146 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012147 *
12148 * @param id a number used to identify the view
12149 *
12150 * @attr ref android.R.styleable#View_id
12151 */
12152 public void setId(int id) {
12153 mID = id;
12154 }
12155
12156 /**
12157 * {@hide}
12158 *
12159 * @param isRoot true if the view belongs to the root namespace, false
12160 * otherwise
12161 */
12162 public void setIsRootNamespace(boolean isRoot) {
12163 if (isRoot) {
12164 mPrivateFlags |= IS_ROOT_NAMESPACE;
12165 } else {
12166 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
12167 }
12168 }
12169
12170 /**
12171 * {@hide}
12172 *
12173 * @return true if the view belongs to the root namespace, false otherwise
12174 */
12175 public boolean isRootNamespace() {
12176 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
12177 }
12178
12179 /**
12180 * Returns this view's identifier.
12181 *
12182 * @return a positive integer used to identify the view or {@link #NO_ID}
12183 * if the view has no ID
12184 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012185 * @see #setId(int)
12186 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012187 * @attr ref android.R.styleable#View_id
12188 */
12189 @ViewDebug.CapturedViewProperty
12190 public int getId() {
12191 return mID;
12192 }
12193
12194 /**
12195 * Returns this view's tag.
12196 *
12197 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070012198 *
12199 * @see #setTag(Object)
12200 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012201 */
12202 @ViewDebug.ExportedProperty
12203 public Object getTag() {
12204 return mTag;
12205 }
12206
12207 /**
12208 * Sets the tag associated with this view. A tag can be used to mark
12209 * a view in its hierarchy and does not have to be unique within the
12210 * hierarchy. Tags can also be used to store data within a view without
12211 * resorting to another data structure.
12212 *
12213 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070012214 *
12215 * @see #getTag()
12216 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012217 */
12218 public void setTag(final Object tag) {
12219 mTag = tag;
12220 }
12221
12222 /**
Romain Guyd90a3312009-05-06 14:54:28 -070012223 * Returns the tag associated with this view and the specified key.
12224 *
12225 * @param key The key identifying the tag
12226 *
12227 * @return the Object stored in this view as a tag
12228 *
12229 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070012230 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070012231 */
12232 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012233 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070012234 return null;
12235 }
12236
12237 /**
12238 * Sets a tag associated with this view and a key. A tag can be used
12239 * to mark a view in its hierarchy and does not have to be unique within
12240 * the hierarchy. Tags can also be used to store data within a view
12241 * without resorting to another data structure.
12242 *
12243 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070012244 * application to ensure it is unique (see the <a
12245 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
12246 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070012247 * the Android framework or not associated with any package will cause
12248 * an {@link IllegalArgumentException} to be thrown.
12249 *
12250 * @param key The key identifying the tag
12251 * @param tag An Object to tag the view with
12252 *
12253 * @throws IllegalArgumentException If they specified key is not valid
12254 *
12255 * @see #setTag(Object)
12256 * @see #getTag(int)
12257 */
12258 public void setTag(int key, final Object tag) {
12259 // If the package id is 0x00 or 0x01, it's either an undefined package
12260 // or a framework id
12261 if ((key >>> 24) < 2) {
12262 throw new IllegalArgumentException("The key must be an application-specific "
12263 + "resource id.");
12264 }
12265
Adam Powell2b2f6d62011-09-23 11:15:39 -070012266 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012267 }
12268
12269 /**
12270 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
12271 * framework id.
12272 *
12273 * @hide
12274 */
12275 public void setTagInternal(int key, Object tag) {
12276 if ((key >>> 24) != 0x1) {
12277 throw new IllegalArgumentException("The key must be a framework-specific "
12278 + "resource id.");
12279 }
12280
Adam Powell2b2f6d62011-09-23 11:15:39 -070012281 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012282 }
12283
Adam Powell2b2f6d62011-09-23 11:15:39 -070012284 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012285 if (mKeyedTags == null) {
12286 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070012287 }
12288
Adam Powell7db82ac2011-09-22 19:44:04 -070012289 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012290 }
12291
12292 /**
Romain Guy13922e02009-05-12 17:56:14 -070012293 * @param consistency The type of consistency. See ViewDebug for more information.
12294 *
12295 * @hide
12296 */
12297 protected boolean dispatchConsistencyCheck(int consistency) {
12298 return onConsistencyCheck(consistency);
12299 }
12300
12301 /**
12302 * Method that subclasses should implement to check their consistency. The type of
12303 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070012304 *
Romain Guy13922e02009-05-12 17:56:14 -070012305 * @param consistency The type of consistency. See ViewDebug for more information.
12306 *
12307 * @throws IllegalStateException if the view is in an inconsistent state.
12308 *
12309 * @hide
12310 */
12311 protected boolean onConsistencyCheck(int consistency) {
12312 boolean result = true;
12313
12314 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
12315 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
12316
12317 if (checkLayout) {
12318 if (getParent() == null) {
12319 result = false;
12320 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12321 "View " + this + " does not have a parent.");
12322 }
12323
12324 if (mAttachInfo == null) {
12325 result = false;
12326 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12327 "View " + this + " is not attached to a window.");
12328 }
12329 }
12330
12331 if (checkDrawing) {
12332 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
12333 // from their draw() method
12334
12335 if ((mPrivateFlags & DRAWN) != DRAWN &&
12336 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
12337 result = false;
12338 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12339 "View " + this + " was invalidated but its drawing cache is valid.");
12340 }
12341 }
12342
12343 return result;
12344 }
12345
12346 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012347 * Prints information about this view in the log output, with the tag
12348 * {@link #VIEW_LOG_TAG}.
12349 *
12350 * @hide
12351 */
12352 public void debug() {
12353 debug(0);
12354 }
12355
12356 /**
12357 * Prints information about this view in the log output, with the tag
12358 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
12359 * indentation defined by the <code>depth</code>.
12360 *
12361 * @param depth the indentation level
12362 *
12363 * @hide
12364 */
12365 protected void debug(int depth) {
12366 String output = debugIndent(depth - 1);
12367
12368 output += "+ " + this;
12369 int id = getId();
12370 if (id != -1) {
12371 output += " (id=" + id + ")";
12372 }
12373 Object tag = getTag();
12374 if (tag != null) {
12375 output += " (tag=" + tag + ")";
12376 }
12377 Log.d(VIEW_LOG_TAG, output);
12378
12379 if ((mPrivateFlags & FOCUSED) != 0) {
12380 output = debugIndent(depth) + " FOCUSED";
12381 Log.d(VIEW_LOG_TAG, output);
12382 }
12383
12384 output = debugIndent(depth);
12385 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
12386 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
12387 + "} ";
12388 Log.d(VIEW_LOG_TAG, output);
12389
12390 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
12391 || mPaddingBottom != 0) {
12392 output = debugIndent(depth);
12393 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
12394 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
12395 Log.d(VIEW_LOG_TAG, output);
12396 }
12397
12398 output = debugIndent(depth);
12399 output += "mMeasureWidth=" + mMeasuredWidth +
12400 " mMeasureHeight=" + mMeasuredHeight;
12401 Log.d(VIEW_LOG_TAG, output);
12402
12403 output = debugIndent(depth);
12404 if (mLayoutParams == null) {
12405 output += "BAD! no layout params";
12406 } else {
12407 output = mLayoutParams.debug(output);
12408 }
12409 Log.d(VIEW_LOG_TAG, output);
12410
12411 output = debugIndent(depth);
12412 output += "flags={";
12413 output += View.printFlags(mViewFlags);
12414 output += "}";
12415 Log.d(VIEW_LOG_TAG, output);
12416
12417 output = debugIndent(depth);
12418 output += "privateFlags={";
12419 output += View.printPrivateFlags(mPrivateFlags);
12420 output += "}";
12421 Log.d(VIEW_LOG_TAG, output);
12422 }
12423
12424 /**
12425 * Creates an string of whitespaces used for indentation.
12426 *
12427 * @param depth the indentation level
12428 * @return a String containing (depth * 2 + 3) * 2 white spaces
12429 *
12430 * @hide
12431 */
12432 protected static String debugIndent(int depth) {
12433 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
12434 for (int i = 0; i < (depth * 2) + 3; i++) {
12435 spaces.append(' ').append(' ');
12436 }
12437 return spaces.toString();
12438 }
12439
12440 /**
12441 * <p>Return the offset of the widget's text baseline from the widget's top
12442 * boundary. If this widget does not support baseline alignment, this
12443 * method returns -1. </p>
12444 *
12445 * @return the offset of the baseline within the widget's bounds or -1
12446 * if baseline alignment is not supported
12447 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012448 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012449 public int getBaseline() {
12450 return -1;
12451 }
12452
12453 /**
12454 * Call this when something has changed which has invalidated the
12455 * layout of this view. This will schedule a layout pass of the view
12456 * tree.
12457 */
12458 public void requestLayout() {
12459 if (ViewDebug.TRACE_HIERARCHY) {
12460 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
12461 }
12462
12463 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012464 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012465
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070012466 if (mParent != null) {
12467 if (mLayoutParams != null) {
12468 mLayoutParams.resolveWithDirection(getResolvedLayoutDirection());
12469 }
12470 if (!mParent.isLayoutRequested()) {
12471 mParent.requestLayout();
12472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012473 }
12474 }
12475
12476 /**
12477 * Forces this view to be laid out during the next layout pass.
12478 * This method does not call requestLayout() or forceLayout()
12479 * on the parent.
12480 */
12481 public void forceLayout() {
12482 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012483 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012484 }
12485
12486 /**
12487 * <p>
12488 * This is called to find out how big a view should be. The parent
12489 * supplies constraint information in the width and height parameters.
12490 * </p>
12491 *
12492 * <p>
12493 * The actual mesurement work of a view is performed in
12494 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
12495 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
12496 * </p>
12497 *
12498 *
12499 * @param widthMeasureSpec Horizontal space requirements as imposed by the
12500 * parent
12501 * @param heightMeasureSpec Vertical space requirements as imposed by the
12502 * parent
12503 *
12504 * @see #onMeasure(int, int)
12505 */
12506 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
12507 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
12508 widthMeasureSpec != mOldWidthMeasureSpec ||
12509 heightMeasureSpec != mOldHeightMeasureSpec) {
12510
12511 // first clears the measured dimension flag
12512 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
12513
12514 if (ViewDebug.TRACE_HIERARCHY) {
12515 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
12516 }
12517
12518 // measure ourselves, this should set the measured dimension flag back
12519 onMeasure(widthMeasureSpec, heightMeasureSpec);
12520
12521 // flag not set, setMeasuredDimension() was not invoked, we raise
12522 // an exception to warn the developer
12523 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
12524 throw new IllegalStateException("onMeasure() did not set the"
12525 + " measured dimension by calling"
12526 + " setMeasuredDimension()");
12527 }
12528
12529 mPrivateFlags |= LAYOUT_REQUIRED;
12530 }
12531
12532 mOldWidthMeasureSpec = widthMeasureSpec;
12533 mOldHeightMeasureSpec = heightMeasureSpec;
12534 }
12535
12536 /**
12537 * <p>
12538 * Measure the view and its content to determine the measured width and the
12539 * measured height. This method is invoked by {@link #measure(int, int)} and
12540 * should be overriden by subclasses to provide accurate and efficient
12541 * measurement of their contents.
12542 * </p>
12543 *
12544 * <p>
12545 * <strong>CONTRACT:</strong> When overriding this method, you
12546 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
12547 * measured width and height of this view. Failure to do so will trigger an
12548 * <code>IllegalStateException</code>, thrown by
12549 * {@link #measure(int, int)}. Calling the superclass'
12550 * {@link #onMeasure(int, int)} is a valid use.
12551 * </p>
12552 *
12553 * <p>
12554 * The base class implementation of measure defaults to the background size,
12555 * unless a larger size is allowed by the MeasureSpec. Subclasses should
12556 * override {@link #onMeasure(int, int)} to provide better measurements of
12557 * their content.
12558 * </p>
12559 *
12560 * <p>
12561 * If this method is overridden, it is the subclass's responsibility to make
12562 * sure the measured height and width are at least the view's minimum height
12563 * and width ({@link #getSuggestedMinimumHeight()} and
12564 * {@link #getSuggestedMinimumWidth()}).
12565 * </p>
12566 *
12567 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
12568 * The requirements are encoded with
12569 * {@link android.view.View.MeasureSpec}.
12570 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
12571 * The requirements are encoded with
12572 * {@link android.view.View.MeasureSpec}.
12573 *
12574 * @see #getMeasuredWidth()
12575 * @see #getMeasuredHeight()
12576 * @see #setMeasuredDimension(int, int)
12577 * @see #getSuggestedMinimumHeight()
12578 * @see #getSuggestedMinimumWidth()
12579 * @see android.view.View.MeasureSpec#getMode(int)
12580 * @see android.view.View.MeasureSpec#getSize(int)
12581 */
12582 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
12583 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
12584 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
12585 }
12586
12587 /**
12588 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
12589 * measured width and measured height. Failing to do so will trigger an
12590 * exception at measurement time.</p>
12591 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080012592 * @param measuredWidth The measured width of this view. May be a complex
12593 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12594 * {@link #MEASURED_STATE_TOO_SMALL}.
12595 * @param measuredHeight The measured height of this view. May be a complex
12596 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12597 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012598 */
12599 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
12600 mMeasuredWidth = measuredWidth;
12601 mMeasuredHeight = measuredHeight;
12602
12603 mPrivateFlags |= MEASURED_DIMENSION_SET;
12604 }
12605
12606 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080012607 * Merge two states as returned by {@link #getMeasuredState()}.
12608 * @param curState The current state as returned from a view or the result
12609 * of combining multiple views.
12610 * @param newState The new view state to combine.
12611 * @return Returns a new integer reflecting the combination of the two
12612 * states.
12613 */
12614 public static int combineMeasuredStates(int curState, int newState) {
12615 return curState | newState;
12616 }
12617
12618 /**
12619 * Version of {@link #resolveSizeAndState(int, int, int)}
12620 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
12621 */
12622 public static int resolveSize(int size, int measureSpec) {
12623 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
12624 }
12625
12626 /**
12627 * Utility to reconcile a desired size and state, with constraints imposed
12628 * by a MeasureSpec. Will take the desired size, unless a different size
12629 * is imposed by the constraints. The returned value is a compound integer,
12630 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
12631 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
12632 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012633 *
12634 * @param size How big the view wants to be
12635 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080012636 * @return Size information bit mask as defined by
12637 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012638 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080012639 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012640 int result = size;
12641 int specMode = MeasureSpec.getMode(measureSpec);
12642 int specSize = MeasureSpec.getSize(measureSpec);
12643 switch (specMode) {
12644 case MeasureSpec.UNSPECIFIED:
12645 result = size;
12646 break;
12647 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080012648 if (specSize < size) {
12649 result = specSize | MEASURED_STATE_TOO_SMALL;
12650 } else {
12651 result = size;
12652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012653 break;
12654 case MeasureSpec.EXACTLY:
12655 result = specSize;
12656 break;
12657 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080012658 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012659 }
12660
12661 /**
12662 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070012663 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012664 * by the MeasureSpec.
12665 *
12666 * @param size Default size for this view
12667 * @param measureSpec Constraints imposed by the parent
12668 * @return The size this view should be.
12669 */
12670 public static int getDefaultSize(int size, int measureSpec) {
12671 int result = size;
12672 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070012673 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012674
12675 switch (specMode) {
12676 case MeasureSpec.UNSPECIFIED:
12677 result = size;
12678 break;
12679 case MeasureSpec.AT_MOST:
12680 case MeasureSpec.EXACTLY:
12681 result = specSize;
12682 break;
12683 }
12684 return result;
12685 }
12686
12687 /**
12688 * Returns the suggested minimum height that the view should use. This
12689 * returns the maximum of the view's minimum height
12690 * and the background's minimum height
12691 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
12692 * <p>
12693 * When being used in {@link #onMeasure(int, int)}, the caller should still
12694 * ensure the returned height is within the requirements of the parent.
12695 *
12696 * @return The suggested minimum height of the view.
12697 */
12698 protected int getSuggestedMinimumHeight() {
12699 int suggestedMinHeight = mMinHeight;
12700
12701 if (mBGDrawable != null) {
12702 final int bgMinHeight = mBGDrawable.getMinimumHeight();
12703 if (suggestedMinHeight < bgMinHeight) {
12704 suggestedMinHeight = bgMinHeight;
12705 }
12706 }
12707
12708 return suggestedMinHeight;
12709 }
12710
12711 /**
12712 * Returns the suggested minimum width that the view should use. This
12713 * returns the maximum of the view's minimum width)
12714 * and the background's minimum width
12715 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
12716 * <p>
12717 * When being used in {@link #onMeasure(int, int)}, the caller should still
12718 * ensure the returned width is within the requirements of the parent.
12719 *
12720 * @return The suggested minimum width of the view.
12721 */
12722 protected int getSuggestedMinimumWidth() {
12723 int suggestedMinWidth = mMinWidth;
12724
12725 if (mBGDrawable != null) {
12726 final int bgMinWidth = mBGDrawable.getMinimumWidth();
12727 if (suggestedMinWidth < bgMinWidth) {
12728 suggestedMinWidth = bgMinWidth;
12729 }
12730 }
12731
12732 return suggestedMinWidth;
12733 }
12734
12735 /**
12736 * Sets the minimum height of the view. It is not guaranteed the view will
12737 * be able to achieve this minimum height (for example, if its parent layout
12738 * constrains it with less available height).
12739 *
12740 * @param minHeight The minimum height the view will try to be.
12741 */
12742 public void setMinimumHeight(int minHeight) {
12743 mMinHeight = minHeight;
12744 }
12745
12746 /**
12747 * Sets the minimum width of the view. It is not guaranteed the view will
12748 * be able to achieve this minimum width (for example, if its parent layout
12749 * constrains it with less available width).
12750 *
12751 * @param minWidth The minimum width the view will try to be.
12752 */
12753 public void setMinimumWidth(int minWidth) {
12754 mMinWidth = minWidth;
12755 }
12756
12757 /**
12758 * Get the animation currently associated with this view.
12759 *
12760 * @return The animation that is currently playing or
12761 * scheduled to play for this view.
12762 */
12763 public Animation getAnimation() {
12764 return mCurrentAnimation;
12765 }
12766
12767 /**
12768 * Start the specified animation now.
12769 *
12770 * @param animation the animation to start now
12771 */
12772 public void startAnimation(Animation animation) {
12773 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
12774 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012775 invalidateParentCaches();
12776 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012777 }
12778
12779 /**
12780 * Cancels any animations for this view.
12781 */
12782 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080012783 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080012784 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080012785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012786 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012787 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012788 }
12789
12790 /**
12791 * Sets the next animation to play for this view.
12792 * If you want the animation to play immediately, use
12793 * startAnimation. This method provides allows fine-grained
12794 * control over the start time and invalidation, but you
12795 * must make sure that 1) the animation has a start time set, and
12796 * 2) the view will be invalidated when the animation is supposed to
12797 * start.
12798 *
12799 * @param animation The next animation, or null.
12800 */
12801 public void setAnimation(Animation animation) {
12802 mCurrentAnimation = animation;
12803 if (animation != null) {
12804 animation.reset();
12805 }
12806 }
12807
12808 /**
12809 * Invoked by a parent ViewGroup to notify the start of the animation
12810 * currently associated with this view. If you override this method,
12811 * always call super.onAnimationStart();
12812 *
12813 * @see #setAnimation(android.view.animation.Animation)
12814 * @see #getAnimation()
12815 */
12816 protected void onAnimationStart() {
12817 mPrivateFlags |= ANIMATION_STARTED;
12818 }
12819
12820 /**
12821 * Invoked by a parent ViewGroup to notify the end of the animation
12822 * currently associated with this view. If you override this method,
12823 * always call super.onAnimationEnd();
12824 *
12825 * @see #setAnimation(android.view.animation.Animation)
12826 * @see #getAnimation()
12827 */
12828 protected void onAnimationEnd() {
12829 mPrivateFlags &= ~ANIMATION_STARTED;
12830 }
12831
12832 /**
12833 * Invoked if there is a Transform that involves alpha. Subclass that can
12834 * draw themselves with the specified alpha should return true, and then
12835 * respect that alpha when their onDraw() is called. If this returns false
12836 * then the view may be redirected to draw into an offscreen buffer to
12837 * fulfill the request, which will look fine, but may be slower than if the
12838 * subclass handles it internally. The default implementation returns false.
12839 *
12840 * @param alpha The alpha (0..255) to apply to the view's drawing
12841 * @return true if the view can draw with the specified alpha.
12842 */
12843 protected boolean onSetAlpha(int alpha) {
12844 return false;
12845 }
12846
12847 /**
12848 * This is used by the RootView to perform an optimization when
12849 * the view hierarchy contains one or several SurfaceView.
12850 * SurfaceView is always considered transparent, but its children are not,
12851 * therefore all View objects remove themselves from the global transparent
12852 * region (passed as a parameter to this function).
12853 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012854 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012855 *
12856 * @return Returns true if the effective visibility of the view at this
12857 * point is opaque, regardless of the transparent region; returns false
12858 * if it is possible for underlying windows to be seen behind the view.
12859 *
12860 * {@hide}
12861 */
12862 public boolean gatherTransparentRegion(Region region) {
12863 final AttachInfo attachInfo = mAttachInfo;
12864 if (region != null && attachInfo != null) {
12865 final int pflags = mPrivateFlags;
12866 if ((pflags & SKIP_DRAW) == 0) {
12867 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
12868 // remove it from the transparent region.
12869 final int[] location = attachInfo.mTransparentLocation;
12870 getLocationInWindow(location);
12871 region.op(location[0], location[1], location[0] + mRight - mLeft,
12872 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
12873 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
12874 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
12875 // exists, so we remove the background drawable's non-transparent
12876 // parts from this transparent region.
12877 applyDrawableToTransparentRegion(mBGDrawable, region);
12878 }
12879 }
12880 return true;
12881 }
12882
12883 /**
12884 * Play a sound effect for this view.
12885 *
12886 * <p>The framework will play sound effects for some built in actions, such as
12887 * clicking, but you may wish to play these effects in your widget,
12888 * for instance, for internal navigation.
12889 *
12890 * <p>The sound effect will only be played if sound effects are enabled by the user, and
12891 * {@link #isSoundEffectsEnabled()} is true.
12892 *
12893 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
12894 */
12895 public void playSoundEffect(int soundConstant) {
12896 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
12897 return;
12898 }
12899 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
12900 }
12901
12902 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012903 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012904 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012905 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012906 *
12907 * <p>The framework will provide haptic feedback for some built in actions,
12908 * such as long presses, but you may wish to provide feedback for your
12909 * own widget.
12910 *
12911 * <p>The feedback will only be performed if
12912 * {@link #isHapticFeedbackEnabled()} is true.
12913 *
12914 * @param feedbackConstant One of the constants defined in
12915 * {@link HapticFeedbackConstants}
12916 */
12917 public boolean performHapticFeedback(int feedbackConstant) {
12918 return performHapticFeedback(feedbackConstant, 0);
12919 }
12920
12921 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012922 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012923 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012924 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012925 *
12926 * @param feedbackConstant One of the constants defined in
12927 * {@link HapticFeedbackConstants}
12928 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
12929 */
12930 public boolean performHapticFeedback(int feedbackConstant, int flags) {
12931 if (mAttachInfo == null) {
12932 return false;
12933 }
Romain Guyf607bdc2010-09-10 19:20:06 -070012934 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070012935 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012936 && !isHapticFeedbackEnabled()) {
12937 return false;
12938 }
Romain Guy812ccbe2010-06-01 14:07:24 -070012939 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
12940 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012941 }
12942
12943 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012944 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040012945 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
12946 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080012947 */
12948 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040012949 if (visibility != mSystemUiVisibility) {
12950 mSystemUiVisibility = visibility;
12951 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12952 mParent.recomputeViewAttributes(this);
12953 }
Joe Onorato664644d2011-01-23 17:53:23 -080012954 }
12955 }
12956
12957 /**
12958 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040012959 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
12960 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080012961 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080012962 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080012963 return mSystemUiVisibility;
12964 }
12965
Scott Mainec6331b2011-05-24 16:55:56 -070012966 /**
12967 * Set a listener to receive callbacks when the visibility of the system bar changes.
12968 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
12969 */
Joe Onorato664644d2011-01-23 17:53:23 -080012970 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
12971 mOnSystemUiVisibilityChangeListener = l;
12972 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12973 mParent.recomputeViewAttributes(this);
12974 }
12975 }
12976
12977 /**
12978 */
12979 public void dispatchSystemUiVisibilityChanged(int visibility) {
12980 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080012981 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080012982 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080012983 }
12984 }
12985
12986 /**
Joe Malin32736f02011-01-19 16:14:20 -080012987 * Creates an image that the system displays during the drag and drop
12988 * operation. This is called a &quot;drag shadow&quot;. The default implementation
12989 * for a DragShadowBuilder based on a View returns an image that has exactly the same
12990 * appearance as the given View. The default also positions the center of the drag shadow
12991 * directly under the touch point. If no View is provided (the constructor with no parameters
12992 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
12993 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
12994 * default is an invisible drag shadow.
12995 * <p>
12996 * You are not required to use the View you provide to the constructor as the basis of the
12997 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
12998 * anything you want as the drag shadow.
12999 * </p>
13000 * <p>
13001 * You pass a DragShadowBuilder object to the system when you start the drag. The system
13002 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
13003 * size and position of the drag shadow. It uses this data to construct a
13004 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
13005 * so that your application can draw the shadow image in the Canvas.
13006 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013007 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013008 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070013009 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070013010
13011 /**
Joe Malin32736f02011-01-19 16:14:20 -080013012 * Constructs a shadow image builder based on a View. By default, the resulting drag
13013 * shadow will have the same appearance and dimensions as the View, with the touch point
13014 * over the center of the View.
13015 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070013016 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013017 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070013018 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070013019 }
13020
Christopher Tate17ed60c2011-01-18 12:50:26 -080013021 /**
13022 * Construct a shadow builder object with no associated View. This
13023 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
13024 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
13025 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080013026 * reference to any View object. If they are not overridden, then the result is an
13027 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080013028 */
13029 public DragShadowBuilder() {
13030 mView = new WeakReference<View>(null);
13031 }
13032
13033 /**
13034 * Returns the View object that had been passed to the
13035 * {@link #View.DragShadowBuilder(View)}
13036 * constructor. If that View parameter was {@code null} or if the
13037 * {@link #View.DragShadowBuilder()}
13038 * constructor was used to instantiate the builder object, this method will return
13039 * null.
13040 *
13041 * @return The View object associate with this builder object.
13042 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070013043 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070013044 final public View getView() {
13045 return mView.get();
13046 }
13047
Christopher Tate2c095f32010-10-04 14:13:40 -070013048 /**
Joe Malin32736f02011-01-19 16:14:20 -080013049 * Provides the metrics for the shadow image. These include the dimensions of
13050 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070013051 * be centered under the touch location while dragging.
13052 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013053 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080013054 * same as the dimensions of the View itself and centers the shadow under
13055 * the touch point.
13056 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013057 *
Joe Malin32736f02011-01-19 16:14:20 -080013058 * @param shadowSize A {@link android.graphics.Point} containing the width and height
13059 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
13060 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
13061 * image.
13062 *
13063 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
13064 * shadow image that should be underneath the touch point during the drag and drop
13065 * operation. Your application must set {@link android.graphics.Point#x} to the
13066 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070013067 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013068 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070013069 final View view = mView.get();
13070 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013071 shadowSize.set(view.getWidth(), view.getHeight());
13072 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070013073 } else {
13074 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
13075 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013076 }
13077
13078 /**
Joe Malin32736f02011-01-19 16:14:20 -080013079 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
13080 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013081 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070013082 *
Joe Malin32736f02011-01-19 16:14:20 -080013083 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070013084 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013085 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070013086 final View view = mView.get();
13087 if (view != null) {
13088 view.draw(canvas);
13089 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013090 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070013091 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013092 }
13093 }
13094
13095 /**
Joe Malin32736f02011-01-19 16:14:20 -080013096 * Starts a drag and drop operation. When your application calls this method, it passes a
13097 * {@link android.view.View.DragShadowBuilder} object to the system. The
13098 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
13099 * to get metrics for the drag shadow, and then calls the object's
13100 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
13101 * <p>
13102 * Once the system has the drag shadow, it begins the drag and drop operation by sending
13103 * drag events to all the View objects in your application that are currently visible. It does
13104 * this either by calling the View object's drag listener (an implementation of
13105 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
13106 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
13107 * Both are passed a {@link android.view.DragEvent} object that has a
13108 * {@link android.view.DragEvent#getAction()} value of
13109 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
13110 * </p>
13111 * <p>
13112 * Your application can invoke startDrag() on any attached View object. The View object does not
13113 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
13114 * be related to the View the user selected for dragging.
13115 * </p>
13116 * @param data A {@link android.content.ClipData} object pointing to the data to be
13117 * transferred by the drag and drop operation.
13118 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
13119 * drag shadow.
13120 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
13121 * drop operation. This Object is put into every DragEvent object sent by the system during the
13122 * current drag.
13123 * <p>
13124 * myLocalState is a lightweight mechanism for the sending information from the dragged View
13125 * to the target Views. For example, it can contain flags that differentiate between a
13126 * a copy operation and a move operation.
13127 * </p>
13128 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
13129 * so the parameter should be set to 0.
13130 * @return {@code true} if the method completes successfully, or
13131 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
13132 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070013133 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013134 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013135 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070013136 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013137 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070013138 }
13139 boolean okay = false;
13140
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013141 Point shadowSize = new Point();
13142 Point shadowTouchPoint = new Point();
13143 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070013144
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013145 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
13146 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
13147 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070013148 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013149
Chris Tatea32dcf72010-10-14 12:13:50 -070013150 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013151 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
13152 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070013153 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013154 Surface surface = new Surface();
13155 try {
13156 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013157 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070013158 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070013159 + " surface=" + surface);
13160 if (token != null) {
13161 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070013162 try {
Chris Tate6b391282010-10-14 15:48:59 -070013163 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013164 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070013165 } finally {
13166 surface.unlockCanvasAndPost(canvas);
13167 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013168
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013169 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080013170
13171 // Cache the local state object for delivery with DragEvents
13172 root.setLocalDragState(myLocalState);
13173
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013174 // repurpose 'shadowSize' for the last touch point
13175 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070013176
Christopher Tatea53146c2010-09-07 11:57:52 -070013177 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013178 shadowSize.x, shadowSize.y,
13179 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070013180 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070013181
13182 // Off and running! Release our local surface instance; the drag
13183 // shadow surface is now managed by the system process.
13184 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070013185 }
13186 } catch (Exception e) {
13187 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
13188 surface.destroy();
13189 }
13190
13191 return okay;
13192 }
13193
Christopher Tatea53146c2010-09-07 11:57:52 -070013194 /**
Joe Malin32736f02011-01-19 16:14:20 -080013195 * Handles drag events sent by the system following a call to
13196 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
13197 *<p>
13198 * When the system calls this method, it passes a
13199 * {@link android.view.DragEvent} object. A call to
13200 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
13201 * in DragEvent. The method uses these to determine what is happening in the drag and drop
13202 * operation.
13203 * @param event The {@link android.view.DragEvent} sent by the system.
13204 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
13205 * in DragEvent, indicating the type of drag event represented by this object.
13206 * @return {@code true} if the method was successful, otherwise {@code false}.
13207 * <p>
13208 * The method should return {@code true} in response to an action type of
13209 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
13210 * operation.
13211 * </p>
13212 * <p>
13213 * The method should also return {@code true} in response to an action type of
13214 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
13215 * {@code false} if it didn't.
13216 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013217 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070013218 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070013219 return false;
13220 }
13221
13222 /**
Joe Malin32736f02011-01-19 16:14:20 -080013223 * Detects if this View is enabled and has a drag event listener.
13224 * If both are true, then it calls the drag event listener with the
13225 * {@link android.view.DragEvent} it received. If the drag event listener returns
13226 * {@code true}, then dispatchDragEvent() returns {@code true}.
13227 * <p>
13228 * For all other cases, the method calls the
13229 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
13230 * method and returns its result.
13231 * </p>
13232 * <p>
13233 * This ensures that a drag event is always consumed, even if the View does not have a drag
13234 * event listener. However, if the View has a listener and the listener returns true, then
13235 * onDragEvent() is not called.
13236 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013237 */
13238 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080013239 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070013240 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
13241 && mOnDragListener.onDrag(this, event)) {
13242 return true;
13243 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013244 return onDragEvent(event);
13245 }
13246
Christopher Tate3d4bf172011-03-28 16:16:46 -070013247 boolean canAcceptDrag() {
13248 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
13249 }
13250
Christopher Tatea53146c2010-09-07 11:57:52 -070013251 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070013252 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
13253 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070013254 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070013255 */
13256 public void onCloseSystemDialogs(String reason) {
13257 }
Joe Malin32736f02011-01-19 16:14:20 -080013258
Dianne Hackbornffa42482009-09-23 22:20:11 -070013259 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013260 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070013261 * update a Region being computed for
13262 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013263 * that any non-transparent parts of the Drawable are removed from the
13264 * given transparent region.
13265 *
13266 * @param dr The Drawable whose transparency is to be applied to the region.
13267 * @param region A Region holding the current transparency information,
13268 * where any parts of the region that are set are considered to be
13269 * transparent. On return, this region will be modified to have the
13270 * transparency information reduced by the corresponding parts of the
13271 * Drawable that are not transparent.
13272 * {@hide}
13273 */
13274 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
13275 if (DBG) {
13276 Log.i("View", "Getting transparent region for: " + this);
13277 }
13278 final Region r = dr.getTransparentRegion();
13279 final Rect db = dr.getBounds();
13280 final AttachInfo attachInfo = mAttachInfo;
13281 if (r != null && attachInfo != null) {
13282 final int w = getRight()-getLeft();
13283 final int h = getBottom()-getTop();
13284 if (db.left > 0) {
13285 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
13286 r.op(0, 0, db.left, h, Region.Op.UNION);
13287 }
13288 if (db.right < w) {
13289 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
13290 r.op(db.right, 0, w, h, Region.Op.UNION);
13291 }
13292 if (db.top > 0) {
13293 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
13294 r.op(0, 0, w, db.top, Region.Op.UNION);
13295 }
13296 if (db.bottom < h) {
13297 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
13298 r.op(0, db.bottom, w, h, Region.Op.UNION);
13299 }
13300 final int[] location = attachInfo.mTransparentLocation;
13301 getLocationInWindow(location);
13302 r.translate(location[0], location[1]);
13303 region.op(r, Region.Op.INTERSECT);
13304 } else {
13305 region.op(db, Region.Op.DIFFERENCE);
13306 }
13307 }
13308
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013309 private void checkForLongClick(int delayOffset) {
13310 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
13311 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013312
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013313 if (mPendingCheckForLongPress == null) {
13314 mPendingCheckForLongPress = new CheckForLongPress();
13315 }
13316 mPendingCheckForLongPress.rememberWindowAttachCount();
13317 postDelayed(mPendingCheckForLongPress,
13318 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013320 }
13321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013322 /**
13323 * Inflate a view from an XML resource. This convenience method wraps the {@link
13324 * LayoutInflater} class, which provides a full range of options for view inflation.
13325 *
13326 * @param context The Context object for your activity or application.
13327 * @param resource The resource ID to inflate
13328 * @param root A view group that will be the parent. Used to properly inflate the
13329 * layout_* parameters.
13330 * @see LayoutInflater
13331 */
13332 public static View inflate(Context context, int resource, ViewGroup root) {
13333 LayoutInflater factory = LayoutInflater.from(context);
13334 return factory.inflate(resource, root);
13335 }
Romain Guy33e72ae2010-07-17 12:40:29 -070013336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013337 /**
Adam Powell637d3372010-08-25 14:37:03 -070013338 * Scroll the view with standard behavior for scrolling beyond the normal
13339 * content boundaries. Views that call this method should override
13340 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
13341 * results of an over-scroll operation.
13342 *
13343 * Views can use this method to handle any touch or fling-based scrolling.
13344 *
13345 * @param deltaX Change in X in pixels
13346 * @param deltaY Change in Y in pixels
13347 * @param scrollX Current X scroll value in pixels before applying deltaX
13348 * @param scrollY Current Y scroll value in pixels before applying deltaY
13349 * @param scrollRangeX Maximum content scroll range along the X axis
13350 * @param scrollRangeY Maximum content scroll range along the Y axis
13351 * @param maxOverScrollX Number of pixels to overscroll by in either direction
13352 * along the X axis.
13353 * @param maxOverScrollY Number of pixels to overscroll by in either direction
13354 * along the Y axis.
13355 * @param isTouchEvent true if this scroll operation is the result of a touch event.
13356 * @return true if scrolling was clamped to an over-scroll boundary along either
13357 * axis, false otherwise.
13358 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013359 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070013360 protected boolean overScrollBy(int deltaX, int deltaY,
13361 int scrollX, int scrollY,
13362 int scrollRangeX, int scrollRangeY,
13363 int maxOverScrollX, int maxOverScrollY,
13364 boolean isTouchEvent) {
13365 final int overScrollMode = mOverScrollMode;
13366 final boolean canScrollHorizontal =
13367 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
13368 final boolean canScrollVertical =
13369 computeVerticalScrollRange() > computeVerticalScrollExtent();
13370 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
13371 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
13372 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
13373 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
13374
13375 int newScrollX = scrollX + deltaX;
13376 if (!overScrollHorizontal) {
13377 maxOverScrollX = 0;
13378 }
13379
13380 int newScrollY = scrollY + deltaY;
13381 if (!overScrollVertical) {
13382 maxOverScrollY = 0;
13383 }
13384
13385 // Clamp values if at the limits and record
13386 final int left = -maxOverScrollX;
13387 final int right = maxOverScrollX + scrollRangeX;
13388 final int top = -maxOverScrollY;
13389 final int bottom = maxOverScrollY + scrollRangeY;
13390
13391 boolean clampedX = false;
13392 if (newScrollX > right) {
13393 newScrollX = right;
13394 clampedX = true;
13395 } else if (newScrollX < left) {
13396 newScrollX = left;
13397 clampedX = true;
13398 }
13399
13400 boolean clampedY = false;
13401 if (newScrollY > bottom) {
13402 newScrollY = bottom;
13403 clampedY = true;
13404 } else if (newScrollY < top) {
13405 newScrollY = top;
13406 clampedY = true;
13407 }
13408
13409 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
13410
13411 return clampedX || clampedY;
13412 }
13413
13414 /**
13415 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
13416 * respond to the results of an over-scroll operation.
13417 *
13418 * @param scrollX New X scroll value in pixels
13419 * @param scrollY New Y scroll value in pixels
13420 * @param clampedX True if scrollX was clamped to an over-scroll boundary
13421 * @param clampedY True if scrollY was clamped to an over-scroll boundary
13422 */
13423 protected void onOverScrolled(int scrollX, int scrollY,
13424 boolean clampedX, boolean clampedY) {
13425 // Intentionally empty.
13426 }
13427
13428 /**
13429 * Returns the over-scroll mode for this view. The result will be
13430 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13431 * (allow over-scrolling only if the view content is larger than the container),
13432 * or {@link #OVER_SCROLL_NEVER}.
13433 *
13434 * @return This view's over-scroll mode.
13435 */
13436 public int getOverScrollMode() {
13437 return mOverScrollMode;
13438 }
13439
13440 /**
13441 * Set the over-scroll mode for this view. Valid over-scroll modes are
13442 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13443 * (allow over-scrolling only if the view content is larger than the container),
13444 * or {@link #OVER_SCROLL_NEVER}.
13445 *
13446 * Setting the over-scroll mode of a view will have an effect only if the
13447 * view is capable of scrolling.
13448 *
13449 * @param overScrollMode The new over-scroll mode for this view.
13450 */
13451 public void setOverScrollMode(int overScrollMode) {
13452 if (overScrollMode != OVER_SCROLL_ALWAYS &&
13453 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
13454 overScrollMode != OVER_SCROLL_NEVER) {
13455 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
13456 }
13457 mOverScrollMode = overScrollMode;
13458 }
13459
13460 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013461 * Gets a scale factor that determines the distance the view should scroll
13462 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
13463 * @return The vertical scroll scale factor.
13464 * @hide
13465 */
13466 protected float getVerticalScrollFactor() {
13467 if (mVerticalScrollFactor == 0) {
13468 TypedValue outValue = new TypedValue();
13469 if (!mContext.getTheme().resolveAttribute(
13470 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
13471 throw new IllegalStateException(
13472 "Expected theme to define listPreferredItemHeight.");
13473 }
13474 mVerticalScrollFactor = outValue.getDimension(
13475 mContext.getResources().getDisplayMetrics());
13476 }
13477 return mVerticalScrollFactor;
13478 }
13479
13480 /**
13481 * Gets a scale factor that determines the distance the view should scroll
13482 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
13483 * @return The horizontal scroll scale factor.
13484 * @hide
13485 */
13486 protected float getHorizontalScrollFactor() {
13487 // TODO: Should use something else.
13488 return getVerticalScrollFactor();
13489 }
13490
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013491 /**
13492 * Return the value specifying the text direction or policy that was set with
13493 * {@link #setTextDirection(int)}.
13494 *
13495 * @return the defined text direction. It can be one of:
13496 *
13497 * {@link #TEXT_DIRECTION_INHERIT},
13498 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13499 * {@link #TEXT_DIRECTION_ANY_RTL},
13500 * {@link #TEXT_DIRECTION_LTR},
13501 * {@link #TEXT_DIRECTION_RTL},
13502 *
13503 * @hide
13504 */
13505 public int getTextDirection() {
13506 return mTextDirection;
13507 }
13508
13509 /**
13510 * Set the text direction.
13511 *
13512 * @param textDirection the direction to set. Should be one of:
13513 *
13514 * {@link #TEXT_DIRECTION_INHERIT},
13515 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13516 * {@link #TEXT_DIRECTION_ANY_RTL},
13517 * {@link #TEXT_DIRECTION_LTR},
13518 * {@link #TEXT_DIRECTION_RTL},
13519 *
13520 * @hide
13521 */
13522 public void setTextDirection(int textDirection) {
13523 if (textDirection != mTextDirection) {
13524 mTextDirection = textDirection;
13525 resetResolvedTextDirection();
13526 requestLayout();
13527 }
13528 }
13529
13530 /**
13531 * Return the resolved text direction.
13532 *
13533 * @return the resolved text direction. Return one of:
13534 *
Doug Feltcb3791202011-07-07 11:57:48 -070013535 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13536 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013537 * {@link #TEXT_DIRECTION_LTR},
13538 * {@link #TEXT_DIRECTION_RTL},
13539 *
13540 * @hide
13541 */
13542 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013543 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013544 resolveTextDirection();
13545 }
13546 return mResolvedTextDirection;
13547 }
13548
13549 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013550 * Resolve the text direction.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013551 *
13552 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013553 */
13554 protected void resolveTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013555 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
13556 mResolvedTextDirection = mTextDirection;
13557 return;
13558 }
13559 if (mParent != null && mParent instanceof ViewGroup) {
13560 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
13561 return;
13562 }
13563 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013564 }
13565
13566 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013567 * Reset resolved text direction. Will be resolved during a call to getResolvedTextDirection().
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013568 *
13569 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013570 */
13571 protected void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013572 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013573 }
13574
Chet Haaseb39f0512011-05-24 14:36:40 -070013575 //
13576 // Properties
13577 //
13578 /**
13579 * A Property wrapper around the <code>alpha</code> functionality handled by the
13580 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
13581 */
Romain Guy02ccac62011-06-24 13:20:23 -070013582 public static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013583 @Override
13584 public void setValue(View object, float value) {
13585 object.setAlpha(value);
13586 }
13587
13588 @Override
13589 public Float get(View object) {
13590 return object.getAlpha();
13591 }
13592 };
13593
13594 /**
13595 * A Property wrapper around the <code>translationX</code> functionality handled by the
13596 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
13597 */
13598 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
13599 @Override
13600 public void setValue(View object, float value) {
13601 object.setTranslationX(value);
13602 }
13603
13604 @Override
13605 public Float get(View object) {
13606 return object.getTranslationX();
13607 }
13608 };
13609
13610 /**
13611 * A Property wrapper around the <code>translationY</code> functionality handled by the
13612 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
13613 */
13614 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
13615 @Override
13616 public void setValue(View object, float value) {
13617 object.setTranslationY(value);
13618 }
13619
13620 @Override
13621 public Float get(View object) {
13622 return object.getTranslationY();
13623 }
13624 };
13625
13626 /**
13627 * A Property wrapper around the <code>x</code> functionality handled by the
13628 * {@link View#setX(float)} and {@link View#getX()} methods.
13629 */
13630 public static Property<View, Float> X = new FloatProperty<View>("x") {
13631 @Override
13632 public void setValue(View object, float value) {
13633 object.setX(value);
13634 }
13635
13636 @Override
13637 public Float get(View object) {
13638 return object.getX();
13639 }
13640 };
13641
13642 /**
13643 * A Property wrapper around the <code>y</code> functionality handled by the
13644 * {@link View#setY(float)} and {@link View#getY()} methods.
13645 */
13646 public static Property<View, Float> Y = new FloatProperty<View>("y") {
13647 @Override
13648 public void setValue(View object, float value) {
13649 object.setY(value);
13650 }
13651
13652 @Override
13653 public Float get(View object) {
13654 return object.getY();
13655 }
13656 };
13657
13658 /**
13659 * A Property wrapper around the <code>rotation</code> functionality handled by the
13660 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
13661 */
13662 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
13663 @Override
13664 public void setValue(View object, float value) {
13665 object.setRotation(value);
13666 }
13667
13668 @Override
13669 public Float get(View object) {
13670 return object.getRotation();
13671 }
13672 };
13673
13674 /**
13675 * A Property wrapper around the <code>rotationX</code> functionality handled by the
13676 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
13677 */
13678 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
13679 @Override
13680 public void setValue(View object, float value) {
13681 object.setRotationX(value);
13682 }
13683
13684 @Override
13685 public Float get(View object) {
13686 return object.getRotationX();
13687 }
13688 };
13689
13690 /**
13691 * A Property wrapper around the <code>rotationY</code> functionality handled by the
13692 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
13693 */
13694 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
13695 @Override
13696 public void setValue(View object, float value) {
13697 object.setRotationY(value);
13698 }
13699
13700 @Override
13701 public Float get(View object) {
13702 return object.getRotationY();
13703 }
13704 };
13705
13706 /**
13707 * A Property wrapper around the <code>scaleX</code> functionality handled by the
13708 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
13709 */
13710 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
13711 @Override
13712 public void setValue(View object, float value) {
13713 object.setScaleX(value);
13714 }
13715
13716 @Override
13717 public Float get(View object) {
13718 return object.getScaleX();
13719 }
13720 };
13721
13722 /**
13723 * A Property wrapper around the <code>scaleY</code> functionality handled by the
13724 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
13725 */
13726 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
13727 @Override
13728 public void setValue(View object, float value) {
13729 object.setScaleY(value);
13730 }
13731
13732 @Override
13733 public Float get(View object) {
13734 return object.getScaleY();
13735 }
13736 };
13737
Jeff Brown33bbfd22011-02-24 20:55:35 -080013738 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013739 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
13740 * Each MeasureSpec represents a requirement for either the width or the height.
13741 * A MeasureSpec is comprised of a size and a mode. There are three possible
13742 * modes:
13743 * <dl>
13744 * <dt>UNSPECIFIED</dt>
13745 * <dd>
13746 * The parent has not imposed any constraint on the child. It can be whatever size
13747 * it wants.
13748 * </dd>
13749 *
13750 * <dt>EXACTLY</dt>
13751 * <dd>
13752 * The parent has determined an exact size for the child. The child is going to be
13753 * given those bounds regardless of how big it wants to be.
13754 * </dd>
13755 *
13756 * <dt>AT_MOST</dt>
13757 * <dd>
13758 * The child can be as large as it wants up to the specified size.
13759 * </dd>
13760 * </dl>
13761 *
13762 * MeasureSpecs are implemented as ints to reduce object allocation. This class
13763 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
13764 */
13765 public static class MeasureSpec {
13766 private static final int MODE_SHIFT = 30;
13767 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
13768
13769 /**
13770 * Measure specification mode: The parent has not imposed any constraint
13771 * on the child. It can be whatever size it wants.
13772 */
13773 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
13774
13775 /**
13776 * Measure specification mode: The parent has determined an exact size
13777 * for the child. The child is going to be given those bounds regardless
13778 * of how big it wants to be.
13779 */
13780 public static final int EXACTLY = 1 << MODE_SHIFT;
13781
13782 /**
13783 * Measure specification mode: The child can be as large as it wants up
13784 * to the specified size.
13785 */
13786 public static final int AT_MOST = 2 << MODE_SHIFT;
13787
13788 /**
13789 * Creates a measure specification based on the supplied size and mode.
13790 *
13791 * The mode must always be one of the following:
13792 * <ul>
13793 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
13794 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
13795 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
13796 * </ul>
13797 *
13798 * @param size the size of the measure specification
13799 * @param mode the mode of the measure specification
13800 * @return the measure specification based on size and mode
13801 */
13802 public static int makeMeasureSpec(int size, int mode) {
13803 return size + mode;
13804 }
13805
13806 /**
13807 * Extracts the mode from the supplied measure specification.
13808 *
13809 * @param measureSpec the measure specification to extract the mode from
13810 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
13811 * {@link android.view.View.MeasureSpec#AT_MOST} or
13812 * {@link android.view.View.MeasureSpec#EXACTLY}
13813 */
13814 public static int getMode(int measureSpec) {
13815 return (measureSpec & MODE_MASK);
13816 }
13817
13818 /**
13819 * Extracts the size from the supplied measure specification.
13820 *
13821 * @param measureSpec the measure specification to extract the size from
13822 * @return the size in pixels defined in the supplied measure specification
13823 */
13824 public static int getSize(int measureSpec) {
13825 return (measureSpec & ~MODE_MASK);
13826 }
13827
13828 /**
13829 * Returns a String representation of the specified measure
13830 * specification.
13831 *
13832 * @param measureSpec the measure specification to convert to a String
13833 * @return a String with the following format: "MeasureSpec: MODE SIZE"
13834 */
13835 public static String toString(int measureSpec) {
13836 int mode = getMode(measureSpec);
13837 int size = getSize(measureSpec);
13838
13839 StringBuilder sb = new StringBuilder("MeasureSpec: ");
13840
13841 if (mode == UNSPECIFIED)
13842 sb.append("UNSPECIFIED ");
13843 else if (mode == EXACTLY)
13844 sb.append("EXACTLY ");
13845 else if (mode == AT_MOST)
13846 sb.append("AT_MOST ");
13847 else
13848 sb.append(mode).append(" ");
13849
13850 sb.append(size);
13851 return sb.toString();
13852 }
13853 }
13854
13855 class CheckForLongPress implements Runnable {
13856
13857 private int mOriginalWindowAttachCount;
13858
13859 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070013860 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013861 && mOriginalWindowAttachCount == mWindowAttachCount) {
13862 if (performLongClick()) {
13863 mHasPerformedLongPress = true;
13864 }
13865 }
13866 }
13867
13868 public void rememberWindowAttachCount() {
13869 mOriginalWindowAttachCount = mWindowAttachCount;
13870 }
13871 }
Joe Malin32736f02011-01-19 16:14:20 -080013872
Adam Powelle14579b2009-12-16 18:39:52 -080013873 private final class CheckForTap implements Runnable {
13874 public void run() {
13875 mPrivateFlags &= ~PREPRESSED;
13876 mPrivateFlags |= PRESSED;
13877 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013878 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080013879 }
13880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013881
Adam Powella35d7682010-03-12 14:48:13 -080013882 private final class PerformClick implements Runnable {
13883 public void run() {
13884 performClick();
13885 }
13886 }
13887
Dianne Hackborn63042d62011-01-26 18:56:29 -080013888 /** @hide */
13889 public void hackTurnOffWindowResizeAnim(boolean off) {
13890 mAttachInfo.mTurnOffWindowResizeAnim = off;
13891 }
Joe Malin32736f02011-01-19 16:14:20 -080013892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013893 /**
Chet Haasea00f3862011-02-22 06:34:40 -080013894 * This method returns a ViewPropertyAnimator object, which can be used to animate
13895 * specific properties on this View.
13896 *
13897 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
13898 */
13899 public ViewPropertyAnimator animate() {
13900 if (mAnimator == null) {
13901 mAnimator = new ViewPropertyAnimator(this);
13902 }
13903 return mAnimator;
13904 }
13905
13906 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013907 * Interface definition for a callback to be invoked when a key event is
13908 * dispatched to this view. The callback will be invoked before the key
13909 * event is given to the view.
13910 */
13911 public interface OnKeyListener {
13912 /**
13913 * Called when a key is dispatched to a view. This allows listeners to
13914 * get a chance to respond before the target view.
13915 *
13916 * @param v The view the key has been dispatched to.
13917 * @param keyCode The code for the physical key that was pressed
13918 * @param event The KeyEvent object containing full information about
13919 * the event.
13920 * @return True if the listener has consumed the event, false otherwise.
13921 */
13922 boolean onKey(View v, int keyCode, KeyEvent event);
13923 }
13924
13925 /**
13926 * Interface definition for a callback to be invoked when a touch event is
13927 * dispatched to this view. The callback will be invoked before the touch
13928 * event is given to the view.
13929 */
13930 public interface OnTouchListener {
13931 /**
13932 * Called when a touch event is dispatched to a view. This allows listeners to
13933 * get a chance to respond before the target view.
13934 *
13935 * @param v The view the touch event has been dispatched to.
13936 * @param event The MotionEvent object containing full information about
13937 * the event.
13938 * @return True if the listener has consumed the event, false otherwise.
13939 */
13940 boolean onTouch(View v, MotionEvent event);
13941 }
13942
13943 /**
Jeff Brown10b62902011-06-20 16:40:37 -070013944 * Interface definition for a callback to be invoked when a hover event is
13945 * dispatched to this view. The callback will be invoked before the hover
13946 * event is given to the view.
13947 */
13948 public interface OnHoverListener {
13949 /**
13950 * Called when a hover event is dispatched to a view. This allows listeners to
13951 * get a chance to respond before the target view.
13952 *
13953 * @param v The view the hover event has been dispatched to.
13954 * @param event The MotionEvent object containing full information about
13955 * the event.
13956 * @return True if the listener has consumed the event, false otherwise.
13957 */
13958 boolean onHover(View v, MotionEvent event);
13959 }
13960
13961 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013962 * Interface definition for a callback to be invoked when a generic motion event is
13963 * dispatched to this view. The callback will be invoked before the generic motion
13964 * event is given to the view.
13965 */
13966 public interface OnGenericMotionListener {
13967 /**
13968 * Called when a generic motion event is dispatched to a view. This allows listeners to
13969 * get a chance to respond before the target view.
13970 *
13971 * @param v The view the generic motion event has been dispatched to.
13972 * @param event The MotionEvent object containing full information about
13973 * the event.
13974 * @return True if the listener has consumed the event, false otherwise.
13975 */
13976 boolean onGenericMotion(View v, MotionEvent event);
13977 }
13978
13979 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013980 * Interface definition for a callback to be invoked when a view has been clicked and held.
13981 */
13982 public interface OnLongClickListener {
13983 /**
13984 * Called when a view has been clicked and held.
13985 *
13986 * @param v The view that was clicked and held.
13987 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080013988 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013989 */
13990 boolean onLongClick(View v);
13991 }
13992
13993 /**
Chris Tate32affef2010-10-18 15:29:21 -070013994 * Interface definition for a callback to be invoked when a drag is being dispatched
13995 * to this view. The callback will be invoked before the hosting view's own
13996 * onDrag(event) method. If the listener wants to fall back to the hosting view's
13997 * onDrag(event) behavior, it should return 'false' from this callback.
13998 */
13999 public interface OnDragListener {
14000 /**
14001 * Called when a drag event is dispatched to a view. This allows listeners
14002 * to get a chance to override base View behavior.
14003 *
Joe Malin32736f02011-01-19 16:14:20 -080014004 * @param v The View that received the drag event.
14005 * @param event The {@link android.view.DragEvent} object for the drag event.
14006 * @return {@code true} if the drag event was handled successfully, or {@code false}
14007 * if the drag event was not handled. Note that {@code false} will trigger the View
14008 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070014009 */
14010 boolean onDrag(View v, DragEvent event);
14011 }
14012
14013 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014014 * Interface definition for a callback to be invoked when the focus state of
14015 * a view changed.
14016 */
14017 public interface OnFocusChangeListener {
14018 /**
14019 * Called when the focus state of a view has changed.
14020 *
14021 * @param v The view whose state has changed.
14022 * @param hasFocus The new focus state of v.
14023 */
14024 void onFocusChange(View v, boolean hasFocus);
14025 }
14026
14027 /**
14028 * Interface definition for a callback to be invoked when a view is clicked.
14029 */
14030 public interface OnClickListener {
14031 /**
14032 * Called when a view has been clicked.
14033 *
14034 * @param v The view that was clicked.
14035 */
14036 void onClick(View v);
14037 }
14038
14039 /**
14040 * Interface definition for a callback to be invoked when the context menu
14041 * for this view is being built.
14042 */
14043 public interface OnCreateContextMenuListener {
14044 /**
14045 * Called when the context menu for this view is being built. It is not
14046 * safe to hold onto the menu after this method returns.
14047 *
14048 * @param menu The context menu that is being built
14049 * @param v The view for which the context menu is being built
14050 * @param menuInfo Extra information about the item for which the
14051 * context menu should be shown. This information will vary
14052 * depending on the class of v.
14053 */
14054 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
14055 }
14056
Joe Onorato664644d2011-01-23 17:53:23 -080014057 /**
14058 * Interface definition for a callback to be invoked when the status bar changes
14059 * visibility.
14060 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070014061 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080014062 */
14063 public interface OnSystemUiVisibilityChangeListener {
14064 /**
14065 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070014066 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080014067 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040014068 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
14069 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080014070 */
14071 public void onSystemUiVisibilityChange(int visibility);
14072 }
14073
Adam Powell4afd62b2011-02-18 15:02:18 -080014074 /**
14075 * Interface definition for a callback to be invoked when this view is attached
14076 * or detached from its window.
14077 */
14078 public interface OnAttachStateChangeListener {
14079 /**
14080 * Called when the view is attached to a window.
14081 * @param v The view that was attached
14082 */
14083 public void onViewAttachedToWindow(View v);
14084 /**
14085 * Called when the view is detached from a window.
14086 * @param v The view that was detached
14087 */
14088 public void onViewDetachedFromWindow(View v);
14089 }
14090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014091 private final class UnsetPressedState implements Runnable {
14092 public void run() {
14093 setPressed(false);
14094 }
14095 }
14096
14097 /**
14098 * Base class for derived classes that want to save and restore their own
14099 * state in {@link android.view.View#onSaveInstanceState()}.
14100 */
14101 public static class BaseSavedState extends AbsSavedState {
14102 /**
14103 * Constructor used when reading from a parcel. Reads the state of the superclass.
14104 *
14105 * @param source
14106 */
14107 public BaseSavedState(Parcel source) {
14108 super(source);
14109 }
14110
14111 /**
14112 * Constructor called by derived classes when creating their SavedState objects
14113 *
14114 * @param superState The state of the superclass of this view
14115 */
14116 public BaseSavedState(Parcelable superState) {
14117 super(superState);
14118 }
14119
14120 public static final Parcelable.Creator<BaseSavedState> CREATOR =
14121 new Parcelable.Creator<BaseSavedState>() {
14122 public BaseSavedState createFromParcel(Parcel in) {
14123 return new BaseSavedState(in);
14124 }
14125
14126 public BaseSavedState[] newArray(int size) {
14127 return new BaseSavedState[size];
14128 }
14129 };
14130 }
14131
14132 /**
14133 * A set of information given to a view when it is attached to its parent
14134 * window.
14135 */
14136 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014137 interface Callbacks {
14138 void playSoundEffect(int effectId);
14139 boolean performHapticFeedback(int effectId, boolean always);
14140 }
14141
14142 /**
14143 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
14144 * to a Handler. This class contains the target (View) to invalidate and
14145 * the coordinates of the dirty rectangle.
14146 *
14147 * For performance purposes, this class also implements a pool of up to
14148 * POOL_LIMIT objects that get reused. This reduces memory allocations
14149 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014150 */
Romain Guyd928d682009-03-31 17:52:16 -070014151 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014152 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070014153 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
14154 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070014155 public InvalidateInfo newInstance() {
14156 return new InvalidateInfo();
14157 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014158
Romain Guyd928d682009-03-31 17:52:16 -070014159 public void onAcquired(InvalidateInfo element) {
14160 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014161
Romain Guyd928d682009-03-31 17:52:16 -070014162 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070014163 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070014164 }
14165 }, POOL_LIMIT)
14166 );
14167
14168 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014169 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014170
14171 View target;
14172
14173 int left;
14174 int top;
14175 int right;
14176 int bottom;
14177
Romain Guyd928d682009-03-31 17:52:16 -070014178 public void setNextPoolable(InvalidateInfo element) {
14179 mNext = element;
14180 }
14181
14182 public InvalidateInfo getNextPoolable() {
14183 return mNext;
14184 }
14185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014186 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070014187 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014188 }
14189
14190 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070014191 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014192 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014193
14194 public boolean isPooled() {
14195 return mIsPooled;
14196 }
14197
14198 public void setPooled(boolean isPooled) {
14199 mIsPooled = isPooled;
14200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014201 }
14202
14203 final IWindowSession mSession;
14204
14205 final IWindow mWindow;
14206
14207 final IBinder mWindowToken;
14208
14209 final Callbacks mRootCallbacks;
14210
Romain Guy59a12ca2011-06-09 17:48:21 -070014211 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080014212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014213 /**
14214 * The top view of the hierarchy.
14215 */
14216 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070014217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014218 IBinder mPanelParentWindowToken;
14219 Surface mSurface;
14220
Romain Guyb051e892010-09-28 19:09:36 -070014221 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014222 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070014223 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080014224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014225 /**
Romain Guy8506ab42009-06-11 17:35:47 -070014226 * Scale factor used by the compatibility mode
14227 */
14228 float mApplicationScale;
14229
14230 /**
14231 * Indicates whether the application is in compatibility mode
14232 */
14233 boolean mScalingRequired;
14234
14235 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014236 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080014237 */
14238 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080014239
Dianne Hackborn63042d62011-01-26 18:56:29 -080014240 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014241 * Left position of this view's window
14242 */
14243 int mWindowLeft;
14244
14245 /**
14246 * Top position of this view's window
14247 */
14248 int mWindowTop;
14249
14250 /**
Adam Powell26153a32010-11-08 15:22:27 -080014251 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070014252 */
Adam Powell26153a32010-11-08 15:22:27 -080014253 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070014254
14255 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014256 * For windows that are full-screen but using insets to layout inside
14257 * of the screen decorations, these are the current insets for the
14258 * content of the window.
14259 */
14260 final Rect mContentInsets = new Rect();
14261
14262 /**
14263 * For windows that are full-screen but using insets to layout inside
14264 * of the screen decorations, these are the current insets for the
14265 * actual visible parts of the window.
14266 */
14267 final Rect mVisibleInsets = new Rect();
14268
14269 /**
14270 * The internal insets given by this window. This value is
14271 * supplied by the client (through
14272 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
14273 * be given to the window manager when changed to be used in laying
14274 * out windows behind it.
14275 */
14276 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
14277 = new ViewTreeObserver.InternalInsetsInfo();
14278
14279 /**
14280 * All views in the window's hierarchy that serve as scroll containers,
14281 * used to determine if the window can be resized or must be panned
14282 * to adjust for a soft input area.
14283 */
14284 final ArrayList<View> mScrollContainers = new ArrayList<View>();
14285
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070014286 final KeyEvent.DispatcherState mKeyDispatchState
14287 = new KeyEvent.DispatcherState();
14288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014289 /**
14290 * Indicates whether the view's window currently has the focus.
14291 */
14292 boolean mHasWindowFocus;
14293
14294 /**
14295 * The current visibility of the window.
14296 */
14297 int mWindowVisibility;
14298
14299 /**
14300 * Indicates the time at which drawing started to occur.
14301 */
14302 long mDrawingTime;
14303
14304 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070014305 * Indicates whether or not ignoring the DIRTY_MASK flags.
14306 */
14307 boolean mIgnoreDirtyState;
14308
14309 /**
Romain Guy02ccac62011-06-24 13:20:23 -070014310 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
14311 * to avoid clearing that flag prematurely.
14312 */
14313 boolean mSetIgnoreDirtyState = false;
14314
14315 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014316 * Indicates whether the view's window is currently in touch mode.
14317 */
14318 boolean mInTouchMode;
14319
14320 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014321 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014322 * the next time it performs a traversal
14323 */
14324 boolean mRecomputeGlobalAttributes;
14325
14326 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014327 * Set during a traveral if any views want to keep the screen on.
14328 */
14329 boolean mKeepScreenOn;
14330
14331 /**
Joe Onorato664644d2011-01-23 17:53:23 -080014332 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
14333 */
14334 int mSystemUiVisibility;
14335
14336 /**
14337 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
14338 * attached.
14339 */
14340 boolean mHasSystemUiListeners;
14341
14342 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014343 * Set if the visibility of any views has changed.
14344 */
14345 boolean mViewVisibilityChanged;
14346
14347 /**
14348 * Set to true if a view has been scrolled.
14349 */
14350 boolean mViewScrollChanged;
14351
14352 /**
14353 * Global to the view hierarchy used as a temporary for dealing with
14354 * x/y points in the transparent region computations.
14355 */
14356 final int[] mTransparentLocation = new int[2];
14357
14358 /**
14359 * Global to the view hierarchy used as a temporary for dealing with
14360 * x/y points in the ViewGroup.invalidateChild implementation.
14361 */
14362 final int[] mInvalidateChildLocation = new int[2];
14363
Chet Haasec3aa3612010-06-17 08:50:37 -070014364
14365 /**
14366 * Global to the view hierarchy used as a temporary for dealing with
14367 * x/y location when view is transformed.
14368 */
14369 final float[] mTmpTransformLocation = new float[2];
14370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014371 /**
14372 * The view tree observer used to dispatch global events like
14373 * layout, pre-draw, touch mode change, etc.
14374 */
14375 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
14376
14377 /**
14378 * A Canvas used by the view hierarchy to perform bitmap caching.
14379 */
14380 Canvas mCanvas;
14381
14382 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014383 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014384 * handler can be used to pump events in the UI events queue.
14385 */
14386 final Handler mHandler;
14387
14388 /**
14389 * Identifier for messages requesting the view to be invalidated.
14390 * Such messages should be sent to {@link #mHandler}.
14391 */
14392 static final int INVALIDATE_MSG = 0x1;
14393
14394 /**
14395 * Identifier for messages requesting the view to invalidate a region.
14396 * Such messages should be sent to {@link #mHandler}.
14397 */
14398 static final int INVALIDATE_RECT_MSG = 0x2;
14399
14400 /**
14401 * Temporary for use in computing invalidate rectangles while
14402 * calling up the hierarchy.
14403 */
14404 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070014405
14406 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070014407 * Temporary for use in computing hit areas with transformed views
14408 */
14409 final RectF mTmpTransformRect = new RectF();
14410
14411 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070014412 * Temporary list for use in collecting focusable descendents of a view.
14413 */
14414 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
14415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014416 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014417 * The id of the window for accessibility purposes.
14418 */
14419 int mAccessibilityWindowId = View.NO_ID;
14420
14421 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014422 * Creates a new set of attachment information with the specified
14423 * events handler and thread.
14424 *
14425 * @param handler the events handler the view must use
14426 */
14427 AttachInfo(IWindowSession session, IWindow window,
14428 Handler handler, Callbacks effectPlayer) {
14429 mSession = session;
14430 mWindow = window;
14431 mWindowToken = window.asBinder();
14432 mHandler = handler;
14433 mRootCallbacks = effectPlayer;
14434 }
14435 }
14436
14437 /**
14438 * <p>ScrollabilityCache holds various fields used by a View when scrolling
14439 * is supported. This avoids keeping too many unused fields in most
14440 * instances of View.</p>
14441 */
Mike Cleronf116bf82009-09-27 19:14:12 -070014442 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080014443
Mike Cleronf116bf82009-09-27 19:14:12 -070014444 /**
14445 * Scrollbars are not visible
14446 */
14447 public static final int OFF = 0;
14448
14449 /**
14450 * Scrollbars are visible
14451 */
14452 public static final int ON = 1;
14453
14454 /**
14455 * Scrollbars are fading away
14456 */
14457 public static final int FADING = 2;
14458
14459 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080014460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014461 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070014462 public int scrollBarDefaultDelayBeforeFade;
14463 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014464
14465 public int scrollBarSize;
14466 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070014467 public float[] interpolatorValues;
14468 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014469
14470 public final Paint paint;
14471 public final Matrix matrix;
14472 public Shader shader;
14473
Mike Cleronf116bf82009-09-27 19:14:12 -070014474 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
14475
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014476 private static final float[] OPAQUE = { 255 };
14477 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080014478
Mike Cleronf116bf82009-09-27 19:14:12 -070014479 /**
14480 * When fading should start. This time moves into the future every time
14481 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
14482 */
14483 public long fadeStartTime;
14484
14485
14486 /**
14487 * The current state of the scrollbars: ON, OFF, or FADING
14488 */
14489 public int state = OFF;
14490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014491 private int mLastColor;
14492
Mike Cleronf116bf82009-09-27 19:14:12 -070014493 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014494 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
14495 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070014496 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
14497 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014498
14499 paint = new Paint();
14500 matrix = new Matrix();
14501 // use use a height of 1, and then wack the matrix each time we
14502 // actually use it.
14503 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014505 paint.setShader(shader);
14506 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070014507 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014508 }
Romain Guy8506ab42009-06-11 17:35:47 -070014509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014510 public void setFadeColor(int color) {
14511 if (color != 0 && color != mLastColor) {
14512 mLastColor = color;
14513 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070014514
Romain Guye55e1a72009-08-27 10:42:26 -070014515 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
14516 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014518 paint.setShader(shader);
14519 // Restore the default transfer mode (src_over)
14520 paint.setXfermode(null);
14521 }
14522 }
Joe Malin32736f02011-01-19 16:14:20 -080014523
Mike Cleronf116bf82009-09-27 19:14:12 -070014524 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070014525 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070014526 if (now >= fadeStartTime) {
14527
14528 // the animation fades the scrollbars out by changing
14529 // the opacity (alpha) from fully opaque to fully
14530 // transparent
14531 int nextFrame = (int) now;
14532 int framesCount = 0;
14533
14534 Interpolator interpolator = scrollBarInterpolator;
14535
14536 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014537 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070014538
14539 // End transparent
14540 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014541 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070014542
14543 state = FADING;
14544
14545 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080014546 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070014547 }
14548 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070014549 }
Mike Cleronf116bf82009-09-27 19:14:12 -070014550
Svetoslav Ganova0156172011-06-26 17:55:44 -070014551 /**
14552 * Resuable callback for sending
14553 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
14554 */
14555 private class SendViewScrolledAccessibilityEvent implements Runnable {
14556 public volatile boolean mIsPending;
14557
14558 public void run() {
14559 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
14560 mIsPending = false;
14561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014562 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070014563
14564 /**
14565 * <p>
14566 * This class represents a delegate that can be registered in a {@link View}
14567 * to enhance accessibility support via composition rather via inheritance.
14568 * It is specifically targeted to widget developers that extend basic View
14569 * classes i.e. classes in package android.view, that would like their
14570 * applications to be backwards compatible.
14571 * </p>
14572 * <p>
14573 * A scenario in which a developer would like to use an accessibility delegate
14574 * is overriding a method introduced in a later API version then the minimal API
14575 * version supported by the application. For example, the method
14576 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
14577 * in API version 4 when the accessibility APIs were first introduced. If a
14578 * developer would like his application to run on API version 4 devices (assuming
14579 * all other APIs used by the application are version 4 or lower) and take advantage
14580 * of this method, instead of overriding the method which would break the application's
14581 * backwards compatibility, he can override the corresponding method in this
14582 * delegate and register the delegate in the target View if the API version of
14583 * the system is high enough i.e. the API version is same or higher to the API
14584 * version that introduced
14585 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
14586 * </p>
14587 * <p>
14588 * Here is an example implementation:
14589 * </p>
14590 * <code><pre><p>
14591 * if (Build.VERSION.SDK_INT >= 14) {
14592 * // If the API version is equal of higher than the version in
14593 * // which onInitializeAccessibilityNodeInfo was introduced we
14594 * // register a delegate with a customized implementation.
14595 * View view = findViewById(R.id.view_id);
14596 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
14597 * public void onInitializeAccessibilityNodeInfo(View host,
14598 * AccessibilityNodeInfo info) {
14599 * // Let the default implementation populate the info.
14600 * super.onInitializeAccessibilityNodeInfo(host, info);
14601 * // Set some other information.
14602 * info.setEnabled(host.isEnabled());
14603 * }
14604 * });
14605 * }
14606 * </code></pre></p>
14607 * <p>
14608 * This delegate contains methods that correspond to the accessibility methods
14609 * in View. If a delegate has been specified the implementation in View hands
14610 * off handling to the corresponding method in this delegate. The default
14611 * implementation the delegate methods behaves exactly as the corresponding
14612 * method in View for the case of no accessibility delegate been set. Hence,
14613 * to customize the behavior of a View method, clients can override only the
14614 * corresponding delegate method without altering the behavior of the rest
14615 * accessibility related methods of the host view.
14616 * </p>
14617 */
14618 public static class AccessibilityDelegate {
14619
14620 /**
14621 * Sends an accessibility event of the given type. If accessibility is not
14622 * enabled this method has no effect.
14623 * <p>
14624 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
14625 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
14626 * been set.
14627 * </p>
14628 *
14629 * @param host The View hosting the delegate.
14630 * @param eventType The type of the event to send.
14631 *
14632 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
14633 */
14634 public void sendAccessibilityEvent(View host, int eventType) {
14635 host.sendAccessibilityEventInternal(eventType);
14636 }
14637
14638 /**
14639 * Sends an accessibility event. This method behaves exactly as
14640 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
14641 * empty {@link AccessibilityEvent} and does not perform a check whether
14642 * accessibility is enabled.
14643 * <p>
14644 * The default implementation behaves as
14645 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14646 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
14647 * the case of no accessibility delegate been set.
14648 * </p>
14649 *
14650 * @param host The View hosting the delegate.
14651 * @param event The event to send.
14652 *
14653 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14654 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14655 */
14656 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
14657 host.sendAccessibilityEventUncheckedInternal(event);
14658 }
14659
14660 /**
14661 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
14662 * to its children for adding their text content to the event.
14663 * <p>
14664 * The default implementation behaves as
14665 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14666 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
14667 * the case of no accessibility delegate been set.
14668 * </p>
14669 *
14670 * @param host The View hosting the delegate.
14671 * @param event The event.
14672 * @return True if the event population was completed.
14673 *
14674 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14675 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14676 */
14677 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14678 return host.dispatchPopulateAccessibilityEventInternal(event);
14679 }
14680
14681 /**
14682 * Gives a chance to the host View to populate the accessibility event with its
14683 * text content.
14684 * <p>
14685 * The default implementation behaves as
14686 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
14687 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
14688 * the case of no accessibility delegate been set.
14689 * </p>
14690 *
14691 * @param host The View hosting the delegate.
14692 * @param event The accessibility event which to populate.
14693 *
14694 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
14695 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
14696 */
14697 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14698 host.onPopulateAccessibilityEventInternal(event);
14699 }
14700
14701 /**
14702 * Initializes an {@link AccessibilityEvent} with information about the
14703 * the host View which is the event source.
14704 * <p>
14705 * The default implementation behaves as
14706 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
14707 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
14708 * the case of no accessibility delegate been set.
14709 * </p>
14710 *
14711 * @param host The View hosting the delegate.
14712 * @param event The event to initialize.
14713 *
14714 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
14715 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
14716 */
14717 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
14718 host.onInitializeAccessibilityEventInternal(event);
14719 }
14720
14721 /**
14722 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
14723 * <p>
14724 * The default implementation behaves as
14725 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14726 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
14727 * the case of no accessibility delegate been set.
14728 * </p>
14729 *
14730 * @param host The View hosting the delegate.
14731 * @param info The instance to initialize.
14732 *
14733 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14734 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14735 */
14736 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
14737 host.onInitializeAccessibilityNodeInfoInternal(info);
14738 }
14739
14740 /**
14741 * Called when a child of the host View has requested sending an
14742 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
14743 * to augment the event.
14744 * <p>
14745 * The default implementation behaves as
14746 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14747 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
14748 * the case of no accessibility delegate been set.
14749 * </p>
14750 *
14751 * @param host The View hosting the delegate.
14752 * @param child The child which requests sending the event.
14753 * @param event The event to be sent.
14754 * @return True if the event should be sent
14755 *
14756 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14757 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14758 */
14759 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
14760 AccessibilityEvent event) {
14761 return host.onRequestSendAccessibilityEventInternal(child, event);
14762 }
14763 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014764}