blob: faddb43c75c5454321079daf7f9cffa7cda6e1ef [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 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -070097 * <div class="special reference">
98 * <h3>Developer Guides</h3>
99 * <p>For information about using this class to develop your application's user interface,
100 * read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700102 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 * <a name="Using"></a>
104 * <h3>Using Views</h3>
105 * <p>
106 * All of the views in a window are arranged in a single tree. You can add views
107 * either from code or by specifying a tree of views in one or more XML layout
108 * files. There are many specialized subclasses of views that act as controls or
109 * are capable of displaying text, images, or other content.
110 * </p>
111 * <p>
112 * Once you have created a tree of views, there are typically a few types of
113 * common operations you may wish to perform:
114 * <ul>
115 * <li><strong>Set properties:</strong> for example setting the text of a
116 * {@link android.widget.TextView}. The available properties and the methods
117 * that set them will vary among the different subclasses of views. Note that
118 * properties that are known at build time can be set in the XML layout
119 * files.</li>
120 * <li><strong>Set focus:</strong> The framework will handled moving focus in
121 * response to user input. To force focus to a specific view, call
122 * {@link #requestFocus}.</li>
123 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
124 * that will be notified when something interesting happens to the view. For
125 * example, all views will let you set a listener to be notified when the view
126 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700127 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
128 * Other view subclasses offer more specialized listeners. For example, a Button
129 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700131 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 * </ul>
133 * </p>
134 * <p><em>
135 * Note: The Android framework is responsible for measuring, laying out and
136 * drawing views. You should not call methods that perform these actions on
137 * views yourself unless you are actually implementing a
138 * {@link android.view.ViewGroup}.
139 * </em></p>
140 *
141 * <a name="Lifecycle"></a>
142 * <h3>Implementing a Custom View</h3>
143 *
144 * <p>
145 * To implement a custom view, you will usually begin by providing overrides for
146 * some of the standard methods that the framework calls on all views. You do
147 * not need to override all of these methods. In fact, you can start by just
148 * overriding {@link #onDraw(android.graphics.Canvas)}.
149 * <table border="2" width="85%" align="center" cellpadding="5">
150 * <thead>
151 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
152 * </thead>
153 *
154 * <tbody>
155 * <tr>
156 * <td rowspan="2">Creation</td>
157 * <td>Constructors</td>
158 * <td>There is a form of the constructor that are called when the view
159 * is created from code and a form that is called when the view is
160 * inflated from a layout file. The second form should parse and apply
161 * any attributes defined in the layout file.
162 * </td>
163 * </tr>
164 * <tr>
165 * <td><code>{@link #onFinishInflate()}</code></td>
166 * <td>Called after a view and all of its children has been inflated
167 * from XML.</td>
168 * </tr>
169 *
170 * <tr>
171 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700172 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 * <td>Called to determine the size requirements for this view and all
174 * of its children.
175 * </td>
176 * </tr>
177 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700178 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 * <td>Called when this view should assign a size and position to all
180 * of its children.
181 * </td>
182 * </tr>
183 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700184 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 * <td>Called when the size of this view has changed.
186 * </td>
187 * </tr>
188 *
189 * <tr>
190 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700191 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 * <td>Called when the view should render its content.
193 * </td>
194 * </tr>
195 *
196 * <tr>
197 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700198 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 * <td>Called when a new key event occurs.
200 * </td>
201 * </tr>
202 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700203 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 * <td>Called when a key up event occurs.
205 * </td>
206 * </tr>
207 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700208 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 * <td>Called when a trackball motion event occurs.
210 * </td>
211 * </tr>
212 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700213 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 * <td>Called when a touch screen motion event occurs.
215 * </td>
216 * </tr>
217 *
218 * <tr>
219 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700220 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 * <td>Called when the view gains or loses focus.
222 * </td>
223 * </tr>
224 *
225 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700226 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 * <td>Called when the window containing the view gains or loses focus.
228 * </td>
229 * </tr>
230 *
231 * <tr>
232 * <td rowspan="3">Attaching</td>
233 * <td><code>{@link #onAttachedToWindow()}</code></td>
234 * <td>Called when the view is attached to a window.
235 * </td>
236 * </tr>
237 *
238 * <tr>
239 * <td><code>{@link #onDetachedFromWindow}</code></td>
240 * <td>Called when the view is detached from its window.
241 * </td>
242 * </tr>
243 *
244 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700245 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 * <td>Called when the visibility of the window containing the view
247 * has changed.
248 * </td>
249 * </tr>
250 * </tbody>
251 *
252 * </table>
253 * </p>
254 *
255 * <a name="IDs"></a>
256 * <h3>IDs</h3>
257 * Views may have an integer id associated with them. These ids are typically
258 * assigned in the layout XML files, and are used to find specific views within
259 * the view tree. A common pattern is to:
260 * <ul>
261 * <li>Define a Button in the layout file and assign it a unique ID.
262 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700263 * &lt;Button
264 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 * android:layout_width="wrap_content"
266 * android:layout_height="wrap_content"
267 * android:text="@string/my_button_text"/&gt;
268 * </pre></li>
269 * <li>From the onCreate method of an Activity, find the Button
270 * <pre class="prettyprint">
271 * Button myButton = (Button) findViewById(R.id.my_button);
272 * </pre></li>
273 * </ul>
274 * <p>
275 * View IDs need not be unique throughout the tree, but it is good practice to
276 * ensure that they are at least unique within the part of the tree you are
277 * searching.
278 * </p>
279 *
280 * <a name="Position"></a>
281 * <h3>Position</h3>
282 * <p>
283 * The geometry of a view is that of a rectangle. A view has a location,
284 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
285 * two dimensions, expressed as a width and a height. The unit for location
286 * and dimensions is the pixel.
287 * </p>
288 *
289 * <p>
290 * It is possible to retrieve the location of a view by invoking the methods
291 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
292 * coordinate of the rectangle representing the view. The latter returns the
293 * top, or Y, coordinate of the rectangle representing the view. These methods
294 * both return the location of the view relative to its parent. For instance,
295 * when getLeft() returns 20, that means the view is located 20 pixels to the
296 * right of the left edge of its direct parent.
297 * </p>
298 *
299 * <p>
300 * In addition, several convenience methods are offered to avoid unnecessary
301 * computations, namely {@link #getRight()} and {@link #getBottom()}.
302 * These methods return the coordinates of the right and bottom edges of the
303 * rectangle representing the view. For instance, calling {@link #getRight()}
304 * is similar to the following computation: <code>getLeft() + getWidth()</code>
305 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
306 * </p>
307 *
308 * <a name="SizePaddingMargins"></a>
309 * <h3>Size, padding and margins</h3>
310 * <p>
311 * The size of a view is expressed with a width and a height. A view actually
312 * possess two pairs of width and height values.
313 * </p>
314 *
315 * <p>
316 * The first pair is known as <em>measured width</em> and
317 * <em>measured height</em>. These dimensions define how big a view wants to be
318 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
319 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
320 * and {@link #getMeasuredHeight()}.
321 * </p>
322 *
323 * <p>
324 * The second pair is simply known as <em>width</em> and <em>height</em>, or
325 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
326 * dimensions define the actual size of the view on screen, at drawing time and
327 * after layout. These values may, but do not have to, be different from the
328 * measured width and height. The width and height can be obtained by calling
329 * {@link #getWidth()} and {@link #getHeight()}.
330 * </p>
331 *
332 * <p>
333 * To measure its dimensions, a view takes into account its padding. The padding
334 * is expressed in pixels for the left, top, right and bottom parts of the view.
335 * Padding can be used to offset the content of the view by a specific amount of
336 * pixels. For instance, a left padding of 2 will push the view's content by
337 * 2 pixels to the right of the left edge. Padding can be set using the
338 * {@link #setPadding(int, int, int, int)} method and queried by calling
339 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
Fabrice Di Megliod8703a92011-06-16 18:54:08 -0700340 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 * </p>
342 *
343 * <p>
344 * Even though a view can define a padding, it does not provide any support for
345 * margins. However, view groups provide such a support. Refer to
346 * {@link android.view.ViewGroup} and
347 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
348 * </p>
349 *
350 * <a name="Layout"></a>
351 * <h3>Layout</h3>
352 * <p>
353 * Layout is a two pass process: a measure pass and a layout pass. The measuring
354 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
355 * of the view tree. Each view pushes dimension specifications down the tree
356 * during the recursion. At the end of the measure pass, every view has stored
357 * its measurements. The second pass happens in
358 * {@link #layout(int,int,int,int)} and is also top-down. During
359 * this pass each parent is responsible for positioning all of its children
360 * using the sizes computed in the measure pass.
361 * </p>
362 *
363 * <p>
364 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
365 * {@link #getMeasuredHeight()} values must be set, along with those for all of
366 * that view's descendants. A view's measured width and measured height values
367 * must respect the constraints imposed by the view's parents. This guarantees
368 * that at the end of the measure pass, all parents accept all of their
369 * children's measurements. A parent view may call measure() more than once on
370 * its children. For example, the parent may measure each child once with
371 * unspecified dimensions to find out how big they want to be, then call
372 * measure() on them again with actual numbers if the sum of all the children's
373 * unconstrained sizes is too big or too small.
374 * </p>
375 *
376 * <p>
377 * The measure pass uses two classes to communicate dimensions. The
378 * {@link MeasureSpec} class is used by views to tell their parents how they
379 * want to be measured and positioned. The base LayoutParams class just
380 * describes how big the view wants to be for both width and height. For each
381 * dimension, it can specify one of:
382 * <ul>
383 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800384 * <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 -0800385 * (minus padding)
386 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
387 * enclose its content (plus padding).
388 * </ul>
389 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
390 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
391 * an X and Y value.
392 * </p>
393 *
394 * <p>
395 * MeasureSpecs are used to push requirements down the tree from parent to
396 * child. A MeasureSpec can be in one of three modes:
397 * <ul>
398 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
399 * of a child view. For example, a LinearLayout may call measure() on its child
400 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
401 * tall the child view wants to be given a width of 240 pixels.
402 * <li>EXACTLY: This is used by the parent to impose an exact size on the
403 * child. The child must use this size, and guarantee that all of its
404 * descendants will fit within this size.
405 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
406 * child. The child must gurantee that it and all of its descendants will fit
407 * within this size.
408 * </ul>
409 * </p>
410 *
411 * <p>
412 * To intiate a layout, call {@link #requestLayout}. This method is typically
413 * called by a view on itself when it believes that is can no longer fit within
414 * its current bounds.
415 * </p>
416 *
417 * <a name="Drawing"></a>
418 * <h3>Drawing</h3>
419 * <p>
420 * Drawing is handled by walking the tree and rendering each view that
421 * intersects the the invalid region. Because the tree is traversed in-order,
422 * this means that parents will draw before (i.e., behind) their children, with
423 * siblings drawn in the order they appear in the tree.
424 * If you set a background drawable for a View, then the View will draw it for you
425 * before calling back to its <code>onDraw()</code> method.
426 * </p>
427 *
428 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700429 * 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 -0800430 * </p>
431 *
432 * <p>
433 * To force a view to draw, call {@link #invalidate()}.
434 * </p>
435 *
436 * <a name="EventHandlingThreading"></a>
437 * <h3>Event Handling and Threading</h3>
438 * <p>
439 * The basic cycle of a view is as follows:
440 * <ol>
441 * <li>An event comes in and is dispatched to the appropriate view. The view
442 * handles the event and notifies any listeners.</li>
443 * <li>If in the course of processing the event, the view's bounds may need
444 * to be changed, the view will call {@link #requestLayout()}.</li>
445 * <li>Similarly, if in the course of processing the event the view's appearance
446 * may need to be changed, the view will call {@link #invalidate()}.</li>
447 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
448 * the framework will take care of measuring, laying out, and drawing the tree
449 * as appropriate.</li>
450 * </ol>
451 * </p>
452 *
453 * <p><em>Note: The entire view tree is single threaded. You must always be on
454 * the UI thread when calling any method on any view.</em>
455 * If you are doing work on other threads and want to update the state of a view
456 * from that thread, you should use a {@link Handler}.
457 * </p>
458 *
459 * <a name="FocusHandling"></a>
460 * <h3>Focus Handling</h3>
461 * <p>
462 * The framework will handle routine focus movement in response to user input.
463 * This includes changing the focus as views are removed or hidden, or as new
464 * views become available. Views indicate their willingness to take focus
465 * through the {@link #isFocusable} method. To change whether a view can take
466 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
467 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
468 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
469 * </p>
470 * <p>
471 * Focus movement is based on an algorithm which finds the nearest neighbor in a
472 * given direction. In rare cases, the default algorithm may not match the
473 * intended behavior of the developer. In these situations, you can provide
474 * explicit overrides by using these XML attributes in the layout file:
475 * <pre>
476 * nextFocusDown
477 * nextFocusLeft
478 * nextFocusRight
479 * nextFocusUp
480 * </pre>
481 * </p>
482 *
483 *
484 * <p>
485 * To get a particular view to take focus, call {@link #requestFocus()}.
486 * </p>
487 *
488 * <a name="TouchMode"></a>
489 * <h3>Touch Mode</h3>
490 * <p>
491 * When a user is navigating a user interface via directional keys such as a D-pad, it is
492 * necessary to give focus to actionable items such as buttons so the user can see
493 * what will take input. If the device has touch capabilities, however, and the user
494 * begins interacting with the interface by touching it, it is no longer necessary to
495 * always highlight, or give focus to, a particular view. This motivates a mode
496 * for interaction named 'touch mode'.
497 * </p>
498 * <p>
499 * For a touch capable device, once the user touches the screen, the device
500 * will enter touch mode. From this point onward, only views for which
501 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
502 * Other views that are touchable, like buttons, will not take focus when touched; they will
503 * only fire the on click listeners.
504 * </p>
505 * <p>
506 * Any time a user hits a directional key, such as a D-pad direction, the view device will
507 * exit touch mode, and find a view to take focus, so that the user may resume interacting
508 * with the user interface without touching the screen again.
509 * </p>
510 * <p>
511 * The touch mode state is maintained across {@link android.app.Activity}s. Call
512 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
513 * </p>
514 *
515 * <a name="Scrolling"></a>
516 * <h3>Scrolling</h3>
517 * <p>
518 * The framework provides basic support for views that wish to internally
519 * scroll their content. This includes keeping track of the X and Y scroll
520 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800521 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700522 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 * </p>
524 *
525 * <a name="Tags"></a>
526 * <h3>Tags</h3>
527 * <p>
528 * Unlike IDs, tags are not used to identify views. Tags are essentially an
529 * extra piece of information that can be associated with a view. They are most
530 * often used as a convenience to store data related to views in the views
531 * themselves rather than by putting them in a separate structure.
532 * </p>
533 *
534 * <a name="Animation"></a>
535 * <h3>Animation</h3>
536 * <p>
537 * You can attach an {@link Animation} object to a view using
538 * {@link #setAnimation(Animation)} or
539 * {@link #startAnimation(Animation)}. The animation can alter the scale,
540 * rotation, translation and alpha of a view over time. If the animation is
541 * attached to a view that has children, the animation will affect the entire
542 * subtree rooted by that node. When an animation is started, the framework will
543 * take care of redrawing the appropriate views until the animation completes.
544 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800545 * <p>
546 * Starting with Android 3.0, the preferred way of animating views is to use the
547 * {@link android.animation} package APIs.
548 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 *
Jeff Brown85a31762010-09-01 17:01:00 -0700550 * <a name="Security"></a>
551 * <h3>Security</h3>
552 * <p>
553 * Sometimes it is essential that an application be able to verify that an action
554 * is being performed with the full knowledge and consent of the user, such as
555 * granting a permission request, making a purchase or clicking on an advertisement.
556 * Unfortunately, a malicious application could try to spoof the user into
557 * performing these actions, unaware, by concealing the intended purpose of the view.
558 * As a remedy, the framework offers a touch filtering mechanism that can be used to
559 * improve the security of views that provide access to sensitive functionality.
560 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700561 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800562 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700563 * will discard touches that are received whenever the view's window is obscured by
564 * another visible window. As a result, the view will not receive touches whenever a
565 * toast, dialog or other window appears above the view's window.
566 * </p><p>
567 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700568 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
569 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700570 * </p>
571 *
Romain Guy171c5922011-01-06 10:04:23 -0800572 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700573 * @attr ref android.R.styleable#View_background
574 * @attr ref android.R.styleable#View_clickable
575 * @attr ref android.R.styleable#View_contentDescription
576 * @attr ref android.R.styleable#View_drawingCacheQuality
577 * @attr ref android.R.styleable#View_duplicateParentState
578 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700579 * @attr ref android.R.styleable#View_requiresFadingEdge
Romain Guyd6a463a2009-05-21 23:10:10 -0700580 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700581 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700583 * @attr ref android.R.styleable#View_isScrollContainer
584 * @attr ref android.R.styleable#View_focusable
585 * @attr ref android.R.styleable#View_focusableInTouchMode
586 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
587 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800588 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700589 * @attr ref android.R.styleable#View_longClickable
590 * @attr ref android.R.styleable#View_minHeight
591 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 * @attr ref android.R.styleable#View_nextFocusDown
593 * @attr ref android.R.styleable#View_nextFocusLeft
594 * @attr ref android.R.styleable#View_nextFocusRight
595 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700596 * @attr ref android.R.styleable#View_onClick
597 * @attr ref android.R.styleable#View_padding
598 * @attr ref android.R.styleable#View_paddingBottom
599 * @attr ref android.R.styleable#View_paddingLeft
600 * @attr ref android.R.styleable#View_paddingRight
601 * @attr ref android.R.styleable#View_paddingTop
602 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800603 * @attr ref android.R.styleable#View_rotation
604 * @attr ref android.R.styleable#View_rotationX
605 * @attr ref android.R.styleable#View_rotationY
606 * @attr ref android.R.styleable#View_scaleX
607 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 * @attr ref android.R.styleable#View_scrollX
609 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700610 * @attr ref android.R.styleable#View_scrollbarSize
611 * @attr ref android.R.styleable#View_scrollbarStyle
612 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700613 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
614 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
616 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 * @attr ref android.R.styleable#View_scrollbarThumbVertical
618 * @attr ref android.R.styleable#View_scrollbarTrackVertical
619 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
620 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700621 * @attr ref android.R.styleable#View_soundEffectsEnabled
622 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800623 * @attr ref android.R.styleable#View_transformPivotX
624 * @attr ref android.R.styleable#View_transformPivotY
625 * @attr ref android.R.styleable#View_translationX
626 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700627 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 *
629 * @see android.view.ViewGroup
630 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700631public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
632 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 private static final boolean DBG = false;
634
635 /**
636 * The logging tag used by this class with android.util.Log.
637 */
638 protected static final String VIEW_LOG_TAG = "View";
639
640 /**
641 * Used to mark a View that has no ID.
642 */
643 public static final int NO_ID = -1;
644
645 /**
646 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
647 * calling setFlags.
648 */
649 private static final int NOT_FOCUSABLE = 0x00000000;
650
651 /**
652 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
653 * setFlags.
654 */
655 private static final int FOCUSABLE = 0x00000001;
656
657 /**
658 * Mask for use with setFlags indicating bits used for focus.
659 */
660 private static final int FOCUSABLE_MASK = 0x00000001;
661
662 /**
663 * This view will adjust its padding to fit sytem windows (e.g. status bar)
664 */
665 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
666
667 /**
Scott Main812634c22011-07-27 13:22:35 -0700668 * This view is visible.
669 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
670 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 */
672 public static final int VISIBLE = 0x00000000;
673
674 /**
675 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700676 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
677 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 */
679 public static final int INVISIBLE = 0x00000004;
680
681 /**
682 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700683 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
684 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 */
686 public static final int GONE = 0x00000008;
687
688 /**
689 * Mask for use with setFlags indicating bits used for visibility.
690 * {@hide}
691 */
692 static final int VISIBILITY_MASK = 0x0000000C;
693
694 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
695
696 /**
697 * This view is enabled. Intrepretation varies by subclass.
698 * Use with ENABLED_MASK when calling setFlags.
699 * {@hide}
700 */
701 static final int ENABLED = 0x00000000;
702
703 /**
704 * This view is disabled. Intrepretation varies by subclass.
705 * Use with ENABLED_MASK when calling setFlags.
706 * {@hide}
707 */
708 static final int DISABLED = 0x00000020;
709
710 /**
711 * Mask for use with setFlags indicating bits used for indicating whether
712 * this view is enabled
713 * {@hide}
714 */
715 static final int ENABLED_MASK = 0x00000020;
716
717 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700718 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
719 * called and further optimizations will be performed. It is okay to have
720 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 * {@hide}
722 */
723 static final int WILL_NOT_DRAW = 0x00000080;
724
725 /**
726 * Mask for use with setFlags indicating bits used for indicating whether
727 * this view is will draw
728 * {@hide}
729 */
730 static final int DRAW_MASK = 0x00000080;
731
732 /**
733 * <p>This view doesn't show scrollbars.</p>
734 * {@hide}
735 */
736 static final int SCROLLBARS_NONE = 0x00000000;
737
738 /**
739 * <p>This view shows horizontal scrollbars.</p>
740 * {@hide}
741 */
742 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
743
744 /**
745 * <p>This view shows vertical scrollbars.</p>
746 * {@hide}
747 */
748 static final int SCROLLBARS_VERTICAL = 0x00000200;
749
750 /**
751 * <p>Mask for use with setFlags indicating bits used for indicating which
752 * scrollbars are enabled.</p>
753 * {@hide}
754 */
755 static final int SCROLLBARS_MASK = 0x00000300;
756
Jeff Brown85a31762010-09-01 17:01:00 -0700757 /**
758 * Indicates that the view should filter touches when its window is obscured.
759 * Refer to the class comments for more information about this security feature.
760 * {@hide}
761 */
762 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
763
764 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765
766 /**
767 * <p>This view doesn't show fading edges.</p>
768 * {@hide}
769 */
770 static final int FADING_EDGE_NONE = 0x00000000;
771
772 /**
773 * <p>This view shows horizontal fading edges.</p>
774 * {@hide}
775 */
776 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
777
778 /**
779 * <p>This view shows vertical fading edges.</p>
780 * {@hide}
781 */
782 static final int FADING_EDGE_VERTICAL = 0x00002000;
783
784 /**
785 * <p>Mask for use with setFlags indicating bits used for indicating which
786 * fading edges are enabled.</p>
787 * {@hide}
788 */
789 static final int FADING_EDGE_MASK = 0x00003000;
790
791 /**
792 * <p>Indicates this view can be clicked. When clickable, a View reacts
793 * to clicks by notifying the OnClickListener.<p>
794 * {@hide}
795 */
796 static final int CLICKABLE = 0x00004000;
797
798 /**
799 * <p>Indicates this view is caching its drawing into a bitmap.</p>
800 * {@hide}
801 */
802 static final int DRAWING_CACHE_ENABLED = 0x00008000;
803
804 /**
805 * <p>Indicates that no icicle should be saved for this view.<p>
806 * {@hide}
807 */
808 static final int SAVE_DISABLED = 0x000010000;
809
810 /**
811 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
812 * property.</p>
813 * {@hide}
814 */
815 static final int SAVE_DISABLED_MASK = 0x000010000;
816
817 /**
818 * <p>Indicates that no drawing cache should ever be created for this view.<p>
819 * {@hide}
820 */
821 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
822
823 /**
824 * <p>Indicates this view can take / keep focus when int touch mode.</p>
825 * {@hide}
826 */
827 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
828
829 /**
830 * <p>Enables low quality mode for the drawing cache.</p>
831 */
832 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
833
834 /**
835 * <p>Enables high quality mode for the drawing cache.</p>
836 */
837 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
838
839 /**
840 * <p>Enables automatic quality mode for the drawing cache.</p>
841 */
842 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
843
844 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
845 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
846 };
847
848 /**
849 * <p>Mask for use with setFlags indicating bits used for the cache
850 * quality property.</p>
851 * {@hide}
852 */
853 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
854
855 /**
856 * <p>
857 * Indicates this view can be long clicked. When long clickable, a View
858 * reacts to long clicks by notifying the OnLongClickListener or showing a
859 * context menu.
860 * </p>
861 * {@hide}
862 */
863 static final int LONG_CLICKABLE = 0x00200000;
864
865 /**
866 * <p>Indicates that this view gets its drawable states from its direct parent
867 * and ignores its original internal states.</p>
868 *
869 * @hide
870 */
871 static final int DUPLICATE_PARENT_STATE = 0x00400000;
872
873 /**
874 * The scrollbar style to display the scrollbars inside the content area,
875 * without increasing the padding. The scrollbars will be overlaid with
876 * translucency on the view's content.
877 */
878 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
879
880 /**
881 * The scrollbar style to display the scrollbars inside the padded area,
882 * increasing the padding of the view. The scrollbars will not overlap the
883 * content area of the view.
884 */
885 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
886
887 /**
888 * The scrollbar style to display the scrollbars at the edge of the view,
889 * without increasing the padding. The scrollbars will be overlaid with
890 * translucency.
891 */
892 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
893
894 /**
895 * The scrollbar style to display the scrollbars at the edge of the view,
896 * increasing the padding of the view. The scrollbars will only overlap the
897 * background, if any.
898 */
899 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
900
901 /**
902 * Mask to check if the scrollbar style is overlay or inset.
903 * {@hide}
904 */
905 static final int SCROLLBARS_INSET_MASK = 0x01000000;
906
907 /**
908 * Mask to check if the scrollbar style is inside or outside.
909 * {@hide}
910 */
911 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
912
913 /**
914 * Mask for scrollbar style.
915 * {@hide}
916 */
917 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
918
919 /**
920 * View flag indicating that the screen should remain on while the
921 * window containing this view is visible to the user. This effectively
922 * takes care of automatically setting the WindowManager's
923 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
924 */
925 public static final int KEEP_SCREEN_ON = 0x04000000;
926
927 /**
928 * View flag indicating whether this view should have sound effects enabled
929 * for events such as clicking and touching.
930 */
931 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
932
933 /**
934 * View flag indicating whether this view should have haptic feedback
935 * enabled for events such as long presses.
936 */
937 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
938
939 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700940 * <p>Indicates that the view hierarchy should stop saving state when
941 * it reaches this view. If state saving is initiated immediately at
942 * the view, it will be allowed.
943 * {@hide}
944 */
945 static final int PARENT_SAVE_DISABLED = 0x20000000;
946
947 /**
948 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
949 * {@hide}
950 */
951 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
952
953 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800954 * Horizontal direction of this view is from Left to Right.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700955 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800956 * {@hide}
957 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700958 public static final int LAYOUT_DIRECTION_LTR = 0x00000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800959
960 /**
961 * Horizontal direction of this view is from Right to Left.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700962 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800963 * {@hide}
964 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700965 public static final int LAYOUT_DIRECTION_RTL = 0x40000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800966
967 /**
968 * Horizontal direction of this view is inherited from its parent.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700969 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800970 * {@hide}
971 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700972 public static final int LAYOUT_DIRECTION_INHERIT = 0x80000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800973
974 /**
975 * Horizontal direction of this view is from deduced from the default language
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700976 * script for the locale. Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800977 * {@hide}
978 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700979 public static final int LAYOUT_DIRECTION_LOCALE = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800980
981 /**
982 * Mask for use with setFlags indicating bits used for horizontalDirection.
983 * {@hide}
984 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700985 static final int LAYOUT_DIRECTION_MASK = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800986
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700987 /*
988 * Array of horizontal direction flags for mapping attribute "horizontalDirection" to correct
989 * flag value.
990 * {@hide}
991 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700992 private static final int[] LAYOUT_DIRECTION_FLAGS = {LAYOUT_DIRECTION_LTR,
993 LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE};
Cibu Johny7632cb92010-02-22 13:01:02 -0800994
995 /**
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700996 * Default horizontalDirection.
997 * {@hide}
998 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700999 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001000
1001 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001002 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1003 * should add all focusable Views regardless if they are focusable in touch mode.
1004 */
1005 public static final int FOCUSABLES_ALL = 0x00000000;
1006
1007 /**
1008 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1009 * should add only Views focusable in touch mode.
1010 */
1011 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1012
1013 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001014 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 * item.
1016 */
1017 public static final int FOCUS_BACKWARD = 0x00000001;
1018
1019 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001020 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 * item.
1022 */
1023 public static final int FOCUS_FORWARD = 0x00000002;
1024
1025 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001026 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 */
1028 public static final int FOCUS_LEFT = 0x00000011;
1029
1030 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001031 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 */
1033 public static final int FOCUS_UP = 0x00000021;
1034
1035 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001036 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 */
1038 public static final int FOCUS_RIGHT = 0x00000042;
1039
1040 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001041 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 */
1043 public static final int FOCUS_DOWN = 0x00000082;
1044
1045 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001046 * Bits of {@link #getMeasuredWidthAndState()} and
1047 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1048 */
1049 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1050
1051 /**
1052 * Bits of {@link #getMeasuredWidthAndState()} and
1053 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1054 */
1055 public static final int MEASURED_STATE_MASK = 0xff000000;
1056
1057 /**
1058 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1059 * for functions that combine both width and height into a single int,
1060 * such as {@link #getMeasuredState()} and the childState argument of
1061 * {@link #resolveSizeAndState(int, int, int)}.
1062 */
1063 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1064
1065 /**
1066 * Bit of {@link #getMeasuredWidthAndState()} and
1067 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1068 * is smaller that the space the view would like to have.
1069 */
1070 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1071
1072 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 * Base View state sets
1074 */
1075 // Singles
1076 /**
1077 * Indicates the view has no states set. States are used with
1078 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1079 * view depending on its state.
1080 *
1081 * @see android.graphics.drawable.Drawable
1082 * @see #getDrawableState()
1083 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001084 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 /**
1086 * Indicates the view is enabled. States are used with
1087 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1088 * view depending on its state.
1089 *
1090 * @see android.graphics.drawable.Drawable
1091 * @see #getDrawableState()
1092 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001093 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 /**
1095 * Indicates the view is focused. States are used with
1096 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1097 * view depending on its state.
1098 *
1099 * @see android.graphics.drawable.Drawable
1100 * @see #getDrawableState()
1101 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001102 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 /**
1104 * Indicates the view is selected. States are used with
1105 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1106 * view depending on its state.
1107 *
1108 * @see android.graphics.drawable.Drawable
1109 * @see #getDrawableState()
1110 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001111 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 /**
1113 * Indicates the view is pressed. States are used with
1114 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1115 * view depending on its state.
1116 *
1117 * @see android.graphics.drawable.Drawable
1118 * @see #getDrawableState()
1119 * @hide
1120 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001121 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 /**
1123 * Indicates the view's window has focus. 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 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001130 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 // Doubles
1132 /**
1133 * Indicates the view is enabled and has the focus.
1134 *
1135 * @see #ENABLED_STATE_SET
1136 * @see #FOCUSED_STATE_SET
1137 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001138 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 /**
1140 * Indicates the view is enabled and selected.
1141 *
1142 * @see #ENABLED_STATE_SET
1143 * @see #SELECTED_STATE_SET
1144 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001145 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 /**
1147 * Indicates the view is enabled and that its window has focus.
1148 *
1149 * @see #ENABLED_STATE_SET
1150 * @see #WINDOW_FOCUSED_STATE_SET
1151 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001152 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 /**
1154 * Indicates the view is focused and selected.
1155 *
1156 * @see #FOCUSED_STATE_SET
1157 * @see #SELECTED_STATE_SET
1158 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001159 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 /**
1161 * Indicates the view has the focus and that its window has the focus.
1162 *
1163 * @see #FOCUSED_STATE_SET
1164 * @see #WINDOW_FOCUSED_STATE_SET
1165 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001166 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 /**
1168 * Indicates the view is selected and that its window has the focus.
1169 *
1170 * @see #SELECTED_STATE_SET
1171 * @see #WINDOW_FOCUSED_STATE_SET
1172 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001173 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 // Triples
1175 /**
1176 * Indicates the view is enabled, focused and selected.
1177 *
1178 * @see #ENABLED_STATE_SET
1179 * @see #FOCUSED_STATE_SET
1180 * @see #SELECTED_STATE_SET
1181 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001182 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 /**
1184 * Indicates the view is enabled, focused and its window has the focus.
1185 *
1186 * @see #ENABLED_STATE_SET
1187 * @see #FOCUSED_STATE_SET
1188 * @see #WINDOW_FOCUSED_STATE_SET
1189 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001190 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 /**
1192 * Indicates the view is enabled, selected and its window has the focus.
1193 *
1194 * @see #ENABLED_STATE_SET
1195 * @see #SELECTED_STATE_SET
1196 * @see #WINDOW_FOCUSED_STATE_SET
1197 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001198 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 /**
1200 * Indicates the view is focused, selected and its window has the focus.
1201 *
1202 * @see #FOCUSED_STATE_SET
1203 * @see #SELECTED_STATE_SET
1204 * @see #WINDOW_FOCUSED_STATE_SET
1205 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001206 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 /**
1208 * Indicates the view is enabled, focused, selected and its window
1209 * has the focus.
1210 *
1211 * @see #ENABLED_STATE_SET
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[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 /**
1218 * Indicates the view is pressed and its window has the focus.
1219 *
1220 * @see #PRESSED_STATE_SET
1221 * @see #WINDOW_FOCUSED_STATE_SET
1222 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001223 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 /**
1225 * Indicates the view is pressed and selected.
1226 *
1227 * @see #PRESSED_STATE_SET
1228 * @see #SELECTED_STATE_SET
1229 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001230 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 /**
1232 * Indicates the view is pressed, selected and its window has the focus.
1233 *
1234 * @see #PRESSED_STATE_SET
1235 * @see #SELECTED_STATE_SET
1236 * @see #WINDOW_FOCUSED_STATE_SET
1237 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001238 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 /**
1240 * Indicates the view is pressed and focused.
1241 *
1242 * @see #PRESSED_STATE_SET
1243 * @see #FOCUSED_STATE_SET
1244 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001245 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 /**
1247 * Indicates the view is pressed, focused and its window has the focus.
1248 *
1249 * @see #PRESSED_STATE_SET
1250 * @see #FOCUSED_STATE_SET
1251 * @see #WINDOW_FOCUSED_STATE_SET
1252 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001253 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 /**
1255 * Indicates the view is pressed, focused and selected.
1256 *
1257 * @see #PRESSED_STATE_SET
1258 * @see #SELECTED_STATE_SET
1259 * @see #FOCUSED_STATE_SET
1260 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001261 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 /**
1263 * Indicates the view is pressed, focused, selected and its window has the focus.
1264 *
1265 * @see #PRESSED_STATE_SET
1266 * @see #FOCUSED_STATE_SET
1267 * @see #SELECTED_STATE_SET
1268 * @see #WINDOW_FOCUSED_STATE_SET
1269 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001270 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 /**
1272 * Indicates the view is pressed and enabled.
1273 *
1274 * @see #PRESSED_STATE_SET
1275 * @see #ENABLED_STATE_SET
1276 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001277 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 /**
1279 * Indicates the view is pressed, enabled and its window has the focus.
1280 *
1281 * @see #PRESSED_STATE_SET
1282 * @see #ENABLED_STATE_SET
1283 * @see #WINDOW_FOCUSED_STATE_SET
1284 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001285 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 /**
1287 * Indicates the view is pressed, enabled and selected.
1288 *
1289 * @see #PRESSED_STATE_SET
1290 * @see #ENABLED_STATE_SET
1291 * @see #SELECTED_STATE_SET
1292 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001293 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 /**
1295 * Indicates the view is pressed, enabled, selected and its window has the
1296 * focus.
1297 *
1298 * @see #PRESSED_STATE_SET
1299 * @see #ENABLED_STATE_SET
1300 * @see #SELECTED_STATE_SET
1301 * @see #WINDOW_FOCUSED_STATE_SET
1302 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001303 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 /**
1305 * Indicates the view is pressed, enabled and focused.
1306 *
1307 * @see #PRESSED_STATE_SET
1308 * @see #ENABLED_STATE_SET
1309 * @see #FOCUSED_STATE_SET
1310 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001311 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 /**
1313 * Indicates the view is pressed, enabled, focused and its window has the
1314 * focus.
1315 *
1316 * @see #PRESSED_STATE_SET
1317 * @see #ENABLED_STATE_SET
1318 * @see #FOCUSED_STATE_SET
1319 * @see #WINDOW_FOCUSED_STATE_SET
1320 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001321 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 /**
1323 * Indicates the view is pressed, enabled, focused and selected.
1324 *
1325 * @see #PRESSED_STATE_SET
1326 * @see #ENABLED_STATE_SET
1327 * @see #SELECTED_STATE_SET
1328 * @see #FOCUSED_STATE_SET
1329 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001330 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 /**
1332 * Indicates the view is pressed, enabled, focused, selected and its window
1333 * has the focus.
1334 *
1335 * @see #PRESSED_STATE_SET
1336 * @see #ENABLED_STATE_SET
1337 * @see #SELECTED_STATE_SET
1338 * @see #FOCUSED_STATE_SET
1339 * @see #WINDOW_FOCUSED_STATE_SET
1340 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342
1343 /**
1344 * The order here is very important to {@link #getDrawableState()}
1345 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001346 private static final int[][] VIEW_STATE_SETS;
1347
Romain Guyb051e892010-09-28 19:09:36 -07001348 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1349 static final int VIEW_STATE_SELECTED = 1 << 1;
1350 static final int VIEW_STATE_FOCUSED = 1 << 2;
1351 static final int VIEW_STATE_ENABLED = 1 << 3;
1352 static final int VIEW_STATE_PRESSED = 1 << 4;
1353 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001354 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001355 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001356 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1357 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001358
1359 static final int[] VIEW_STATE_IDS = new int[] {
1360 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1361 R.attr.state_selected, VIEW_STATE_SELECTED,
1362 R.attr.state_focused, VIEW_STATE_FOCUSED,
1363 R.attr.state_enabled, VIEW_STATE_ENABLED,
1364 R.attr.state_pressed, VIEW_STATE_PRESSED,
1365 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001366 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001367 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001368 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1369 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 };
1371
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001372 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001373 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1374 throw new IllegalStateException(
1375 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1376 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001377 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001378 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001379 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001380 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001381 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001382 orderedIds[i * 2] = viewState;
1383 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001384 }
1385 }
1386 }
Romain Guyb051e892010-09-28 19:09:36 -07001387 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1388 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1389 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001390 int numBits = Integer.bitCount(i);
1391 int[] set = new int[numBits];
1392 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001393 for (int j = 0; j < orderedIds.length; j += 2) {
1394 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001395 set[pos++] = orderedIds[j];
1396 }
1397 }
1398 VIEW_STATE_SETS[i] = set;
1399 }
1400
1401 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1402 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1403 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1404 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1405 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1406 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1407 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1408 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1409 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1410 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1411 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1412 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1413 | VIEW_STATE_FOCUSED];
1414 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1415 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1416 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1417 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1418 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1419 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1421 | VIEW_STATE_ENABLED];
1422 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1423 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1424 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1425 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1426 | VIEW_STATE_ENABLED];
1427 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1428 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1429 | VIEW_STATE_ENABLED];
1430 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1431 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1432 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1433
1434 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1435 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1436 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1437 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1438 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1439 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1440 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1441 | VIEW_STATE_PRESSED];
1442 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1444 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1445 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1446 | VIEW_STATE_PRESSED];
1447 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1449 | VIEW_STATE_PRESSED];
1450 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1451 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1452 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1453 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1454 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1455 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1457 | VIEW_STATE_PRESSED];
1458 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1459 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1460 | VIEW_STATE_PRESSED];
1461 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1462 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1463 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1464 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1465 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1466 | VIEW_STATE_PRESSED];
1467 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1468 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1469 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1470 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1471 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1472 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1473 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1474 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1475 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1476 | VIEW_STATE_PRESSED];
1477 }
1478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 * Temporary Rect currently for use in setBackground(). This will probably
1481 * be extended in the future to hold our own class with more than just
1482 * a Rect. :)
1483 */
1484 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001485
1486 /**
1487 * Map used to store views' tags.
1488 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001489 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001492 * The next available accessiiblity id.
1493 */
1494 private static int sNextAccessibilityViewId;
1495
1496 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 * The animation currently associated with this view.
1498 * @hide
1499 */
1500 protected Animation mCurrentAnimation = null;
1501
1502 /**
1503 * Width as measured during measure pass.
1504 * {@hide}
1505 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001506 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001507 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508
1509 /**
1510 * Height as measured during measure pass.
1511 * {@hide}
1512 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001513 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001514 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515
1516 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001517 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1518 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1519 * its display list. This flag, used only when hw accelerated, allows us to clear the
1520 * flag while retaining this information until it's needed (at getDisplayList() time and
1521 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1522 *
1523 * {@hide}
1524 */
1525 boolean mRecreateDisplayList = false;
1526
1527 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 * The view's identifier.
1529 * {@hide}
1530 *
1531 * @see #setId(int)
1532 * @see #getId()
1533 */
1534 @ViewDebug.ExportedProperty(resolveId = true)
1535 int mID = NO_ID;
1536
1537 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001538 * The stable ID of this view for accessibility porposes.
1539 */
1540 int mAccessibilityViewId = NO_ID;
1541
1542 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 * The view's tag.
1544 * {@hide}
1545 *
1546 * @see #setTag(Object)
1547 * @see #getTag()
1548 */
1549 protected Object mTag;
1550
1551 // for mPrivateFlags:
1552 /** {@hide} */
1553 static final int WANTS_FOCUS = 0x00000001;
1554 /** {@hide} */
1555 static final int FOCUSED = 0x00000002;
1556 /** {@hide} */
1557 static final int SELECTED = 0x00000004;
1558 /** {@hide} */
1559 static final int IS_ROOT_NAMESPACE = 0x00000008;
1560 /** {@hide} */
1561 static final int HAS_BOUNDS = 0x00000010;
1562 /** {@hide} */
1563 static final int DRAWN = 0x00000020;
1564 /**
1565 * When this flag is set, this view is running an animation on behalf of its
1566 * children and should therefore not cancel invalidate requests, even if they
1567 * lie outside of this view's bounds.
1568 *
1569 * {@hide}
1570 */
1571 static final int DRAW_ANIMATION = 0x00000040;
1572 /** {@hide} */
1573 static final int SKIP_DRAW = 0x00000080;
1574 /** {@hide} */
1575 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1576 /** {@hide} */
1577 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1578 /** {@hide} */
1579 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1580 /** {@hide} */
1581 static final int MEASURED_DIMENSION_SET = 0x00000800;
1582 /** {@hide} */
1583 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001584 /** {@hide} */
1585 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586
1587 private static final int PRESSED = 0x00004000;
1588
1589 /** {@hide} */
1590 static final int DRAWING_CACHE_VALID = 0x00008000;
1591 /**
1592 * Flag used to indicate that this view should be drawn once more (and only once
1593 * more) after its animation has completed.
1594 * {@hide}
1595 */
1596 static final int ANIMATION_STARTED = 0x00010000;
1597
1598 private static final int SAVE_STATE_CALLED = 0x00020000;
1599
1600 /**
1601 * Indicates that the View returned true when onSetAlpha() was called and that
1602 * the alpha must be restored.
1603 * {@hide}
1604 */
1605 static final int ALPHA_SET = 0x00040000;
1606
1607 /**
1608 * Set by {@link #setScrollContainer(boolean)}.
1609 */
1610 static final int SCROLL_CONTAINER = 0x00080000;
1611
1612 /**
1613 * Set by {@link #setScrollContainer(boolean)}.
1614 */
1615 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1616
1617 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001618 * View flag indicating whether this view was invalidated (fully or partially.)
1619 *
1620 * @hide
1621 */
1622 static final int DIRTY = 0x00200000;
1623
1624 /**
1625 * View flag indicating whether this view was invalidated by an opaque
1626 * invalidate request.
1627 *
1628 * @hide
1629 */
1630 static final int DIRTY_OPAQUE = 0x00400000;
1631
1632 /**
1633 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1634 *
1635 * @hide
1636 */
1637 static final int DIRTY_MASK = 0x00600000;
1638
1639 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001640 * Indicates whether the background is opaque.
1641 *
1642 * @hide
1643 */
1644 static final int OPAQUE_BACKGROUND = 0x00800000;
1645
1646 /**
1647 * Indicates whether the scrollbars are opaque.
1648 *
1649 * @hide
1650 */
1651 static final int OPAQUE_SCROLLBARS = 0x01000000;
1652
1653 /**
1654 * Indicates whether the view is opaque.
1655 *
1656 * @hide
1657 */
1658 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001659
Adam Powelle14579b2009-12-16 18:39:52 -08001660 /**
1661 * Indicates a prepressed state;
1662 * the short time between ACTION_DOWN and recognizing
1663 * a 'real' press. Prepressed is used to recognize quick taps
1664 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001665 *
Adam Powelle14579b2009-12-16 18:39:52 -08001666 * @hide
1667 */
1668 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001669
Adam Powellc9fbaab2010-02-16 17:16:19 -08001670 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001671 * Indicates whether the view is temporarily detached.
1672 *
1673 * @hide
1674 */
1675 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001676
Adam Powell8568c3a2010-04-19 14:26:11 -07001677 /**
1678 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001679 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001680 * @hide
1681 */
1682 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001683
1684 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001685 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1686 * @hide
1687 */
1688 private static final int HOVERED = 0x10000000;
1689
1690 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001691 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1692 * for transform operations
1693 *
1694 * @hide
1695 */
Adam Powellf37df072010-09-17 16:22:49 -07001696 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001697
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001698 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001699 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001700
Chet Haasefd2b0022010-08-06 13:08:56 -07001701 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001702 * Indicates that this view was specifically invalidated, not just dirtied because some
1703 * child view was invalidated. The flag is used to determine when we need to recreate
1704 * a view's display list (as opposed to just returning a reference to its existing
1705 * display list).
1706 *
1707 * @hide
1708 */
1709 static final int INVALIDATED = 0x80000000;
1710
Christopher Tate3d4bf172011-03-28 16:16:46 -07001711 /* Masks for mPrivateFlags2 */
1712
1713 /**
1714 * Indicates that this view has reported that it can accept the current drag's content.
1715 * Cleared when the drag operation concludes.
1716 * @hide
1717 */
1718 static final int DRAG_CAN_ACCEPT = 0x00000001;
1719
1720 /**
1721 * Indicates that this view is currently directly under the drag location in a
1722 * drag-and-drop operation involving content that it can accept. Cleared when
1723 * the drag exits the view, or when the drag operation concludes.
1724 * @hide
1725 */
1726 static final int DRAG_HOVERED = 0x00000002;
1727
Cibu Johny86666632010-02-22 13:01:02 -08001728 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001729 * Indicates whether the view layout direction has been resolved and drawn to the
1730 * right-to-left direction.
Cibu Johny86666632010-02-22 13:01:02 -08001731 *
1732 * @hide
1733 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001734 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004;
1735
1736 /**
1737 * Indicates whether the view layout direction has been resolved.
1738 *
1739 * @hide
1740 */
1741 static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008;
1742
Cibu Johny86666632010-02-22 13:01:02 -08001743
Christopher Tate3d4bf172011-03-28 16:16:46 -07001744 /* End of masks for mPrivateFlags2 */
1745
1746 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1747
Chet Haasedaf98e92011-01-10 14:10:36 -08001748 /**
Adam Powell637d3372010-08-25 14:37:03 -07001749 * Always allow a user to over-scroll this view, provided it is a
1750 * view that can scroll.
1751 *
1752 * @see #getOverScrollMode()
1753 * @see #setOverScrollMode(int)
1754 */
1755 public static final int OVER_SCROLL_ALWAYS = 0;
1756
1757 /**
1758 * Allow a user to over-scroll this view only if the content is large
1759 * enough to meaningfully scroll, provided it is a view that can scroll.
1760 *
1761 * @see #getOverScrollMode()
1762 * @see #setOverScrollMode(int)
1763 */
1764 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1765
1766 /**
1767 * Never allow a user to over-scroll this view.
1768 *
1769 * @see #getOverScrollMode()
1770 * @see #setOverScrollMode(int)
1771 */
1772 public static final int OVER_SCROLL_NEVER = 2;
1773
1774 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001775 * View has requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08001776 *
Joe Malin32736f02011-01-19 16:14:20 -08001777 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001778 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001779 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08001780
1781 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001782 * View has requested the system UI to enter an unobtrusive "low profile" mode.
1783 *
1784 * This is for use in games, book readers, video players, or any other "immersive" application
1785 * where the usual system chrome is deemed too distracting.
1786 *
1787 * In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08001788 *
Joe Malin32736f02011-01-19 16:14:20 -08001789 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001790 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001791 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
1792
1793 /**
1794 * View has requested that the system navigation be temporarily hidden.
1795 *
1796 * This is an even less obtrusive state than that called for by
1797 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
1798 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
1799 * those to disappear. This is useful (in conjunction with the
1800 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
1801 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
1802 * window flags) for displaying content using every last pixel on the display.
1803 *
1804 * There is a limitation: because navigation controls are so important, the least user
1805 * interaction will cause them to reappear immediately.
1806 *
1807 * @see #setSystemUiVisibility(int)
1808 */
1809 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
1810
1811 /**
1812 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
1813 */
1814 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
1815
1816 /**
1817 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
1818 */
1819 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08001820
1821 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001822 * @hide
1823 *
1824 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1825 * out of the public fields to keep the undefined bits out of the developer's way.
1826 *
1827 * Flag to make the status bar not expandable. Unless you also
1828 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1829 */
1830 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1831
1832 /**
1833 * @hide
1834 *
1835 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1836 * out of the public fields to keep the undefined bits out of the developer's way.
1837 *
1838 * Flag to hide notification icons and scrolling ticker text.
1839 */
1840 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
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 disable incoming notification alerts. This will not block
1849 * icons, but it will block sound, vibrating and other visual or aural notifications.
1850 */
1851 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1852
1853 /**
1854 * @hide
1855 *
1856 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1857 * out of the public fields to keep the undefined bits out of the developer's way.
1858 *
1859 * Flag to hide only the scrolling ticker. Note that
1860 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1861 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1862 */
1863 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1864
1865 /**
1866 * @hide
1867 *
1868 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1869 * out of the public fields to keep the undefined bits out of the developer's way.
1870 *
1871 * Flag to hide the center system info area.
1872 */
1873 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
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 only the navigation buttons. Don't use this
1882 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001883 *
1884 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001885 */
1886 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1887
1888 /**
1889 * @hide
1890 *
1891 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1892 * out of the public fields to keep the undefined bits out of the developer's way.
1893 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001894 * Flag to hide only the back button. Don't use this
1895 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1896 */
1897 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1898
1899 /**
1900 * @hide
1901 *
1902 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1903 * out of the public fields to keep the undefined bits out of the developer's way.
1904 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001905 * Flag to hide only the clock. You might use this if your activity has
1906 * its own clock making the status bar's clock redundant.
1907 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001908 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1909
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001910 /**
1911 * @hide
1912 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001913 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001914
1915 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07001916 * Find views that render the specified text.
1917 *
1918 * @see #findViewsWithText(ArrayList, CharSequence, int)
1919 */
1920 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
1921
1922 /**
1923 * Find find views that contain the specified content description.
1924 *
1925 * @see #findViewsWithText(ArrayList, CharSequence, int)
1926 */
1927 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
1928
1929 /**
Adam Powell637d3372010-08-25 14:37:03 -07001930 * Controls the over-scroll mode for this view.
1931 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1932 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1933 * and {@link #OVER_SCROLL_NEVER}.
1934 */
1935 private int mOverScrollMode;
1936
1937 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 * The parent this view is attached to.
1939 * {@hide}
1940 *
1941 * @see #getParent()
1942 */
1943 protected ViewParent mParent;
1944
1945 /**
1946 * {@hide}
1947 */
1948 AttachInfo mAttachInfo;
1949
1950 /**
1951 * {@hide}
1952 */
Romain Guy809a7f62009-05-14 15:44:42 -07001953 @ViewDebug.ExportedProperty(flagMapping = {
1954 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1955 name = "FORCE_LAYOUT"),
1956 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1957 name = "LAYOUT_REQUIRED"),
1958 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001959 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001960 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1961 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1962 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1963 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1964 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001966 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967
1968 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001969 * This view's request for the visibility of the status bar.
1970 * @hide
1971 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001972 @ViewDebug.ExportedProperty(flagMapping = {
1973 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
1974 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
1975 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
1976 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
1977 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
1978 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
1979 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
1980 equals = SYSTEM_UI_FLAG_VISIBLE,
1981 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
1982 })
Joe Onorato664644d2011-01-23 17:53:23 -08001983 int mSystemUiVisibility;
1984
1985 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 * Count of how many windows this view has been attached to.
1987 */
1988 int mWindowAttachCount;
1989
1990 /**
1991 * The layout parameters associated with this view and used by the parent
1992 * {@link android.view.ViewGroup} to determine how this view should be
1993 * laid out.
1994 * {@hide}
1995 */
1996 protected ViewGroup.LayoutParams mLayoutParams;
1997
1998 /**
1999 * The view flags hold various views states.
2000 * {@hide}
2001 */
2002 @ViewDebug.ExportedProperty
2003 int mViewFlags;
2004
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002005 static class TransformationInfo {
2006 /**
2007 * The transform matrix for the View. This transform is calculated internally
2008 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2009 * is used by default. Do *not* use this variable directly; instead call
2010 * getMatrix(), which will automatically recalculate the matrix if necessary
2011 * to get the correct matrix based on the latest rotation and scale properties.
2012 */
2013 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002014
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002015 /**
2016 * The transform matrix for the View. This transform is calculated internally
2017 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2018 * is used by default. Do *not* use this variable directly; instead call
2019 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2020 * to get the correct matrix based on the latest rotation and scale properties.
2021 */
2022 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002023
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002024 /**
2025 * An internal variable that tracks whether we need to recalculate the
2026 * transform matrix, based on whether the rotation or scaleX/Y properties
2027 * have changed since the matrix was last calculated.
2028 */
2029 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002030
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002031 /**
2032 * An internal variable that tracks whether we need to recalculate the
2033 * transform matrix, based on whether the rotation or scaleX/Y properties
2034 * have changed since the matrix was last calculated.
2035 */
2036 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002037
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002038 /**
2039 * A variable that tracks whether we need to recalculate the
2040 * transform matrix, based on whether the rotation or scaleX/Y properties
2041 * have changed since the matrix was last calculated. This variable
2042 * is only valid after a call to updateMatrix() or to a function that
2043 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2044 */
2045 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002046
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002047 /**
2048 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2049 */
2050 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002051
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002052 /**
2053 * This matrix is used when computing the matrix for 3D rotations.
2054 */
2055 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002056
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002057 /**
2058 * These prev values are used to recalculate a centered pivot point when necessary. The
2059 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2060 * set), so thes values are only used then as well.
2061 */
2062 private int mPrevWidth = -1;
2063 private int mPrevHeight = -1;
2064
2065 /**
2066 * The degrees rotation around the vertical axis through the pivot point.
2067 */
2068 @ViewDebug.ExportedProperty
2069 float mRotationY = 0f;
2070
2071 /**
2072 * The degrees rotation around the horizontal axis through the pivot point.
2073 */
2074 @ViewDebug.ExportedProperty
2075 float mRotationX = 0f;
2076
2077 /**
2078 * The degrees rotation around the pivot point.
2079 */
2080 @ViewDebug.ExportedProperty
2081 float mRotation = 0f;
2082
2083 /**
2084 * The amount of translation of the object away from its left property (post-layout).
2085 */
2086 @ViewDebug.ExportedProperty
2087 float mTranslationX = 0f;
2088
2089 /**
2090 * The amount of translation of the object away from its top property (post-layout).
2091 */
2092 @ViewDebug.ExportedProperty
2093 float mTranslationY = 0f;
2094
2095 /**
2096 * The amount of scale in the x direction around the pivot point. A
2097 * value of 1 means no scaling is applied.
2098 */
2099 @ViewDebug.ExportedProperty
2100 float mScaleX = 1f;
2101
2102 /**
2103 * The amount of scale in the y direction around the pivot point. A
2104 * value of 1 means no scaling is applied.
2105 */
2106 @ViewDebug.ExportedProperty
2107 float mScaleY = 1f;
2108
2109 /**
2110 * The amount of scale in the x direction around the pivot point. A
2111 * value of 1 means no scaling is applied.
2112 */
2113 @ViewDebug.ExportedProperty
2114 float mPivotX = 0f;
2115
2116 /**
2117 * The amount of scale in the y direction around the pivot point. A
2118 * value of 1 means no scaling is applied.
2119 */
2120 @ViewDebug.ExportedProperty
2121 float mPivotY = 0f;
2122
2123 /**
2124 * The opacity of the View. This is a value from 0 to 1, where 0 means
2125 * completely transparent and 1 means completely opaque.
2126 */
2127 @ViewDebug.ExportedProperty
2128 float mAlpha = 1f;
2129 }
2130
2131 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002132
Joe Malin32736f02011-01-19 16:14:20 -08002133 private boolean mLastIsOpaque;
2134
Chet Haasefd2b0022010-08-06 13:08:56 -07002135 /**
2136 * Convenience value to check for float values that are close enough to zero to be considered
2137 * zero.
2138 */
Romain Guy2542d192010-08-18 11:47:12 -07002139 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002140
2141 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 * The distance in pixels from the left edge of this view's parent
2143 * to the left edge of this view.
2144 * {@hide}
2145 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002146 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 protected int mLeft;
2148 /**
2149 * The distance in pixels from the left edge of this view's parent
2150 * to the right edge of this view.
2151 * {@hide}
2152 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002153 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 protected int mRight;
2155 /**
2156 * The distance in pixels from the top edge of this view's parent
2157 * to the top edge of this view.
2158 * {@hide}
2159 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002160 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 protected int mTop;
2162 /**
2163 * The distance in pixels from the top edge of this view's parent
2164 * to the bottom edge of this view.
2165 * {@hide}
2166 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002167 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 protected int mBottom;
2169
2170 /**
2171 * The offset, in pixels, by which the content of this view is scrolled
2172 * horizontally.
2173 * {@hide}
2174 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002175 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 protected int mScrollX;
2177 /**
2178 * The offset, in pixels, by which the content of this view is scrolled
2179 * vertically.
2180 * {@hide}
2181 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002182 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 protected int mScrollY;
2184
2185 /**
2186 * The left padding in pixels, that is the distance in pixels between the
2187 * left edge of this view and the left edge of its content.
2188 * {@hide}
2189 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002190 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 protected int mPaddingLeft;
2192 /**
2193 * The right padding in pixels, that is the distance in pixels between the
2194 * right edge of this view and the right edge of its content.
2195 * {@hide}
2196 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002197 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 protected int mPaddingRight;
2199 /**
2200 * The top padding in pixels, that is the distance in pixels between the
2201 * top edge of this view and the top edge of its content.
2202 * {@hide}
2203 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002204 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 protected int mPaddingTop;
2206 /**
2207 * The bottom padding in pixels, that is the distance in pixels between the
2208 * bottom edge of this view and the bottom edge of its content.
2209 * {@hide}
2210 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002211 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 protected int mPaddingBottom;
2213
2214 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002215 * Briefly describes the view and is primarily used for accessibility support.
2216 */
2217 private CharSequence mContentDescription;
2218
2219 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002221 *
2222 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002224 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002225 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226
2227 /**
2228 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002229 *
2230 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002232 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002233 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002235 /**
Adam Powell20232d02010-12-08 21:08:53 -08002236 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002237 *
2238 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002239 */
2240 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002241 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002242
2243 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002244 * Cache if the user padding is relative.
2245 *
2246 */
2247 @ViewDebug.ExportedProperty(category = "padding")
2248 boolean mUserPaddingRelative;
2249
2250 /**
2251 * Cache the paddingStart set by the user to append to the scrollbar's size.
2252 *
2253 */
2254 @ViewDebug.ExportedProperty(category = "padding")
2255 int mUserPaddingStart;
2256
2257 /**
2258 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2259 *
2260 */
2261 @ViewDebug.ExportedProperty(category = "padding")
2262 int mUserPaddingEnd;
2263
2264 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002265 * @hide
2266 */
2267 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2268 /**
2269 * @hide
2270 */
2271 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 private Drawable mBGDrawable;
2274
2275 private int mBackgroundResource;
2276 private boolean mBackgroundSizeChanged;
2277
2278 /**
2279 * Listener used to dispatch focus change events.
2280 * This field should be made private, so it is hidden from the SDK.
2281 * {@hide}
2282 */
2283 protected OnFocusChangeListener mOnFocusChangeListener;
2284
2285 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002286 * Listeners for layout change events.
2287 */
2288 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2289
2290 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002291 * Listeners for attach events.
2292 */
2293 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2294
2295 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 * Listener used to dispatch click events.
2297 * This field should be made private, so it is hidden from the SDK.
2298 * {@hide}
2299 */
2300 protected OnClickListener mOnClickListener;
2301
2302 /**
2303 * Listener used to dispatch long click events.
2304 * This field should be made private, so it is hidden from the SDK.
2305 * {@hide}
2306 */
2307 protected OnLongClickListener mOnLongClickListener;
2308
2309 /**
2310 * Listener used to build the context menu.
2311 * This field should be made private, so it is hidden from the SDK.
2312 * {@hide}
2313 */
2314 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2315
2316 private OnKeyListener mOnKeyListener;
2317
2318 private OnTouchListener mOnTouchListener;
2319
Jeff Brown10b62902011-06-20 16:40:37 -07002320 private OnHoverListener mOnHoverListener;
2321
Jeff Brown33bbfd22011-02-24 20:55:35 -08002322 private OnGenericMotionListener mOnGenericMotionListener;
2323
Chris Tate32affef2010-10-18 15:29:21 -07002324 private OnDragListener mOnDragListener;
2325
Joe Onorato664644d2011-01-23 17:53:23 -08002326 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 /**
2329 * The application environment this view lives in.
2330 * This field should be made private, so it is hidden from the SDK.
2331 * {@hide}
2332 */
2333 protected Context mContext;
2334
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002335 private final Resources mResources;
2336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 private ScrollabilityCache mScrollCache;
2338
2339 private int[] mDrawableState = null;
2340
Romain Guy0211a0a2011-02-14 16:34:59 -08002341 /**
2342 * Set to true when drawing cache is enabled and cannot be created.
2343 *
2344 * @hide
2345 */
2346 public boolean mCachingFailed;
2347
Romain Guy02890fd2010-08-06 17:58:44 -07002348 private Bitmap mDrawingCache;
2349 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002350 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002351 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352
2353 /**
2354 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2355 * the user may specify which view to go to next.
2356 */
2357 private int mNextFocusLeftId = View.NO_ID;
2358
2359 /**
2360 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2361 * the user may specify which view to go to next.
2362 */
2363 private int mNextFocusRightId = View.NO_ID;
2364
2365 /**
2366 * When this view has focus and the next focus is {@link #FOCUS_UP},
2367 * the user may specify which view to go to next.
2368 */
2369 private int mNextFocusUpId = View.NO_ID;
2370
2371 /**
2372 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2373 * the user may specify which view to go to next.
2374 */
2375 private int mNextFocusDownId = View.NO_ID;
2376
Jeff Brown4e6319b2010-12-13 10:36:51 -08002377 /**
2378 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2379 * the user may specify which view to go to next.
2380 */
2381 int mNextFocusForwardId = View.NO_ID;
2382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002384 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002385 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002386 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 private UnsetPressedState mUnsetPressedState;
2389
2390 /**
2391 * Whether the long press's action has been invoked. The tap's action is invoked on the
2392 * up event while a long press is invoked as soon as the long press duration is reached, so
2393 * a long press could be performed before the tap is checked, in which case the tap's action
2394 * should not be invoked.
2395 */
2396 private boolean mHasPerformedLongPress;
2397
2398 /**
2399 * The minimum height of the view. We'll try our best to have the height
2400 * of this view to at least this amount.
2401 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002402 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 private int mMinHeight;
2404
2405 /**
2406 * The minimum width of the view. We'll try our best to have the width
2407 * of this view to at least this amount.
2408 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002409 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 private int mMinWidth;
2411
2412 /**
2413 * The delegate to handle touch events that are physically in this view
2414 * but should be handled by another view.
2415 */
2416 private TouchDelegate mTouchDelegate = null;
2417
2418 /**
2419 * Solid color to use as a background when creating the drawing cache. Enables
2420 * the cache to use 16 bit bitmaps instead of 32 bit.
2421 */
2422 private int mDrawingCacheBackgroundColor = 0;
2423
2424 /**
2425 * Special tree observer used when mAttachInfo is null.
2426 */
2427 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002428
Adam Powelle14579b2009-12-16 18:39:52 -08002429 /**
2430 * Cache the touch slop from the context that created the view.
2431 */
2432 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002435 * Object that handles automatic animation of view properties.
2436 */
2437 private ViewPropertyAnimator mAnimator = null;
2438
2439 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002440 * Flag indicating that a drag can cross window boundaries. When
2441 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2442 * with this flag set, all visible applications will be able to participate
2443 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002444 *
2445 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002446 */
2447 public static final int DRAG_FLAG_GLOBAL = 1;
2448
2449 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002450 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2451 */
2452 private float mVerticalScrollFactor;
2453
2454 /**
Adam Powell20232d02010-12-08 21:08:53 -08002455 * Position of the vertical scroll bar.
2456 */
2457 private int mVerticalScrollbarPosition;
2458
2459 /**
2460 * Position the scroll bar at the default position as determined by the system.
2461 */
2462 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2463
2464 /**
2465 * Position the scroll bar along the left edge.
2466 */
2467 public static final int SCROLLBAR_POSITION_LEFT = 1;
2468
2469 /**
2470 * Position the scroll bar along the right edge.
2471 */
2472 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2473
2474 /**
Romain Guy171c5922011-01-06 10:04:23 -08002475 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002476 *
2477 * @see #getLayerType()
2478 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002479 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002480 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002481 */
2482 public static final int LAYER_TYPE_NONE = 0;
2483
2484 /**
2485 * <p>Indicates that the view has a software layer. A software layer is backed
2486 * by a bitmap and causes the view to be rendered using Android's software
2487 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002488 *
Romain Guy171c5922011-01-06 10:04:23 -08002489 * <p>Software layers have various usages:</p>
2490 * <p>When the application is not using hardware acceleration, a software layer
2491 * is useful to apply a specific color filter and/or blending mode and/or
2492 * translucency to a view and all its children.</p>
2493 * <p>When the application is using hardware acceleration, a software layer
2494 * is useful to render drawing primitives not supported by the hardware
2495 * accelerated pipeline. It can also be used to cache a complex view tree
2496 * into a texture and reduce the complexity of drawing operations. For instance,
2497 * when animating a complex view tree with a translation, a software layer can
2498 * be used to render the view tree only once.</p>
2499 * <p>Software layers should be avoided when the affected view tree updates
2500 * often. Every update will require to re-render the software layer, which can
2501 * potentially be slow (particularly when hardware acceleration is turned on
2502 * since the layer will have to be uploaded into a hardware texture after every
2503 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002504 *
2505 * @see #getLayerType()
2506 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002507 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002508 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002509 */
2510 public static final int LAYER_TYPE_SOFTWARE = 1;
2511
2512 /**
2513 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2514 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2515 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2516 * rendering pipeline, but only if hardware acceleration is turned on for the
2517 * view hierarchy. When hardware acceleration is turned off, hardware layers
2518 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002519 *
Romain Guy171c5922011-01-06 10:04:23 -08002520 * <p>A hardware layer is useful to apply a specific color filter and/or
2521 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002522 * <p>A hardware layer can be used to cache a complex view tree into a
2523 * texture and reduce the complexity of drawing operations. For instance,
2524 * when animating a complex view tree with a translation, a hardware layer can
2525 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002526 * <p>A hardware layer can also be used to increase the rendering quality when
2527 * rotation transformations are applied on a view. It can also be used to
2528 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002529 *
2530 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002531 * @see #setLayerType(int, android.graphics.Paint)
2532 * @see #LAYER_TYPE_NONE
2533 * @see #LAYER_TYPE_SOFTWARE
2534 */
2535 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002536
Romain Guy3aaff3a2011-01-12 14:18:47 -08002537 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2538 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2539 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2540 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2541 })
Romain Guy171c5922011-01-06 10:04:23 -08002542 int mLayerType = LAYER_TYPE_NONE;
2543 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002544 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002545
2546 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002547 * Set to true when the view is sending hover accessibility events because it
2548 * is the innermost hovered view.
2549 */
2550 private boolean mSendingHoverAccessibilityEvents;
2551
2552 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002553 * Delegate for injecting accessiblity functionality.
2554 */
2555 AccessibilityDelegate mAccessibilityDelegate;
2556
2557 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002558 * Text direction is inherited thru {@link ViewGroup}
2559 * @hide
2560 */
2561 public static final int TEXT_DIRECTION_INHERIT = 0;
2562
2563 /**
2564 * Text direction is using "first strong algorithm". The first strong directional character
2565 * determines the paragraph direction. If there is no strong directional character, the
Doug Feltcb3791202011-07-07 11:57:48 -07002566 * paragraph direction is the view's resolved ayout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002567 *
2568 * @hide
2569 */
2570 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2571
2572 /**
2573 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2574 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002575 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002576 *
2577 * @hide
2578 */
2579 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2580
2581 /**
2582 * Text direction is forced to LTR.
2583 *
2584 * @hide
2585 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002586 public static final int TEXT_DIRECTION_LTR = 3;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002587
2588 /**
2589 * Text direction is forced to RTL.
2590 *
2591 * @hide
2592 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002593 public static final int TEXT_DIRECTION_RTL = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002594
2595 /**
2596 * Default text direction is inherited
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07002597 *
2598 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002599 */
2600 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2601
2602 /**
2603 * The text direction that has been defined by {@link #setTextDirection(int)}.
2604 *
2605 * {@hide}
2606 */
2607 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002608 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2609 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2610 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2611 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2612 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2613 })
Doug Feltcb3791202011-07-07 11:57:48 -07002614 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002615
2616 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002617 * The resolved text direction. This needs resolution if the value is
2618 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if that is
2619 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2620 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002621 *
2622 * {@hide}
2623 */
2624 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002625 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2626 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2627 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002628 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2629 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2630 })
Doug Feltcb3791202011-07-07 11:57:48 -07002631 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002632
2633 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002634 * Consistency verifier for debugging purposes.
2635 * @hide
2636 */
2637 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2638 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2639 new InputEventConsistencyVerifier(this, 0) : null;
2640
2641 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 * Simple constructor to use when creating a view from code.
2643 *
2644 * @param context The Context the view is running in, through which it can
2645 * access the current theme, resources, etc.
2646 */
2647 public View(Context context) {
2648 mContext = context;
2649 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002650 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002651 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002652 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002653 mUserPaddingStart = -1;
2654 mUserPaddingEnd = -1;
2655 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 }
2657
2658 /**
2659 * Constructor that is called when inflating a view from XML. This is called
2660 * when a view is being constructed from an XML file, supplying attributes
2661 * that were specified in the XML file. This version uses a default style of
2662 * 0, so the only attribute values applied are those in the Context's Theme
2663 * and the given AttributeSet.
2664 *
2665 * <p>
2666 * The method onFinishInflate() will be called after all children have been
2667 * added.
2668 *
2669 * @param context The Context the view is running in, through which it can
2670 * access the current theme, resources, etc.
2671 * @param attrs The attributes of the XML tag that is inflating the view.
2672 * @see #View(Context, AttributeSet, int)
2673 */
2674 public View(Context context, AttributeSet attrs) {
2675 this(context, attrs, 0);
2676 }
2677
2678 /**
2679 * Perform inflation from XML and apply a class-specific base style. This
2680 * constructor of View allows subclasses to use their own base style when
2681 * they are inflating. For example, a Button class's constructor would call
2682 * this version of the super class constructor and supply
2683 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2684 * the theme's button style to modify all of the base view attributes (in
2685 * particular its background) as well as the Button class's attributes.
2686 *
2687 * @param context The Context the view is running in, through which it can
2688 * access the current theme, resources, etc.
2689 * @param attrs The attributes of the XML tag that is inflating the view.
2690 * @param defStyle The default style to apply to this view. If 0, no style
2691 * will be applied (beyond what is included in the theme). This may
2692 * either be an attribute resource, whose value will be retrieved
2693 * from the current theme, or an explicit style resource.
2694 * @see #View(Context, AttributeSet)
2695 */
2696 public View(Context context, AttributeSet attrs, int defStyle) {
2697 this(context);
2698
2699 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2700 defStyle, 0);
2701
2702 Drawable background = null;
2703
2704 int leftPadding = -1;
2705 int topPadding = -1;
2706 int rightPadding = -1;
2707 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002708 int startPadding = -1;
2709 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710
2711 int padding = -1;
2712
2713 int viewFlagValues = 0;
2714 int viewFlagMasks = 0;
2715
2716 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 int x = 0;
2719 int y = 0;
2720
Chet Haase73066682010-11-29 15:55:32 -08002721 float tx = 0;
2722 float ty = 0;
2723 float rotation = 0;
2724 float rotationX = 0;
2725 float rotationY = 0;
2726 float sx = 1f;
2727 float sy = 1f;
2728 boolean transformSet = false;
2729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2731
Adam Powell637d3372010-08-25 14:37:03 -07002732 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 final int N = a.getIndexCount();
2734 for (int i = 0; i < N; i++) {
2735 int attr = a.getIndex(i);
2736 switch (attr) {
2737 case com.android.internal.R.styleable.View_background:
2738 background = a.getDrawable(attr);
2739 break;
2740 case com.android.internal.R.styleable.View_padding:
2741 padding = a.getDimensionPixelSize(attr, -1);
2742 break;
2743 case com.android.internal.R.styleable.View_paddingLeft:
2744 leftPadding = a.getDimensionPixelSize(attr, -1);
2745 break;
2746 case com.android.internal.R.styleable.View_paddingTop:
2747 topPadding = a.getDimensionPixelSize(attr, -1);
2748 break;
2749 case com.android.internal.R.styleable.View_paddingRight:
2750 rightPadding = a.getDimensionPixelSize(attr, -1);
2751 break;
2752 case com.android.internal.R.styleable.View_paddingBottom:
2753 bottomPadding = a.getDimensionPixelSize(attr, -1);
2754 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002755 case com.android.internal.R.styleable.View_paddingStart:
2756 startPadding = a.getDimensionPixelSize(attr, -1);
2757 break;
2758 case com.android.internal.R.styleable.View_paddingEnd:
2759 endPadding = a.getDimensionPixelSize(attr, -1);
2760 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 case com.android.internal.R.styleable.View_scrollX:
2762 x = a.getDimensionPixelOffset(attr, 0);
2763 break;
2764 case com.android.internal.R.styleable.View_scrollY:
2765 y = a.getDimensionPixelOffset(attr, 0);
2766 break;
Chet Haase73066682010-11-29 15:55:32 -08002767 case com.android.internal.R.styleable.View_alpha:
2768 setAlpha(a.getFloat(attr, 1f));
2769 break;
2770 case com.android.internal.R.styleable.View_transformPivotX:
2771 setPivotX(a.getDimensionPixelOffset(attr, 0));
2772 break;
2773 case com.android.internal.R.styleable.View_transformPivotY:
2774 setPivotY(a.getDimensionPixelOffset(attr, 0));
2775 break;
2776 case com.android.internal.R.styleable.View_translationX:
2777 tx = a.getDimensionPixelOffset(attr, 0);
2778 transformSet = true;
2779 break;
2780 case com.android.internal.R.styleable.View_translationY:
2781 ty = a.getDimensionPixelOffset(attr, 0);
2782 transformSet = true;
2783 break;
2784 case com.android.internal.R.styleable.View_rotation:
2785 rotation = a.getFloat(attr, 0);
2786 transformSet = true;
2787 break;
2788 case com.android.internal.R.styleable.View_rotationX:
2789 rotationX = a.getFloat(attr, 0);
2790 transformSet = true;
2791 break;
2792 case com.android.internal.R.styleable.View_rotationY:
2793 rotationY = a.getFloat(attr, 0);
2794 transformSet = true;
2795 break;
2796 case com.android.internal.R.styleable.View_scaleX:
2797 sx = a.getFloat(attr, 1f);
2798 transformSet = true;
2799 break;
2800 case com.android.internal.R.styleable.View_scaleY:
2801 sy = a.getFloat(attr, 1f);
2802 transformSet = true;
2803 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 case com.android.internal.R.styleable.View_id:
2805 mID = a.getResourceId(attr, NO_ID);
2806 break;
2807 case com.android.internal.R.styleable.View_tag:
2808 mTag = a.getText(attr);
2809 break;
2810 case com.android.internal.R.styleable.View_fitsSystemWindows:
2811 if (a.getBoolean(attr, false)) {
2812 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2813 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2814 }
2815 break;
2816 case com.android.internal.R.styleable.View_focusable:
2817 if (a.getBoolean(attr, false)) {
2818 viewFlagValues |= FOCUSABLE;
2819 viewFlagMasks |= FOCUSABLE_MASK;
2820 }
2821 break;
2822 case com.android.internal.R.styleable.View_focusableInTouchMode:
2823 if (a.getBoolean(attr, false)) {
2824 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2825 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2826 }
2827 break;
2828 case com.android.internal.R.styleable.View_clickable:
2829 if (a.getBoolean(attr, false)) {
2830 viewFlagValues |= CLICKABLE;
2831 viewFlagMasks |= CLICKABLE;
2832 }
2833 break;
2834 case com.android.internal.R.styleable.View_longClickable:
2835 if (a.getBoolean(attr, false)) {
2836 viewFlagValues |= LONG_CLICKABLE;
2837 viewFlagMasks |= LONG_CLICKABLE;
2838 }
2839 break;
2840 case com.android.internal.R.styleable.View_saveEnabled:
2841 if (!a.getBoolean(attr, true)) {
2842 viewFlagValues |= SAVE_DISABLED;
2843 viewFlagMasks |= SAVE_DISABLED_MASK;
2844 }
2845 break;
2846 case com.android.internal.R.styleable.View_duplicateParentState:
2847 if (a.getBoolean(attr, false)) {
2848 viewFlagValues |= DUPLICATE_PARENT_STATE;
2849 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2850 }
2851 break;
2852 case com.android.internal.R.styleable.View_visibility:
2853 final int visibility = a.getInt(attr, 0);
2854 if (visibility != 0) {
2855 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2856 viewFlagMasks |= VISIBILITY_MASK;
2857 }
2858 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002859 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002860 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002861 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002862 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002863 final int layoutDirection = a.getInt(attr, -1);
2864 if (layoutDirection != -1) {
2865 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002866 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002867 // Set to default (LAYOUT_DIRECTION_INHERIT)
2868 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002869 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002870 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002871 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 case com.android.internal.R.styleable.View_drawingCacheQuality:
2873 final int cacheQuality = a.getInt(attr, 0);
2874 if (cacheQuality != 0) {
2875 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2876 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2877 }
2878 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002879 case com.android.internal.R.styleable.View_contentDescription:
2880 mContentDescription = a.getString(attr);
2881 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2883 if (!a.getBoolean(attr, true)) {
2884 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2885 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2886 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002887 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2889 if (!a.getBoolean(attr, true)) {
2890 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2891 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2892 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002893 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 case R.styleable.View_scrollbars:
2895 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2896 if (scrollbars != SCROLLBARS_NONE) {
2897 viewFlagValues |= scrollbars;
2898 viewFlagMasks |= SCROLLBARS_MASK;
2899 initializeScrollbars(a);
2900 }
2901 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07002902 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07002904 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
2905 // Ignore the attribute starting with ICS
2906 break;
2907 }
2908 // With builds < ICS, fall through and apply fading edges
2909 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2911 if (fadingEdge != FADING_EDGE_NONE) {
2912 viewFlagValues |= fadingEdge;
2913 viewFlagMasks |= FADING_EDGE_MASK;
2914 initializeFadingEdge(a);
2915 }
2916 break;
2917 case R.styleable.View_scrollbarStyle:
2918 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2919 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2920 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2921 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2922 }
2923 break;
2924 case R.styleable.View_isScrollContainer:
2925 setScrollContainer = true;
2926 if (a.getBoolean(attr, false)) {
2927 setScrollContainer(true);
2928 }
2929 break;
2930 case com.android.internal.R.styleable.View_keepScreenOn:
2931 if (a.getBoolean(attr, false)) {
2932 viewFlagValues |= KEEP_SCREEN_ON;
2933 viewFlagMasks |= KEEP_SCREEN_ON;
2934 }
2935 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002936 case R.styleable.View_filterTouchesWhenObscured:
2937 if (a.getBoolean(attr, false)) {
2938 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2939 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2940 }
2941 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942 case R.styleable.View_nextFocusLeft:
2943 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2944 break;
2945 case R.styleable.View_nextFocusRight:
2946 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2947 break;
2948 case R.styleable.View_nextFocusUp:
2949 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2950 break;
2951 case R.styleable.View_nextFocusDown:
2952 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2953 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002954 case R.styleable.View_nextFocusForward:
2955 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2956 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 case R.styleable.View_minWidth:
2958 mMinWidth = a.getDimensionPixelSize(attr, 0);
2959 break;
2960 case R.styleable.View_minHeight:
2961 mMinHeight = a.getDimensionPixelSize(attr, 0);
2962 break;
Romain Guy9a817362009-05-01 10:57:14 -07002963 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002964 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002965 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002966 + "be used within a restricted context");
2967 }
2968
Romain Guy9a817362009-05-01 10:57:14 -07002969 final String handlerName = a.getString(attr);
2970 if (handlerName != null) {
2971 setOnClickListener(new OnClickListener() {
2972 private Method mHandler;
2973
2974 public void onClick(View v) {
2975 if (mHandler == null) {
2976 try {
2977 mHandler = getContext().getClass().getMethod(handlerName,
2978 View.class);
2979 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002980 int id = getId();
2981 String idText = id == NO_ID ? "" : " with id '"
2982 + getContext().getResources().getResourceEntryName(
2983 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002984 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002985 handlerName + "(View) in the activity "
2986 + getContext().getClass() + " for onClick handler"
2987 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002988 }
2989 }
2990
2991 try {
2992 mHandler.invoke(getContext(), View.this);
2993 } catch (IllegalAccessException e) {
2994 throw new IllegalStateException("Could not execute non "
2995 + "public method of the activity", e);
2996 } catch (InvocationTargetException e) {
2997 throw new IllegalStateException("Could not execute "
2998 + "method of the activity", e);
2999 }
3000 }
3001 });
3002 }
3003 break;
Adam Powell637d3372010-08-25 14:37:03 -07003004 case R.styleable.View_overScrollMode:
3005 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3006 break;
Adam Powell20232d02010-12-08 21:08:53 -08003007 case R.styleable.View_verticalScrollbarPosition:
3008 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3009 break;
Romain Guy171c5922011-01-06 10:04:23 -08003010 case R.styleable.View_layerType:
3011 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3012 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003013 case R.styleable.View_textDirection:
3014 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
3015 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 }
3017 }
3018
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003019 a.recycle();
3020
Adam Powell637d3372010-08-25 14:37:03 -07003021 setOverScrollMode(overScrollMode);
3022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 if (background != null) {
3024 setBackgroundDrawable(background);
3025 }
3026
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003027 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
3028
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003029 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3030 // layout direction). Those cached values will be used later during padding resolution.
3031 mUserPaddingStart = startPadding;
3032 mUserPaddingEnd = endPadding;
3033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 if (padding >= 0) {
3035 leftPadding = padding;
3036 topPadding = padding;
3037 rightPadding = padding;
3038 bottomPadding = padding;
3039 }
3040
3041 // If the user specified the padding (either with android:padding or
3042 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3043 // use the default padding or the padding from the background drawable
3044 // (stored at this point in mPadding*)
3045 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3046 topPadding >= 0 ? topPadding : mPaddingTop,
3047 rightPadding >= 0 ? rightPadding : mPaddingRight,
3048 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3049
3050 if (viewFlagMasks != 0) {
3051 setFlags(viewFlagValues, viewFlagMasks);
3052 }
3053
3054 // Needs to be called after mViewFlags is set
3055 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3056 recomputePadding();
3057 }
3058
3059 if (x != 0 || y != 0) {
3060 scrollTo(x, y);
3061 }
3062
Chet Haase73066682010-11-29 15:55:32 -08003063 if (transformSet) {
3064 setTranslationX(tx);
3065 setTranslationY(ty);
3066 setRotation(rotation);
3067 setRotationX(rotationX);
3068 setRotationY(rotationY);
3069 setScaleX(sx);
3070 setScaleY(sy);
3071 }
3072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003073 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3074 setScrollContainer(true);
3075 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003076
3077 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078 }
3079
3080 /**
3081 * Non-public constructor for use in testing
3082 */
3083 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003084 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 }
3086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 /**
3088 * <p>
3089 * Initializes the fading edges from a given set of styled attributes. This
3090 * method should be called by subclasses that need fading edges and when an
3091 * instance of these subclasses is created programmatically rather than
3092 * being inflated from XML. This method is automatically called when the XML
3093 * is inflated.
3094 * </p>
3095 *
3096 * @param a the styled attributes set to initialize the fading edges from
3097 */
3098 protected void initializeFadingEdge(TypedArray a) {
3099 initScrollCache();
3100
3101 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3102 R.styleable.View_fadingEdgeLength,
3103 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3104 }
3105
3106 /**
3107 * Returns the size of the vertical faded edges used to indicate that more
3108 * content in this view is visible.
3109 *
3110 * @return The size in pixels of the vertical faded edge or 0 if vertical
3111 * faded edges are not enabled for this view.
3112 * @attr ref android.R.styleable#View_fadingEdgeLength
3113 */
3114 public int getVerticalFadingEdgeLength() {
3115 if (isVerticalFadingEdgeEnabled()) {
3116 ScrollabilityCache cache = mScrollCache;
3117 if (cache != null) {
3118 return cache.fadingEdgeLength;
3119 }
3120 }
3121 return 0;
3122 }
3123
3124 /**
3125 * Set the size of the faded edge used to indicate that more content in this
3126 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003127 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3128 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3129 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003130 *
3131 * @param length The size in pixels of the faded edge used to indicate that more
3132 * content in this view is visible.
3133 */
3134 public void setFadingEdgeLength(int length) {
3135 initScrollCache();
3136 mScrollCache.fadingEdgeLength = length;
3137 }
3138
3139 /**
3140 * Returns the size of the horizontal faded edges used to indicate that more
3141 * content in this view is visible.
3142 *
3143 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3144 * faded edges are not enabled for this view.
3145 * @attr ref android.R.styleable#View_fadingEdgeLength
3146 */
3147 public int getHorizontalFadingEdgeLength() {
3148 if (isHorizontalFadingEdgeEnabled()) {
3149 ScrollabilityCache cache = mScrollCache;
3150 if (cache != null) {
3151 return cache.fadingEdgeLength;
3152 }
3153 }
3154 return 0;
3155 }
3156
3157 /**
3158 * Returns the width of the vertical scrollbar.
3159 *
3160 * @return The width in pixels of the vertical scrollbar or 0 if there
3161 * is no vertical scrollbar.
3162 */
3163 public int getVerticalScrollbarWidth() {
3164 ScrollabilityCache cache = mScrollCache;
3165 if (cache != null) {
3166 ScrollBarDrawable scrollBar = cache.scrollBar;
3167 if (scrollBar != null) {
3168 int size = scrollBar.getSize(true);
3169 if (size <= 0) {
3170 size = cache.scrollBarSize;
3171 }
3172 return size;
3173 }
3174 return 0;
3175 }
3176 return 0;
3177 }
3178
3179 /**
3180 * Returns the height of the horizontal scrollbar.
3181 *
3182 * @return The height in pixels of the horizontal scrollbar or 0 if
3183 * there is no horizontal scrollbar.
3184 */
3185 protected int getHorizontalScrollbarHeight() {
3186 ScrollabilityCache cache = mScrollCache;
3187 if (cache != null) {
3188 ScrollBarDrawable scrollBar = cache.scrollBar;
3189 if (scrollBar != null) {
3190 int size = scrollBar.getSize(false);
3191 if (size <= 0) {
3192 size = cache.scrollBarSize;
3193 }
3194 return size;
3195 }
3196 return 0;
3197 }
3198 return 0;
3199 }
3200
3201 /**
3202 * <p>
3203 * Initializes the scrollbars from a given set of styled attributes. This
3204 * method should be called by subclasses that need scrollbars and when an
3205 * instance of these subclasses is created programmatically rather than
3206 * being inflated from XML. This method is automatically called when the XML
3207 * is inflated.
3208 * </p>
3209 *
3210 * @param a the styled attributes set to initialize the scrollbars from
3211 */
3212 protected void initializeScrollbars(TypedArray a) {
3213 initScrollCache();
3214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003216
Mike Cleronf116bf82009-09-27 19:14:12 -07003217 if (scrollabilityCache.scrollBar == null) {
3218 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3219 }
Joe Malin32736f02011-01-19 16:14:20 -08003220
Romain Guy8bda2482010-03-02 11:42:11 -08003221 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222
Mike Cleronf116bf82009-09-27 19:14:12 -07003223 if (!fadeScrollbars) {
3224 scrollabilityCache.state = ScrollabilityCache.ON;
3225 }
3226 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003227
3228
Mike Cleronf116bf82009-09-27 19:14:12 -07003229 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3230 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3231 .getScrollBarFadeDuration());
3232 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3233 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3234 ViewConfiguration.getScrollDefaultDelay());
3235
Joe Malin32736f02011-01-19 16:14:20 -08003236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3238 com.android.internal.R.styleable.View_scrollbarSize,
3239 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3240
3241 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3242 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3243
3244 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3245 if (thumb != null) {
3246 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3247 }
3248
3249 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3250 false);
3251 if (alwaysDraw) {
3252 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3253 }
3254
3255 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3256 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3257
3258 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3259 if (thumb != null) {
3260 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3261 }
3262
3263 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3264 false);
3265 if (alwaysDraw) {
3266 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3267 }
3268
3269 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003270 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003271 }
3272
3273 /**
3274 * <p>
3275 * Initalizes the scrollability cache if necessary.
3276 * </p>
3277 */
3278 private void initScrollCache() {
3279 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003280 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 }
3282 }
3283
3284 /**
Adam Powell20232d02010-12-08 21:08:53 -08003285 * Set the position of the vertical scroll bar. Should be one of
3286 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3287 * {@link #SCROLLBAR_POSITION_RIGHT}.
3288 *
3289 * @param position Where the vertical scroll bar should be positioned.
3290 */
3291 public void setVerticalScrollbarPosition(int position) {
3292 if (mVerticalScrollbarPosition != position) {
3293 mVerticalScrollbarPosition = position;
3294 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003295 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003296 }
3297 }
3298
3299 /**
3300 * @return The position where the vertical scroll bar will show, if applicable.
3301 * @see #setVerticalScrollbarPosition(int)
3302 */
3303 public int getVerticalScrollbarPosition() {
3304 return mVerticalScrollbarPosition;
3305 }
3306
3307 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 * Register a callback to be invoked when focus of this view changed.
3309 *
3310 * @param l The callback that will run.
3311 */
3312 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3313 mOnFocusChangeListener = l;
3314 }
3315
3316 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003317 * Add a listener that will be called when the bounds of the view change due to
3318 * layout processing.
3319 *
3320 * @param listener The listener that will be called when layout bounds change.
3321 */
3322 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3323 if (mOnLayoutChangeListeners == null) {
3324 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3325 }
3326 mOnLayoutChangeListeners.add(listener);
3327 }
3328
3329 /**
3330 * Remove a listener for layout changes.
3331 *
3332 * @param listener The listener for layout bounds change.
3333 */
3334 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3335 if (mOnLayoutChangeListeners == null) {
3336 return;
3337 }
3338 mOnLayoutChangeListeners.remove(listener);
3339 }
3340
3341 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003342 * Add a listener for attach state changes.
3343 *
3344 * This listener will be called whenever this view is attached or detached
3345 * from a window. Remove the listener using
3346 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3347 *
3348 * @param listener Listener to attach
3349 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3350 */
3351 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3352 if (mOnAttachStateChangeListeners == null) {
3353 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3354 }
3355 mOnAttachStateChangeListeners.add(listener);
3356 }
3357
3358 /**
3359 * Remove a listener for attach state changes. The listener will receive no further
3360 * notification of window attach/detach events.
3361 *
3362 * @param listener Listener to remove
3363 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3364 */
3365 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3366 if (mOnAttachStateChangeListeners == null) {
3367 return;
3368 }
3369 mOnAttachStateChangeListeners.remove(listener);
3370 }
3371
3372 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 * Returns the focus-change callback registered for this view.
3374 *
3375 * @return The callback, or null if one is not registered.
3376 */
3377 public OnFocusChangeListener getOnFocusChangeListener() {
3378 return mOnFocusChangeListener;
3379 }
3380
3381 /**
3382 * Register a callback to be invoked when this view is clicked. If this view is not
3383 * clickable, it becomes clickable.
3384 *
3385 * @param l The callback that will run
3386 *
3387 * @see #setClickable(boolean)
3388 */
3389 public void setOnClickListener(OnClickListener l) {
3390 if (!isClickable()) {
3391 setClickable(true);
3392 }
3393 mOnClickListener = l;
3394 }
3395
3396 /**
3397 * Register a callback to be invoked when this view is clicked and held. If this view is not
3398 * long clickable, it becomes long clickable.
3399 *
3400 * @param l The callback that will run
3401 *
3402 * @see #setLongClickable(boolean)
3403 */
3404 public void setOnLongClickListener(OnLongClickListener l) {
3405 if (!isLongClickable()) {
3406 setLongClickable(true);
3407 }
3408 mOnLongClickListener = l;
3409 }
3410
3411 /**
3412 * Register a callback to be invoked when the context menu for this view is
3413 * being built. If this view is not long clickable, it becomes long clickable.
3414 *
3415 * @param l The callback that will run
3416 *
3417 */
3418 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3419 if (!isLongClickable()) {
3420 setLongClickable(true);
3421 }
3422 mOnCreateContextMenuListener = l;
3423 }
3424
3425 /**
3426 * Call this view's OnClickListener, if it is defined.
3427 *
3428 * @return True there was an assigned OnClickListener that was called, false
3429 * otherwise is returned.
3430 */
3431 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003432 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 if (mOnClickListener != null) {
3435 playSoundEffect(SoundEffectConstants.CLICK);
3436 mOnClickListener.onClick(this);
3437 return true;
3438 }
3439
3440 return false;
3441 }
3442
3443 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003444 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3445 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003447 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 */
3449 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003450 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 boolean handled = false;
3453 if (mOnLongClickListener != null) {
3454 handled = mOnLongClickListener.onLongClick(View.this);
3455 }
3456 if (!handled) {
3457 handled = showContextMenu();
3458 }
3459 if (handled) {
3460 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3461 }
3462 return handled;
3463 }
3464
3465 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003466 * Performs button-related actions during a touch down event.
3467 *
3468 * @param event The event.
3469 * @return True if the down was consumed.
3470 *
3471 * @hide
3472 */
3473 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3474 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3475 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3476 return true;
3477 }
3478 }
3479 return false;
3480 }
3481
3482 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003483 * Bring up the context menu for this view.
3484 *
3485 * @return Whether a context menu was displayed.
3486 */
3487 public boolean showContextMenu() {
3488 return getParent().showContextMenuForChild(this);
3489 }
3490
3491 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003492 * Bring up the context menu for this view, referring to the item under the specified point.
3493 *
3494 * @param x The referenced x coordinate.
3495 * @param y The referenced y coordinate.
3496 * @param metaState The keyboard modifiers that were pressed.
3497 * @return Whether a context menu was displayed.
3498 *
3499 * @hide
3500 */
3501 public boolean showContextMenu(float x, float y, int metaState) {
3502 return showContextMenu();
3503 }
3504
3505 /**
Adam Powell6e346362010-07-23 10:18:23 -07003506 * Start an action mode.
3507 *
3508 * @param callback Callback that will control the lifecycle of the action mode
3509 * @return The new action mode if it is started, null otherwise
3510 *
3511 * @see ActionMode
3512 */
3513 public ActionMode startActionMode(ActionMode.Callback callback) {
3514 return getParent().startActionModeForChild(this, callback);
3515 }
3516
3517 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 * Register a callback to be invoked when a key is pressed in this view.
3519 * @param l the key listener to attach to this view
3520 */
3521 public void setOnKeyListener(OnKeyListener l) {
3522 mOnKeyListener = l;
3523 }
3524
3525 /**
3526 * Register a callback to be invoked when a touch event is sent to this view.
3527 * @param l the touch listener to attach to this view
3528 */
3529 public void setOnTouchListener(OnTouchListener l) {
3530 mOnTouchListener = l;
3531 }
3532
3533 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003534 * Register a callback to be invoked when a generic motion event is sent to this view.
3535 * @param l the generic motion listener to attach to this view
3536 */
3537 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3538 mOnGenericMotionListener = l;
3539 }
3540
3541 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003542 * Register a callback to be invoked when a hover event is sent to this view.
3543 * @param l the hover listener to attach to this view
3544 */
3545 public void setOnHoverListener(OnHoverListener l) {
3546 mOnHoverListener = l;
3547 }
3548
3549 /**
Joe Malin32736f02011-01-19 16:14:20 -08003550 * Register a drag event listener callback object for this View. The parameter is
3551 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3552 * View, the system calls the
3553 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3554 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003555 */
3556 public void setOnDragListener(OnDragListener l) {
3557 mOnDragListener = l;
3558 }
3559
3560 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003561 * Give this view focus. This will cause
3562 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003563 *
3564 * Note: this does not check whether this {@link View} should get focus, it just
3565 * gives it focus no matter what. It should only be called internally by framework
3566 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3567 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003568 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3569 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 * focus moved when requestFocus() is called. It may not always
3571 * apply, in which case use the default View.FOCUS_DOWN.
3572 * @param previouslyFocusedRect The rectangle of the view that had focus
3573 * prior in this View's coordinate system.
3574 */
3575 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3576 if (DBG) {
3577 System.out.println(this + " requestFocus()");
3578 }
3579
3580 if ((mPrivateFlags & FOCUSED) == 0) {
3581 mPrivateFlags |= FOCUSED;
3582
3583 if (mParent != null) {
3584 mParent.requestChildFocus(this, this);
3585 }
3586
3587 onFocusChanged(true, direction, previouslyFocusedRect);
3588 refreshDrawableState();
3589 }
3590 }
3591
3592 /**
3593 * Request that a rectangle of this view be visible on the screen,
3594 * scrolling if necessary just enough.
3595 *
3596 * <p>A View should call this if it maintains some notion of which part
3597 * of its content is interesting. For example, a text editing view
3598 * should call this when its cursor moves.
3599 *
3600 * @param rectangle The rectangle.
3601 * @return Whether any parent scrolled.
3602 */
3603 public boolean requestRectangleOnScreen(Rect rectangle) {
3604 return requestRectangleOnScreen(rectangle, false);
3605 }
3606
3607 /**
3608 * Request that a rectangle of this view be visible on the screen,
3609 * scrolling if necessary just enough.
3610 *
3611 * <p>A View should call this if it maintains some notion of which part
3612 * of its content is interesting. For example, a text editing view
3613 * should call this when its cursor moves.
3614 *
3615 * <p>When <code>immediate</code> is set to true, scrolling will not be
3616 * animated.
3617 *
3618 * @param rectangle The rectangle.
3619 * @param immediate True to forbid animated scrolling, false otherwise
3620 * @return Whether any parent scrolled.
3621 */
3622 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3623 View child = this;
3624 ViewParent parent = mParent;
3625 boolean scrolled = false;
3626 while (parent != null) {
3627 scrolled |= parent.requestChildRectangleOnScreen(child,
3628 rectangle, immediate);
3629
3630 // offset rect so next call has the rectangle in the
3631 // coordinate system of its direct child.
3632 rectangle.offset(child.getLeft(), child.getTop());
3633 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3634
3635 if (!(parent instanceof View)) {
3636 break;
3637 }
Romain Guy8506ab42009-06-11 17:35:47 -07003638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003639 child = (View) parent;
3640 parent = child.getParent();
3641 }
3642 return scrolled;
3643 }
3644
3645 /**
3646 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003647 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003648 */
3649 public void clearFocus() {
3650 if (DBG) {
3651 System.out.println(this + " clearFocus()");
3652 }
3653
3654 if ((mPrivateFlags & FOCUSED) != 0) {
3655 mPrivateFlags &= ~FOCUSED;
3656
3657 if (mParent != null) {
3658 mParent.clearChildFocus(this);
3659 }
3660
3661 onFocusChanged(false, 0, null);
3662 refreshDrawableState();
3663 }
3664 }
3665
3666 /**
3667 * Called to clear the focus of a view that is about to be removed.
3668 * Doesn't call clearChildFocus, which prevents this view from taking
3669 * focus again before it has been removed from the parent
3670 */
3671 void clearFocusForRemoval() {
3672 if ((mPrivateFlags & FOCUSED) != 0) {
3673 mPrivateFlags &= ~FOCUSED;
3674
3675 onFocusChanged(false, 0, null);
3676 refreshDrawableState();
3677 }
3678 }
3679
3680 /**
3681 * Called internally by the view system when a new view is getting focus.
3682 * This is what clears the old focus.
3683 */
3684 void unFocus() {
3685 if (DBG) {
3686 System.out.println(this + " unFocus()");
3687 }
3688
3689 if ((mPrivateFlags & FOCUSED) != 0) {
3690 mPrivateFlags &= ~FOCUSED;
3691
3692 onFocusChanged(false, 0, null);
3693 refreshDrawableState();
3694 }
3695 }
3696
3697 /**
3698 * Returns true if this view has focus iteself, or is the ancestor of the
3699 * view that has focus.
3700 *
3701 * @return True if this view has or contains focus, false otherwise.
3702 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003703 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003704 public boolean hasFocus() {
3705 return (mPrivateFlags & FOCUSED) != 0;
3706 }
3707
3708 /**
3709 * Returns true if this view is focusable or if it contains a reachable View
3710 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3711 * is a View whose parents do not block descendants focus.
3712 *
3713 * Only {@link #VISIBLE} views are considered focusable.
3714 *
3715 * @return True if the view is focusable or if the view contains a focusable
3716 * View, false otherwise.
3717 *
3718 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3719 */
3720 public boolean hasFocusable() {
3721 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3722 }
3723
3724 /**
3725 * Called by the view system when the focus state of this view changes.
3726 * When the focus change event is caused by directional navigation, direction
3727 * and previouslyFocusedRect provide insight into where the focus is coming from.
3728 * When overriding, be sure to call up through to the super class so that
3729 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003730 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003731 * @param gainFocus True if the View has focus; false otherwise.
3732 * @param direction The direction focus has moved when requestFocus()
3733 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003734 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3735 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3736 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003737 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3738 * system, of the previously focused view. If applicable, this will be
3739 * passed in as finer grained information about where the focus is coming
3740 * from (in addition to direction). Will be <code>null</code> otherwise.
3741 */
3742 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003743 if (gainFocus) {
3744 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3745 }
3746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747 InputMethodManager imm = InputMethodManager.peekInstance();
3748 if (!gainFocus) {
3749 if (isPressed()) {
3750 setPressed(false);
3751 }
3752 if (imm != null && mAttachInfo != null
3753 && mAttachInfo.mHasWindowFocus) {
3754 imm.focusOut(this);
3755 }
Romain Guya2431d02009-04-30 16:30:00 -07003756 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 } else if (imm != null && mAttachInfo != null
3758 && mAttachInfo.mHasWindowFocus) {
3759 imm.focusIn(this);
3760 }
Romain Guy8506ab42009-06-11 17:35:47 -07003761
Romain Guy0fd89bf2011-01-26 15:41:30 -08003762 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 if (mOnFocusChangeListener != null) {
3764 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3765 }
Joe Malin32736f02011-01-19 16:14:20 -08003766
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003767 if (mAttachInfo != null) {
3768 mAttachInfo.mKeyDispatchState.reset(this);
3769 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 }
3771
3772 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003773 * Sends an accessibility event of the given type. If accessiiblity is
3774 * not enabled this method has no effect. The default implementation calls
3775 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3776 * to populate information about the event source (this View), then calls
3777 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3778 * populate the text content of the event source including its descendants,
3779 * and last calls
3780 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3781 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003782 * <p>
3783 * If an {@link AccessibilityDelegate} has been specified via calling
3784 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3785 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
3786 * responsible for handling this call.
3787 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003788 *
3789 * @param eventType The type of the event to send.
3790 *
3791 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3792 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3793 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003794 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07003795 */
3796 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003797 if (mAccessibilityDelegate != null) {
3798 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
3799 } else {
3800 sendAccessibilityEventInternal(eventType);
3801 }
3802 }
3803
3804 /**
3805 * @see #sendAccessibilityEvent(int)
3806 *
3807 * Note: Called from the default {@link AccessibilityDelegate}.
3808 */
3809 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003810 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3811 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3812 }
3813 }
3814
3815 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003816 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3817 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003818 * perform a check whether accessibility is enabled.
3819 * <p>
3820 * If an {@link AccessibilityDelegate} has been specified via calling
3821 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3822 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
3823 * is responsible for handling this call.
3824 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003825 *
3826 * @param event The event to send.
3827 *
3828 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003829 */
3830 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003831 if (mAccessibilityDelegate != null) {
3832 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
3833 } else {
3834 sendAccessibilityEventUncheckedInternal(event);
3835 }
3836 }
3837
3838 /**
3839 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
3840 *
3841 * Note: Called from the default {@link AccessibilityDelegate}.
3842 */
3843 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003844 if (!isShown()) {
3845 return;
3846 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003847 onInitializeAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003848 dispatchPopulateAccessibilityEvent(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003849 // In the beginning we called #isShown(), so we know that getParent() is not null.
3850 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003851 }
3852
3853 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003854 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3855 * to its children for adding their text content to the event. Note that the
3856 * event text is populated in a separate dispatch path since we add to the
3857 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003858 * A typical implementation will call
3859 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3860 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3861 * on each child. Override this method if custom population of the event text
3862 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003863 * <p>
3864 * If an {@link AccessibilityDelegate} has been specified via calling
3865 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3866 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
3867 * is responsible for handling this call.
3868 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07003869 *
3870 * @param event The event.
3871 *
3872 * @return True if the event population was completed.
3873 */
3874 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003875 if (mAccessibilityDelegate != null) {
3876 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
3877 } else {
3878 return dispatchPopulateAccessibilityEventInternal(event);
3879 }
3880 }
3881
3882 /**
3883 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3884 *
3885 * Note: Called from the default {@link AccessibilityDelegate}.
3886 */
3887 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganovb84b94e2011-09-22 19:26:56 -07003888 // Do not populate text to scroll events. They describe position change
3889 // and usually come from container with a lot of text which is not very
3890 // informative for accessibility purposes. Also they are fired frequently.
3891 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) {
3892 return true;
3893 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003894 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003895 return false;
3896 }
3897
3898 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003899 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003900 * giving a chance to this View to populate the accessibility event with its
3901 * text content. While the implementation is free to modify other event
3902 * attributes this should be performed in
3903 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
3904 * <p>
3905 * Example: Adding formatted date string to an accessibility event in addition
3906 * to the text added by the super implementation.
3907 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003908 * public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3909 * super.onPopulateAccessibilityEvent(event);
3910 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
3911 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
3912 * mCurrentDate.getTimeInMillis(), flags);
3913 * event.getText().add(selectedDateUtterance);
3914 * }
3915 * </code></pre></p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003916 * <p>
3917 * If an {@link AccessibilityDelegate} has been specified via calling
3918 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3919 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
3920 * is responsible for handling this call.
3921 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003922 *
3923 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003924 *
3925 * @see #sendAccessibilityEvent(int)
3926 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003927 */
3928 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003929 if (mAccessibilityDelegate != null) {
3930 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
3931 } else {
3932 onPopulateAccessibilityEventInternal(event);
3933 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003934 }
3935
3936 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003937 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
3938 *
3939 * Note: Called from the default {@link AccessibilityDelegate}.
3940 */
3941 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
3942
3943 }
3944
3945 /**
3946 * Initializes an {@link AccessibilityEvent} with information about
3947 * this View which is the event source. In other words, the source of
3948 * an accessibility event is the view whose state change triggered firing
3949 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003950 * <p>
3951 * Example: Setting the password property of an event in addition
3952 * to properties set by the super implementation.
3953 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003954 * public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
3955 * super.onInitializeAccessibilityEvent(event);
3956 * event.setPassword(true);
3957 * }
3958 * </code></pre></p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003959 * <p>
3960 * If an {@link AccessibilityDelegate} has been specified via calling
3961 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3962 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
3963 * is responsible for handling this call.
3964 * </p>
3965 *
3966 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003967 *
3968 * @see #sendAccessibilityEvent(int)
3969 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3970 */
3971 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003972 if (mAccessibilityDelegate != null) {
3973 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
3974 } else {
3975 onInitializeAccessibilityEventInternal(event);
3976 }
3977 }
3978
3979 /**
3980 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3981 *
3982 * Note: Called from the default {@link AccessibilityDelegate}.
3983 */
3984 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003985 event.setSource(this);
Svetoslav Ganov30401322011-05-12 18:53:45 -07003986 event.setClassName(getClass().getName());
3987 event.setPackageName(getContext().getPackageName());
3988 event.setEnabled(isEnabled());
3989 event.setContentDescription(mContentDescription);
3990
Svetoslav Ganova0156172011-06-26 17:55:44 -07003991 final int eventType = event.getEventType();
3992 switch (eventType) {
3993 case AccessibilityEvent.TYPE_VIEW_FOCUSED: {
3994 if (mAttachInfo != null) {
3995 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3996 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
3997 FOCUSABLES_ALL);
3998 event.setItemCount(focusablesTempList.size());
3999 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4000 focusablesTempList.clear();
4001 }
4002 } break;
4003 case AccessibilityEvent.TYPE_VIEW_SCROLLED: {
4004 event.setScrollX(mScrollX);
4005 event.setScrollY(mScrollY);
4006 event.setItemCount(getHeight());
4007 } break;
Svetoslav Ganov30401322011-05-12 18:53:45 -07004008 }
4009 }
4010
4011 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004012 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4013 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4014 * This method is responsible for obtaining an accessibility node info from a
4015 * pool of reusable instances and calling
4016 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4017 * initialize the former.
4018 * <p>
4019 * Note: The client is responsible for recycling the obtained instance by calling
4020 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4021 * </p>
4022 * @return A populated {@link AccessibilityNodeInfo}.
4023 *
4024 * @see AccessibilityNodeInfo
4025 */
4026 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
4027 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4028 onInitializeAccessibilityNodeInfo(info);
4029 return info;
4030 }
4031
4032 /**
4033 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4034 * The base implementation sets:
4035 * <ul>
4036 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004037 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4038 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004039 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4040 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4041 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4042 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4043 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4044 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4045 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4046 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4047 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4048 * </ul>
4049 * <p>
4050 * Subclasses should override this method, call the super implementation,
4051 * and set additional attributes.
4052 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004053 * <p>
4054 * If an {@link AccessibilityDelegate} has been specified via calling
4055 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4056 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4057 * is responsible for handling this call.
4058 * </p>
4059 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004060 * @param info The instance to initialize.
4061 */
4062 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004063 if (mAccessibilityDelegate != null) {
4064 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4065 } else {
4066 onInitializeAccessibilityNodeInfoInternal(info);
4067 }
4068 }
4069
4070 /**
4071 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4072 *
4073 * Note: Called from the default {@link AccessibilityDelegate}.
4074 */
4075 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004076 Rect bounds = mAttachInfo.mTmpInvalRect;
4077 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004078 info.setBoundsInParent(bounds);
4079
4080 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4081 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004082 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004083 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4084 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004085
4086 ViewParent parent = getParent();
4087 if (parent instanceof View) {
4088 View parentView = (View) parent;
4089 info.setParent(parentView);
4090 }
4091
4092 info.setPackageName(mContext.getPackageName());
4093 info.setClassName(getClass().getName());
4094 info.setContentDescription(getContentDescription());
4095
4096 info.setEnabled(isEnabled());
4097 info.setClickable(isClickable());
4098 info.setFocusable(isFocusable());
4099 info.setFocused(isFocused());
4100 info.setSelected(isSelected());
4101 info.setLongClickable(isLongClickable());
4102
4103 // TODO: These make sense only if we are in an AdapterView but all
4104 // views can be selected. Maybe from accessiiblity perspective
4105 // we should report as selectable view in an AdapterView.
4106 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4107 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4108
4109 if (isFocusable()) {
4110 if (isFocused()) {
4111 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4112 } else {
4113 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4114 }
4115 }
4116 }
4117
4118 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004119 * Sets a delegate for implementing accessibility support via compositon as
4120 * opposed to inheritance. The delegate's primary use is for implementing
4121 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4122 *
4123 * @param delegate The delegate instance.
4124 *
4125 * @see AccessibilityDelegate
4126 */
4127 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4128 mAccessibilityDelegate = delegate;
4129 }
4130
4131 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004132 * Gets the unique identifier of this view on the screen for accessibility purposes.
4133 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4134 *
4135 * @return The view accessibility id.
4136 *
4137 * @hide
4138 */
4139 public int getAccessibilityViewId() {
4140 if (mAccessibilityViewId == NO_ID) {
4141 mAccessibilityViewId = sNextAccessibilityViewId++;
4142 }
4143 return mAccessibilityViewId;
4144 }
4145
4146 /**
4147 * Gets the unique identifier of the window in which this View reseides.
4148 *
4149 * @return The window accessibility id.
4150 *
4151 * @hide
4152 */
4153 public int getAccessibilityWindowId() {
4154 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4155 }
4156
4157 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004158 * Gets the {@link View} description. It briefly describes the view and is
4159 * primarily used for accessibility support. Set this property to enable
4160 * better accessibility support for your application. This is especially
4161 * true for views that do not have textual representation (For example,
4162 * ImageButton).
4163 *
4164 * @return The content descriptiopn.
4165 *
4166 * @attr ref android.R.styleable#View_contentDescription
4167 */
4168 public CharSequence getContentDescription() {
4169 return mContentDescription;
4170 }
4171
4172 /**
4173 * Sets the {@link View} description. It briefly describes the view and is
4174 * primarily used for accessibility support. Set this property to enable
4175 * better accessibility support for your application. This is especially
4176 * true for views that do not have textual representation (For example,
4177 * ImageButton).
4178 *
4179 * @param contentDescription The content description.
4180 *
4181 * @attr ref android.R.styleable#View_contentDescription
4182 */
4183 public void setContentDescription(CharSequence contentDescription) {
4184 mContentDescription = contentDescription;
4185 }
4186
4187 /**
Romain Guya2431d02009-04-30 16:30:00 -07004188 * Invoked whenever this view loses focus, either by losing window focus or by losing
4189 * focus within its window. This method can be used to clear any state tied to the
4190 * focus. For instance, if a button is held pressed with the trackball and the window
4191 * loses focus, this method can be used to cancel the press.
4192 *
4193 * Subclasses of View overriding this method should always call super.onFocusLost().
4194 *
4195 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004196 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004197 *
4198 * @hide pending API council approval
4199 */
4200 protected void onFocusLost() {
4201 resetPressedState();
4202 }
4203
4204 private void resetPressedState() {
4205 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4206 return;
4207 }
4208
4209 if (isPressed()) {
4210 setPressed(false);
4211
4212 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004213 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004214 }
4215 }
4216 }
4217
4218 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004219 * Returns true if this view has focus
4220 *
4221 * @return True if this view has focus, false otherwise.
4222 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004223 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004224 public boolean isFocused() {
4225 return (mPrivateFlags & FOCUSED) != 0;
4226 }
4227
4228 /**
4229 * Find the view in the hierarchy rooted at this view that currently has
4230 * focus.
4231 *
4232 * @return The view that currently has focus, or null if no focused view can
4233 * be found.
4234 */
4235 public View findFocus() {
4236 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4237 }
4238
4239 /**
4240 * Change whether this view is one of the set of scrollable containers in
4241 * its window. This will be used to determine whether the window can
4242 * resize or must pan when a soft input area is open -- scrollable
4243 * containers allow the window to use resize mode since the container
4244 * will appropriately shrink.
4245 */
4246 public void setScrollContainer(boolean isScrollContainer) {
4247 if (isScrollContainer) {
4248 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4249 mAttachInfo.mScrollContainers.add(this);
4250 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4251 }
4252 mPrivateFlags |= SCROLL_CONTAINER;
4253 } else {
4254 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4255 mAttachInfo.mScrollContainers.remove(this);
4256 }
4257 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4258 }
4259 }
4260
4261 /**
4262 * Returns the quality of the drawing cache.
4263 *
4264 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4265 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4266 *
4267 * @see #setDrawingCacheQuality(int)
4268 * @see #setDrawingCacheEnabled(boolean)
4269 * @see #isDrawingCacheEnabled()
4270 *
4271 * @attr ref android.R.styleable#View_drawingCacheQuality
4272 */
4273 public int getDrawingCacheQuality() {
4274 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4275 }
4276
4277 /**
4278 * Set the drawing cache quality of this view. This value is used only when the
4279 * drawing cache is enabled
4280 *
4281 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4282 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4283 *
4284 * @see #getDrawingCacheQuality()
4285 * @see #setDrawingCacheEnabled(boolean)
4286 * @see #isDrawingCacheEnabled()
4287 *
4288 * @attr ref android.R.styleable#View_drawingCacheQuality
4289 */
4290 public void setDrawingCacheQuality(int quality) {
4291 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4292 }
4293
4294 /**
4295 * Returns whether the screen should remain on, corresponding to the current
4296 * value of {@link #KEEP_SCREEN_ON}.
4297 *
4298 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4299 *
4300 * @see #setKeepScreenOn(boolean)
4301 *
4302 * @attr ref android.R.styleable#View_keepScreenOn
4303 */
4304 public boolean getKeepScreenOn() {
4305 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4306 }
4307
4308 /**
4309 * Controls whether the screen should remain on, modifying the
4310 * value of {@link #KEEP_SCREEN_ON}.
4311 *
4312 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4313 *
4314 * @see #getKeepScreenOn()
4315 *
4316 * @attr ref android.R.styleable#View_keepScreenOn
4317 */
4318 public void setKeepScreenOn(boolean keepScreenOn) {
4319 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4320 }
4321
4322 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004323 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4324 * @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 -08004325 *
4326 * @attr ref android.R.styleable#View_nextFocusLeft
4327 */
4328 public int getNextFocusLeftId() {
4329 return mNextFocusLeftId;
4330 }
4331
4332 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004333 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4334 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4335 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004336 *
4337 * @attr ref android.R.styleable#View_nextFocusLeft
4338 */
4339 public void setNextFocusLeftId(int nextFocusLeftId) {
4340 mNextFocusLeftId = nextFocusLeftId;
4341 }
4342
4343 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004344 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4345 * @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 -08004346 *
4347 * @attr ref android.R.styleable#View_nextFocusRight
4348 */
4349 public int getNextFocusRightId() {
4350 return mNextFocusRightId;
4351 }
4352
4353 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004354 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4355 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4356 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357 *
4358 * @attr ref android.R.styleable#View_nextFocusRight
4359 */
4360 public void setNextFocusRightId(int nextFocusRightId) {
4361 mNextFocusRightId = nextFocusRightId;
4362 }
4363
4364 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004365 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4366 * @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 -08004367 *
4368 * @attr ref android.R.styleable#View_nextFocusUp
4369 */
4370 public int getNextFocusUpId() {
4371 return mNextFocusUpId;
4372 }
4373
4374 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004375 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4376 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4377 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004378 *
4379 * @attr ref android.R.styleable#View_nextFocusUp
4380 */
4381 public void setNextFocusUpId(int nextFocusUpId) {
4382 mNextFocusUpId = nextFocusUpId;
4383 }
4384
4385 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004386 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4387 * @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 -08004388 *
4389 * @attr ref android.R.styleable#View_nextFocusDown
4390 */
4391 public int getNextFocusDownId() {
4392 return mNextFocusDownId;
4393 }
4394
4395 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004396 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4397 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4398 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004399 *
4400 * @attr ref android.R.styleable#View_nextFocusDown
4401 */
4402 public void setNextFocusDownId(int nextFocusDownId) {
4403 mNextFocusDownId = nextFocusDownId;
4404 }
4405
4406 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004407 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4408 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4409 *
4410 * @attr ref android.R.styleable#View_nextFocusForward
4411 */
4412 public int getNextFocusForwardId() {
4413 return mNextFocusForwardId;
4414 }
4415
4416 /**
4417 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4418 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4419 * decide automatically.
4420 *
4421 * @attr ref android.R.styleable#View_nextFocusForward
4422 */
4423 public void setNextFocusForwardId(int nextFocusForwardId) {
4424 mNextFocusForwardId = nextFocusForwardId;
4425 }
4426
4427 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004428 * Returns the visibility of this view and all of its ancestors
4429 *
4430 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4431 */
4432 public boolean isShown() {
4433 View current = this;
4434 //noinspection ConstantConditions
4435 do {
4436 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4437 return false;
4438 }
4439 ViewParent parent = current.mParent;
4440 if (parent == null) {
4441 return false; // We are not attached to the view root
4442 }
4443 if (!(parent instanceof View)) {
4444 return true;
4445 }
4446 current = (View) parent;
4447 } while (current != null);
4448
4449 return false;
4450 }
4451
4452 /**
4453 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4454 * is set
4455 *
4456 * @param insets Insets for system windows
4457 *
4458 * @return True if this view applied the insets, false otherwise
4459 */
4460 protected boolean fitSystemWindows(Rect insets) {
4461 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4462 mPaddingLeft = insets.left;
4463 mPaddingTop = insets.top;
4464 mPaddingRight = insets.right;
4465 mPaddingBottom = insets.bottom;
4466 requestLayout();
4467 return true;
4468 }
4469 return false;
4470 }
4471
4472 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004473 * Set whether or not this view should account for system screen decorations
4474 * such as the status bar and inset its content. This allows this view to be
4475 * positioned in absolute screen coordinates and remain visible to the user.
4476 *
4477 * <p>This should only be used by top-level window decor views.
4478 *
4479 * @param fitSystemWindows true to inset content for system screen decorations, false for
4480 * default behavior.
4481 *
4482 * @attr ref android.R.styleable#View_fitsSystemWindows
4483 */
4484 public void setFitsSystemWindows(boolean fitSystemWindows) {
4485 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4486 }
4487
4488 /**
4489 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4490 * will account for system screen decorations such as the status bar and inset its
4491 * content. This allows the view to be positioned in absolute screen coordinates
4492 * and remain visible to the user.
4493 *
4494 * @return true if this view will adjust its content bounds for system screen decorations.
4495 *
4496 * @attr ref android.R.styleable#View_fitsSystemWindows
4497 */
4498 public boolean fitsSystemWindows() {
4499 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4500 }
4501
4502 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004503 * Returns the visibility status for this view.
4504 *
4505 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4506 * @attr ref android.R.styleable#View_visibility
4507 */
4508 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004509 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4510 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4511 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004512 })
4513 public int getVisibility() {
4514 return mViewFlags & VISIBILITY_MASK;
4515 }
4516
4517 /**
4518 * Set the enabled state of this view.
4519 *
4520 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4521 * @attr ref android.R.styleable#View_visibility
4522 */
4523 @RemotableViewMethod
4524 public void setVisibility(int visibility) {
4525 setFlags(visibility, VISIBILITY_MASK);
4526 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4527 }
4528
4529 /**
4530 * Returns the enabled status for this view. The interpretation of the
4531 * enabled state varies by subclass.
4532 *
4533 * @return True if this view is enabled, false otherwise.
4534 */
4535 @ViewDebug.ExportedProperty
4536 public boolean isEnabled() {
4537 return (mViewFlags & ENABLED_MASK) == ENABLED;
4538 }
4539
4540 /**
4541 * Set the enabled state of this view. The interpretation of the enabled
4542 * state varies by subclass.
4543 *
4544 * @param enabled True if this view is enabled, false otherwise.
4545 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004546 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004547 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004548 if (enabled == isEnabled()) return;
4549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004550 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4551
4552 /*
4553 * The View most likely has to change its appearance, so refresh
4554 * the drawable state.
4555 */
4556 refreshDrawableState();
4557
4558 // Invalidate too, since the default behavior for views is to be
4559 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004560 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004561 }
4562
4563 /**
4564 * Set whether this view can receive the focus.
4565 *
4566 * Setting this to false will also ensure that this view is not focusable
4567 * in touch mode.
4568 *
4569 * @param focusable If true, this view can receive the focus.
4570 *
4571 * @see #setFocusableInTouchMode(boolean)
4572 * @attr ref android.R.styleable#View_focusable
4573 */
4574 public void setFocusable(boolean focusable) {
4575 if (!focusable) {
4576 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4577 }
4578 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4579 }
4580
4581 /**
4582 * Set whether this view can receive focus while in touch mode.
4583 *
4584 * Setting this to true will also ensure that this view is focusable.
4585 *
4586 * @param focusableInTouchMode If true, this view can receive the focus while
4587 * in touch mode.
4588 *
4589 * @see #setFocusable(boolean)
4590 * @attr ref android.R.styleable#View_focusableInTouchMode
4591 */
4592 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4593 // Focusable in touch mode should always be set before the focusable flag
4594 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4595 // which, in touch mode, will not successfully request focus on this view
4596 // because the focusable in touch mode flag is not set
4597 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4598 if (focusableInTouchMode) {
4599 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4600 }
4601 }
4602
4603 /**
4604 * Set whether this view should have sound effects enabled for events such as
4605 * clicking and touching.
4606 *
4607 * <p>You may wish to disable sound effects for a view if you already play sounds,
4608 * for instance, a dial key that plays dtmf tones.
4609 *
4610 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4611 * @see #isSoundEffectsEnabled()
4612 * @see #playSoundEffect(int)
4613 * @attr ref android.R.styleable#View_soundEffectsEnabled
4614 */
4615 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4616 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4617 }
4618
4619 /**
4620 * @return whether this view should have sound effects enabled for events such as
4621 * clicking and touching.
4622 *
4623 * @see #setSoundEffectsEnabled(boolean)
4624 * @see #playSoundEffect(int)
4625 * @attr ref android.R.styleable#View_soundEffectsEnabled
4626 */
4627 @ViewDebug.ExportedProperty
4628 public boolean isSoundEffectsEnabled() {
4629 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4630 }
4631
4632 /**
4633 * Set whether this view should have haptic feedback for events such as
4634 * long presses.
4635 *
4636 * <p>You may wish to disable haptic feedback if your view already controls
4637 * its own haptic feedback.
4638 *
4639 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4640 * @see #isHapticFeedbackEnabled()
4641 * @see #performHapticFeedback(int)
4642 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4643 */
4644 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4645 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4646 }
4647
4648 /**
4649 * @return whether this view should have haptic feedback enabled for events
4650 * long presses.
4651 *
4652 * @see #setHapticFeedbackEnabled(boolean)
4653 * @see #performHapticFeedback(int)
4654 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4655 */
4656 @ViewDebug.ExportedProperty
4657 public boolean isHapticFeedbackEnabled() {
4658 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4659 }
4660
4661 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004662 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004663 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004664 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4665 * {@link #LAYOUT_DIRECTION_RTL},
4666 * {@link #LAYOUT_DIRECTION_INHERIT} or
4667 * {@link #LAYOUT_DIRECTION_LOCALE}.
4668 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004669 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004670 * @hide
4671 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004672 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004673 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4674 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4675 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4676 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004677 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004678 public int getLayoutDirection() {
4679 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004680 }
4681
4682 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004683 * Set the layout direction for this view. This will propagate a reset of layout direction
4684 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004685 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004686 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4687 * {@link #LAYOUT_DIRECTION_RTL},
4688 * {@link #LAYOUT_DIRECTION_INHERIT} or
4689 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004690 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004691 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004692 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004693 * @hide
4694 */
4695 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004696 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004697 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004698 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004699 // Setting the flag will also request a layout.
4700 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4701 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004702 }
4703
4704 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004705 * Returns the resolved layout direction for this view.
4706 *
4707 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4708 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4709 *
4710 * @hide
4711 */
4712 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4713 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4714 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4715 })
4716 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004717 resolveLayoutDirectionIfNeeded();
4718 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004719 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4720 }
4721
4722 /**
4723 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4724 * layout attribute and/or the inherited value from the parent.</p>
4725 *
4726 * @return true if the layout is right-to-left.
4727 *
4728 * @hide
4729 */
4730 @ViewDebug.ExportedProperty(category = "layout")
4731 public boolean isLayoutRtl() {
4732 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4733 }
4734
4735 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004736 * If this view doesn't do any drawing on its own, set this flag to
4737 * allow further optimizations. By default, this flag is not set on
4738 * View, but could be set on some View subclasses such as ViewGroup.
4739 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004740 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4741 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004742 *
4743 * @param willNotDraw whether or not this View draw on its own
4744 */
4745 public void setWillNotDraw(boolean willNotDraw) {
4746 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4747 }
4748
4749 /**
4750 * Returns whether or not this View draws on its own.
4751 *
4752 * @return true if this view has nothing to draw, false otherwise
4753 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004754 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004755 public boolean willNotDraw() {
4756 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4757 }
4758
4759 /**
4760 * When a View's drawing cache is enabled, drawing is redirected to an
4761 * offscreen bitmap. Some views, like an ImageView, must be able to
4762 * bypass this mechanism if they already draw a single bitmap, to avoid
4763 * unnecessary usage of the memory.
4764 *
4765 * @param willNotCacheDrawing true if this view does not cache its
4766 * drawing, false otherwise
4767 */
4768 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4769 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4770 }
4771
4772 /**
4773 * Returns whether or not this View can cache its drawing or not.
4774 *
4775 * @return true if this view does not cache its drawing, false otherwise
4776 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004777 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004778 public boolean willNotCacheDrawing() {
4779 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4780 }
4781
4782 /**
4783 * Indicates whether this view reacts to click events or not.
4784 *
4785 * @return true if the view is clickable, false otherwise
4786 *
4787 * @see #setClickable(boolean)
4788 * @attr ref android.R.styleable#View_clickable
4789 */
4790 @ViewDebug.ExportedProperty
4791 public boolean isClickable() {
4792 return (mViewFlags & CLICKABLE) == CLICKABLE;
4793 }
4794
4795 /**
4796 * Enables or disables click events for this view. When a view
4797 * is clickable it will change its state to "pressed" on every click.
4798 * Subclasses should set the view clickable to visually react to
4799 * user's clicks.
4800 *
4801 * @param clickable true to make the view clickable, false otherwise
4802 *
4803 * @see #isClickable()
4804 * @attr ref android.R.styleable#View_clickable
4805 */
4806 public void setClickable(boolean clickable) {
4807 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4808 }
4809
4810 /**
4811 * Indicates whether this view reacts to long click events or not.
4812 *
4813 * @return true if the view is long clickable, false otherwise
4814 *
4815 * @see #setLongClickable(boolean)
4816 * @attr ref android.R.styleable#View_longClickable
4817 */
4818 public boolean isLongClickable() {
4819 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4820 }
4821
4822 /**
4823 * Enables or disables long click events for this view. When a view is long
4824 * clickable it reacts to the user holding down the button for a longer
4825 * duration than a tap. This event can either launch the listener or a
4826 * context menu.
4827 *
4828 * @param longClickable true to make the view long clickable, false otherwise
4829 * @see #isLongClickable()
4830 * @attr ref android.R.styleable#View_longClickable
4831 */
4832 public void setLongClickable(boolean longClickable) {
4833 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4834 }
4835
4836 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004837 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004838 *
4839 * @see #isClickable()
4840 * @see #setClickable(boolean)
4841 *
4842 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4843 * the View's internal state from a previously set "pressed" state.
4844 */
4845 public void setPressed(boolean pressed) {
4846 if (pressed) {
4847 mPrivateFlags |= PRESSED;
4848 } else {
4849 mPrivateFlags &= ~PRESSED;
4850 }
4851 refreshDrawableState();
4852 dispatchSetPressed(pressed);
4853 }
4854
4855 /**
4856 * Dispatch setPressed to all of this View's children.
4857 *
4858 * @see #setPressed(boolean)
4859 *
4860 * @param pressed The new pressed state
4861 */
4862 protected void dispatchSetPressed(boolean pressed) {
4863 }
4864
4865 /**
4866 * Indicates whether the view is currently in pressed state. Unless
4867 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4868 * the pressed state.
4869 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004870 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004871 * @see #isClickable()
4872 * @see #setClickable(boolean)
4873 *
4874 * @return true if the view is currently pressed, false otherwise
4875 */
4876 public boolean isPressed() {
4877 return (mPrivateFlags & PRESSED) == PRESSED;
4878 }
4879
4880 /**
4881 * Indicates whether this view will save its state (that is,
4882 * whether its {@link #onSaveInstanceState} method will be called).
4883 *
4884 * @return Returns true if the view state saving is enabled, else false.
4885 *
4886 * @see #setSaveEnabled(boolean)
4887 * @attr ref android.R.styleable#View_saveEnabled
4888 */
4889 public boolean isSaveEnabled() {
4890 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4891 }
4892
4893 /**
4894 * Controls whether the saving of this view's state is
4895 * enabled (that is, whether its {@link #onSaveInstanceState} method
4896 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004897 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004898 * for its state to be saved. This flag can only disable the
4899 * saving of this view; any child views may still have their state saved.
4900 *
4901 * @param enabled Set to false to <em>disable</em> state saving, or true
4902 * (the default) to allow it.
4903 *
4904 * @see #isSaveEnabled()
4905 * @see #setId(int)
4906 * @see #onSaveInstanceState()
4907 * @attr ref android.R.styleable#View_saveEnabled
4908 */
4909 public void setSaveEnabled(boolean enabled) {
4910 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4911 }
4912
Jeff Brown85a31762010-09-01 17:01:00 -07004913 /**
4914 * Gets whether the framework should discard touches when the view's
4915 * window is obscured by another visible window.
4916 * Refer to the {@link View} security documentation for more details.
4917 *
4918 * @return True if touch filtering is enabled.
4919 *
4920 * @see #setFilterTouchesWhenObscured(boolean)
4921 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4922 */
4923 @ViewDebug.ExportedProperty
4924 public boolean getFilterTouchesWhenObscured() {
4925 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4926 }
4927
4928 /**
4929 * Sets whether the framework should discard touches when the view's
4930 * window is obscured by another visible window.
4931 * Refer to the {@link View} security documentation for more details.
4932 *
4933 * @param enabled True if touch filtering should be enabled.
4934 *
4935 * @see #getFilterTouchesWhenObscured
4936 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4937 */
4938 public void setFilterTouchesWhenObscured(boolean enabled) {
4939 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4940 FILTER_TOUCHES_WHEN_OBSCURED);
4941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004942
4943 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004944 * Indicates whether the entire hierarchy under this view will save its
4945 * state when a state saving traversal occurs from its parent. The default
4946 * is true; if false, these views will not be saved unless
4947 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4948 *
4949 * @return Returns true if the view state saving from parent is enabled, else false.
4950 *
4951 * @see #setSaveFromParentEnabled(boolean)
4952 */
4953 public boolean isSaveFromParentEnabled() {
4954 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4955 }
4956
4957 /**
4958 * Controls whether the entire hierarchy under this view will save its
4959 * state when a state saving traversal occurs from its parent. The default
4960 * is true; if false, these views will not be saved unless
4961 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4962 *
4963 * @param enabled Set to false to <em>disable</em> state saving, or true
4964 * (the default) to allow it.
4965 *
4966 * @see #isSaveFromParentEnabled()
4967 * @see #setId(int)
4968 * @see #onSaveInstanceState()
4969 */
4970 public void setSaveFromParentEnabled(boolean enabled) {
4971 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4972 }
4973
4974
4975 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 * Returns whether this View is able to take focus.
4977 *
4978 * @return True if this view can take focus, or false otherwise.
4979 * @attr ref android.R.styleable#View_focusable
4980 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004981 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004982 public final boolean isFocusable() {
4983 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4984 }
4985
4986 /**
4987 * When a view is focusable, it may not want to take focus when in touch mode.
4988 * For example, a button would like focus when the user is navigating via a D-pad
4989 * so that the user can click on it, but once the user starts touching the screen,
4990 * the button shouldn't take focus
4991 * @return Whether the view is focusable in touch mode.
4992 * @attr ref android.R.styleable#View_focusableInTouchMode
4993 */
4994 @ViewDebug.ExportedProperty
4995 public final boolean isFocusableInTouchMode() {
4996 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4997 }
4998
4999 /**
5000 * Find the nearest view in the specified direction that can take focus.
5001 * This does not actually give focus to that view.
5002 *
5003 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5004 *
5005 * @return The nearest focusable in the specified direction, or null if none
5006 * can be found.
5007 */
5008 public View focusSearch(int direction) {
5009 if (mParent != null) {
5010 return mParent.focusSearch(this, direction);
5011 } else {
5012 return null;
5013 }
5014 }
5015
5016 /**
5017 * This method is the last chance for the focused view and its ancestors to
5018 * respond to an arrow key. This is called when the focused view did not
5019 * consume the key internally, nor could the view system find a new view in
5020 * the requested direction to give focus to.
5021 *
5022 * @param focused The currently focused view.
5023 * @param direction The direction focus wants to move. One of FOCUS_UP,
5024 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5025 * @return True if the this view consumed this unhandled move.
5026 */
5027 public boolean dispatchUnhandledMove(View focused, int direction) {
5028 return false;
5029 }
5030
5031 /**
5032 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005033 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005034 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005035 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5036 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005037 * @return The user specified next view, or null if there is none.
5038 */
5039 View findUserSetNextFocus(View root, int direction) {
5040 switch (direction) {
5041 case FOCUS_LEFT:
5042 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005043 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005044 case FOCUS_RIGHT:
5045 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005046 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005047 case FOCUS_UP:
5048 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005049 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005050 case FOCUS_DOWN:
5051 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005052 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005053 case FOCUS_FORWARD:
5054 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005055 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005056 case FOCUS_BACKWARD: {
5057 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005058 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005059 @Override
5060 public boolean apply(View t) {
5061 return t.mNextFocusForwardId == id;
5062 }
5063 });
5064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005065 }
5066 return null;
5067 }
5068
Jeff Brown4dfbec22011-08-15 14:55:37 -07005069 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5070 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5071 @Override
5072 public boolean apply(View t) {
5073 return t.mID == childViewId;
5074 }
5075 });
5076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005077 if (result == null) {
5078 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5079 + "by user for id " + childViewId);
5080 }
5081 return result;
5082 }
5083
5084 /**
5085 * Find and return all focusable views that are descendants of this view,
5086 * possibly including this view if it is focusable itself.
5087 *
5088 * @param direction The direction of the focus
5089 * @return A list of focusable views
5090 */
5091 public ArrayList<View> getFocusables(int direction) {
5092 ArrayList<View> result = new ArrayList<View>(24);
5093 addFocusables(result, direction);
5094 return result;
5095 }
5096
5097 /**
5098 * Add any focusable views that are descendants of this view (possibly
5099 * including this view if it is focusable itself) to views. If we are in touch mode,
5100 * only add views that are also focusable in touch mode.
5101 *
5102 * @param views Focusable views found so far
5103 * @param direction The direction of the focus
5104 */
5105 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005106 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005108
svetoslavganov75986cf2009-05-14 22:28:01 -07005109 /**
5110 * Adds any focusable views that are descendants of this view (possibly
5111 * including this view if it is focusable itself) to views. This method
5112 * adds all focusable views regardless if we are in touch mode or
5113 * only views focusable in touch mode if we are in touch mode depending on
5114 * the focusable mode paramater.
5115 *
5116 * @param views Focusable views found so far or null if all we are interested is
5117 * the number of focusables.
5118 * @param direction The direction of the focus.
5119 * @param focusableMode The type of focusables to be added.
5120 *
5121 * @see #FOCUSABLES_ALL
5122 * @see #FOCUSABLES_TOUCH_MODE
5123 */
5124 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5125 if (!isFocusable()) {
5126 return;
5127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005128
svetoslavganov75986cf2009-05-14 22:28:01 -07005129 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5130 isInTouchMode() && !isFocusableInTouchMode()) {
5131 return;
5132 }
5133
5134 if (views != null) {
5135 views.add(this);
5136 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005137 }
5138
5139 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005140 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005141 * The search is performed by either the text that the View renders or the content
5142 * description that describes the view for accessibility purposes and the view does
5143 * not render or both. Clients can specify how the search is to be performed via
5144 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5145 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005146 *
5147 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005148 * @param searched The text to match against.
5149 *
5150 * @see #FIND_VIEWS_WITH_TEXT
5151 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5152 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005153 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005154 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
5155 if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0 && !TextUtils.isEmpty(searched)
5156 && !TextUtils.isEmpty(mContentDescription)) {
5157 String searchedLowerCase = searched.toString().toLowerCase();
5158 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5159 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5160 outViews.add(this);
5161 }
5162 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005163 }
5164
5165 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005166 * Find and return all touchable views that are descendants of this view,
5167 * possibly including this view if it is touchable itself.
5168 *
5169 * @return A list of touchable views
5170 */
5171 public ArrayList<View> getTouchables() {
5172 ArrayList<View> result = new ArrayList<View>();
5173 addTouchables(result);
5174 return result;
5175 }
5176
5177 /**
5178 * Add any touchable views that are descendants of this view (possibly
5179 * including this view if it is touchable itself) to views.
5180 *
5181 * @param views Touchable views found so far
5182 */
5183 public void addTouchables(ArrayList<View> views) {
5184 final int viewFlags = mViewFlags;
5185
5186 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5187 && (viewFlags & ENABLED_MASK) == ENABLED) {
5188 views.add(this);
5189 }
5190 }
5191
5192 /**
5193 * Call this to try to give focus to a specific view or to one of its
5194 * descendants.
5195 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005196 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5197 * false), or if it is focusable and it is not focusable in touch mode
5198 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005199 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005200 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005201 * have focus, and you want your parent to look for the next one.
5202 *
5203 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5204 * {@link #FOCUS_DOWN} and <code>null</code>.
5205 *
5206 * @return Whether this view or one of its descendants actually took focus.
5207 */
5208 public final boolean requestFocus() {
5209 return requestFocus(View.FOCUS_DOWN);
5210 }
5211
5212
5213 /**
5214 * Call this to try to give focus to a specific view or to one of its
5215 * descendants and give it a hint about what direction focus is heading.
5216 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005217 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5218 * false), or if it is focusable and it is not focusable in touch mode
5219 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005220 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005221 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005222 * have focus, and you want your parent to look for the next one.
5223 *
5224 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5225 * <code>null</code> set for the previously focused rectangle.
5226 *
5227 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5228 * @return Whether this view or one of its descendants actually took focus.
5229 */
5230 public final boolean requestFocus(int direction) {
5231 return requestFocus(direction, null);
5232 }
5233
5234 /**
5235 * Call this to try to give focus to a specific view or to one of its descendants
5236 * and give it hints about the direction and a specific rectangle that the focus
5237 * is coming from. The rectangle can help give larger views a finer grained hint
5238 * about where focus is coming from, and therefore, where to show selection, or
5239 * forward focus change internally.
5240 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005241 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5242 * false), or if it is focusable and it is not focusable in touch mode
5243 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005244 *
5245 * A View will not take focus if it is not visible.
5246 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005247 * A View will not take focus if one of its parents has
5248 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5249 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005250 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005251 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005252 * have focus, and you want your parent to look for the next one.
5253 *
5254 * You may wish to override this method if your custom {@link View} has an internal
5255 * {@link View} that it wishes to forward the request to.
5256 *
5257 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5258 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5259 * to give a finer grained hint about where focus is coming from. May be null
5260 * if there is no hint.
5261 * @return Whether this view or one of its descendants actually took focus.
5262 */
5263 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5264 // need to be focusable
5265 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5266 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5267 return false;
5268 }
5269
5270 // need to be focusable in touch mode if in touch mode
5271 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005272 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5273 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005274 }
5275
5276 // need to not have any parents blocking us
5277 if (hasAncestorThatBlocksDescendantFocus()) {
5278 return false;
5279 }
5280
5281 handleFocusGainInternal(direction, previouslyFocusedRect);
5282 return true;
5283 }
5284
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005285 /** Gets the ViewAncestor, or null if not attached. */
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005286 /*package*/ ViewRootImpl getViewRootImpl() {
Christopher Tate2c095f32010-10-04 14:13:40 -07005287 View root = getRootView();
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005288 return root != null ? (ViewRootImpl)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07005289 }
5290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005291 /**
5292 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5293 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5294 * touch mode to request focus when they are touched.
5295 *
5296 * @return Whether this view or one of its descendants actually took focus.
5297 *
5298 * @see #isInTouchMode()
5299 *
5300 */
5301 public final boolean requestFocusFromTouch() {
5302 // Leave touch mode if we need to
5303 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005304 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005305 if (viewRoot != null) {
5306 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005307 }
5308 }
5309 return requestFocus(View.FOCUS_DOWN);
5310 }
5311
5312 /**
5313 * @return Whether any ancestor of this view blocks descendant focus.
5314 */
5315 private boolean hasAncestorThatBlocksDescendantFocus() {
5316 ViewParent ancestor = mParent;
5317 while (ancestor instanceof ViewGroup) {
5318 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5319 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5320 return true;
5321 } else {
5322 ancestor = vgAncestor.getParent();
5323 }
5324 }
5325 return false;
5326 }
5327
5328 /**
Romain Guya440b002010-02-24 15:57:54 -08005329 * @hide
5330 */
5331 public void dispatchStartTemporaryDetach() {
5332 onStartTemporaryDetach();
5333 }
5334
5335 /**
5336 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005337 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5338 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005339 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005340 */
5341 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005342 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005343 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005344 }
5345
5346 /**
5347 * @hide
5348 */
5349 public void dispatchFinishTemporaryDetach() {
5350 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005351 }
Romain Guy8506ab42009-06-11 17:35:47 -07005352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005353 /**
5354 * Called after {@link #onStartTemporaryDetach} when the container is done
5355 * changing the view.
5356 */
5357 public void onFinishTemporaryDetach() {
5358 }
Romain Guy8506ab42009-06-11 17:35:47 -07005359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005360 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005361 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5362 * for this view's window. Returns null if the view is not currently attached
5363 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005364 * just use the standard high-level event callbacks like
5365 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005366 */
5367 public KeyEvent.DispatcherState getKeyDispatcherState() {
5368 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5369 }
Joe Malin32736f02011-01-19 16:14:20 -08005370
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005371 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005372 * Dispatch a key event before it is processed by any input method
5373 * associated with the view hierarchy. This can be used to intercept
5374 * key events in special situations before the IME consumes them; a
5375 * typical example would be handling the BACK key to update the application's
5376 * UI instead of allowing the IME to see it and close itself.
5377 *
5378 * @param event The key event to be dispatched.
5379 * @return True if the event was handled, false otherwise.
5380 */
5381 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5382 return onKeyPreIme(event.getKeyCode(), event);
5383 }
5384
5385 /**
5386 * Dispatch a key event to the next view on the focus path. This path runs
5387 * from the top of the view tree down to the currently focused view. If this
5388 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5389 * the next node down the focus path. This method also fires any key
5390 * listeners.
5391 *
5392 * @param event The key event to be dispatched.
5393 * @return True if the event was handled, false otherwise.
5394 */
5395 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005396 if (mInputEventConsistencyVerifier != null) {
5397 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005399
Jeff Brown21bc5c92011-02-28 18:27:14 -08005400 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005401 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005402 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5403 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
5404 return true;
5405 }
5406
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005407 if (event.dispatch(this, mAttachInfo != null
5408 ? mAttachInfo.mKeyDispatchState : null, this)) {
5409 return true;
5410 }
5411
5412 if (mInputEventConsistencyVerifier != null) {
5413 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5414 }
5415 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005416 }
5417
5418 /**
5419 * Dispatches a key shortcut event.
5420 *
5421 * @param event The key event to be dispatched.
5422 * @return True if the event was handled by the view, false otherwise.
5423 */
5424 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5425 return onKeyShortcut(event.getKeyCode(), event);
5426 }
5427
5428 /**
5429 * Pass the touch screen motion event down to the target view, or this
5430 * view if it is the target.
5431 *
5432 * @param event The motion event to be dispatched.
5433 * @return True if the event was handled by the view, false otherwise.
5434 */
5435 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005436 if (mInputEventConsistencyVerifier != null) {
5437 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5438 }
5439
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005440 if (onFilterTouchEventForSecurity(event)) {
5441 //noinspection SimplifiableIfStatement
5442 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
5443 mOnTouchListener.onTouch(this, event)) {
5444 return true;
5445 }
5446
5447 if (onTouchEvent(event)) {
5448 return true;
5449 }
Jeff Brown85a31762010-09-01 17:01:00 -07005450 }
5451
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005452 if (mInputEventConsistencyVerifier != null) {
5453 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005454 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005455 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005456 }
5457
5458 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005459 * Filter the touch event to apply security policies.
5460 *
5461 * @param event The motion event to be filtered.
5462 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005463 *
Jeff Brown85a31762010-09-01 17:01:00 -07005464 * @see #getFilterTouchesWhenObscured
5465 */
5466 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005467 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005468 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5469 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5470 // Window is obscured, drop this touch.
5471 return false;
5472 }
5473 return true;
5474 }
5475
5476 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005477 * Pass a trackball motion event down to the focused view.
5478 *
5479 * @param event The motion event to be dispatched.
5480 * @return True if the event was handled by the view, false otherwise.
5481 */
5482 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005483 if (mInputEventConsistencyVerifier != null) {
5484 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5485 }
5486
Romain Guy02ccac62011-06-24 13:20:23 -07005487 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005488 }
5489
5490 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005491 * Dispatch a generic motion event.
5492 * <p>
5493 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5494 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005495 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005496 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005497 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005498 *
5499 * @param event The motion event to be dispatched.
5500 * @return True if the event was handled by the view, false otherwise.
5501 */
5502 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005503 if (mInputEventConsistencyVerifier != null) {
5504 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5505 }
5506
Jeff Browna032cc02011-03-07 16:56:21 -08005507 final int source = event.getSource();
5508 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5509 final int action = event.getAction();
5510 if (action == MotionEvent.ACTION_HOVER_ENTER
5511 || action == MotionEvent.ACTION_HOVER_MOVE
5512 || action == MotionEvent.ACTION_HOVER_EXIT) {
5513 if (dispatchHoverEvent(event)) {
5514 return true;
5515 }
5516 } else if (dispatchGenericPointerEvent(event)) {
5517 return true;
5518 }
5519 } else if (dispatchGenericFocusedEvent(event)) {
5520 return true;
5521 }
5522
Jeff Brown10b62902011-06-20 16:40:37 -07005523 if (dispatchGenericMotionEventInternal(event)) {
5524 return true;
5525 }
5526
5527 if (mInputEventConsistencyVerifier != null) {
5528 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5529 }
5530 return false;
5531 }
5532
5533 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005534 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08005535 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5536 && mOnGenericMotionListener.onGenericMotion(this, event)) {
5537 return true;
5538 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005539
5540 if (onGenericMotionEvent(event)) {
5541 return true;
5542 }
5543
5544 if (mInputEventConsistencyVerifier != null) {
5545 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5546 }
5547 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005548 }
5549
5550 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005551 * Dispatch a hover event.
5552 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005553 * Do not call this method directly.
5554 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005555 * </p>
5556 *
5557 * @param event The motion event to be dispatched.
5558 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005559 */
5560 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005561 //noinspection SimplifiableIfStatement
Jeff Brown10b62902011-06-20 16:40:37 -07005562 if (mOnHoverListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5563 && mOnHoverListener.onHover(this, event)) {
5564 return true;
5565 }
5566
Jeff Browna032cc02011-03-07 16:56:21 -08005567 return onHoverEvent(event);
5568 }
5569
5570 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005571 * Returns true if the view has a child to which it has recently sent
5572 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5573 * it does not have a hovered child, then it must be the innermost hovered view.
5574 * @hide
5575 */
5576 protected boolean hasHoveredChild() {
5577 return false;
5578 }
5579
5580 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005581 * Dispatch a generic motion event to the view under the first pointer.
5582 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005583 * Do not call this method directly.
5584 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005585 * </p>
5586 *
5587 * @param event The motion event to be dispatched.
5588 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005589 */
5590 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5591 return false;
5592 }
5593
5594 /**
5595 * Dispatch a generic motion event to the currently focused view.
5596 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005597 * Do not call this method directly.
5598 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005599 * </p>
5600 *
5601 * @param event The motion event to be dispatched.
5602 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005603 */
5604 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5605 return false;
5606 }
5607
5608 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005609 * Dispatch a pointer event.
5610 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005611 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5612 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5613 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005614 * and should not be expected to handle other pointing device features.
5615 * </p>
5616 *
5617 * @param event The motion event to be dispatched.
5618 * @return True if the event was handled by the view, false otherwise.
5619 * @hide
5620 */
5621 public final boolean dispatchPointerEvent(MotionEvent event) {
5622 if (event.isTouchEvent()) {
5623 return dispatchTouchEvent(event);
5624 } else {
5625 return dispatchGenericMotionEvent(event);
5626 }
5627 }
5628
5629 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005630 * Called when the window containing this view gains or loses window focus.
5631 * ViewGroups should override to route to their children.
5632 *
5633 * @param hasFocus True if the window containing this view now has focus,
5634 * false otherwise.
5635 */
5636 public void dispatchWindowFocusChanged(boolean hasFocus) {
5637 onWindowFocusChanged(hasFocus);
5638 }
5639
5640 /**
5641 * Called when the window containing this view gains or loses focus. Note
5642 * that this is separate from view focus: to receive key events, both
5643 * your view and its window must have focus. If a window is displayed
5644 * on top of yours that takes input focus, then your own window will lose
5645 * focus but the view focus will remain unchanged.
5646 *
5647 * @param hasWindowFocus True if the window containing this view now has
5648 * focus, false otherwise.
5649 */
5650 public void onWindowFocusChanged(boolean hasWindowFocus) {
5651 InputMethodManager imm = InputMethodManager.peekInstance();
5652 if (!hasWindowFocus) {
5653 if (isPressed()) {
5654 setPressed(false);
5655 }
5656 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5657 imm.focusOut(this);
5658 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005659 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005660 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005661 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005662 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5663 imm.focusIn(this);
5664 }
5665 refreshDrawableState();
5666 }
5667
5668 /**
5669 * Returns true if this view is in a window that currently has window focus.
5670 * Note that this is not the same as the view itself having focus.
5671 *
5672 * @return True if this view is in a window that currently has window focus.
5673 */
5674 public boolean hasWindowFocus() {
5675 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5676 }
5677
5678 /**
Adam Powell326d8082009-12-09 15:10:07 -08005679 * Dispatch a view visibility change down the view hierarchy.
5680 * ViewGroups should override to route to their children.
5681 * @param changedView The view whose visibility changed. Could be 'this' or
5682 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005683 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5684 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005685 */
5686 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5687 onVisibilityChanged(changedView, visibility);
5688 }
5689
5690 /**
5691 * Called when the visibility of the view or an ancestor of the view is changed.
5692 * @param changedView The view whose visibility changed. Could be 'this' or
5693 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005694 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5695 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005696 */
5697 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005698 if (visibility == VISIBLE) {
5699 if (mAttachInfo != null) {
5700 initialAwakenScrollBars();
5701 } else {
5702 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5703 }
5704 }
Adam Powell326d8082009-12-09 15:10:07 -08005705 }
5706
5707 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005708 * Dispatch a hint about whether this view is displayed. For instance, when
5709 * a View moves out of the screen, it might receives a display hint indicating
5710 * the view is not displayed. Applications should not <em>rely</em> on this hint
5711 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005712 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005713 * @param hint A hint about whether or not this view is displayed:
5714 * {@link #VISIBLE} or {@link #INVISIBLE}.
5715 */
5716 public void dispatchDisplayHint(int hint) {
5717 onDisplayHint(hint);
5718 }
5719
5720 /**
5721 * Gives this view a hint about whether is displayed or not. For instance, when
5722 * a View moves out of the screen, it might receives a display hint indicating
5723 * the view is not displayed. Applications should not <em>rely</em> on this hint
5724 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005725 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005726 * @param hint A hint about whether or not this view is displayed:
5727 * {@link #VISIBLE} or {@link #INVISIBLE}.
5728 */
5729 protected void onDisplayHint(int hint) {
5730 }
5731
5732 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005733 * Dispatch a window visibility change down the view hierarchy.
5734 * ViewGroups should override to route to their children.
5735 *
5736 * @param visibility The new visibility of the window.
5737 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005738 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005739 */
5740 public void dispatchWindowVisibilityChanged(int visibility) {
5741 onWindowVisibilityChanged(visibility);
5742 }
5743
5744 /**
5745 * Called when the window containing has change its visibility
5746 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5747 * that this tells you whether or not your window is being made visible
5748 * to the window manager; this does <em>not</em> tell you whether or not
5749 * your window is obscured by other windows on the screen, even if it
5750 * is itself visible.
5751 *
5752 * @param visibility The new visibility of the window.
5753 */
5754 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005755 if (visibility == VISIBLE) {
5756 initialAwakenScrollBars();
5757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005758 }
5759
5760 /**
5761 * Returns the current visibility of the window this view is attached to
5762 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5763 *
5764 * @return Returns the current visibility of the view's window.
5765 */
5766 public int getWindowVisibility() {
5767 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5768 }
5769
5770 /**
5771 * Retrieve the overall visible display size in which the window this view is
5772 * attached to has been positioned in. This takes into account screen
5773 * decorations above the window, for both cases where the window itself
5774 * is being position inside of them or the window is being placed under
5775 * then and covered insets are used for the window to position its content
5776 * inside. In effect, this tells you the available area where content can
5777 * be placed and remain visible to users.
5778 *
5779 * <p>This function requires an IPC back to the window manager to retrieve
5780 * the requested information, so should not be used in performance critical
5781 * code like drawing.
5782 *
5783 * @param outRect Filled in with the visible display frame. If the view
5784 * is not attached to a window, this is simply the raw display size.
5785 */
5786 public void getWindowVisibleDisplayFrame(Rect outRect) {
5787 if (mAttachInfo != null) {
5788 try {
5789 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5790 } catch (RemoteException e) {
5791 return;
5792 }
5793 // XXX This is really broken, and probably all needs to be done
5794 // in the window manager, and we need to know more about whether
5795 // we want the area behind or in front of the IME.
5796 final Rect insets = mAttachInfo.mVisibleInsets;
5797 outRect.left += insets.left;
5798 outRect.top += insets.top;
5799 outRect.right -= insets.right;
5800 outRect.bottom -= insets.bottom;
5801 return;
5802 }
5803 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005804 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005805 }
5806
5807 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005808 * Dispatch a notification about a resource configuration change down
5809 * the view hierarchy.
5810 * ViewGroups should override to route to their children.
5811 *
5812 * @param newConfig The new resource configuration.
5813 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005814 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005815 */
5816 public void dispatchConfigurationChanged(Configuration newConfig) {
5817 onConfigurationChanged(newConfig);
5818 }
5819
5820 /**
5821 * Called when the current configuration of the resources being used
5822 * by the application have changed. You can use this to decide when
5823 * to reload resources that can changed based on orientation and other
5824 * configuration characterstics. You only need to use this if you are
5825 * not relying on the normal {@link android.app.Activity} mechanism of
5826 * recreating the activity instance upon a configuration change.
5827 *
5828 * @param newConfig The new resource configuration.
5829 */
5830 protected void onConfigurationChanged(Configuration newConfig) {
5831 }
5832
5833 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005834 * Private function to aggregate all per-view attributes in to the view
5835 * root.
5836 */
5837 void dispatchCollectViewAttributes(int visibility) {
5838 performCollectViewAttributes(visibility);
5839 }
5840
5841 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005842 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005843 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5844 mAttachInfo.mKeepScreenOn = true;
5845 }
5846 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5847 if (mOnSystemUiVisibilityChangeListener != null) {
5848 mAttachInfo.mHasSystemUiListeners = true;
5849 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005850 }
5851 }
5852
5853 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005854 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005855 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005856 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5857 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005858 ai.mRecomputeGlobalAttributes = true;
5859 }
5860 }
5861 }
5862
5863 /**
5864 * Returns whether the device is currently in touch mode. Touch mode is entered
5865 * once the user begins interacting with the device by touch, and affects various
5866 * things like whether focus is always visible to the user.
5867 *
5868 * @return Whether the device is in touch mode.
5869 */
5870 @ViewDebug.ExportedProperty
5871 public boolean isInTouchMode() {
5872 if (mAttachInfo != null) {
5873 return mAttachInfo.mInTouchMode;
5874 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005875 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005876 }
5877 }
5878
5879 /**
5880 * Returns the context the view is running in, through which it can
5881 * access the current theme, resources, etc.
5882 *
5883 * @return The view's Context.
5884 */
5885 @ViewDebug.CapturedViewProperty
5886 public final Context getContext() {
5887 return mContext;
5888 }
5889
5890 /**
5891 * Handle a key event before it is processed by any input method
5892 * associated with the view hierarchy. This can be used to intercept
5893 * key events in special situations before the IME consumes them; a
5894 * typical example would be handling the BACK key to update the application's
5895 * UI instead of allowing the IME to see it and close itself.
5896 *
5897 * @param keyCode The value in event.getKeyCode().
5898 * @param event Description of the key event.
5899 * @return If you handled the event, return true. If you want to allow the
5900 * event to be handled by the next receiver, return false.
5901 */
5902 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5903 return false;
5904 }
5905
5906 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005907 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5908 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005909 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5910 * is released, if the view is enabled and clickable.
5911 *
5912 * @param keyCode A key code that represents the button pressed, from
5913 * {@link android.view.KeyEvent}.
5914 * @param event The KeyEvent object that defines the button action.
5915 */
5916 public boolean onKeyDown(int keyCode, KeyEvent event) {
5917 boolean result = false;
5918
5919 switch (keyCode) {
5920 case KeyEvent.KEYCODE_DPAD_CENTER:
5921 case KeyEvent.KEYCODE_ENTER: {
5922 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5923 return true;
5924 }
5925 // Long clickable items don't necessarily have to be clickable
5926 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5927 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5928 (event.getRepeatCount() == 0)) {
5929 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005930 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005931 return true;
5932 }
5933 break;
5934 }
5935 }
5936 return result;
5937 }
5938
5939 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005940 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5941 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5942 * the event).
5943 */
5944 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5945 return false;
5946 }
5947
5948 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005949 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5950 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005951 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5952 * {@link KeyEvent#KEYCODE_ENTER} is released.
5953 *
5954 * @param keyCode A key code that represents the button pressed, from
5955 * {@link android.view.KeyEvent}.
5956 * @param event The KeyEvent object that defines the button action.
5957 */
5958 public boolean onKeyUp(int keyCode, KeyEvent event) {
5959 boolean result = false;
5960
5961 switch (keyCode) {
5962 case KeyEvent.KEYCODE_DPAD_CENTER:
5963 case KeyEvent.KEYCODE_ENTER: {
5964 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5965 return true;
5966 }
5967 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5968 setPressed(false);
5969
5970 if (!mHasPerformedLongPress) {
5971 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005972 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005973
5974 result = performClick();
5975 }
5976 }
5977 break;
5978 }
5979 }
5980 return result;
5981 }
5982
5983 /**
5984 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5985 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5986 * the event).
5987 *
5988 * @param keyCode A key code that represents the button pressed, from
5989 * {@link android.view.KeyEvent}.
5990 * @param repeatCount The number of times the action was made.
5991 * @param event The KeyEvent object that defines the button action.
5992 */
5993 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5994 return false;
5995 }
5996
5997 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005998 * Called on the focused view when a key shortcut event is not handled.
5999 * Override this method to implement local key shortcuts for the View.
6000 * Key shortcuts can also be implemented by setting the
6001 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006002 *
6003 * @param keyCode The value in event.getKeyCode().
6004 * @param event Description of the key event.
6005 * @return If you handled the event, return true. If you want to allow the
6006 * event to be handled by the next receiver, return false.
6007 */
6008 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6009 return false;
6010 }
6011
6012 /**
6013 * Check whether the called view is a text editor, in which case it
6014 * would make sense to automatically display a soft input window for
6015 * it. Subclasses should override this if they implement
6016 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006017 * a call on that method would return a non-null InputConnection, and
6018 * they are really a first-class editor that the user would normally
6019 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006020 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006021 * <p>The default implementation always returns false. This does
6022 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6023 * will not be called or the user can not otherwise perform edits on your
6024 * view; it is just a hint to the system that this is not the primary
6025 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006026 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006027 * @return Returns true if this view is a text editor, else false.
6028 */
6029 public boolean onCheckIsTextEditor() {
6030 return false;
6031 }
Romain Guy8506ab42009-06-11 17:35:47 -07006032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006033 /**
6034 * Create a new InputConnection for an InputMethod to interact
6035 * with the view. The default implementation returns null, since it doesn't
6036 * support input methods. You can override this to implement such support.
6037 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006038 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039 * <p>When implementing this, you probably also want to implement
6040 * {@link #onCheckIsTextEditor()} to indicate you will return a
6041 * non-null InputConnection.
6042 *
6043 * @param outAttrs Fill in with attribute information about the connection.
6044 */
6045 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6046 return null;
6047 }
6048
6049 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006050 * Called by the {@link android.view.inputmethod.InputMethodManager}
6051 * when a view who is not the current
6052 * input connection target is trying to make a call on the manager. The
6053 * default implementation returns false; you can override this to return
6054 * true for certain views if you are performing InputConnection proxying
6055 * to them.
6056 * @param view The View that is making the InputMethodManager call.
6057 * @return Return true to allow the call, false to reject.
6058 */
6059 public boolean checkInputConnectionProxy(View view) {
6060 return false;
6061 }
Romain Guy8506ab42009-06-11 17:35:47 -07006062
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006063 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006064 * Show the context menu for this view. It is not safe to hold on to the
6065 * menu after returning from this method.
6066 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006067 * You should normally not overload this method. Overload
6068 * {@link #onCreateContextMenu(ContextMenu)} or define an
6069 * {@link OnCreateContextMenuListener} to add items to the context menu.
6070 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006071 * @param menu The context menu to populate
6072 */
6073 public void createContextMenu(ContextMenu menu) {
6074 ContextMenuInfo menuInfo = getContextMenuInfo();
6075
6076 // Sets the current menu info so all items added to menu will have
6077 // my extra info set.
6078 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6079
6080 onCreateContextMenu(menu);
6081 if (mOnCreateContextMenuListener != null) {
6082 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
6083 }
6084
6085 // Clear the extra information so subsequent items that aren't mine don't
6086 // have my extra info.
6087 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6088
6089 if (mParent != null) {
6090 mParent.createContextMenu(menu);
6091 }
6092 }
6093
6094 /**
6095 * Views should implement this if they have extra information to associate
6096 * with the context menu. The return result is supplied as a parameter to
6097 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6098 * callback.
6099 *
6100 * @return Extra information about the item for which the context menu
6101 * should be shown. This information will vary across different
6102 * subclasses of View.
6103 */
6104 protected ContextMenuInfo getContextMenuInfo() {
6105 return null;
6106 }
6107
6108 /**
6109 * Views should implement this if the view itself is going to add items to
6110 * the context menu.
6111 *
6112 * @param menu the context menu to populate
6113 */
6114 protected void onCreateContextMenu(ContextMenu menu) {
6115 }
6116
6117 /**
6118 * Implement this method to handle trackball motion events. The
6119 * <em>relative</em> movement of the trackball since the last event
6120 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6121 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6122 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6123 * they will often be fractional values, representing the more fine-grained
6124 * movement information available from a trackball).
6125 *
6126 * @param event The motion event.
6127 * @return True if the event was handled, false otherwise.
6128 */
6129 public boolean onTrackballEvent(MotionEvent event) {
6130 return false;
6131 }
6132
6133 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006134 * Implement this method to handle generic motion events.
6135 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006136 * Generic motion events describe joystick movements, mouse hovers, track pad
6137 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006138 * {@link MotionEvent#getSource() source} of the motion event specifies
6139 * the class of input that was received. Implementations of this method
6140 * must examine the bits in the source before processing the event.
6141 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006142 * </p><p>
6143 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6144 * are delivered to the view under the pointer. All other generic motion events are
6145 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006146 * </p>
6147 * <code>
6148 * public boolean onGenericMotionEvent(MotionEvent event) {
6149 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006150 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6151 * // process the joystick movement...
6152 * return true;
6153 * }
6154 * }
6155 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6156 * switch (event.getAction()) {
6157 * case MotionEvent.ACTION_HOVER_MOVE:
6158 * // process the mouse hover movement...
6159 * return true;
6160 * case MotionEvent.ACTION_SCROLL:
6161 * // process the scroll wheel movement...
6162 * return true;
6163 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006164 * }
6165 * return super.onGenericMotionEvent(event);
6166 * }
6167 * </code>
6168 *
6169 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006170 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006171 */
6172 public boolean onGenericMotionEvent(MotionEvent event) {
6173 return false;
6174 }
6175
6176 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006177 * Implement this method to handle hover events.
6178 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006179 * This method is called whenever a pointer is hovering into, over, or out of the
6180 * bounds of a view and the view is not currently being touched.
6181 * Hover events are represented as pointer events with action
6182 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6183 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6184 * </p>
6185 * <ul>
6186 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6187 * when the pointer enters the bounds of the view.</li>
6188 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6189 * when the pointer has already entered the bounds of the view and has moved.</li>
6190 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6191 * when the pointer has exited the bounds of the view or when the pointer is
6192 * about to go down due to a button click, tap, or similar user action that
6193 * causes the view to be touched.</li>
6194 * </ul>
6195 * <p>
6196 * The view should implement this method to return true to indicate that it is
6197 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006198 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006199 * The default implementation calls {@link #setHovered} to update the hovered state
6200 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006201 * is enabled and is clickable. The default implementation also sends hover
6202 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006203 * </p>
6204 *
6205 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006206 * @return True if the view handled the hover event.
6207 *
6208 * @see #isHovered
6209 * @see #setHovered
6210 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006211 */
6212 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006213 // The root view may receive hover (or touch) events that are outside the bounds of
6214 // the window. This code ensures that we only send accessibility events for
6215 // hovers that are actually within the bounds of the root view.
6216 final int action = event.getAction();
6217 if (!mSendingHoverAccessibilityEvents) {
6218 if ((action == MotionEvent.ACTION_HOVER_ENTER
6219 || action == MotionEvent.ACTION_HOVER_MOVE)
6220 && !hasHoveredChild()
6221 && pointInView(event.getX(), event.getY())) {
6222 mSendingHoverAccessibilityEvents = true;
6223 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6224 }
6225 } else {
6226 if (action == MotionEvent.ACTION_HOVER_EXIT
6227 || (action == MotionEvent.ACTION_HOVER_MOVE
6228 && !pointInView(event.getX(), event.getY()))) {
6229 mSendingHoverAccessibilityEvents = false;
6230 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6231 }
Jeff Browna1b24182011-07-28 13:38:24 -07006232 }
6233
Jeff Brown87b7f802011-06-21 18:35:45 -07006234 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006235 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006236 case MotionEvent.ACTION_HOVER_ENTER:
6237 setHovered(true);
6238 break;
6239 case MotionEvent.ACTION_HOVER_EXIT:
6240 setHovered(false);
6241 break;
6242 }
Jeff Browna1b24182011-07-28 13:38:24 -07006243
6244 // Dispatch the event to onGenericMotionEvent before returning true.
6245 // This is to provide compatibility with existing applications that
6246 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6247 // break because of the new default handling for hoverable views
6248 // in onHoverEvent.
6249 // Note that onGenericMotionEvent will be called by default when
6250 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6251 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006252 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006253 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006254 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006255 }
6256
6257 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006258 * Returns true if the view should handle {@link #onHoverEvent}
6259 * by calling {@link #setHovered} to change its hovered state.
6260 *
6261 * @return True if the view is hoverable.
6262 */
6263 private boolean isHoverable() {
6264 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006265 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006266 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6267 return false;
6268 }
6269
6270 return (viewFlags & CLICKABLE) == CLICKABLE
6271 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6272 }
6273
6274 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006275 * Returns true if the view is currently hovered.
6276 *
6277 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006278 *
6279 * @see #setHovered
6280 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006281 */
Jeff Brown10b62902011-06-20 16:40:37 -07006282 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006283 public boolean isHovered() {
6284 return (mPrivateFlags & HOVERED) != 0;
6285 }
6286
6287 /**
6288 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006289 * <p>
6290 * Calling this method also changes the drawable state of the view. This
6291 * enables the view to react to hover by using different drawable resources
6292 * to change its appearance.
6293 * </p><p>
6294 * The {@link #onHoverChanged} method is called when the hovered state changes.
6295 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006296 *
6297 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006298 *
6299 * @see #isHovered
6300 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006301 */
6302 public void setHovered(boolean hovered) {
6303 if (hovered) {
6304 if ((mPrivateFlags & HOVERED) == 0) {
6305 mPrivateFlags |= HOVERED;
6306 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006307 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006308 }
6309 } else {
6310 if ((mPrivateFlags & HOVERED) != 0) {
6311 mPrivateFlags &= ~HOVERED;
6312 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006313 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006314 }
6315 }
6316 }
6317
6318 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006319 * Implement this method to handle hover state changes.
6320 * <p>
6321 * This method is called whenever the hover state changes as a result of a
6322 * call to {@link #setHovered}.
6323 * </p>
6324 *
6325 * @param hovered The current hover state, as returned by {@link #isHovered}.
6326 *
6327 * @see #isHovered
6328 * @see #setHovered
6329 */
6330 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006331 }
6332
6333 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006334 * Implement this method to handle touch screen motion events.
6335 *
6336 * @param event The motion event.
6337 * @return True if the event was handled, false otherwise.
6338 */
6339 public boolean onTouchEvent(MotionEvent event) {
6340 final int viewFlags = mViewFlags;
6341
6342 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006343 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
6344 mPrivateFlags &= ~PRESSED;
6345 refreshDrawableState();
6346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006347 // A disabled view that is clickable still consumes the touch
6348 // events, it just doesn't respond to them.
6349 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6350 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6351 }
6352
6353 if (mTouchDelegate != null) {
6354 if (mTouchDelegate.onTouchEvent(event)) {
6355 return true;
6356 }
6357 }
6358
6359 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6360 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6361 switch (event.getAction()) {
6362 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006363 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6364 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006365 // take focus if we don't have it already and we should in
6366 // touch mode.
6367 boolean focusTaken = false;
6368 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6369 focusTaken = requestFocus();
6370 }
6371
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006372 if (prepressed) {
6373 // The button is being released before we actually
6374 // showed it as pressed. Make it show the pressed
6375 // state now (before scheduling the click) to ensure
6376 // the user sees it.
6377 mPrivateFlags |= PRESSED;
6378 refreshDrawableState();
6379 }
Joe Malin32736f02011-01-19 16:14:20 -08006380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006381 if (!mHasPerformedLongPress) {
6382 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006383 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006384
6385 // Only perform take click actions if we were in the pressed state
6386 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006387 // Use a Runnable and post this rather than calling
6388 // performClick directly. This lets other visual state
6389 // of the view update before click actions start.
6390 if (mPerformClick == null) {
6391 mPerformClick = new PerformClick();
6392 }
6393 if (!post(mPerformClick)) {
6394 performClick();
6395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006396 }
6397 }
6398
6399 if (mUnsetPressedState == null) {
6400 mUnsetPressedState = new UnsetPressedState();
6401 }
6402
Adam Powelle14579b2009-12-16 18:39:52 -08006403 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006404 postDelayed(mUnsetPressedState,
6405 ViewConfiguration.getPressedStateDuration());
6406 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006407 // If the post failed, unpress right now
6408 mUnsetPressedState.run();
6409 }
Adam Powelle14579b2009-12-16 18:39:52 -08006410 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006411 }
6412 break;
6413
6414 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006415 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006416
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006417 if (performButtonActionOnTouchDown(event)) {
6418 break;
6419 }
6420
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006421 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006422 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006423
6424 // For views inside a scrolling container, delay the pressed feedback for
6425 // a short period in case this is a scroll.
6426 if (isInScrollingContainer) {
6427 mPrivateFlags |= PREPRESSED;
6428 if (mPendingCheckForTap == null) {
6429 mPendingCheckForTap = new CheckForTap();
6430 }
6431 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6432 } else {
6433 // Not inside a scrolling container, so show the feedback right away
6434 mPrivateFlags |= PRESSED;
6435 refreshDrawableState();
6436 checkForLongClick(0);
6437 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006438 break;
6439
6440 case MotionEvent.ACTION_CANCEL:
6441 mPrivateFlags &= ~PRESSED;
6442 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08006443 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006444 break;
6445
6446 case MotionEvent.ACTION_MOVE:
6447 final int x = (int) event.getX();
6448 final int y = (int) event.getY();
6449
6450 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006451 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006452 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006453 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006454 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006455 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006456 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006457
6458 // Need to switch from pressed to not pressed
6459 mPrivateFlags &= ~PRESSED;
6460 refreshDrawableState();
6461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006462 }
6463 break;
6464 }
6465 return true;
6466 }
6467
6468 return false;
6469 }
6470
6471 /**
Adam Powell10298662011-08-14 18:26:30 -07006472 * @hide
6473 */
6474 public boolean isInScrollingContainer() {
6475 ViewParent p = getParent();
6476 while (p != null && p instanceof ViewGroup) {
6477 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6478 return true;
6479 }
6480 p = p.getParent();
6481 }
6482 return false;
6483 }
6484
6485 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006486 * Remove the longpress detection timer.
6487 */
6488 private void removeLongPressCallback() {
6489 if (mPendingCheckForLongPress != null) {
6490 removeCallbacks(mPendingCheckForLongPress);
6491 }
6492 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006493
6494 /**
6495 * Remove the pending click action
6496 */
6497 private void removePerformClickCallback() {
6498 if (mPerformClick != null) {
6499 removeCallbacks(mPerformClick);
6500 }
6501 }
6502
Adam Powelle14579b2009-12-16 18:39:52 -08006503 /**
Romain Guya440b002010-02-24 15:57:54 -08006504 * Remove the prepress detection timer.
6505 */
6506 private void removeUnsetPressCallback() {
6507 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6508 setPressed(false);
6509 removeCallbacks(mUnsetPressedState);
6510 }
6511 }
6512
6513 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006514 * Remove the tap detection timer.
6515 */
6516 private void removeTapCallback() {
6517 if (mPendingCheckForTap != null) {
6518 mPrivateFlags &= ~PREPRESSED;
6519 removeCallbacks(mPendingCheckForTap);
6520 }
6521 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006522
6523 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006524 * Cancels a pending long press. Your subclass can use this if you
6525 * want the context menu to come up if the user presses and holds
6526 * at the same place, but you don't want it to come up if they press
6527 * and then move around enough to cause scrolling.
6528 */
6529 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006530 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006531
6532 /*
6533 * The prepressed state handled by the tap callback is a display
6534 * construct, but the tap callback will post a long press callback
6535 * less its own timeout. Remove it here.
6536 */
6537 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006538 }
6539
6540 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006541 * Remove the pending callback for sending a
6542 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6543 */
6544 private void removeSendViewScrolledAccessibilityEventCallback() {
6545 if (mSendViewScrolledAccessibilityEvent != null) {
6546 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6547 }
6548 }
6549
6550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006551 * Sets the TouchDelegate for this View.
6552 */
6553 public void setTouchDelegate(TouchDelegate delegate) {
6554 mTouchDelegate = delegate;
6555 }
6556
6557 /**
6558 * Gets the TouchDelegate for this View.
6559 */
6560 public TouchDelegate getTouchDelegate() {
6561 return mTouchDelegate;
6562 }
6563
6564 /**
6565 * Set flags controlling behavior of this view.
6566 *
6567 * @param flags Constant indicating the value which should be set
6568 * @param mask Constant indicating the bit range that should be changed
6569 */
6570 void setFlags(int flags, int mask) {
6571 int old = mViewFlags;
6572 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6573
6574 int changed = mViewFlags ^ old;
6575 if (changed == 0) {
6576 return;
6577 }
6578 int privateFlags = mPrivateFlags;
6579
6580 /* Check if the FOCUSABLE bit has changed */
6581 if (((changed & FOCUSABLE_MASK) != 0) &&
6582 ((privateFlags & HAS_BOUNDS) !=0)) {
6583 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6584 && ((privateFlags & FOCUSED) != 0)) {
6585 /* Give up focus if we are no longer focusable */
6586 clearFocus();
6587 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6588 && ((privateFlags & FOCUSED) == 0)) {
6589 /*
6590 * Tell the view system that we are now available to take focus
6591 * if no one else already has it.
6592 */
6593 if (mParent != null) mParent.focusableViewAvailable(this);
6594 }
6595 }
6596
6597 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6598 if ((changed & VISIBILITY_MASK) != 0) {
6599 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006600 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006601 * it was not visible. Marking it drawn ensures that the invalidation will
6602 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006603 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006604 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006605 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006606
6607 needGlobalAttributesUpdate(true);
6608
6609 // a view becoming visible is worth notifying the parent
6610 // about in case nothing has focus. even if this specific view
6611 // isn't focusable, it may contain something that is, so let
6612 // the root view try to give this focus if nothing else does.
6613 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6614 mParent.focusableViewAvailable(this);
6615 }
6616 }
6617 }
6618
6619 /* Check if the GONE bit has changed */
6620 if ((changed & GONE) != 0) {
6621 needGlobalAttributesUpdate(false);
6622 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006623
Romain Guyecd80ee2009-12-03 17:13:02 -08006624 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6625 if (hasFocus()) clearFocus();
6626 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006627 if (mParent instanceof View) {
6628 // GONE views noop invalidation, so invalidate the parent
6629 ((View) mParent).invalidate(true);
6630 }
6631 // Mark the view drawn to ensure that it gets invalidated properly the next
6632 // time it is visible and gets invalidated
6633 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006634 }
6635 if (mAttachInfo != null) {
6636 mAttachInfo.mViewVisibilityChanged = true;
6637 }
6638 }
6639
6640 /* Check if the VISIBLE bit has changed */
6641 if ((changed & INVISIBLE) != 0) {
6642 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006643 /*
6644 * If this view is becoming invisible, set the DRAWN flag so that
6645 * the next invalidate() will not be skipped.
6646 */
6647 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006648
6649 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6650 // root view becoming invisible shouldn't clear focus
6651 if (getRootView() != this) {
6652 clearFocus();
6653 }
6654 }
6655 if (mAttachInfo != null) {
6656 mAttachInfo.mViewVisibilityChanged = true;
6657 }
6658 }
6659
Adam Powell326d8082009-12-09 15:10:07 -08006660 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006661 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006662 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6663 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006664 } else if (mParent != null) {
6665 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006666 }
Adam Powell326d8082009-12-09 15:10:07 -08006667 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6668 }
6669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006670 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6671 destroyDrawingCache();
6672 }
6673
6674 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6675 destroyDrawingCache();
6676 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006677 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006678 }
6679
6680 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6681 destroyDrawingCache();
6682 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6683 }
6684
6685 if ((changed & DRAW_MASK) != 0) {
6686 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6687 if (mBGDrawable != null) {
6688 mPrivateFlags &= ~SKIP_DRAW;
6689 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6690 } else {
6691 mPrivateFlags |= SKIP_DRAW;
6692 }
6693 } else {
6694 mPrivateFlags &= ~SKIP_DRAW;
6695 }
6696 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006697 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006698 }
6699
6700 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006701 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006702 mParent.recomputeViewAttributes(this);
6703 }
6704 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006705
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006706 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006707 requestLayout();
6708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006709 }
6710
6711 /**
6712 * Change the view's z order in the tree, so it's on top of other sibling
6713 * views
6714 */
6715 public void bringToFront() {
6716 if (mParent != null) {
6717 mParent.bringChildToFront(this);
6718 }
6719 }
6720
6721 /**
6722 * This is called in response to an internal scroll in this view (i.e., the
6723 * view scrolled its own contents). This is typically as a result of
6724 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6725 * called.
6726 *
6727 * @param l Current horizontal scroll origin.
6728 * @param t Current vertical scroll origin.
6729 * @param oldl Previous horizontal scroll origin.
6730 * @param oldt Previous vertical scroll origin.
6731 */
6732 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006733 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6734 postSendViewScrolledAccessibilityEventCallback();
6735 }
6736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006737 mBackgroundSizeChanged = true;
6738
6739 final AttachInfo ai = mAttachInfo;
6740 if (ai != null) {
6741 ai.mViewScrollChanged = true;
6742 }
6743 }
6744
6745 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006746 * Interface definition for a callback to be invoked when the layout bounds of a view
6747 * changes due to layout processing.
6748 */
6749 public interface OnLayoutChangeListener {
6750 /**
6751 * Called when the focus state of a view has changed.
6752 *
6753 * @param v The view whose state has changed.
6754 * @param left The new value of the view's left property.
6755 * @param top The new value of the view's top property.
6756 * @param right The new value of the view's right property.
6757 * @param bottom The new value of the view's bottom property.
6758 * @param oldLeft The previous value of the view's left property.
6759 * @param oldTop The previous value of the view's top property.
6760 * @param oldRight The previous value of the view's right property.
6761 * @param oldBottom The previous value of the view's bottom property.
6762 */
6763 void onLayoutChange(View v, int left, int top, int right, int bottom,
6764 int oldLeft, int oldTop, int oldRight, int oldBottom);
6765 }
6766
6767 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006768 * This is called during layout when the size of this view has changed. If
6769 * you were just added to the view hierarchy, you're called with the old
6770 * values of 0.
6771 *
6772 * @param w Current width of this view.
6773 * @param h Current height of this view.
6774 * @param oldw Old width of this view.
6775 * @param oldh Old height of this view.
6776 */
6777 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6778 }
6779
6780 /**
6781 * Called by draw to draw the child views. This may be overridden
6782 * by derived classes to gain control just before its children are drawn
6783 * (but after its own view has been drawn).
6784 * @param canvas the canvas on which to draw the view
6785 */
6786 protected void dispatchDraw(Canvas canvas) {
6787 }
6788
6789 /**
6790 * Gets the parent of this view. Note that the parent is a
6791 * ViewParent and not necessarily a View.
6792 *
6793 * @return Parent of this view.
6794 */
6795 public final ViewParent getParent() {
6796 return mParent;
6797 }
6798
6799 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006800 * Set the horizontal scrolled position of your view. This will cause a call to
6801 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6802 * invalidated.
6803 * @param value the x position to scroll to
6804 */
6805 public void setScrollX(int value) {
6806 scrollTo(value, mScrollY);
6807 }
6808
6809 /**
6810 * Set the vertical scrolled position of your view. This will cause a call to
6811 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6812 * invalidated.
6813 * @param value the y position to scroll to
6814 */
6815 public void setScrollY(int value) {
6816 scrollTo(mScrollX, value);
6817 }
6818
6819 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006820 * Return the scrolled left position of this view. This is the left edge of
6821 * the displayed part of your view. You do not need to draw any pixels
6822 * farther left, since those are outside of the frame of your view on
6823 * screen.
6824 *
6825 * @return The left edge of the displayed part of your view, in pixels.
6826 */
6827 public final int getScrollX() {
6828 return mScrollX;
6829 }
6830
6831 /**
6832 * Return the scrolled top position of this view. This is the top edge of
6833 * the displayed part of your view. You do not need to draw any pixels above
6834 * it, since those are outside of the frame of your view on screen.
6835 *
6836 * @return The top edge of the displayed part of your view, in pixels.
6837 */
6838 public final int getScrollY() {
6839 return mScrollY;
6840 }
6841
6842 /**
6843 * Return the width of the your view.
6844 *
6845 * @return The width of your view, in pixels.
6846 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006847 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006848 public final int getWidth() {
6849 return mRight - mLeft;
6850 }
6851
6852 /**
6853 * Return the height of your view.
6854 *
6855 * @return The height of your view, in pixels.
6856 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006857 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006858 public final int getHeight() {
6859 return mBottom - mTop;
6860 }
6861
6862 /**
6863 * Return the visible drawing bounds of your view. Fills in the output
6864 * rectangle with the values from getScrollX(), getScrollY(),
6865 * getWidth(), and getHeight().
6866 *
6867 * @param outRect The (scrolled) drawing bounds of the view.
6868 */
6869 public void getDrawingRect(Rect outRect) {
6870 outRect.left = mScrollX;
6871 outRect.top = mScrollY;
6872 outRect.right = mScrollX + (mRight - mLeft);
6873 outRect.bottom = mScrollY + (mBottom - mTop);
6874 }
6875
6876 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006877 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6878 * raw width component (that is the result is masked by
6879 * {@link #MEASURED_SIZE_MASK}).
6880 *
6881 * @return The raw measured width of this view.
6882 */
6883 public final int getMeasuredWidth() {
6884 return mMeasuredWidth & MEASURED_SIZE_MASK;
6885 }
6886
6887 /**
6888 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006889 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006890 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006891 * This should be used during measurement and layout calculations only. Use
6892 * {@link #getWidth()} to see how wide a view is after layout.
6893 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006894 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006895 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006896 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006897 return mMeasuredWidth;
6898 }
6899
6900 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006901 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6902 * raw width component (that is the result is masked by
6903 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006904 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006905 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006906 */
6907 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006908 return mMeasuredHeight & MEASURED_SIZE_MASK;
6909 }
6910
6911 /**
6912 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006913 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006914 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6915 * This should be used during measurement and layout calculations only. Use
6916 * {@link #getHeight()} to see how wide a view is after layout.
6917 *
6918 * @return The measured width of this view as a bit mask.
6919 */
6920 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006921 return mMeasuredHeight;
6922 }
6923
6924 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006925 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6926 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6927 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6928 * and the height component is at the shifted bits
6929 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6930 */
6931 public final int getMeasuredState() {
6932 return (mMeasuredWidth&MEASURED_STATE_MASK)
6933 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6934 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6935 }
6936
6937 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006938 * The transform matrix of this view, which is calculated based on the current
6939 * roation, scale, and pivot properties.
6940 *
6941 * @see #getRotation()
6942 * @see #getScaleX()
6943 * @see #getScaleY()
6944 * @see #getPivotX()
6945 * @see #getPivotY()
6946 * @return The current transform matrix for the view
6947 */
6948 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07006949 if (mTransformationInfo != null) {
6950 updateMatrix();
6951 return mTransformationInfo.mMatrix;
6952 }
6953 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07006954 }
6955
6956 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006957 * Utility function to determine if the value is far enough away from zero to be
6958 * considered non-zero.
6959 * @param value A floating point value to check for zero-ness
6960 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6961 */
6962 private static boolean nonzero(float value) {
6963 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6964 }
6965
6966 /**
Jeff Brown86671742010-09-30 20:00:15 -07006967 * Returns true if the transform matrix is the identity matrix.
6968 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006969 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006970 * @return True if the transform matrix is the identity matrix, false otherwise.
6971 */
Jeff Brown86671742010-09-30 20:00:15 -07006972 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07006973 if (mTransformationInfo != null) {
6974 updateMatrix();
6975 return mTransformationInfo.mMatrixIsIdentity;
6976 }
6977 return true;
6978 }
6979
6980 void ensureTransformationInfo() {
6981 if (mTransformationInfo == null) {
6982 mTransformationInfo = new TransformationInfo();
6983 }
Jeff Brown86671742010-09-30 20:00:15 -07006984 }
6985
6986 /**
6987 * Recomputes the transform matrix if necessary.
6988 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006989 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07006990 final TransformationInfo info = mTransformationInfo;
6991 if (info == null) {
6992 return;
6993 }
6994 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006995 // transform-related properties have changed since the last time someone
6996 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07006997
6998 // Figure out if we need to update the pivot point
6999 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007000 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7001 info.mPrevWidth = mRight - mLeft;
7002 info.mPrevHeight = mBottom - mTop;
7003 info.mPivotX = info.mPrevWidth / 2f;
7004 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007005 }
7006 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007007 info.mMatrix.reset();
7008 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7009 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7010 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7011 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007012 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007013 if (info.mCamera == null) {
7014 info.mCamera = new Camera();
7015 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007016 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007017 info.mCamera.save();
7018 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7019 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7020 info.mCamera.getMatrix(info.matrix3D);
7021 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7022 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7023 info.mPivotY + info.mTranslationY);
7024 info.mMatrix.postConcat(info.matrix3D);
7025 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007026 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007027 info.mMatrixDirty = false;
7028 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7029 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007030 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007031 }
7032
7033 /**
7034 * Utility method to retrieve the inverse of the current mMatrix property.
7035 * We cache the matrix to avoid recalculating it when transform properties
7036 * have not changed.
7037 *
7038 * @return The inverse of the current matrix of this view.
7039 */
Jeff Brown86671742010-09-30 20:00:15 -07007040 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007041 final TransformationInfo info = mTransformationInfo;
7042 if (info != null) {
7043 updateMatrix();
7044 if (info.mInverseMatrixDirty) {
7045 if (info.mInverseMatrix == null) {
7046 info.mInverseMatrix = new Matrix();
7047 }
7048 info.mMatrix.invert(info.mInverseMatrix);
7049 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007050 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007051 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007052 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007053 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007054 }
7055
7056 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007057 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7058 * views are drawn) from the camera to this view. The camera's distance
7059 * affects 3D transformations, for instance rotations around the X and Y
7060 * axis. If the rotationX or rotationY properties are changed and this view is
7061 * large (more than half the size of the screen), it is recommended to always
7062 * use a camera distance that's greater than the height (X axis rotation) or
7063 * the width (Y axis rotation) of this view.</p>
7064 *
7065 * <p>The distance of the camera from the view plane can have an affect on the
7066 * perspective distortion of the view when it is rotated around the x or y axis.
7067 * For example, a large distance will result in a large viewing angle, and there
7068 * will not be much perspective distortion of the view as it rotates. A short
7069 * distance may cause much more perspective distortion upon rotation, and can
7070 * also result in some drawing artifacts if the rotated view ends up partially
7071 * behind the camera (which is why the recommendation is to use a distance at
7072 * least as far as the size of the view, if the view is to be rotated.)</p>
7073 *
7074 * <p>The distance is expressed in "depth pixels." The default distance depends
7075 * on the screen density. For instance, on a medium density display, the
7076 * default distance is 1280. On a high density display, the default distance
7077 * is 1920.</p>
7078 *
7079 * <p>If you want to specify a distance that leads to visually consistent
7080 * results across various densities, use the following formula:</p>
7081 * <pre>
7082 * float scale = context.getResources().getDisplayMetrics().density;
7083 * view.setCameraDistance(distance * scale);
7084 * </pre>
7085 *
7086 * <p>The density scale factor of a high density display is 1.5,
7087 * and 1920 = 1280 * 1.5.</p>
7088 *
7089 * @param distance The distance in "depth pixels", if negative the opposite
7090 * value is used
7091 *
7092 * @see #setRotationX(float)
7093 * @see #setRotationY(float)
7094 */
7095 public void setCameraDistance(float distance) {
7096 invalidateParentCaches();
7097 invalidate(false);
7098
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007099 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007100 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007101 final TransformationInfo info = mTransformationInfo;
7102 if (info.mCamera == null) {
7103 info.mCamera = new Camera();
7104 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007105 }
7106
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007107 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7108 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007109
7110 invalidate(false);
7111 }
7112
7113 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007114 * The degrees that the view is rotated around the pivot point.
7115 *
Romain Guya5364ee2011-02-24 14:46:04 -08007116 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007117 * @see #getPivotX()
7118 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007119 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007120 * @return The degrees of rotation.
7121 */
7122 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007123 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007124 }
7125
7126 /**
Chet Haase897247b2010-09-09 14:54:47 -07007127 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7128 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007129 *
7130 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007131 *
7132 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007133 * @see #getPivotX()
7134 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007135 * @see #setRotationX(float)
7136 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007137 *
7138 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007139 */
7140 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007141 ensureTransformationInfo();
7142 final TransformationInfo info = mTransformationInfo;
7143 if (info.mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007144 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007145 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007146 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007147 info.mRotation = rotation;
7148 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007149 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007150 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007151 }
7152 }
7153
7154 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007155 * The degrees that the view is rotated around the vertical axis through the pivot point.
7156 *
7157 * @see #getPivotX()
7158 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007159 * @see #setRotationY(float)
7160 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007161 * @return The degrees of Y rotation.
7162 */
7163 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007164 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007165 }
7166
7167 /**
Chet Haase897247b2010-09-09 14:54:47 -07007168 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7169 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7170 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007171 *
7172 * When rotating large views, it is recommended to adjust the camera distance
7173 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007174 *
7175 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007176 *
7177 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007178 * @see #getPivotX()
7179 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007180 * @see #setRotation(float)
7181 * @see #setRotationX(float)
7182 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007183 *
7184 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007185 */
7186 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007187 ensureTransformationInfo();
7188 final TransformationInfo info = mTransformationInfo;
7189 if (info.mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007190 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007191 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007192 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007193 info.mRotationY = rotationY;
7194 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007195 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007196 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007197 }
7198 }
7199
7200 /**
7201 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7202 *
7203 * @see #getPivotX()
7204 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007205 * @see #setRotationX(float)
7206 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007207 * @return The degrees of X rotation.
7208 */
7209 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007210 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007211 }
7212
7213 /**
Chet Haase897247b2010-09-09 14:54:47 -07007214 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7215 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7216 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007217 *
7218 * When rotating large views, it is recommended to adjust the camera distance
7219 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007220 *
7221 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007222 *
7223 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007224 * @see #getPivotX()
7225 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007226 * @see #setRotation(float)
7227 * @see #setRotationY(float)
7228 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007229 *
7230 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007231 */
7232 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007233 ensureTransformationInfo();
7234 final TransformationInfo info = mTransformationInfo;
7235 if (info.mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007236 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007237 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007238 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007239 info.mRotationX = rotationX;
7240 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007241 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007242 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007243 }
7244 }
7245
7246 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007247 * The amount that the view is scaled in x around the pivot point, as a proportion of
7248 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7249 *
Joe Onorato93162322010-09-16 15:42:01 -04007250 * <p>By default, this is 1.0f.
7251 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007252 * @see #getPivotX()
7253 * @see #getPivotY()
7254 * @return The scaling factor.
7255 */
7256 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007257 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007258 }
7259
7260 /**
7261 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7262 * the view's unscaled width. A value of 1 means that no scaling is applied.
7263 *
7264 * @param scaleX The scaling factor.
7265 * @see #getPivotX()
7266 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007267 *
7268 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007269 */
7270 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007271 ensureTransformationInfo();
7272 final TransformationInfo info = mTransformationInfo;
7273 if (info.mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007274 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007275 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007276 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007277 info.mScaleX = scaleX;
7278 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007279 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007280 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007281 }
7282 }
7283
7284 /**
7285 * The amount that the view is scaled in y around the pivot point, as a proportion of
7286 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7287 *
Joe Onorato93162322010-09-16 15:42:01 -04007288 * <p>By default, this is 1.0f.
7289 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007290 * @see #getPivotX()
7291 * @see #getPivotY()
7292 * @return The scaling factor.
7293 */
7294 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007295 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007296 }
7297
7298 /**
7299 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7300 * the view's unscaled width. A value of 1 means that no scaling is applied.
7301 *
7302 * @param scaleY The scaling factor.
7303 * @see #getPivotX()
7304 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007305 *
7306 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007307 */
7308 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007309 ensureTransformationInfo();
7310 final TransformationInfo info = mTransformationInfo;
7311 if (info.mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007312 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007313 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007314 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007315 info.mScaleY = scaleY;
7316 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007317 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007318 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007319 }
7320 }
7321
7322 /**
7323 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7324 * and {@link #setScaleX(float) scaled}.
7325 *
7326 * @see #getRotation()
7327 * @see #getScaleX()
7328 * @see #getScaleY()
7329 * @see #getPivotY()
7330 * @return The x location of the pivot point.
7331 */
7332 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007333 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007334 }
7335
7336 /**
7337 * Sets the x location of the point around which the view is
7338 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007339 * By default, the pivot point is centered on the object.
7340 * Setting this property disables this behavior and causes the view to use only the
7341 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007342 *
7343 * @param pivotX The x location of the pivot point.
7344 * @see #getRotation()
7345 * @see #getScaleX()
7346 * @see #getScaleY()
7347 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007348 *
7349 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007350 */
7351 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007352 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007353 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007354 final TransformationInfo info = mTransformationInfo;
7355 if (info.mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007356 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007357 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007358 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007359 info.mPivotX = pivotX;
7360 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007361 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007362 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007363 }
7364 }
7365
7366 /**
7367 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7368 * and {@link #setScaleY(float) scaled}.
7369 *
7370 * @see #getRotation()
7371 * @see #getScaleX()
7372 * @see #getScaleY()
7373 * @see #getPivotY()
7374 * @return The y location of the pivot point.
7375 */
7376 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007377 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007378 }
7379
7380 /**
7381 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007382 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7383 * Setting this property disables this behavior and causes the view to use only the
7384 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007385 *
7386 * @param pivotY The y location of the pivot point.
7387 * @see #getRotation()
7388 * @see #getScaleX()
7389 * @see #getScaleY()
7390 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007391 *
7392 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007393 */
7394 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007395 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007396 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007397 final TransformationInfo info = mTransformationInfo;
7398 if (info.mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007399 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007400 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007401 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007402 info.mPivotY = pivotY;
7403 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007404 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007405 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007406 }
7407 }
7408
7409 /**
7410 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7411 * completely transparent and 1 means the view is completely opaque.
7412 *
Joe Onorato93162322010-09-16 15:42:01 -04007413 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007414 * @return The opacity of the view.
7415 */
7416 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007417 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007418 }
7419
7420 /**
Romain Guy171c5922011-01-06 10:04:23 -08007421 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7422 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007423 *
Romain Guy171c5922011-01-06 10:04:23 -08007424 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7425 * responsible for applying the opacity itself. Otherwise, calling this method is
7426 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007427 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007428 *
7429 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007430 *
Joe Malin32736f02011-01-19 16:14:20 -08007431 * @see #setLayerType(int, android.graphics.Paint)
7432 *
Chet Haase73066682010-11-29 15:55:32 -08007433 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007434 */
7435 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007436 ensureTransformationInfo();
7437 mTransformationInfo.mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007438 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07007439 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07007440 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007441 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007442 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07007443 } else {
Romain Guya3496a92010-10-12 11:53:24 -07007444 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007445 invalidate(false);
7446 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007447 }
7448
7449 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007450 * Faster version of setAlpha() which performs the same steps except there are
7451 * no calls to invalidate(). The caller of this function should perform proper invalidation
7452 * on the parent and this object. The return value indicates whether the subclass handles
7453 * alpha (the return value for onSetAlpha()).
7454 *
7455 * @param alpha The new value for the alpha property
7456 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
7457 */
7458 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007459 ensureTransformationInfo();
7460 mTransformationInfo.mAlpha = alpha;
Chet Haasea00f3862011-02-22 06:34:40 -08007461 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7462 if (subclassHandlesAlpha) {
7463 mPrivateFlags |= ALPHA_SET;
7464 } else {
7465 mPrivateFlags &= ~ALPHA_SET;
7466 }
7467 return subclassHandlesAlpha;
7468 }
7469
7470 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007471 * Top position of this view relative to its parent.
7472 *
7473 * @return The top of this view, in pixels.
7474 */
7475 @ViewDebug.CapturedViewProperty
7476 public final int getTop() {
7477 return mTop;
7478 }
7479
7480 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007481 * Sets the top position of this view relative to its parent. This method is meant to be called
7482 * by the layout system and should not generally be called otherwise, because the property
7483 * may be changed at any time by the layout.
7484 *
7485 * @param top The top of this view, in pixels.
7486 */
7487 public final void setTop(int top) {
7488 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007489 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007490 final boolean matrixIsIdentity = mTransformationInfo == null
7491 || mTransformationInfo.mMatrixIsIdentity;
7492 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007493 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007494 int minTop;
7495 int yLoc;
7496 if (top < mTop) {
7497 minTop = top;
7498 yLoc = top - mTop;
7499 } else {
7500 minTop = mTop;
7501 yLoc = 0;
7502 }
Chet Haasee9140a72011-02-16 16:23:29 -08007503 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007504 }
7505 } else {
7506 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007507 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007508 }
7509
Chet Haaseed032702010-10-01 14:05:54 -07007510 int width = mRight - mLeft;
7511 int oldHeight = mBottom - mTop;
7512
Chet Haase21cd1382010-09-01 17:42:29 -07007513 mTop = top;
7514
Chet Haaseed032702010-10-01 14:05:54 -07007515 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7516
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007517 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007518 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7519 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007520 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007521 }
Chet Haase21cd1382010-09-01 17:42:29 -07007522 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007523 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007524 }
Chet Haase55dbb652010-12-21 20:15:08 -08007525 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007526 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007527 }
7528 }
7529
7530 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007531 * Bottom position of this view relative to its parent.
7532 *
7533 * @return The bottom of this view, in pixels.
7534 */
7535 @ViewDebug.CapturedViewProperty
7536 public final int getBottom() {
7537 return mBottom;
7538 }
7539
7540 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007541 * True if this view has changed since the last time being drawn.
7542 *
7543 * @return The dirty state of this view.
7544 */
7545 public boolean isDirty() {
7546 return (mPrivateFlags & DIRTY_MASK) != 0;
7547 }
7548
7549 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007550 * Sets the bottom position of this view relative to its parent. This method is meant to be
7551 * called by the layout system and should not generally be called otherwise, because the
7552 * property may be changed at any time by the layout.
7553 *
7554 * @param bottom The bottom of this view, in pixels.
7555 */
7556 public final void setBottom(int bottom) {
7557 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007558 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007559 final boolean matrixIsIdentity = mTransformationInfo == null
7560 || mTransformationInfo.mMatrixIsIdentity;
7561 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007562 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007563 int maxBottom;
7564 if (bottom < mBottom) {
7565 maxBottom = mBottom;
7566 } else {
7567 maxBottom = bottom;
7568 }
Chet Haasee9140a72011-02-16 16:23:29 -08007569 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007570 }
7571 } else {
7572 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007573 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007574 }
7575
Chet Haaseed032702010-10-01 14:05:54 -07007576 int width = mRight - mLeft;
7577 int oldHeight = mBottom - mTop;
7578
Chet Haase21cd1382010-09-01 17:42:29 -07007579 mBottom = bottom;
7580
Chet Haaseed032702010-10-01 14:05:54 -07007581 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7582
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007583 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007584 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7585 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007586 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007587 }
Chet Haase21cd1382010-09-01 17:42:29 -07007588 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007589 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007590 }
Chet Haase55dbb652010-12-21 20:15:08 -08007591 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007592 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007593 }
7594 }
7595
7596 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007597 * Left position of this view relative to its parent.
7598 *
7599 * @return The left edge of this view, in pixels.
7600 */
7601 @ViewDebug.CapturedViewProperty
7602 public final int getLeft() {
7603 return mLeft;
7604 }
7605
7606 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007607 * Sets the left position of this view relative to its parent. This method is meant to be called
7608 * by the layout system and should not generally be called otherwise, because the property
7609 * may be changed at any time by the layout.
7610 *
7611 * @param left The bottom of this view, in pixels.
7612 */
7613 public final void setLeft(int left) {
7614 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007615 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007616 final boolean matrixIsIdentity = mTransformationInfo == null
7617 || mTransformationInfo.mMatrixIsIdentity;
7618 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007619 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007620 int minLeft;
7621 int xLoc;
7622 if (left < mLeft) {
7623 minLeft = left;
7624 xLoc = left - mLeft;
7625 } else {
7626 minLeft = mLeft;
7627 xLoc = 0;
7628 }
Chet Haasee9140a72011-02-16 16:23:29 -08007629 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007630 }
7631 } else {
7632 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007633 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007634 }
7635
Chet Haaseed032702010-10-01 14:05:54 -07007636 int oldWidth = mRight - mLeft;
7637 int height = mBottom - mTop;
7638
Chet Haase21cd1382010-09-01 17:42:29 -07007639 mLeft = left;
7640
Chet Haaseed032702010-10-01 14:05:54 -07007641 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7642
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007643 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007644 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7645 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007646 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007647 }
Chet Haase21cd1382010-09-01 17:42:29 -07007648 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007649 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007650 }
Chet Haase55dbb652010-12-21 20:15:08 -08007651 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007652 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007653 }
7654 }
7655
7656 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007657 * Right position of this view relative to its parent.
7658 *
7659 * @return The right edge of this view, in pixels.
7660 */
7661 @ViewDebug.CapturedViewProperty
7662 public final int getRight() {
7663 return mRight;
7664 }
7665
7666 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007667 * Sets the right position of this view relative to its parent. This method is meant to be called
7668 * by the layout system and should not generally be called otherwise, because the property
7669 * may be changed at any time by the layout.
7670 *
7671 * @param right The bottom of this view, in pixels.
7672 */
7673 public final void setRight(int right) {
7674 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007675 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007676 final boolean matrixIsIdentity = mTransformationInfo == null
7677 || mTransformationInfo.mMatrixIsIdentity;
7678 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007679 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007680 int maxRight;
7681 if (right < mRight) {
7682 maxRight = mRight;
7683 } else {
7684 maxRight = right;
7685 }
Chet Haasee9140a72011-02-16 16:23:29 -08007686 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007687 }
7688 } else {
7689 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007690 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007691 }
7692
Chet Haaseed032702010-10-01 14:05:54 -07007693 int oldWidth = mRight - mLeft;
7694 int height = mBottom - mTop;
7695
Chet Haase21cd1382010-09-01 17:42:29 -07007696 mRight = right;
7697
Chet Haaseed032702010-10-01 14:05:54 -07007698 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7699
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007700 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007701 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7702 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007703 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007704 }
Chet Haase21cd1382010-09-01 17:42:29 -07007705 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007706 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007707 }
Chet Haase55dbb652010-12-21 20:15:08 -08007708 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007709 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007710 }
7711 }
7712
7713 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007714 * The visual x position of this view, in pixels. This is equivalent to the
7715 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007716 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007717 *
Chet Haasedf030d22010-07-30 17:22:38 -07007718 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007719 */
Chet Haasedf030d22010-07-30 17:22:38 -07007720 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007721 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007722 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007723
Chet Haasedf030d22010-07-30 17:22:38 -07007724 /**
7725 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7726 * {@link #setTranslationX(float) translationX} property to be the difference between
7727 * the x value passed in and the current {@link #getLeft() left} property.
7728 *
7729 * @param x The visual x position of this view, in pixels.
7730 */
7731 public void setX(float x) {
7732 setTranslationX(x - mLeft);
7733 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007734
Chet Haasedf030d22010-07-30 17:22:38 -07007735 /**
7736 * The visual y position of this view, in pixels. This is equivalent to the
7737 * {@link #setTranslationY(float) translationY} property plus the current
7738 * {@link #getTop() top} property.
7739 *
7740 * @return The visual y position of this view, in pixels.
7741 */
7742 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007743 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007744 }
7745
7746 /**
7747 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7748 * {@link #setTranslationY(float) translationY} property to be the difference between
7749 * the y value passed in and the current {@link #getTop() top} property.
7750 *
7751 * @param y The visual y position of this view, in pixels.
7752 */
7753 public void setY(float y) {
7754 setTranslationY(y - mTop);
7755 }
7756
7757
7758 /**
7759 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7760 * This position is post-layout, in addition to wherever the object's
7761 * layout placed it.
7762 *
7763 * @return The horizontal position of this view relative to its left position, in pixels.
7764 */
7765 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007766 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07007767 }
7768
7769 /**
7770 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7771 * This effectively positions the object post-layout, in addition to wherever the object's
7772 * layout placed it.
7773 *
7774 * @param translationX The horizontal position of this view relative to its left position,
7775 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007776 *
7777 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007778 */
7779 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007780 ensureTransformationInfo();
7781 final TransformationInfo info = mTransformationInfo;
7782 if (info.mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007783 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007784 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007785 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007786 info.mTranslationX = translationX;
7787 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07007788 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007789 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007790 }
7791 }
7792
7793 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007794 * The horizontal location of this view relative to its {@link #getTop() top} position.
7795 * This position is post-layout, in addition to wherever the object's
7796 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007797 *
Chet Haasedf030d22010-07-30 17:22:38 -07007798 * @return The vertical position of this view relative to its top position,
7799 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007800 */
Chet Haasedf030d22010-07-30 17:22:38 -07007801 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007802 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007803 }
7804
7805 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007806 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7807 * This effectively positions the object post-layout, in addition to wherever the object's
7808 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007809 *
Chet Haasedf030d22010-07-30 17:22:38 -07007810 * @param translationY The vertical position of this view relative to its top position,
7811 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007812 *
7813 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007814 */
Chet Haasedf030d22010-07-30 17:22:38 -07007815 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007816 ensureTransformationInfo();
7817 final TransformationInfo info = mTransformationInfo;
7818 if (info.mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007819 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007820 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007821 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007822 info.mTranslationY = translationY;
7823 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07007824 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007825 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007826 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007827 }
7828
7829 /**
Romain Guyda489792011-02-03 01:05:15 -08007830 * @hide
7831 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007832 public void setFastTranslationX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007833 ensureTransformationInfo();
7834 final TransformationInfo info = mTransformationInfo;
7835 info.mTranslationX = x;
7836 info.mMatrixDirty = true;
Michael Jurkadece29f2011-02-03 01:41:49 -08007837 }
7838
7839 /**
7840 * @hide
7841 */
7842 public void setFastTranslationY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007843 ensureTransformationInfo();
7844 final TransformationInfo info = mTransformationInfo;
7845 info.mTranslationY = y;
7846 info.mMatrixDirty = true;
Michael Jurkadece29f2011-02-03 01:41:49 -08007847 }
7848
7849 /**
7850 * @hide
7851 */
Romain Guyda489792011-02-03 01:05:15 -08007852 public void setFastX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007853 ensureTransformationInfo();
7854 final TransformationInfo info = mTransformationInfo;
7855 info.mTranslationX = x - mLeft;
7856 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007857 }
7858
7859 /**
7860 * @hide
7861 */
7862 public void setFastY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007863 ensureTransformationInfo();
7864 final TransformationInfo info = mTransformationInfo;
7865 info.mTranslationY = y - mTop;
7866 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007867 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007868
Romain Guyda489792011-02-03 01:05:15 -08007869 /**
7870 * @hide
7871 */
7872 public void setFastScaleX(float x) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007873 ensureTransformationInfo();
7874 final TransformationInfo info = mTransformationInfo;
7875 info.mScaleX = x;
7876 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007877 }
7878
7879 /**
7880 * @hide
7881 */
7882 public void setFastScaleY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007883 ensureTransformationInfo();
7884 final TransformationInfo info = mTransformationInfo;
7885 info.mScaleY = y;
7886 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007887 }
7888
7889 /**
7890 * @hide
7891 */
7892 public void setFastAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007893 ensureTransformationInfo();
7894 mTransformationInfo.mAlpha = alpha;
Romain Guyda489792011-02-03 01:05:15 -08007895 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007896
Romain Guyda489792011-02-03 01:05:15 -08007897 /**
7898 * @hide
7899 */
7900 public void setFastRotationY(float y) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007901 ensureTransformationInfo();
7902 final TransformationInfo info = mTransformationInfo;
7903 info.mRotationY = y;
7904 info.mMatrixDirty = true;
Romain Guyda489792011-02-03 01:05:15 -08007905 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007906
Romain Guyda489792011-02-03 01:05:15 -08007907 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007908 * Hit rectangle in parent's coordinates
7909 *
7910 * @param outRect The hit rectangle of the view.
7911 */
7912 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07007913 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007914 final TransformationInfo info = mTransformationInfo;
7915 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007916 outRect.set(mLeft, mTop, mRight, mBottom);
7917 } else {
7918 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007919 tmpRect.set(-info.mPivotX, -info.mPivotY,
7920 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
7921 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07007922 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
7923 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07007924 }
7925 }
7926
7927 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007928 * Determines whether the given point, in local coordinates is inside the view.
7929 */
7930 /*package*/ final boolean pointInView(float localX, float localY) {
7931 return localX >= 0 && localX < (mRight - mLeft)
7932 && localY >= 0 && localY < (mBottom - mTop);
7933 }
7934
7935 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007936 * Utility method to determine whether the given point, in local coordinates,
7937 * is inside the view, where the area of the view is expanded by the slop factor.
7938 * This method is called while processing touch-move events to determine if the event
7939 * is still within the view.
7940 */
7941 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007942 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007943 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007944 }
7945
7946 /**
7947 * When a view has focus and the user navigates away from it, the next view is searched for
7948 * starting from the rectangle filled in by this method.
7949 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07007950 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
7951 * of the view. However, if your view maintains some idea of internal selection,
7952 * such as a cursor, or a selected row or column, you should override this method and
7953 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007954 *
7955 * @param r The rectangle to fill in, in this view's coordinates.
7956 */
7957 public void getFocusedRect(Rect r) {
7958 getDrawingRect(r);
7959 }
7960
7961 /**
7962 * If some part of this view is not clipped by any of its parents, then
7963 * return that area in r in global (root) coordinates. To convert r to local
7964 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
7965 * -globalOffset.y)) If the view is completely clipped or translated out,
7966 * return false.
7967 *
7968 * @param r If true is returned, r holds the global coordinates of the
7969 * visible portion of this view.
7970 * @param globalOffset If true is returned, globalOffset holds the dx,dy
7971 * between this view and its root. globalOffet may be null.
7972 * @return true if r is non-empty (i.e. part of the view is visible at the
7973 * root level.
7974 */
7975 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7976 int width = mRight - mLeft;
7977 int height = mBottom - mTop;
7978 if (width > 0 && height > 0) {
7979 r.set(0, 0, width, height);
7980 if (globalOffset != null) {
7981 globalOffset.set(-mScrollX, -mScrollY);
7982 }
7983 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7984 }
7985 return false;
7986 }
7987
7988 public final boolean getGlobalVisibleRect(Rect r) {
7989 return getGlobalVisibleRect(r, null);
7990 }
7991
7992 public final boolean getLocalVisibleRect(Rect r) {
7993 Point offset = new Point();
7994 if (getGlobalVisibleRect(r, offset)) {
7995 r.offset(-offset.x, -offset.y); // make r local
7996 return true;
7997 }
7998 return false;
7999 }
8000
8001 /**
8002 * Offset this view's vertical location by the specified number of pixels.
8003 *
8004 * @param offset the number of pixels to offset the view by
8005 */
8006 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008007 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008008 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008009 final boolean matrixIsIdentity = mTransformationInfo == null
8010 || mTransformationInfo.mMatrixIsIdentity;
8011 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008012 final ViewParent p = mParent;
8013 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008014 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008015 int minTop;
8016 int maxBottom;
8017 int yLoc;
8018 if (offset < 0) {
8019 minTop = mTop + offset;
8020 maxBottom = mBottom;
8021 yLoc = offset;
8022 } else {
8023 minTop = mTop;
8024 maxBottom = mBottom + offset;
8025 yLoc = 0;
8026 }
8027 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8028 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008029 }
8030 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008031 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008032 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008033
Chet Haasec3aa3612010-06-17 08:50:37 -07008034 mTop += offset;
8035 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008036
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008037 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008038 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008039 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008040 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008041 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008042 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008043 }
8044
8045 /**
8046 * Offset this view's horizontal location by the specified amount of pixels.
8047 *
8048 * @param offset the numer of pixels to offset the view by
8049 */
8050 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008051 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008052 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008053 final boolean matrixIsIdentity = mTransformationInfo == null
8054 || mTransformationInfo.mMatrixIsIdentity;
8055 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008056 final ViewParent p = mParent;
8057 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008058 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008059 int minLeft;
8060 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008061 if (offset < 0) {
8062 minLeft = mLeft + offset;
8063 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008064 } else {
8065 minLeft = mLeft;
8066 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008067 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008068 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008069 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008070 }
8071 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008072 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008073 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008074
Chet Haasec3aa3612010-06-17 08:50:37 -07008075 mLeft += offset;
8076 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008077
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008078 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008079 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008080 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008081 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008082 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008083 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008084 }
8085
8086 /**
8087 * Get the LayoutParams associated with this view. All views should have
8088 * layout parameters. These supply parameters to the <i>parent</i> of this
8089 * view specifying how it should be arranged. There are many subclasses of
8090 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8091 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008092 *
8093 * This method may return null if this View is not attached to a parent
8094 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8095 * was not invoked successfully. When a View is attached to a parent
8096 * ViewGroup, this method must not return null.
8097 *
8098 * @return The LayoutParams associated with this view, or null if no
8099 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008100 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008101 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008102 public ViewGroup.LayoutParams getLayoutParams() {
8103 return mLayoutParams;
8104 }
8105
8106 /**
8107 * Set the layout parameters associated with this view. These supply
8108 * parameters to the <i>parent</i> of this view specifying how it should be
8109 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8110 * correspond to the different subclasses of ViewGroup that are responsible
8111 * for arranging their children.
8112 *
Romain Guy01c174b2011-02-22 11:51:06 -08008113 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008114 */
8115 public void setLayoutParams(ViewGroup.LayoutParams params) {
8116 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008117 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008118 }
8119 mLayoutParams = params;
8120 requestLayout();
8121 }
8122
8123 /**
8124 * Set the scrolled position of your view. This will cause a call to
8125 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8126 * invalidated.
8127 * @param x the x position to scroll to
8128 * @param y the y position to scroll to
8129 */
8130 public void scrollTo(int x, int y) {
8131 if (mScrollX != x || mScrollY != y) {
8132 int oldX = mScrollX;
8133 int oldY = mScrollY;
8134 mScrollX = x;
8135 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008136 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008137 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008138 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008139 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008140 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008141 }
8142 }
8143
8144 /**
8145 * Move the scrolled position of your view. This will cause a call to
8146 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8147 * invalidated.
8148 * @param x the amount of pixels to scroll by horizontally
8149 * @param y the amount of pixels to scroll by vertically
8150 */
8151 public void scrollBy(int x, int y) {
8152 scrollTo(mScrollX + x, mScrollY + y);
8153 }
8154
8155 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008156 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8157 * animation to fade the scrollbars out after a default delay. If a subclass
8158 * provides animated scrolling, the start delay should equal the duration
8159 * of the scrolling animation.</p>
8160 *
8161 * <p>The animation starts only if at least one of the scrollbars is
8162 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8163 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8164 * this method returns true, and false otherwise. If the animation is
8165 * started, this method calls {@link #invalidate()}; in that case the
8166 * caller should not call {@link #invalidate()}.</p>
8167 *
8168 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008169 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008170 *
8171 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8172 * and {@link #scrollTo(int, int)}.</p>
8173 *
8174 * @return true if the animation is played, false otherwise
8175 *
8176 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008177 * @see #scrollBy(int, int)
8178 * @see #scrollTo(int, int)
8179 * @see #isHorizontalScrollBarEnabled()
8180 * @see #isVerticalScrollBarEnabled()
8181 * @see #setHorizontalScrollBarEnabled(boolean)
8182 * @see #setVerticalScrollBarEnabled(boolean)
8183 */
8184 protected boolean awakenScrollBars() {
8185 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008186 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008187 }
8188
8189 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008190 * Trigger the scrollbars to draw.
8191 * This method differs from awakenScrollBars() only in its default duration.
8192 * initialAwakenScrollBars() will show the scroll bars for longer than
8193 * usual to give the user more of a chance to notice them.
8194 *
8195 * @return true if the animation is played, false otherwise.
8196 */
8197 private boolean initialAwakenScrollBars() {
8198 return mScrollCache != null &&
8199 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8200 }
8201
8202 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008203 * <p>
8204 * Trigger the scrollbars to draw. When invoked this method starts an
8205 * animation to fade the scrollbars out after a fixed delay. If a subclass
8206 * provides animated scrolling, the start delay should equal the duration of
8207 * the scrolling animation.
8208 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008209 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008210 * <p>
8211 * The animation starts only if at least one of the scrollbars is enabled,
8212 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8213 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8214 * this method returns true, and false otherwise. If the animation is
8215 * started, this method calls {@link #invalidate()}; in that case the caller
8216 * should not call {@link #invalidate()}.
8217 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008218 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008219 * <p>
8220 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008221 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008222 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008223 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008224 * @param startDelay the delay, in milliseconds, after which the animation
8225 * should start; when the delay is 0, the animation starts
8226 * immediately
8227 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008228 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008229 * @see #scrollBy(int, int)
8230 * @see #scrollTo(int, int)
8231 * @see #isHorizontalScrollBarEnabled()
8232 * @see #isVerticalScrollBarEnabled()
8233 * @see #setHorizontalScrollBarEnabled(boolean)
8234 * @see #setVerticalScrollBarEnabled(boolean)
8235 */
8236 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008237 return awakenScrollBars(startDelay, true);
8238 }
Joe Malin32736f02011-01-19 16:14:20 -08008239
Mike Cleron290947b2009-09-29 18:34:32 -07008240 /**
8241 * <p>
8242 * Trigger the scrollbars to draw. When invoked this method starts an
8243 * animation to fade the scrollbars out after a fixed delay. If a subclass
8244 * provides animated scrolling, the start delay should equal the duration of
8245 * the scrolling animation.
8246 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008247 *
Mike Cleron290947b2009-09-29 18:34:32 -07008248 * <p>
8249 * The animation starts only if at least one of the scrollbars is enabled,
8250 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8251 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8252 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008253 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008254 * is set to true; in that case the caller
8255 * should not call {@link #invalidate()}.
8256 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008257 *
Mike Cleron290947b2009-09-29 18:34:32 -07008258 * <p>
8259 * This method should be invoked everytime a subclass directly updates the
8260 * scroll parameters.
8261 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008262 *
Mike Cleron290947b2009-09-29 18:34:32 -07008263 * @param startDelay the delay, in milliseconds, after which the animation
8264 * should start; when the delay is 0, the animation starts
8265 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008266 *
Mike Cleron290947b2009-09-29 18:34:32 -07008267 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008268 *
Mike Cleron290947b2009-09-29 18:34:32 -07008269 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008270 *
Mike Cleron290947b2009-09-29 18:34:32 -07008271 * @see #scrollBy(int, int)
8272 * @see #scrollTo(int, int)
8273 * @see #isHorizontalScrollBarEnabled()
8274 * @see #isVerticalScrollBarEnabled()
8275 * @see #setHorizontalScrollBarEnabled(boolean)
8276 * @see #setVerticalScrollBarEnabled(boolean)
8277 */
8278 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008279 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008280
Mike Cleronf116bf82009-09-27 19:14:12 -07008281 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8282 return false;
8283 }
8284
8285 if (scrollCache.scrollBar == null) {
8286 scrollCache.scrollBar = new ScrollBarDrawable();
8287 }
8288
8289 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8290
Mike Cleron290947b2009-09-29 18:34:32 -07008291 if (invalidate) {
8292 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008293 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008294 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008295
8296 if (scrollCache.state == ScrollabilityCache.OFF) {
8297 // FIXME: this is copied from WindowManagerService.
8298 // We should get this value from the system when it
8299 // is possible to do so.
8300 final int KEY_REPEAT_FIRST_DELAY = 750;
8301 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8302 }
8303
8304 // Tell mScrollCache when we should start fading. This may
8305 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008306 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008307 scrollCache.fadeStartTime = fadeStartTime;
8308 scrollCache.state = ScrollabilityCache.ON;
8309
8310 // Schedule our fader to run, unscheduling any old ones first
8311 if (mAttachInfo != null) {
8312 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8313 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8314 }
8315
8316 return true;
8317 }
8318
8319 return false;
8320 }
8321
8322 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008323 * Do not invalidate views which are not visible and which are not running an animation. They
8324 * will not get drawn and they should not set dirty flags as if they will be drawn
8325 */
8326 private boolean skipInvalidate() {
8327 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8328 (!(mParent instanceof ViewGroup) ||
8329 !((ViewGroup) mParent).isViewTransitioning(this));
8330 }
8331 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008332 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008333 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8334 * in the future. This must be called from a UI thread. To call from a non-UI
8335 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008336 *
8337 * WARNING: This method is destructive to dirty.
8338 * @param dirty the rectangle representing the bounds of the dirty region
8339 */
8340 public void invalidate(Rect dirty) {
8341 if (ViewDebug.TRACE_HIERARCHY) {
8342 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8343 }
8344
Chet Haaseaceafe62011-08-26 15:44:33 -07008345 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008346 return;
8347 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008348 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008349 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8350 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008351 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008352 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008353 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008354 final ViewParent p = mParent;
8355 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008356 //noinspection PointlessBooleanExpression,ConstantConditions
8357 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8358 if (p != null && ai != null && ai.mHardwareAccelerated) {
8359 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008360 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008361 p.invalidateChild(this, null);
8362 return;
8363 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008365 if (p != null && ai != null) {
8366 final int scrollX = mScrollX;
8367 final int scrollY = mScrollY;
8368 final Rect r = ai.mTmpInvalRect;
8369 r.set(dirty.left - scrollX, dirty.top - scrollY,
8370 dirty.right - scrollX, dirty.bottom - scrollY);
8371 mParent.invalidateChild(this, r);
8372 }
8373 }
8374 }
8375
8376 /**
8377 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
8378 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008379 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8380 * will be called at some point in the future. This must be called from
8381 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008382 * @param l the left position of the dirty region
8383 * @param t the top position of the dirty region
8384 * @param r the right position of the dirty region
8385 * @param b the bottom position of the dirty region
8386 */
8387 public void invalidate(int l, int t, int r, int b) {
8388 if (ViewDebug.TRACE_HIERARCHY) {
8389 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8390 }
8391
Chet Haaseaceafe62011-08-26 15:44:33 -07008392 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008393 return;
8394 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008395 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008396 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8397 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008398 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008399 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008400 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008401 final ViewParent p = mParent;
8402 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008403 //noinspection PointlessBooleanExpression,ConstantConditions
8404 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8405 if (p != null && ai != null && ai.mHardwareAccelerated) {
8406 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008407 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008408 p.invalidateChild(this, null);
8409 return;
8410 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008412 if (p != null && ai != null && l < r && t < b) {
8413 final int scrollX = mScrollX;
8414 final int scrollY = mScrollY;
8415 final Rect tmpr = ai.mTmpInvalRect;
8416 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8417 p.invalidateChild(this, tmpr);
8418 }
8419 }
8420 }
8421
8422 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008423 * Invalidate the whole view. If the view is visible,
8424 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8425 * the future. This must be called from a UI thread. To call from a non-UI thread,
8426 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008427 */
8428 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008429 invalidate(true);
8430 }
Joe Malin32736f02011-01-19 16:14:20 -08008431
Chet Haaseed032702010-10-01 14:05:54 -07008432 /**
8433 * This is where the invalidate() work actually happens. A full invalidate()
8434 * causes the drawing cache to be invalidated, but this function can be called with
8435 * invalidateCache set to false to skip that invalidation step for cases that do not
8436 * need it (for example, a component that remains at the same dimensions with the same
8437 * content).
8438 *
8439 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8440 * well. This is usually true for a full invalidate, but may be set to false if the
8441 * View's contents or dimensions have not changed.
8442 */
Romain Guy849d0a32011-02-01 17:20:48 -08008443 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008444 if (ViewDebug.TRACE_HIERARCHY) {
8445 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8446 }
8447
Chet Haaseaceafe62011-08-26 15:44:33 -07008448 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008449 return;
8450 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008451 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008452 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008453 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8454 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008455 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008456 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008457 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008458 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008459 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8460 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008461 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008462 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008463 //noinspection PointlessBooleanExpression,ConstantConditions
8464 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8465 if (p != null && ai != null && ai.mHardwareAccelerated) {
8466 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008467 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008468 p.invalidateChild(this, null);
8469 return;
8470 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008471 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008473 if (p != null && ai != null) {
8474 final Rect r = ai.mTmpInvalRect;
8475 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8476 // Don't call invalidate -- we don't want to internally scroll
8477 // our own bounds
8478 p.invalidateChild(this, r);
8479 }
8480 }
8481 }
8482
8483 /**
Romain Guyda489792011-02-03 01:05:15 -08008484 * @hide
8485 */
8486 public void fastInvalidate() {
Chet Haaseaceafe62011-08-26 15:44:33 -07008487 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008488 return;
8489 }
Romain Guyda489792011-02-03 01:05:15 -08008490 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
8491 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8492 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
8493 if (mParent instanceof View) {
8494 ((View) mParent).mPrivateFlags |= INVALIDATED;
8495 }
8496 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008497 mPrivateFlags |= DIRTY;
Romain Guyda489792011-02-03 01:05:15 -08008498 mPrivateFlags |= INVALIDATED;
8499 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07008500 if (mParent != null && mAttachInfo != null) {
8501 if (mAttachInfo.mHardwareAccelerated) {
8502 mParent.invalidateChild(this, null);
8503 } else {
8504 final Rect r = mAttachInfo.mTmpInvalRect;
8505 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8506 // Don't call invalidate -- we don't want to internally scroll
8507 // our own bounds
8508 mParent.invalidateChild(this, r);
8509 }
Romain Guyda489792011-02-03 01:05:15 -08008510 }
8511 }
8512 }
8513
8514 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008515 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008516 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8517 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008518 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8519 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008520 *
8521 * @hide
8522 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008523 protected void invalidateParentCaches() {
8524 if (mParent instanceof View) {
8525 ((View) mParent).mPrivateFlags |= INVALIDATED;
8526 }
8527 }
Joe Malin32736f02011-01-19 16:14:20 -08008528
Romain Guy0fd89bf2011-01-26 15:41:30 -08008529 /**
8530 * Used to indicate that the parent of this view should be invalidated. This functionality
8531 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8532 * which is necessary when various parent-managed properties of the view change, such as
8533 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8534 * an invalidation event to the parent.
8535 *
8536 * @hide
8537 */
8538 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008539 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008540 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008541 }
8542 }
8543
8544 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008545 * Indicates whether this View is opaque. An opaque View guarantees that it will
8546 * draw all the pixels overlapping its bounds using a fully opaque color.
8547 *
8548 * Subclasses of View should override this method whenever possible to indicate
8549 * whether an instance is opaque. Opaque Views are treated in a special way by
8550 * the View hierarchy, possibly allowing it to perform optimizations during
8551 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008552 *
Romain Guy24443ea2009-05-11 11:56:30 -07008553 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008554 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008555 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008556 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008557 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008558 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8559 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008560 }
8561
Adam Powell20232d02010-12-08 21:08:53 -08008562 /**
8563 * @hide
8564 */
8565 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008566 // Opaque if:
8567 // - Has a background
8568 // - Background is opaque
8569 // - Doesn't have scrollbars or scrollbars are inside overlay
8570
8571 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8572 mPrivateFlags |= OPAQUE_BACKGROUND;
8573 } else {
8574 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8575 }
8576
8577 final int flags = mViewFlags;
8578 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8579 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8580 mPrivateFlags |= OPAQUE_SCROLLBARS;
8581 } else {
8582 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8583 }
8584 }
8585
8586 /**
8587 * @hide
8588 */
8589 protected boolean hasOpaqueScrollbars() {
8590 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008591 }
8592
8593 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008594 * @return A handler associated with the thread running the View. This
8595 * handler can be used to pump events in the UI events queue.
8596 */
8597 public Handler getHandler() {
8598 if (mAttachInfo != null) {
8599 return mAttachInfo.mHandler;
8600 }
8601 return null;
8602 }
8603
8604 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008605 * <p>Causes the Runnable to be added to the message queue.
8606 * The runnable will be run on the user interface thread.</p>
8607 *
8608 * <p>This method can be invoked from outside of the UI thread
8609 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008610 *
8611 * @param action The Runnable that will be executed.
8612 *
8613 * @return Returns true if the Runnable was successfully placed in to the
8614 * message queue. Returns false on failure, usually because the
8615 * looper processing the message queue is exiting.
8616 */
8617 public boolean post(Runnable action) {
8618 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008619 AttachInfo attachInfo = mAttachInfo;
8620 if (attachInfo != null) {
8621 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008622 } else {
8623 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008624 ViewRootImpl.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008625 return true;
8626 }
8627
8628 return handler.post(action);
8629 }
8630
8631 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008632 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008633 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008634 * The runnable will be run on the user interface thread.</p>
8635 *
8636 * <p>This method can be invoked from outside of the UI thread
8637 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008638 *
8639 * @param action The Runnable that will be executed.
8640 * @param delayMillis The delay (in milliseconds) until the Runnable
8641 * will be executed.
8642 *
8643 * @return true if the Runnable was successfully placed in to the
8644 * message queue. Returns false on failure, usually because the
8645 * looper processing the message queue is exiting. Note that a
8646 * result of true does not mean the Runnable will be processed --
8647 * if the looper is quit before the delivery time of the message
8648 * occurs then the message will be dropped.
8649 */
8650 public boolean postDelayed(Runnable action, long delayMillis) {
8651 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008652 AttachInfo attachInfo = mAttachInfo;
8653 if (attachInfo != null) {
8654 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008655 } else {
8656 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008657 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008658 return true;
8659 }
8660
8661 return handler.postDelayed(action, delayMillis);
8662 }
8663
8664 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008665 * <p>Removes the specified Runnable from the message queue.</p>
8666 *
8667 * <p>This method can be invoked from outside of the UI thread
8668 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008669 *
8670 * @param action The Runnable to remove from the message handling queue
8671 *
8672 * @return true if this view could ask the Handler to remove the Runnable,
8673 * false otherwise. When the returned value is true, the Runnable
8674 * may or may not have been actually removed from the message queue
8675 * (for instance, if the Runnable was not in the queue already.)
8676 */
8677 public boolean removeCallbacks(Runnable action) {
8678 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008679 AttachInfo attachInfo = mAttachInfo;
8680 if (attachInfo != null) {
8681 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008682 } else {
8683 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008684 ViewRootImpl.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008685 return true;
8686 }
8687
8688 handler.removeCallbacks(action);
8689 return true;
8690 }
8691
8692 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008693 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
8694 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008695 *
Romain Guye63a4f32011-08-11 11:33:31 -07008696 * <p>This method can be invoked from outside of the UI thread
8697 * only when this View is attached to a window.</p>
8698 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008699 * @see #invalidate()
8700 */
8701 public void postInvalidate() {
8702 postInvalidateDelayed(0);
8703 }
8704
8705 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008706 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8707 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
8708 *
8709 * <p>This method can be invoked from outside of the UI thread
8710 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008711 *
8712 * @param left The left coordinate of the rectangle to invalidate.
8713 * @param top The top coordinate of the rectangle to invalidate.
8714 * @param right The right coordinate of the rectangle to invalidate.
8715 * @param bottom The bottom coordinate of the rectangle to invalidate.
8716 *
8717 * @see #invalidate(int, int, int, int)
8718 * @see #invalidate(Rect)
8719 */
8720 public void postInvalidate(int left, int top, int right, int bottom) {
8721 postInvalidateDelayed(0, left, top, right, bottom);
8722 }
8723
8724 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008725 * <p>Cause an invalidate to happen on a subsequent cycle through the event
8726 * loop. Waits for the specified amount of time.</p>
8727 *
8728 * <p>This method can be invoked from outside of the UI thread
8729 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008730 *
8731 * @param delayMilliseconds the duration in milliseconds to delay the
8732 * invalidation by
8733 */
8734 public void postInvalidateDelayed(long delayMilliseconds) {
8735 // We try only with the AttachInfo because there's no point in invalidating
8736 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008737 AttachInfo attachInfo = mAttachInfo;
8738 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008739 Message msg = Message.obtain();
8740 msg.what = AttachInfo.INVALIDATE_MSG;
8741 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008742 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008743 }
8744 }
8745
8746 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008747 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8748 * through the event loop. Waits for the specified amount of time.</p>
8749 *
8750 * <p>This method can be invoked from outside of the UI thread
8751 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008752 *
8753 * @param delayMilliseconds the duration in milliseconds to delay the
8754 * invalidation by
8755 * @param left The left coordinate of the rectangle to invalidate.
8756 * @param top The top coordinate of the rectangle to invalidate.
8757 * @param right The right coordinate of the rectangle to invalidate.
8758 * @param bottom The bottom coordinate of the rectangle to invalidate.
8759 */
8760 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8761 int right, int bottom) {
8762
8763 // We try only with the AttachInfo because there's no point in invalidating
8764 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008765 AttachInfo attachInfo = mAttachInfo;
8766 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008767 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8768 info.target = this;
8769 info.left = left;
8770 info.top = top;
8771 info.right = right;
8772 info.bottom = bottom;
8773
8774 final Message msg = Message.obtain();
8775 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8776 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008777 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008778 }
8779 }
8780
8781 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008782 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8783 * This event is sent at most once every
8784 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8785 */
8786 private void postSendViewScrolledAccessibilityEventCallback() {
8787 if (mSendViewScrolledAccessibilityEvent == null) {
8788 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8789 }
8790 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8791 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8792 postDelayed(mSendViewScrolledAccessibilityEvent,
8793 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8794 }
8795 }
8796
8797 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008798 * Called by a parent to request that a child update its values for mScrollX
8799 * and mScrollY if necessary. This will typically be done if the child is
8800 * animating a scroll using a {@link android.widget.Scroller Scroller}
8801 * object.
8802 */
8803 public void computeScroll() {
8804 }
8805
8806 /**
8807 * <p>Indicate whether the horizontal edges are faded when the view is
8808 * scrolled horizontally.</p>
8809 *
8810 * @return true if the horizontal edges should are faded on scroll, false
8811 * otherwise
8812 *
8813 * @see #setHorizontalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008814 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008815 */
8816 public boolean isHorizontalFadingEdgeEnabled() {
8817 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8818 }
8819
8820 /**
8821 * <p>Define whether the horizontal edges should be faded when this view
8822 * is scrolled horizontally.</p>
8823 *
8824 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8825 * be faded when the view is scrolled
8826 * horizontally
8827 *
8828 * @see #isHorizontalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008829 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008830 */
8831 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8832 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8833 if (horizontalFadingEdgeEnabled) {
8834 initScrollCache();
8835 }
8836
8837 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8838 }
8839 }
8840
8841 /**
8842 * <p>Indicate whether the vertical edges are faded when the view is
8843 * scrolled horizontally.</p>
8844 *
8845 * @return true if the vertical edges should are faded on scroll, false
8846 * otherwise
8847 *
8848 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008849 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008850 */
8851 public boolean isVerticalFadingEdgeEnabled() {
8852 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8853 }
8854
8855 /**
8856 * <p>Define whether the vertical edges should be faded when this view
8857 * is scrolled vertically.</p>
8858 *
8859 * @param verticalFadingEdgeEnabled true if the vertical edges should
8860 * be faded when the view is scrolled
8861 * vertically
8862 *
8863 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008864 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008865 */
8866 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8867 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8868 if (verticalFadingEdgeEnabled) {
8869 initScrollCache();
8870 }
8871
8872 mViewFlags ^= FADING_EDGE_VERTICAL;
8873 }
8874 }
8875
8876 /**
8877 * Returns the strength, or intensity, of the top faded edge. The strength is
8878 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8879 * returns 0.0 or 1.0 but no value in between.
8880 *
8881 * Subclasses should override this method to provide a smoother fade transition
8882 * when scrolling occurs.
8883 *
8884 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8885 */
8886 protected float getTopFadingEdgeStrength() {
8887 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8888 }
8889
8890 /**
8891 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8892 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8893 * returns 0.0 or 1.0 but no value in between.
8894 *
8895 * Subclasses should override this method to provide a smoother fade transition
8896 * when scrolling occurs.
8897 *
8898 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8899 */
8900 protected float getBottomFadingEdgeStrength() {
8901 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8902 computeVerticalScrollRange() ? 1.0f : 0.0f;
8903 }
8904
8905 /**
8906 * Returns the strength, or intensity, of the left faded edge. The strength is
8907 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8908 * returns 0.0 or 1.0 but no value in between.
8909 *
8910 * Subclasses should override this method to provide a smoother fade transition
8911 * when scrolling occurs.
8912 *
8913 * @return the intensity of the left fade as a float between 0.0f and 1.0f
8914 */
8915 protected float getLeftFadingEdgeStrength() {
8916 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
8917 }
8918
8919 /**
8920 * Returns the strength, or intensity, of the right faded edge. The strength is
8921 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8922 * returns 0.0 or 1.0 but no value in between.
8923 *
8924 * Subclasses should override this method to provide a smoother fade transition
8925 * when scrolling occurs.
8926 *
8927 * @return the intensity of the right fade as a float between 0.0f and 1.0f
8928 */
8929 protected float getRightFadingEdgeStrength() {
8930 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
8931 computeHorizontalScrollRange() ? 1.0f : 0.0f;
8932 }
8933
8934 /**
8935 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
8936 * scrollbar is not drawn by default.</p>
8937 *
8938 * @return true if the horizontal scrollbar should be painted, false
8939 * otherwise
8940 *
8941 * @see #setHorizontalScrollBarEnabled(boolean)
8942 */
8943 public boolean isHorizontalScrollBarEnabled() {
8944 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8945 }
8946
8947 /**
8948 * <p>Define whether the horizontal scrollbar should be drawn or not. The
8949 * scrollbar is not drawn by default.</p>
8950 *
8951 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
8952 * be painted
8953 *
8954 * @see #isHorizontalScrollBarEnabled()
8955 */
8956 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
8957 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
8958 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008959 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008960 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008961 }
8962 }
8963
8964 /**
8965 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
8966 * scrollbar is not drawn by default.</p>
8967 *
8968 * @return true if the vertical scrollbar should be painted, false
8969 * otherwise
8970 *
8971 * @see #setVerticalScrollBarEnabled(boolean)
8972 */
8973 public boolean isVerticalScrollBarEnabled() {
8974 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
8975 }
8976
8977 /**
8978 * <p>Define whether the vertical scrollbar should be drawn or not. The
8979 * scrollbar is not drawn by default.</p>
8980 *
8981 * @param verticalScrollBarEnabled true if the vertical scrollbar should
8982 * be painted
8983 *
8984 * @see #isVerticalScrollBarEnabled()
8985 */
8986 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
8987 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
8988 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008989 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008990 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008991 }
8992 }
8993
Adam Powell20232d02010-12-08 21:08:53 -08008994 /**
8995 * @hide
8996 */
8997 protected void recomputePadding() {
8998 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008999 }
Joe Malin32736f02011-01-19 16:14:20 -08009000
Mike Cleronfe81d382009-09-28 14:22:16 -07009001 /**
9002 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009003 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009004 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009005 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009006 */
9007 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9008 initScrollCache();
9009 final ScrollabilityCache scrollabilityCache = mScrollCache;
9010 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009011 if (fadeScrollbars) {
9012 scrollabilityCache.state = ScrollabilityCache.OFF;
9013 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009014 scrollabilityCache.state = ScrollabilityCache.ON;
9015 }
9016 }
Joe Malin32736f02011-01-19 16:14:20 -08009017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009018 /**
Joe Malin32736f02011-01-19 16:14:20 -08009019 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009020 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009021 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009022 * @return true if scrollbar fading is enabled
9023 */
9024 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009025 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009026 }
Joe Malin32736f02011-01-19 16:14:20 -08009027
Mike Cleron52f0a642009-09-28 18:21:37 -07009028 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009029 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9030 * inset. When inset, they add to the padding of the view. And the scrollbars
9031 * can be drawn inside the padding area or on the edge of the view. For example,
9032 * if a view has a background drawable and you want to draw the scrollbars
9033 * inside the padding specified by the drawable, you can use
9034 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9035 * appear at the edge of the view, ignoring the padding, then you can use
9036 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9037 * @param style the style of the scrollbars. Should be one of
9038 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9039 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9040 * @see #SCROLLBARS_INSIDE_OVERLAY
9041 * @see #SCROLLBARS_INSIDE_INSET
9042 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9043 * @see #SCROLLBARS_OUTSIDE_INSET
9044 */
9045 public void setScrollBarStyle(int style) {
9046 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9047 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009048 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009049 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009050 }
9051 }
9052
9053 /**
9054 * <p>Returns the current scrollbar style.</p>
9055 * @return the current scrollbar style
9056 * @see #SCROLLBARS_INSIDE_OVERLAY
9057 * @see #SCROLLBARS_INSIDE_INSET
9058 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9059 * @see #SCROLLBARS_OUTSIDE_INSET
9060 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009061 @ViewDebug.ExportedProperty(mapping = {
9062 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9063 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9064 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9065 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9066 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009067 public int getScrollBarStyle() {
9068 return mViewFlags & SCROLLBARS_STYLE_MASK;
9069 }
9070
9071 /**
9072 * <p>Compute the horizontal range that the horizontal scrollbar
9073 * represents.</p>
9074 *
9075 * <p>The range is expressed in arbitrary units that must be the same as the
9076 * units used by {@link #computeHorizontalScrollExtent()} and
9077 * {@link #computeHorizontalScrollOffset()}.</p>
9078 *
9079 * <p>The default range is the drawing width of this view.</p>
9080 *
9081 * @return the total horizontal range represented by the horizontal
9082 * scrollbar
9083 *
9084 * @see #computeHorizontalScrollExtent()
9085 * @see #computeHorizontalScrollOffset()
9086 * @see android.widget.ScrollBarDrawable
9087 */
9088 protected int computeHorizontalScrollRange() {
9089 return getWidth();
9090 }
9091
9092 /**
9093 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9094 * within the horizontal range. This value is used to compute the position
9095 * of the thumb within the scrollbar's track.</p>
9096 *
9097 * <p>The range is expressed in arbitrary units that must be the same as the
9098 * units used by {@link #computeHorizontalScrollRange()} and
9099 * {@link #computeHorizontalScrollExtent()}.</p>
9100 *
9101 * <p>The default offset is the scroll offset of this view.</p>
9102 *
9103 * @return the horizontal offset of the scrollbar's thumb
9104 *
9105 * @see #computeHorizontalScrollRange()
9106 * @see #computeHorizontalScrollExtent()
9107 * @see android.widget.ScrollBarDrawable
9108 */
9109 protected int computeHorizontalScrollOffset() {
9110 return mScrollX;
9111 }
9112
9113 /**
9114 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9115 * within the horizontal range. This value is used to compute the length
9116 * of the thumb within the scrollbar's track.</p>
9117 *
9118 * <p>The range is expressed in arbitrary units that must be the same as the
9119 * units used by {@link #computeHorizontalScrollRange()} and
9120 * {@link #computeHorizontalScrollOffset()}.</p>
9121 *
9122 * <p>The default extent is the drawing width of this view.</p>
9123 *
9124 * @return the horizontal extent of the scrollbar's thumb
9125 *
9126 * @see #computeHorizontalScrollRange()
9127 * @see #computeHorizontalScrollOffset()
9128 * @see android.widget.ScrollBarDrawable
9129 */
9130 protected int computeHorizontalScrollExtent() {
9131 return getWidth();
9132 }
9133
9134 /**
9135 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9136 *
9137 * <p>The range is expressed in arbitrary units that must be the same as the
9138 * units used by {@link #computeVerticalScrollExtent()} and
9139 * {@link #computeVerticalScrollOffset()}.</p>
9140 *
9141 * @return the total vertical range represented by the vertical scrollbar
9142 *
9143 * <p>The default range is the drawing height of this view.</p>
9144 *
9145 * @see #computeVerticalScrollExtent()
9146 * @see #computeVerticalScrollOffset()
9147 * @see android.widget.ScrollBarDrawable
9148 */
9149 protected int computeVerticalScrollRange() {
9150 return getHeight();
9151 }
9152
9153 /**
9154 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9155 * within the horizontal range. This value is used to compute the position
9156 * of the thumb within the scrollbar's track.</p>
9157 *
9158 * <p>The range is expressed in arbitrary units that must be the same as the
9159 * units used by {@link #computeVerticalScrollRange()} and
9160 * {@link #computeVerticalScrollExtent()}.</p>
9161 *
9162 * <p>The default offset is the scroll offset of this view.</p>
9163 *
9164 * @return the vertical offset of the scrollbar's thumb
9165 *
9166 * @see #computeVerticalScrollRange()
9167 * @see #computeVerticalScrollExtent()
9168 * @see android.widget.ScrollBarDrawable
9169 */
9170 protected int computeVerticalScrollOffset() {
9171 return mScrollY;
9172 }
9173
9174 /**
9175 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9176 * within the vertical range. This value is used to compute the length
9177 * of the thumb within the scrollbar's track.</p>
9178 *
9179 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009180 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009181 * {@link #computeVerticalScrollOffset()}.</p>
9182 *
9183 * <p>The default extent is the drawing height of this view.</p>
9184 *
9185 * @return the vertical extent of the scrollbar's thumb
9186 *
9187 * @see #computeVerticalScrollRange()
9188 * @see #computeVerticalScrollOffset()
9189 * @see android.widget.ScrollBarDrawable
9190 */
9191 protected int computeVerticalScrollExtent() {
9192 return getHeight();
9193 }
9194
9195 /**
Adam Powell69159442011-06-13 17:53:06 -07009196 * Check if this view can be scrolled horizontally in a certain direction.
9197 *
9198 * @param direction Negative to check scrolling left, positive to check scrolling right.
9199 * @return true if this view can be scrolled in the specified direction, false otherwise.
9200 */
9201 public boolean canScrollHorizontally(int direction) {
9202 final int offset = computeHorizontalScrollOffset();
9203 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9204 if (range == 0) return false;
9205 if (direction < 0) {
9206 return offset > 0;
9207 } else {
9208 return offset < range - 1;
9209 }
9210 }
9211
9212 /**
9213 * Check if this view can be scrolled vertically in a certain direction.
9214 *
9215 * @param direction Negative to check scrolling up, positive to check scrolling down.
9216 * @return true if this view can be scrolled in the specified direction, false otherwise.
9217 */
9218 public boolean canScrollVertically(int direction) {
9219 final int offset = computeVerticalScrollOffset();
9220 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9221 if (range == 0) return false;
9222 if (direction < 0) {
9223 return offset > 0;
9224 } else {
9225 return offset < range - 1;
9226 }
9227 }
9228
9229 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009230 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9231 * scrollbars are painted only if they have been awakened first.</p>
9232 *
9233 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009234 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009235 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009236 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009237 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009238 // scrollbars are drawn only when the animation is running
9239 final ScrollabilityCache cache = mScrollCache;
9240 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009241
Mike Cleronf116bf82009-09-27 19:14:12 -07009242 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009243
Mike Cleronf116bf82009-09-27 19:14:12 -07009244 if (state == ScrollabilityCache.OFF) {
9245 return;
9246 }
Joe Malin32736f02011-01-19 16:14:20 -08009247
Mike Cleronf116bf82009-09-27 19:14:12 -07009248 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009249
Mike Cleronf116bf82009-09-27 19:14:12 -07009250 if (state == ScrollabilityCache.FADING) {
9251 // We're fading -- get our fade interpolation
9252 if (cache.interpolatorValues == null) {
9253 cache.interpolatorValues = new float[1];
9254 }
Joe Malin32736f02011-01-19 16:14:20 -08009255
Mike Cleronf116bf82009-09-27 19:14:12 -07009256 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009257
Mike Cleronf116bf82009-09-27 19:14:12 -07009258 // Stops the animation if we're done
9259 if (cache.scrollBarInterpolator.timeToValues(values) ==
9260 Interpolator.Result.FREEZE_END) {
9261 cache.state = ScrollabilityCache.OFF;
9262 } else {
9263 cache.scrollBar.setAlpha(Math.round(values[0]));
9264 }
Joe Malin32736f02011-01-19 16:14:20 -08009265
9266 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009267 // drawing. We only want this when we're fading so that
9268 // we prevent excessive redraws
9269 invalidate = true;
9270 } else {
9271 // We're just on -- but we may have been fading before so
9272 // reset alpha
9273 cache.scrollBar.setAlpha(255);
9274 }
9275
Joe Malin32736f02011-01-19 16:14:20 -08009276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009277 final int viewFlags = mViewFlags;
9278
9279 final boolean drawHorizontalScrollBar =
9280 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9281 final boolean drawVerticalScrollBar =
9282 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9283 && !isVerticalScrollBarHidden();
9284
9285 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9286 final int width = mRight - mLeft;
9287 final int height = mBottom - mTop;
9288
9289 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009290
Mike Reede8853fc2009-09-04 14:01:48 -04009291 final int scrollX = mScrollX;
9292 final int scrollY = mScrollY;
9293 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9294
Mike Cleronf116bf82009-09-27 19:14:12 -07009295 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009297 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009298 int size = scrollBar.getSize(false);
9299 if (size <= 0) {
9300 size = cache.scrollBarSize;
9301 }
9302
Mike Cleronf116bf82009-09-27 19:14:12 -07009303 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009304 computeHorizontalScrollOffset(),
9305 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009306 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009307 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009308 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009309 left = scrollX + (mPaddingLeft & inside);
9310 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9311 bottom = top + size;
9312 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9313 if (invalidate) {
9314 invalidate(left, top, right, bottom);
9315 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009316 }
9317
9318 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009319 int size = scrollBar.getSize(true);
9320 if (size <= 0) {
9321 size = cache.scrollBarSize;
9322 }
9323
Mike Reede8853fc2009-09-04 14:01:48 -04009324 scrollBar.setParameters(computeVerticalScrollRange(),
9325 computeVerticalScrollOffset(),
9326 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009327 switch (mVerticalScrollbarPosition) {
9328 default:
9329 case SCROLLBAR_POSITION_DEFAULT:
9330 case SCROLLBAR_POSITION_RIGHT:
9331 left = scrollX + width - size - (mUserPaddingRight & inside);
9332 break;
9333 case SCROLLBAR_POSITION_LEFT:
9334 left = scrollX + (mUserPaddingLeft & inside);
9335 break;
9336 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009337 top = scrollY + (mPaddingTop & inside);
9338 right = left + size;
9339 bottom = scrollY + height - (mUserPaddingBottom & inside);
9340 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9341 if (invalidate) {
9342 invalidate(left, top, right, bottom);
9343 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009344 }
9345 }
9346 }
9347 }
Romain Guy8506ab42009-06-11 17:35:47 -07009348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009349 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009350 * 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 -08009351 * FastScroller is visible.
9352 * @return whether to temporarily hide the vertical scrollbar
9353 * @hide
9354 */
9355 protected boolean isVerticalScrollBarHidden() {
9356 return false;
9357 }
9358
9359 /**
9360 * <p>Draw the horizontal scrollbar if
9361 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9362 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009363 * @param canvas the canvas on which to draw the scrollbar
9364 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009365 *
9366 * @see #isHorizontalScrollBarEnabled()
9367 * @see #computeHorizontalScrollRange()
9368 * @see #computeHorizontalScrollExtent()
9369 * @see #computeHorizontalScrollOffset()
9370 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009371 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009372 */
Romain Guy8fb95422010-08-17 18:38:51 -07009373 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9374 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009375 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009376 scrollBar.draw(canvas);
9377 }
Mike Reede8853fc2009-09-04 14:01:48 -04009378
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009379 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009380 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9381 * returns true.</p>
9382 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009383 * @param canvas the canvas on which to draw the scrollbar
9384 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009385 *
9386 * @see #isVerticalScrollBarEnabled()
9387 * @see #computeVerticalScrollRange()
9388 * @see #computeVerticalScrollExtent()
9389 * @see #computeVerticalScrollOffset()
9390 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009391 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009392 */
Romain Guy8fb95422010-08-17 18:38:51 -07009393 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9394 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009395 scrollBar.setBounds(l, t, r, b);
9396 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009397 }
9398
9399 /**
9400 * Implement this to do your drawing.
9401 *
9402 * @param canvas the canvas on which the background will be drawn
9403 */
9404 protected void onDraw(Canvas canvas) {
9405 }
9406
9407 /*
9408 * Caller is responsible for calling requestLayout if necessary.
9409 * (This allows addViewInLayout to not request a new layout.)
9410 */
9411 void assignParent(ViewParent parent) {
9412 if (mParent == null) {
9413 mParent = parent;
9414 } else if (parent == null) {
9415 mParent = null;
9416 } else {
9417 throw new RuntimeException("view " + this + " being added, but"
9418 + " it already has a parent");
9419 }
9420 }
9421
9422 /**
9423 * This is called when the view is attached to a window. At this point it
9424 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009425 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9426 * however it may be called any time before the first onDraw -- including
9427 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009428 *
9429 * @see #onDetachedFromWindow()
9430 */
9431 protected void onAttachedToWindow() {
9432 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9433 mParent.requestTransparentRegion(this);
9434 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009435 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9436 initialAwakenScrollBars();
9437 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9438 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009439 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009440 // Order is important here: LayoutDirection MUST be resolved before Padding
9441 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009442 resolveLayoutDirectionIfNeeded();
9443 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009444 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009445 if (isFocused()) {
9446 InputMethodManager imm = InputMethodManager.peekInstance();
9447 imm.focusIn(this);
9448 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009449 }
Cibu Johny86666632010-02-22 13:01:02 -08009450
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009451 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009452 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9453 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009454 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009455 private void resolveLayoutDirectionIfNeeded() {
9456 // Do not resolve if it is not needed
9457 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9458
9459 // Clear any previous layout direction resolution
9460 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9461
Fabrice Di Meglio4b60c302011-08-17 16:56:55 -07009462 // Reset also TextDirection as a change into LayoutDirection may impact the selected
9463 // TextDirectionHeuristic
9464 resetResolvedTextDirection();
9465
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009466 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009467 switch (getLayoutDirection()) {
9468 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009469 // We cannot do the resolution if there is no parent
9470 if (mParent == null) return;
9471
Cibu Johny86666632010-02-22 13:01:02 -08009472 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009473 if (mParent instanceof ViewGroup) {
9474 ViewGroup viewGroup = ((ViewGroup) mParent);
9475
9476 // Check if the parent view group can resolve
9477 if (! viewGroup.canResolveLayoutDirection()) {
9478 return;
9479 }
9480 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9481 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9482 }
Cibu Johny86666632010-02-22 13:01:02 -08009483 }
9484 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009485 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009486 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009487 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009488 case LAYOUT_DIRECTION_LOCALE:
9489 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009490 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009491 }
9492 break;
9493 default:
9494 // Nothing to do, LTR by default
9495 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009496
9497 // Set to resolved
9498 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
9499 }
9500
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009501 /**
9502 * @hide
9503 */
9504 protected void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009505 // If the user specified the absolute padding (either with android:padding or
9506 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9507 // use the default padding or the padding from the background drawable
9508 // (stored at this point in mPadding*)
9509 switch (getResolvedLayoutDirection()) {
9510 case LAYOUT_DIRECTION_RTL:
9511 // Start user padding override Right user padding. Otherwise, if Right user
9512 // padding is not defined, use the default Right padding. If Right user padding
9513 // is defined, just use it.
9514 if (mUserPaddingStart >= 0) {
9515 mUserPaddingRight = mUserPaddingStart;
9516 } else if (mUserPaddingRight < 0) {
9517 mUserPaddingRight = mPaddingRight;
9518 }
9519 if (mUserPaddingEnd >= 0) {
9520 mUserPaddingLeft = mUserPaddingEnd;
9521 } else if (mUserPaddingLeft < 0) {
9522 mUserPaddingLeft = mPaddingLeft;
9523 }
9524 break;
9525 case LAYOUT_DIRECTION_LTR:
9526 default:
9527 // Start user padding override Left user padding. Otherwise, if Left user
9528 // padding is not defined, use the default left padding. If Left user padding
9529 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009530 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009531 mUserPaddingLeft = mUserPaddingStart;
9532 } else if (mUserPaddingLeft < 0) {
9533 mUserPaddingLeft = mPaddingLeft;
9534 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009535 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009536 mUserPaddingRight = mUserPaddingEnd;
9537 } else if (mUserPaddingRight < 0) {
9538 mUserPaddingRight = mPaddingRight;
9539 }
9540 }
9541
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009542 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9543
9544 recomputePadding();
9545 }
9546
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009547 /**
9548 * Return true if layout direction resolution can be done
9549 *
9550 * @hide
9551 */
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009552 protected boolean canResolveLayoutDirection() {
9553 switch (getLayoutDirection()) {
9554 case LAYOUT_DIRECTION_INHERIT:
9555 return (mParent != null);
9556 default:
9557 return true;
9558 }
9559 }
9560
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009561 /**
Doug Feltc0ccf0c2011-06-23 16:13:18 -07009562 * Reset the resolved layout direction.
9563 *
9564 * Subclasses need to override this method to clear cached information that depends on the
9565 * resolved layout direction, or to inform child views that inherit their layout direction.
9566 * Overrides must also call the superclass implementation at the start of their implementation.
9567 *
9568 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009569 */
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009570 protected void resetResolvedLayoutDirection() {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07009571 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009572 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009573 }
9574
9575 /**
9576 * Check if a Locale is corresponding to a RTL script.
9577 *
9578 * @param locale Locale to check
9579 * @return true if a Locale is corresponding to a RTL script.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009580 *
9581 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009582 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009583 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07009584 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
9585 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009586 }
9587
9588 /**
9589 * This is called when the view is detached from a window. At this point it
9590 * no longer has a surface for drawing.
9591 *
9592 * @see #onAttachedToWindow()
9593 */
9594 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009595 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009596
Romain Guya440b002010-02-24 15:57:54 -08009597 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009598 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009599 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009600 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009602 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009603
Romain Guy6d7475d2011-07-27 16:28:21 -07009604 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009605
Chet Haasedaf98e92011-01-10 14:10:36 -08009606 if (mDisplayList != null) {
9607 mDisplayList.invalidate();
9608 }
9609
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009610 if (mAttachInfo != null) {
9611 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009612 }
9613
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009614 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009615
9616 resetResolvedLayoutDirection();
9617 resetResolvedTextDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009618 }
9619
9620 /**
9621 * @return The number of times this view has been attached to a window
9622 */
9623 protected int getWindowAttachCount() {
9624 return mWindowAttachCount;
9625 }
9626
9627 /**
9628 * Retrieve a unique token identifying the window this view is attached to.
9629 * @return Return the window's token for use in
9630 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9631 */
9632 public IBinder getWindowToken() {
9633 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9634 }
9635
9636 /**
9637 * Retrieve a unique token identifying the top-level "real" window of
9638 * the window that this view is attached to. That is, this is like
9639 * {@link #getWindowToken}, except if the window this view in is a panel
9640 * window (attached to another containing window), then the token of
9641 * the containing window is returned instead.
9642 *
9643 * @return Returns the associated window token, either
9644 * {@link #getWindowToken()} or the containing window's token.
9645 */
9646 public IBinder getApplicationWindowToken() {
9647 AttachInfo ai = mAttachInfo;
9648 if (ai != null) {
9649 IBinder appWindowToken = ai.mPanelParentWindowToken;
9650 if (appWindowToken == null) {
9651 appWindowToken = ai.mWindowToken;
9652 }
9653 return appWindowToken;
9654 }
9655 return null;
9656 }
9657
9658 /**
9659 * Retrieve private session object this view hierarchy is using to
9660 * communicate with the window manager.
9661 * @return the session object to communicate with the window manager
9662 */
9663 /*package*/ IWindowSession getWindowSession() {
9664 return mAttachInfo != null ? mAttachInfo.mSession : null;
9665 }
9666
9667 /**
9668 * @param info the {@link android.view.View.AttachInfo} to associated with
9669 * this view
9670 */
9671 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9672 //System.out.println("Attached! " + this);
9673 mAttachInfo = info;
9674 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009675 // We will need to evaluate the drawable state at least once.
9676 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009677 if (mFloatingTreeObserver != null) {
9678 info.mTreeObserver.merge(mFloatingTreeObserver);
9679 mFloatingTreeObserver = null;
9680 }
9681 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9682 mAttachInfo.mScrollContainers.add(this);
9683 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9684 }
9685 performCollectViewAttributes(visibility);
9686 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009687
9688 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9689 mOnAttachStateChangeListeners;
9690 if (listeners != null && listeners.size() > 0) {
9691 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9692 // perform the dispatching. The iterator is a safe guard against listeners that
9693 // could mutate the list by calling the various add/remove methods. This prevents
9694 // the array from being modified while we iterate it.
9695 for (OnAttachStateChangeListener listener : listeners) {
9696 listener.onViewAttachedToWindow(this);
9697 }
9698 }
9699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009700 int vis = info.mWindowVisibility;
9701 if (vis != GONE) {
9702 onWindowVisibilityChanged(vis);
9703 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009704 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9705 // If nobody has evaluated the drawable state yet, then do it now.
9706 refreshDrawableState();
9707 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009708 }
9709
9710 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009711 AttachInfo info = mAttachInfo;
9712 if (info != null) {
9713 int vis = info.mWindowVisibility;
9714 if (vis != GONE) {
9715 onWindowVisibilityChanged(GONE);
9716 }
9717 }
9718
9719 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009720
Adam Powell4afd62b2011-02-18 15:02:18 -08009721 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9722 mOnAttachStateChangeListeners;
9723 if (listeners != null && listeners.size() > 0) {
9724 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9725 // perform the dispatching. The iterator is a safe guard against listeners that
9726 // could mutate the list by calling the various add/remove methods. This prevents
9727 // the array from being modified while we iterate it.
9728 for (OnAttachStateChangeListener listener : listeners) {
9729 listener.onViewDetachedFromWindow(this);
9730 }
9731 }
9732
Romain Guy01d5edc2011-01-28 11:28:53 -08009733 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009734 mAttachInfo.mScrollContainers.remove(this);
9735 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9736 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009738 mAttachInfo = null;
9739 }
9740
9741 /**
9742 * Store this view hierarchy's frozen state into the given container.
9743 *
9744 * @param container The SparseArray in which to save the view's state.
9745 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009746 * @see #restoreHierarchyState(android.util.SparseArray)
9747 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9748 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009749 */
9750 public void saveHierarchyState(SparseArray<Parcelable> container) {
9751 dispatchSaveInstanceState(container);
9752 }
9753
9754 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009755 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9756 * this view and its children. May be overridden to modify how freezing happens to a
9757 * 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 -08009758 *
9759 * @param container The SparseArray in which to save the view's state.
9760 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009761 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9762 * @see #saveHierarchyState(android.util.SparseArray)
9763 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009764 */
9765 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9766 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9767 mPrivateFlags &= ~SAVE_STATE_CALLED;
9768 Parcelable state = onSaveInstanceState();
9769 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9770 throw new IllegalStateException(
9771 "Derived class did not call super.onSaveInstanceState()");
9772 }
9773 if (state != null) {
9774 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9775 // + ": " + state);
9776 container.put(mID, state);
9777 }
9778 }
9779 }
9780
9781 /**
9782 * Hook allowing a view to generate a representation of its internal state
9783 * that can later be used to create a new instance with that same state.
9784 * This state should only contain information that is not persistent or can
9785 * not be reconstructed later. For example, you will never store your
9786 * current position on screen because that will be computed again when a
9787 * new instance of the view is placed in its view hierarchy.
9788 * <p>
9789 * Some examples of things you may store here: the current cursor position
9790 * in a text view (but usually not the text itself since that is stored in a
9791 * content provider or other persistent storage), the currently selected
9792 * item in a list view.
9793 *
9794 * @return Returns a Parcelable object containing the view's current dynamic
9795 * state, or null if there is nothing interesting to save. The
9796 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009797 * @see #onRestoreInstanceState(android.os.Parcelable)
9798 * @see #saveHierarchyState(android.util.SparseArray)
9799 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009800 * @see #setSaveEnabled(boolean)
9801 */
9802 protected Parcelable onSaveInstanceState() {
9803 mPrivateFlags |= SAVE_STATE_CALLED;
9804 return BaseSavedState.EMPTY_STATE;
9805 }
9806
9807 /**
9808 * Restore this view hierarchy's frozen state from the given container.
9809 *
9810 * @param container The SparseArray which holds previously frozen states.
9811 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009812 * @see #saveHierarchyState(android.util.SparseArray)
9813 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9814 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009815 */
9816 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9817 dispatchRestoreInstanceState(container);
9818 }
9819
9820 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009821 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9822 * state for this view and its children. May be overridden to modify how restoring
9823 * happens to a view's children; for example, some views may want to not store state
9824 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009825 *
9826 * @param container The SparseArray which holds previously saved state.
9827 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009828 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9829 * @see #restoreHierarchyState(android.util.SparseArray)
9830 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009831 */
9832 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9833 if (mID != NO_ID) {
9834 Parcelable state = container.get(mID);
9835 if (state != null) {
9836 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9837 // + ": " + state);
9838 mPrivateFlags &= ~SAVE_STATE_CALLED;
9839 onRestoreInstanceState(state);
9840 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9841 throw new IllegalStateException(
9842 "Derived class did not call super.onRestoreInstanceState()");
9843 }
9844 }
9845 }
9846 }
9847
9848 /**
9849 * Hook allowing a view to re-apply a representation of its internal state that had previously
9850 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9851 * null state.
9852 *
9853 * @param state The frozen state that had previously been returned by
9854 * {@link #onSaveInstanceState}.
9855 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009856 * @see #onSaveInstanceState()
9857 * @see #restoreHierarchyState(android.util.SparseArray)
9858 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009859 */
9860 protected void onRestoreInstanceState(Parcelable state) {
9861 mPrivateFlags |= SAVE_STATE_CALLED;
9862 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009863 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9864 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009865 + "when two views of different type have the same id in the same hierarchy. "
9866 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009867 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009868 }
9869 }
9870
9871 /**
9872 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9873 *
9874 * @return the drawing start time in milliseconds
9875 */
9876 public long getDrawingTime() {
9877 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9878 }
9879
9880 /**
9881 * <p>Enables or disables the duplication of the parent's state into this view. When
9882 * duplication is enabled, this view gets its drawable state from its parent rather
9883 * than from its own internal properties.</p>
9884 *
9885 * <p>Note: in the current implementation, setting this property to true after the
9886 * view was added to a ViewGroup might have no effect at all. This property should
9887 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
9888 *
9889 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
9890 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009891 *
Gilles Debunnefb817032011-01-13 13:52:49 -08009892 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
9893 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009894 *
9895 * @param enabled True to enable duplication of the parent's drawable state, false
9896 * to disable it.
9897 *
9898 * @see #getDrawableState()
9899 * @see #isDuplicateParentStateEnabled()
9900 */
9901 public void setDuplicateParentStateEnabled(boolean enabled) {
9902 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
9903 }
9904
9905 /**
9906 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
9907 *
9908 * @return True if this view's drawable state is duplicated from the parent,
9909 * false otherwise
9910 *
9911 * @see #getDrawableState()
9912 * @see #setDuplicateParentStateEnabled(boolean)
9913 */
9914 public boolean isDuplicateParentStateEnabled() {
9915 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
9916 }
9917
9918 /**
Romain Guy171c5922011-01-06 10:04:23 -08009919 * <p>Specifies the type of layer backing this view. The layer can be
9920 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
9921 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009922 *
Romain Guy171c5922011-01-06 10:04:23 -08009923 * <p>A layer is associated with an optional {@link android.graphics.Paint}
9924 * instance that controls how the layer is composed on screen. The following
9925 * properties of the paint are taken into account when composing the layer:</p>
9926 * <ul>
9927 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
9928 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
9929 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
9930 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08009931 *
Romain Guy171c5922011-01-06 10:04:23 -08009932 * <p>If this view has an alpha value set to < 1.0 by calling
9933 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
9934 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
9935 * equivalent to setting a hardware layer on this view and providing a paint with
9936 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08009937 *
Romain Guy171c5922011-01-06 10:04:23 -08009938 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
9939 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
9940 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009941 *
Romain Guy171c5922011-01-06 10:04:23 -08009942 * @param layerType The ype of layer to use with this view, must be one of
9943 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9944 * {@link #LAYER_TYPE_HARDWARE}
9945 * @param paint The paint used to compose the layer. This argument is optional
9946 * and can be null. It is ignored when the layer type is
9947 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08009948 *
9949 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08009950 * @see #LAYER_TYPE_NONE
9951 * @see #LAYER_TYPE_SOFTWARE
9952 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08009953 * @see #setAlpha(float)
9954 *
Romain Guy171c5922011-01-06 10:04:23 -08009955 * @attr ref android.R.styleable#View_layerType
9956 */
9957 public void setLayerType(int layerType, Paint paint) {
9958 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08009959 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08009960 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
9961 }
Chet Haasedaf98e92011-01-10 14:10:36 -08009962
Romain Guyd6cd5722011-01-17 14:42:41 -08009963 if (layerType == mLayerType) {
9964 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
9965 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009966 invalidateParentCaches();
9967 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08009968 }
9969 return;
9970 }
Romain Guy171c5922011-01-06 10:04:23 -08009971
9972 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08009973 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -07009974 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -07009975 destroyLayer();
Chet Haase6f33e812011-05-17 12:42:19 -07009976 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -08009977 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -07009978 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009979 break;
Romain Guy6c319ca2011-01-11 14:29:25 -08009980 default:
9981 break;
Romain Guy171c5922011-01-06 10:04:23 -08009982 }
9983
9984 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08009985 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
9986 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
9987 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08009988
Romain Guy0fd89bf2011-01-26 15:41:30 -08009989 invalidateParentCaches();
9990 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08009991 }
9992
9993 /**
Romain Guy59c7f802011-09-29 17:21:45 -07009994 * Indicates whether this view has a static layer. A view with layer type
9995 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
9996 * dynamic.
9997 */
9998 boolean hasStaticLayer() {
9999 return mLayerType == LAYER_TYPE_NONE;
10000 }
10001
10002 /**
Romain Guy171c5922011-01-06 10:04:23 -080010003 * Indicates what type of layer is currently associated with this view. By default
10004 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10005 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10006 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010007 *
Romain Guy171c5922011-01-06 10:04:23 -080010008 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10009 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010010 *
10011 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010012 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010013 * @see #LAYER_TYPE_NONE
10014 * @see #LAYER_TYPE_SOFTWARE
10015 * @see #LAYER_TYPE_HARDWARE
10016 */
10017 public int getLayerType() {
10018 return mLayerType;
10019 }
Joe Malin32736f02011-01-19 16:14:20 -080010020
Romain Guy6c319ca2011-01-11 14:29:25 -080010021 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010022 * Forces this view's layer to be created and this view to be rendered
10023 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10024 * invoking this method will have no effect.
10025 *
10026 * This method can for instance be used to render a view into its layer before
10027 * starting an animation. If this view is complex, rendering into the layer
10028 * before starting the animation will avoid skipping frames.
10029 *
10030 * @throws IllegalStateException If this view is not attached to a window
10031 *
10032 * @see #setLayerType(int, android.graphics.Paint)
10033 */
10034 public void buildLayer() {
10035 if (mLayerType == LAYER_TYPE_NONE) return;
10036
10037 if (mAttachInfo == null) {
10038 throw new IllegalStateException("This view must be attached to a window first");
10039 }
10040
10041 switch (mLayerType) {
10042 case LAYER_TYPE_HARDWARE:
10043 getHardwareLayer();
10044 break;
10045 case LAYER_TYPE_SOFTWARE:
10046 buildDrawingCache(true);
10047 break;
10048 }
10049 }
10050
10051 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010052 * <p>Returns a hardware layer that can be used to draw this view again
10053 * without executing its draw method.</p>
10054 *
10055 * @return A HardwareLayer ready to render, or null if an error occurred.
10056 */
Romain Guy5e7f7662011-01-24 22:35:56 -080010057 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010058 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10059 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010060 return null;
10061 }
10062
10063 final int width = mRight - mLeft;
10064 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010065
Romain Guy6c319ca2011-01-11 14:29:25 -080010066 if (width == 0 || height == 0) {
10067 return null;
10068 }
10069
10070 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10071 if (mHardwareLayer == null) {
10072 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10073 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -080010074 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010075 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10076 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -080010077 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010078 }
10079
Romain Guy59a12ca2011-06-09 17:48:21 -070010080 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -080010081 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
10082 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010083 try {
10084 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080010085 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -080010086 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010087
Chet Haase88172fe2011-03-07 17:36:33 -080010088 final int restoreCount = canvas.save();
10089
Romain Guy6c319ca2011-01-11 14:29:25 -080010090 computeScroll();
10091 canvas.translate(-mScrollX, -mScrollY);
10092
Romain Guy6c319ca2011-01-11 14:29:25 -080010093 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -080010094
Romain Guy6c319ca2011-01-11 14:29:25 -080010095 // Fast path for layouts with no backgrounds
10096 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10097 mPrivateFlags &= ~DIRTY_MASK;
10098 dispatchDraw(canvas);
10099 } else {
10100 draw(canvas);
10101 }
Joe Malin32736f02011-01-19 16:14:20 -080010102
Chet Haase88172fe2011-03-07 17:36:33 -080010103 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -080010104 } finally {
10105 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -080010106 mHardwareLayer.end(currentCanvas);
10107 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010108 }
10109 }
10110
10111 return mHardwareLayer;
10112 }
Romain Guy171c5922011-01-06 10:04:23 -080010113
Romain Guy65b345f2011-07-27 18:51:50 -070010114 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -070010115 if (mHardwareLayer != null) {
10116 mHardwareLayer.destroy();
10117 mHardwareLayer = null;
Romain Guy65b345f2011-07-27 18:51:50 -070010118 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010119 }
Romain Guy65b345f2011-07-27 18:51:50 -070010120 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010121 }
10122
Romain Guy171c5922011-01-06 10:04:23 -080010123 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010124 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10125 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10126 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10127 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10128 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10129 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010130 *
Romain Guy171c5922011-01-06 10:04:23 -080010131 * <p>Enabling the drawing cache is similar to
10132 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010133 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10134 * drawing cache has no effect on rendering because the system uses a different mechanism
10135 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10136 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10137 * for information on how to enable software and hardware layers.</p>
10138 *
10139 * <p>This API can be used to manually generate
10140 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10141 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010142 *
10143 * @param enabled true to enable the drawing cache, false otherwise
10144 *
10145 * @see #isDrawingCacheEnabled()
10146 * @see #getDrawingCache()
10147 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010148 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010149 */
10150 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010151 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010152 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10153 }
10154
10155 /**
10156 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10157 *
10158 * @return true if the drawing cache is enabled
10159 *
10160 * @see #setDrawingCacheEnabled(boolean)
10161 * @see #getDrawingCache()
10162 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010163 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010164 public boolean isDrawingCacheEnabled() {
10165 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10166 }
10167
10168 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010169 * Debugging utility which recursively outputs the dirty state of a view and its
10170 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010171 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010172 * @hide
10173 */
Romain Guy676b1732011-02-14 14:45:33 -080010174 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010175 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10176 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10177 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10178 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10179 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10180 if (clear) {
10181 mPrivateFlags &= clearMask;
10182 }
10183 if (this instanceof ViewGroup) {
10184 ViewGroup parent = (ViewGroup) this;
10185 final int count = parent.getChildCount();
10186 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010187 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010188 child.outputDirtyFlags(indent + " ", clear, clearMask);
10189 }
10190 }
10191 }
10192
10193 /**
10194 * This method is used by ViewGroup to cause its children to restore or recreate their
10195 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10196 * to recreate its own display list, which would happen if it went through the normal
10197 * draw/dispatchDraw mechanisms.
10198 *
10199 * @hide
10200 */
10201 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010202
10203 /**
10204 * A view that is not attached or hardware accelerated cannot create a display list.
10205 * This method checks these conditions and returns the appropriate result.
10206 *
10207 * @return true if view has the ability to create a display list, false otherwise.
10208 *
10209 * @hide
10210 */
10211 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010212 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010213 }
Joe Malin32736f02011-01-19 16:14:20 -080010214
Chet Haasedaf98e92011-01-10 14:10:36 -080010215 /**
Romain Guyb051e892010-09-28 19:09:36 -070010216 * <p>Returns a display list that can be used to draw this view again
10217 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010218 *
Romain Guyb051e892010-09-28 19:09:36 -070010219 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010220 *
10221 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010222 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010223 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -080010224 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -070010225 return null;
10226 }
10227
Chet Haasedaf98e92011-01-10 14:10:36 -080010228 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10229 mDisplayList == null || !mDisplayList.isValid() ||
10230 mRecreateDisplayList)) {
10231 // Don't need to recreate the display list, just need to tell our
10232 // children to restore/recreate theirs
10233 if (mDisplayList != null && mDisplayList.isValid() &&
10234 !mRecreateDisplayList) {
10235 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10236 mPrivateFlags &= ~DIRTY_MASK;
10237 dispatchGetDisplayList();
10238
10239 return mDisplayList;
10240 }
10241
10242 // If we got here, we're recreating it. Mark it as such to ensure that
10243 // we copy in child display lists into ours in drawChild()
10244 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -080010245 if (mDisplayList == null) {
Jeff Brown162a0212011-07-21 17:02:54 -070010246 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList();
Chet Haasedaf98e92011-01-10 14:10:36 -080010247 // If we're creating a new display list, make sure our parent gets invalidated
10248 // since they will need to recreate their display list to account for this
10249 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -080010250 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -080010251 }
Romain Guyb051e892010-09-28 19:09:36 -070010252
10253 final HardwareCanvas canvas = mDisplayList.start();
Romain Guye080af32011-09-08 15:03:39 -070010254 int restoreCount = 0;
Romain Guyb051e892010-09-28 19:09:36 -070010255 try {
10256 int width = mRight - mLeft;
10257 int height = mBottom - mTop;
10258
10259 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -080010260 // The dirty rect should always be null for a display list
10261 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -070010262
Chet Haasedaf98e92011-01-10 14:10:36 -080010263 computeScroll();
Romain Guye080af32011-09-08 15:03:39 -070010264
10265 restoreCount = canvas.save();
Chet Haasedaf98e92011-01-10 14:10:36 -080010266 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010267 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -070010268 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -080010269
Romain Guyb051e892010-09-28 19:09:36 -070010270 // Fast path for layouts with no backgrounds
10271 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -070010272 dispatchDraw(canvas);
10273 } else {
10274 draw(canvas);
10275 }
Romain Guyb051e892010-09-28 19:09:36 -070010276 } finally {
Romain Guye080af32011-09-08 15:03:39 -070010277 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -070010278 canvas.onPostDraw();
10279
10280 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -070010281 }
Chet Haase77785f92011-01-25 23:22:09 -080010282 } else {
10283 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10284 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -070010285 }
10286
10287 return mDisplayList;
10288 }
10289
10290 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010291 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010292 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010293 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010294 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010295 * @see #getDrawingCache(boolean)
10296 */
10297 public Bitmap getDrawingCache() {
10298 return getDrawingCache(false);
10299 }
10300
10301 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010302 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10303 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10304 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10305 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10306 * request the drawing cache by calling this method and draw it on screen if the
10307 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010308 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010309 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10310 * this method will create a bitmap of the same size as this view. Because this bitmap
10311 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10312 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10313 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10314 * size than the view. This implies that your application must be able to handle this
10315 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010316 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010317 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10318 * the current density of the screen when the application is in compatibility
10319 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010320 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010321 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010322 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010323 * @see #setDrawingCacheEnabled(boolean)
10324 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010325 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010326 * @see #destroyDrawingCache()
10327 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010328 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010329 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10330 return null;
10331 }
10332 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010333 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010334 }
Romain Guy02890fd2010-08-06 17:58:44 -070010335 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010336 }
10337
10338 /**
10339 * <p>Frees the resources used by the drawing cache. If you call
10340 * {@link #buildDrawingCache()} manually without calling
10341 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10342 * should cleanup the cache with this method afterwards.</p>
10343 *
10344 * @see #setDrawingCacheEnabled(boolean)
10345 * @see #buildDrawingCache()
10346 * @see #getDrawingCache()
10347 */
10348 public void destroyDrawingCache() {
10349 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010350 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010351 mDrawingCache = null;
10352 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010353 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010354 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010355 mUnscaledDrawingCache = null;
10356 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010357 }
10358
10359 /**
10360 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070010361 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010362 * view will always be drawn on top of a solid color.
10363 *
10364 * @param color The background color to use for the drawing cache's bitmap
10365 *
10366 * @see #setDrawingCacheEnabled(boolean)
10367 * @see #buildDrawingCache()
10368 * @see #getDrawingCache()
10369 */
10370 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010371 if (color != mDrawingCacheBackgroundColor) {
10372 mDrawingCacheBackgroundColor = color;
10373 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010375 }
10376
10377 /**
10378 * @see #setDrawingCacheBackgroundColor(int)
10379 *
10380 * @return The background color to used for the drawing cache's bitmap
10381 */
10382 public int getDrawingCacheBackgroundColor() {
10383 return mDrawingCacheBackgroundColor;
10384 }
10385
10386 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010387 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010388 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010389 * @see #buildDrawingCache(boolean)
10390 */
10391 public void buildDrawingCache() {
10392 buildDrawingCache(false);
10393 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010394
Romain Guyfbd8f692009-06-26 14:51:58 -070010395 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010396 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10397 *
10398 * <p>If you call {@link #buildDrawingCache()} manually without calling
10399 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10400 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010401 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010402 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10403 * this method will create a bitmap of the same size as this view. Because this bitmap
10404 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10405 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10406 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10407 * size than the view. This implies that your application must be able to handle this
10408 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010409 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010410 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10411 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010412 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010413 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010414 *
10415 * @see #getDrawingCache()
10416 * @see #destroyDrawingCache()
10417 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010418 public void buildDrawingCache(boolean autoScale) {
10419 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010420 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010421 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010422
10423 if (ViewDebug.TRACE_HIERARCHY) {
10424 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10425 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010426
Romain Guy8506ab42009-06-11 17:35:47 -070010427 int width = mRight - mLeft;
10428 int height = mBottom - mTop;
10429
10430 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010431 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010432
Romain Guye1123222009-06-29 14:24:56 -070010433 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010434 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10435 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010437
10438 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010439 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010440 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010441
10442 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010443 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010444 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010445 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10446 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010447 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010448 return;
10449 }
10450
10451 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010452 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010453
10454 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010455 Bitmap.Config quality;
10456 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010457 // Never pick ARGB_4444 because it looks awful
10458 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010459 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10460 case DRAWING_CACHE_QUALITY_AUTO:
10461 quality = Bitmap.Config.ARGB_8888;
10462 break;
10463 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010464 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010465 break;
10466 case DRAWING_CACHE_QUALITY_HIGH:
10467 quality = Bitmap.Config.ARGB_8888;
10468 break;
10469 default:
10470 quality = Bitmap.Config.ARGB_8888;
10471 break;
10472 }
10473 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010474 // Optimization for translucent windows
10475 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010476 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010477 }
10478
10479 // Try to cleanup memory
10480 if (bitmap != null) bitmap.recycle();
10481
10482 try {
10483 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010484 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010485 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010486 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010487 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010488 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010489 }
Adam Powell26153a32010-11-08 15:22:27 -080010490 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010491 } catch (OutOfMemoryError e) {
10492 // If there is not enough memory to create the bitmap cache, just
10493 // ignore the issue as bitmap caches are not required to draw the
10494 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010495 if (autoScale) {
10496 mDrawingCache = null;
10497 } else {
10498 mUnscaledDrawingCache = null;
10499 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010500 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010501 return;
10502 }
10503
10504 clear = drawingCacheBackgroundColor != 0;
10505 }
10506
10507 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010508 if (attachInfo != null) {
10509 canvas = attachInfo.mCanvas;
10510 if (canvas == null) {
10511 canvas = new Canvas();
10512 }
10513 canvas.setBitmap(bitmap);
10514 // Temporarily clobber the cached Canvas in case one of our children
10515 // is also using a drawing cache. Without this, the children would
10516 // steal the canvas by attaching their own bitmap to it and bad, bad
10517 // thing would happen (invisible views, corrupted drawings, etc.)
10518 attachInfo.mCanvas = null;
10519 } else {
10520 // This case should hopefully never or seldom happen
10521 canvas = new Canvas(bitmap);
10522 }
10523
10524 if (clear) {
10525 bitmap.eraseColor(drawingCacheBackgroundColor);
10526 }
10527
10528 computeScroll();
10529 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010530
Romain Guye1123222009-06-29 14:24:56 -070010531 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010532 final float scale = attachInfo.mApplicationScale;
10533 canvas.scale(scale, scale);
10534 }
Joe Malin32736f02011-01-19 16:14:20 -080010535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010536 canvas.translate(-mScrollX, -mScrollY);
10537
Romain Guy5bcdff42009-05-14 21:27:18 -070010538 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010539 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10540 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010541 mPrivateFlags |= DRAWING_CACHE_VALID;
10542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010543
10544 // Fast path for layouts with no backgrounds
10545 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10546 if (ViewDebug.TRACE_HIERARCHY) {
10547 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10548 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010549 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010550 dispatchDraw(canvas);
10551 } else {
10552 draw(canvas);
10553 }
10554
10555 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010556 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010557
10558 if (attachInfo != null) {
10559 // Restore the cached Canvas for our siblings
10560 attachInfo.mCanvas = canvas;
10561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010562 }
10563 }
10564
10565 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010566 * Create a snapshot of the view into a bitmap. We should probably make
10567 * some form of this public, but should think about the API.
10568 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010569 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010570 int width = mRight - mLeft;
10571 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010572
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010573 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010574 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010575 width = (int) ((width * scale) + 0.5f);
10576 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010577
Romain Guy8c11e312009-09-14 15:15:30 -070010578 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010579 if (bitmap == null) {
10580 throw new OutOfMemoryError();
10581 }
10582
Romain Guyc529d8d2011-09-06 15:01:39 -070010583 Resources resources = getResources();
10584 if (resources != null) {
10585 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
10586 }
Joe Malin32736f02011-01-19 16:14:20 -080010587
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010588 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010589 if (attachInfo != null) {
10590 canvas = attachInfo.mCanvas;
10591 if (canvas == null) {
10592 canvas = new Canvas();
10593 }
10594 canvas.setBitmap(bitmap);
10595 // Temporarily clobber the cached Canvas in case one of our children
10596 // is also using a drawing cache. Without this, the children would
10597 // steal the canvas by attaching their own bitmap to it and bad, bad
10598 // things would happen (invisible views, corrupted drawings, etc.)
10599 attachInfo.mCanvas = null;
10600 } else {
10601 // This case should hopefully never or seldom happen
10602 canvas = new Canvas(bitmap);
10603 }
10604
Romain Guy5bcdff42009-05-14 21:27:18 -070010605 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010606 bitmap.eraseColor(backgroundColor);
10607 }
10608
10609 computeScroll();
10610 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010611 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010612 canvas.translate(-mScrollX, -mScrollY);
10613
Romain Guy5bcdff42009-05-14 21:27:18 -070010614 // Temporarily remove the dirty mask
10615 int flags = mPrivateFlags;
10616 mPrivateFlags &= ~DIRTY_MASK;
10617
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010618 // Fast path for layouts with no backgrounds
10619 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10620 dispatchDraw(canvas);
10621 } else {
10622 draw(canvas);
10623 }
10624
Romain Guy5bcdff42009-05-14 21:27:18 -070010625 mPrivateFlags = flags;
10626
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010627 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010628 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010629
10630 if (attachInfo != null) {
10631 // Restore the cached Canvas for our siblings
10632 attachInfo.mCanvas = canvas;
10633 }
Romain Guy8506ab42009-06-11 17:35:47 -070010634
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010635 return bitmap;
10636 }
10637
10638 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010639 * Indicates whether this View is currently in edit mode. A View is usually
10640 * in edit mode when displayed within a developer tool. For instance, if
10641 * this View is being drawn by a visual user interface builder, this method
10642 * should return true.
10643 *
10644 * Subclasses should check the return value of this method to provide
10645 * different behaviors if their normal behavior might interfere with the
10646 * host environment. For instance: the class spawns a thread in its
10647 * constructor, the drawing code relies on device-specific features, etc.
10648 *
10649 * This method is usually checked in the drawing code of custom widgets.
10650 *
10651 * @return True if this View is in edit mode, false otherwise.
10652 */
10653 public boolean isInEditMode() {
10654 return false;
10655 }
10656
10657 /**
10658 * If the View draws content inside its padding and enables fading edges,
10659 * it needs to support padding offsets. Padding offsets are added to the
10660 * fading edges to extend the length of the fade so that it covers pixels
10661 * drawn inside the padding.
10662 *
10663 * Subclasses of this class should override this method if they need
10664 * to draw content inside the padding.
10665 *
10666 * @return True if padding offset must be applied, false otherwise.
10667 *
10668 * @see #getLeftPaddingOffset()
10669 * @see #getRightPaddingOffset()
10670 * @see #getTopPaddingOffset()
10671 * @see #getBottomPaddingOffset()
10672 *
10673 * @since CURRENT
10674 */
10675 protected boolean isPaddingOffsetRequired() {
10676 return false;
10677 }
10678
10679 /**
10680 * Amount by which to extend the left fading region. Called only when
10681 * {@link #isPaddingOffsetRequired()} returns true.
10682 *
10683 * @return The left padding offset in pixels.
10684 *
10685 * @see #isPaddingOffsetRequired()
10686 *
10687 * @since CURRENT
10688 */
10689 protected int getLeftPaddingOffset() {
10690 return 0;
10691 }
10692
10693 /**
10694 * Amount by which to extend the right fading region. Called only when
10695 * {@link #isPaddingOffsetRequired()} returns true.
10696 *
10697 * @return The right padding offset in pixels.
10698 *
10699 * @see #isPaddingOffsetRequired()
10700 *
10701 * @since CURRENT
10702 */
10703 protected int getRightPaddingOffset() {
10704 return 0;
10705 }
10706
10707 /**
10708 * Amount by which to extend the top fading region. Called only when
10709 * {@link #isPaddingOffsetRequired()} returns true.
10710 *
10711 * @return The top padding offset in pixels.
10712 *
10713 * @see #isPaddingOffsetRequired()
10714 *
10715 * @since CURRENT
10716 */
10717 protected int getTopPaddingOffset() {
10718 return 0;
10719 }
10720
10721 /**
10722 * Amount by which to extend the bottom fading region. Called only when
10723 * {@link #isPaddingOffsetRequired()} returns true.
10724 *
10725 * @return The bottom padding offset in pixels.
10726 *
10727 * @see #isPaddingOffsetRequired()
10728 *
10729 * @since CURRENT
10730 */
10731 protected int getBottomPaddingOffset() {
10732 return 0;
10733 }
10734
10735 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070010736 * @hide
10737 * @param offsetRequired
10738 */
10739 protected int getFadeTop(boolean offsetRequired) {
10740 int top = mPaddingTop;
10741 if (offsetRequired) top += getTopPaddingOffset();
10742 return top;
10743 }
10744
10745 /**
10746 * @hide
10747 * @param offsetRequired
10748 */
10749 protected int getFadeHeight(boolean offsetRequired) {
10750 int padding = mPaddingTop;
10751 if (offsetRequired) padding += getTopPaddingOffset();
10752 return mBottom - mTop - mPaddingBottom - padding;
10753 }
10754
10755 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010756 * <p>Indicates whether this view is attached to an hardware accelerated
10757 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010758 *
Romain Guy2bffd262010-09-12 17:40:02 -070010759 * <p>Even if this method returns true, it does not mean that every call
10760 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10761 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10762 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10763 * window is hardware accelerated,
10764 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10765 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010766 *
Romain Guy2bffd262010-09-12 17:40:02 -070010767 * @return True if the view is attached to a window and the window is
10768 * hardware accelerated; false in any other case.
10769 */
10770 public boolean isHardwareAccelerated() {
10771 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10772 }
Joe Malin32736f02011-01-19 16:14:20 -080010773
Romain Guy2bffd262010-09-12 17:40:02 -070010774 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010775 * Manually render this view (and all of its children) to the given Canvas.
10776 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010777 * called. When implementing a view, implement
10778 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
10779 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010780 *
10781 * @param canvas The Canvas to which the View is rendered.
10782 */
10783 public void draw(Canvas canvas) {
10784 if (ViewDebug.TRACE_HIERARCHY) {
10785 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10786 }
10787
Romain Guy5bcdff42009-05-14 21:27:18 -070010788 final int privateFlags = mPrivateFlags;
10789 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
10790 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
10791 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070010792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010793 /*
10794 * Draw traversal performs several drawing steps which must be executed
10795 * in the appropriate order:
10796 *
10797 * 1. Draw the background
10798 * 2. If necessary, save the canvas' layers to prepare for fading
10799 * 3. Draw view's content
10800 * 4. Draw children
10801 * 5. If necessary, draw the fading edges and restore layers
10802 * 6. Draw decorations (scrollbars for instance)
10803 */
10804
10805 // Step 1, draw the background, if needed
10806 int saveCount;
10807
Romain Guy24443ea2009-05-11 11:56:30 -070010808 if (!dirtyOpaque) {
10809 final Drawable background = mBGDrawable;
10810 if (background != null) {
10811 final int scrollX = mScrollX;
10812 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010813
Romain Guy24443ea2009-05-11 11:56:30 -070010814 if (mBackgroundSizeChanged) {
10815 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
10816 mBackgroundSizeChanged = false;
10817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010818
Romain Guy24443ea2009-05-11 11:56:30 -070010819 if ((scrollX | scrollY) == 0) {
10820 background.draw(canvas);
10821 } else {
10822 canvas.translate(scrollX, scrollY);
10823 background.draw(canvas);
10824 canvas.translate(-scrollX, -scrollY);
10825 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010826 }
10827 }
10828
10829 // skip step 2 & 5 if possible (common case)
10830 final int viewFlags = mViewFlags;
10831 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
10832 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
10833 if (!verticalEdges && !horizontalEdges) {
10834 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010835 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010836
10837 // Step 4, draw the children
10838 dispatchDraw(canvas);
10839
10840 // Step 6, draw decorations (scrollbars)
10841 onDrawScrollBars(canvas);
10842
10843 // we're done...
10844 return;
10845 }
10846
10847 /*
10848 * Here we do the full fledged routine...
10849 * (this is an uncommon case where speed matters less,
10850 * this is why we repeat some of the tests that have been
10851 * done above)
10852 */
10853
10854 boolean drawTop = false;
10855 boolean drawBottom = false;
10856 boolean drawLeft = false;
10857 boolean drawRight = false;
10858
10859 float topFadeStrength = 0.0f;
10860 float bottomFadeStrength = 0.0f;
10861 float leftFadeStrength = 0.0f;
10862 float rightFadeStrength = 0.0f;
10863
10864 // Step 2, save the canvas' layers
10865 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010866
10867 final boolean offsetRequired = isPaddingOffsetRequired();
10868 if (offsetRequired) {
10869 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010870 }
10871
10872 int left = mScrollX + paddingLeft;
10873 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070010874 int top = mScrollY + getFadeTop(offsetRequired);
10875 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010876
10877 if (offsetRequired) {
10878 right += getRightPaddingOffset();
10879 bottom += getBottomPaddingOffset();
10880 }
10881
10882 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010883 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
10884 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010885
10886 // clip the fade length if top and bottom fades overlap
10887 // overlapping fades produce odd-looking artifacts
10888 if (verticalEdges && (top + length > bottom - length)) {
10889 length = (bottom - top) / 2;
10890 }
10891
10892 // also clip horizontal fades if necessary
10893 if (horizontalEdges && (left + length > right - length)) {
10894 length = (right - left) / 2;
10895 }
10896
10897 if (verticalEdges) {
10898 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010899 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010900 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010901 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010902 }
10903
10904 if (horizontalEdges) {
10905 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010906 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010907 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010908 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010909 }
10910
10911 saveCount = canvas.getSaveCount();
10912
10913 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070010914 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010915 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
10916
10917 if (drawTop) {
10918 canvas.saveLayer(left, top, right, top + length, null, flags);
10919 }
10920
10921 if (drawBottom) {
10922 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
10923 }
10924
10925 if (drawLeft) {
10926 canvas.saveLayer(left, top, left + length, bottom, null, flags);
10927 }
10928
10929 if (drawRight) {
10930 canvas.saveLayer(right - length, top, right, bottom, null, flags);
10931 }
10932 } else {
10933 scrollabilityCache.setFadeColor(solidColor);
10934 }
10935
10936 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010937 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010938
10939 // Step 4, draw the children
10940 dispatchDraw(canvas);
10941
10942 // Step 5, draw the fade effect and restore layers
10943 final Paint p = scrollabilityCache.paint;
10944 final Matrix matrix = scrollabilityCache.matrix;
10945 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010946
10947 if (drawTop) {
10948 matrix.setScale(1, fadeHeight * topFadeStrength);
10949 matrix.postTranslate(left, top);
10950 fade.setLocalMatrix(matrix);
10951 canvas.drawRect(left, top, right, top + length, p);
10952 }
10953
10954 if (drawBottom) {
10955 matrix.setScale(1, fadeHeight * bottomFadeStrength);
10956 matrix.postRotate(180);
10957 matrix.postTranslate(left, bottom);
10958 fade.setLocalMatrix(matrix);
10959 canvas.drawRect(left, bottom - length, right, bottom, p);
10960 }
10961
10962 if (drawLeft) {
10963 matrix.setScale(1, fadeHeight * leftFadeStrength);
10964 matrix.postRotate(-90);
10965 matrix.postTranslate(left, top);
10966 fade.setLocalMatrix(matrix);
10967 canvas.drawRect(left, top, left + length, bottom, p);
10968 }
10969
10970 if (drawRight) {
10971 matrix.setScale(1, fadeHeight * rightFadeStrength);
10972 matrix.postRotate(90);
10973 matrix.postTranslate(right, top);
10974 fade.setLocalMatrix(matrix);
10975 canvas.drawRect(right - length, top, right, bottom, p);
10976 }
10977
10978 canvas.restoreToCount(saveCount);
10979
10980 // Step 6, draw decorations (scrollbars)
10981 onDrawScrollBars(canvas);
10982 }
10983
10984 /**
10985 * Override this if your view is known to always be drawn on top of a solid color background,
10986 * and needs to draw fading edges. Returning a non-zero color enables the view system to
10987 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
10988 * should be set to 0xFF.
10989 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010990 * @see #setVerticalFadingEdgeEnabled(boolean)
10991 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010992 *
10993 * @return The known solid color background for this view, or 0 if the color may vary
10994 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010995 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010996 public int getSolidColor() {
10997 return 0;
10998 }
10999
11000 /**
11001 * Build a human readable string representation of the specified view flags.
11002 *
11003 * @param flags the view flags to convert to a string
11004 * @return a String representing the supplied flags
11005 */
11006 private static String printFlags(int flags) {
11007 String output = "";
11008 int numFlags = 0;
11009 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
11010 output += "TAKES_FOCUS";
11011 numFlags++;
11012 }
11013
11014 switch (flags & VISIBILITY_MASK) {
11015 case INVISIBLE:
11016 if (numFlags > 0) {
11017 output += " ";
11018 }
11019 output += "INVISIBLE";
11020 // USELESS HERE numFlags++;
11021 break;
11022 case GONE:
11023 if (numFlags > 0) {
11024 output += " ";
11025 }
11026 output += "GONE";
11027 // USELESS HERE numFlags++;
11028 break;
11029 default:
11030 break;
11031 }
11032 return output;
11033 }
11034
11035 /**
11036 * Build a human readable string representation of the specified private
11037 * view flags.
11038 *
11039 * @param privateFlags the private view flags to convert to a string
11040 * @return a String representing the supplied flags
11041 */
11042 private static String printPrivateFlags(int privateFlags) {
11043 String output = "";
11044 int numFlags = 0;
11045
11046 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
11047 output += "WANTS_FOCUS";
11048 numFlags++;
11049 }
11050
11051 if ((privateFlags & FOCUSED) == FOCUSED) {
11052 if (numFlags > 0) {
11053 output += " ";
11054 }
11055 output += "FOCUSED";
11056 numFlags++;
11057 }
11058
11059 if ((privateFlags & SELECTED) == SELECTED) {
11060 if (numFlags > 0) {
11061 output += " ";
11062 }
11063 output += "SELECTED";
11064 numFlags++;
11065 }
11066
11067 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
11068 if (numFlags > 0) {
11069 output += " ";
11070 }
11071 output += "IS_ROOT_NAMESPACE";
11072 numFlags++;
11073 }
11074
11075 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
11076 if (numFlags > 0) {
11077 output += " ";
11078 }
11079 output += "HAS_BOUNDS";
11080 numFlags++;
11081 }
11082
11083 if ((privateFlags & DRAWN) == DRAWN) {
11084 if (numFlags > 0) {
11085 output += " ";
11086 }
11087 output += "DRAWN";
11088 // USELESS HERE numFlags++;
11089 }
11090 return output;
11091 }
11092
11093 /**
11094 * <p>Indicates whether or not this view's layout will be requested during
11095 * the next hierarchy layout pass.</p>
11096 *
11097 * @return true if the layout will be forced during next layout pass
11098 */
11099 public boolean isLayoutRequested() {
11100 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
11101 }
11102
11103 /**
11104 * Assign a size and position to a view and all of its
11105 * descendants
11106 *
11107 * <p>This is the second phase of the layout mechanism.
11108 * (The first is measuring). In this phase, each parent calls
11109 * layout on all of its children to position them.
11110 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080011111 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011112 *
Chet Haase9c087442011-01-12 16:20:16 -080011113 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011114 * Derived classes with children should override
11115 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080011116 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011117 *
11118 * @param l Left position, relative to parent
11119 * @param t Top position, relative to parent
11120 * @param r Right position, relative to parent
11121 * @param b Bottom position, relative to parent
11122 */
Romain Guy5429e1d2010-09-07 12:38:00 -070011123 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080011124 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070011125 int oldL = mLeft;
11126 int oldT = mTop;
11127 int oldB = mBottom;
11128 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011129 boolean changed = setFrame(l, t, r, b);
11130 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
11131 if (ViewDebug.TRACE_HIERARCHY) {
11132 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
11133 }
11134
11135 onLayout(changed, l, t, r, b);
11136 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070011137
11138 if (mOnLayoutChangeListeners != null) {
11139 ArrayList<OnLayoutChangeListener> listenersCopy =
11140 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
11141 int numListeners = listenersCopy.size();
11142 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070011143 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070011144 }
11145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011146 }
11147 mPrivateFlags &= ~FORCE_LAYOUT;
11148 }
11149
11150 /**
11151 * Called from layout when this view should
11152 * assign a size and position to each of its children.
11153 *
11154 * Derived classes with children should override
11155 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070011156 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011157 * @param changed This is a new size or position for this view
11158 * @param left Left position, relative to parent
11159 * @param top Top position, relative to parent
11160 * @param right Right position, relative to parent
11161 * @param bottom Bottom position, relative to parent
11162 */
11163 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
11164 }
11165
11166 /**
11167 * Assign a size and position to this view.
11168 *
11169 * This is called from layout.
11170 *
11171 * @param left Left position, relative to parent
11172 * @param top Top position, relative to parent
11173 * @param right Right position, relative to parent
11174 * @param bottom Bottom position, relative to parent
11175 * @return true if the new size and position are different than the
11176 * previous ones
11177 * {@hide}
11178 */
11179 protected boolean setFrame(int left, int top, int right, int bottom) {
11180 boolean changed = false;
11181
11182 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070011183 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011184 + right + "," + bottom + ")");
11185 }
11186
11187 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
11188 changed = true;
11189
11190 // Remember our drawn bit
11191 int drawn = mPrivateFlags & DRAWN;
11192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011193 int oldWidth = mRight - mLeft;
11194 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070011195 int newWidth = right - left;
11196 int newHeight = bottom - top;
11197 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
11198
11199 // Invalidate our old position
11200 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011201
11202 mLeft = left;
11203 mTop = top;
11204 mRight = right;
11205 mBottom = bottom;
11206
11207 mPrivateFlags |= HAS_BOUNDS;
11208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011209
Chet Haase75755e22011-07-18 17:48:25 -070011210 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011211 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
11212 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070011213 if (mTransformationInfo != null) {
11214 mTransformationInfo.mMatrixDirty = true;
11215 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011217 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
11218 }
11219
11220 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
11221 // If we are visible, force the DRAWN bit to on so that
11222 // this invalidate will go through (at least to our parent).
11223 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011224 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011225 // the DRAWN bit.
11226 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070011227 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080011228 // parent display list may need to be recreated based on a change in the bounds
11229 // of any child
11230 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011231 }
11232
11233 // Reset drawn bit to original value (invalidate turns it off)
11234 mPrivateFlags |= drawn;
11235
11236 mBackgroundSizeChanged = true;
11237 }
11238 return changed;
11239 }
11240
11241 /**
11242 * Finalize inflating a view from XML. This is called as the last phase
11243 * of inflation, after all child views have been added.
11244 *
11245 * <p>Even if the subclass overrides onFinishInflate, they should always be
11246 * sure to call the super method, so that we get called.
11247 */
11248 protected void onFinishInflate() {
11249 }
11250
11251 /**
11252 * Returns the resources associated with this view.
11253 *
11254 * @return Resources object.
11255 */
11256 public Resources getResources() {
11257 return mResources;
11258 }
11259
11260 /**
11261 * Invalidates the specified Drawable.
11262 *
11263 * @param drawable the drawable to invalidate
11264 */
11265 public void invalidateDrawable(Drawable drawable) {
11266 if (verifyDrawable(drawable)) {
11267 final Rect dirty = drawable.getBounds();
11268 final int scrollX = mScrollX;
11269 final int scrollY = mScrollY;
11270
11271 invalidate(dirty.left + scrollX, dirty.top + scrollY,
11272 dirty.right + scrollX, dirty.bottom + scrollY);
11273 }
11274 }
11275
11276 /**
11277 * Schedules an action on a drawable to occur at a specified time.
11278 *
11279 * @param who the recipient of the action
11280 * @param what the action to run on the drawable
11281 * @param when the time at which the action must occur. Uses the
11282 * {@link SystemClock#uptimeMillis} timebase.
11283 */
11284 public void scheduleDrawable(Drawable who, Runnable what, long when) {
11285 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
11286 mAttachInfo.mHandler.postAtTime(what, who, when);
11287 }
11288 }
11289
11290 /**
11291 * Cancels a scheduled action on a drawable.
11292 *
11293 * @param who the recipient of the action
11294 * @param what the action to cancel
11295 */
11296 public void unscheduleDrawable(Drawable who, Runnable what) {
11297 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
11298 mAttachInfo.mHandler.removeCallbacks(what, who);
11299 }
11300 }
11301
11302 /**
11303 * Unschedule any events associated with the given Drawable. This can be
11304 * used when selecting a new Drawable into a view, so that the previous
11305 * one is completely unscheduled.
11306 *
11307 * @param who The Drawable to unschedule.
11308 *
11309 * @see #drawableStateChanged
11310 */
11311 public void unscheduleDrawable(Drawable who) {
11312 if (mAttachInfo != null) {
11313 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
11314 }
11315 }
11316
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011317 /**
11318 * Return the layout direction of a given Drawable.
11319 *
11320 * @param who the Drawable to query
11321 *
11322 * @hide
11323 */
11324 public int getResolvedLayoutDirection(Drawable who) {
11325 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070011326 }
11327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011328 /**
11329 * If your view subclass is displaying its own Drawable objects, it should
11330 * override this function and return true for any Drawable it is
11331 * displaying. This allows animations for those drawables to be
11332 * scheduled.
11333 *
11334 * <p>Be sure to call through to the super class when overriding this
11335 * function.
11336 *
11337 * @param who The Drawable to verify. Return true if it is one you are
11338 * displaying, else return the result of calling through to the
11339 * super class.
11340 *
11341 * @return boolean If true than the Drawable is being displayed in the
11342 * view; else false and it is not allowed to animate.
11343 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011344 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
11345 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011346 */
11347 protected boolean verifyDrawable(Drawable who) {
11348 return who == mBGDrawable;
11349 }
11350
11351 /**
11352 * This function is called whenever the state of the view changes in such
11353 * a way that it impacts the state of drawables being shown.
11354 *
11355 * <p>Be sure to call through to the superclass when overriding this
11356 * function.
11357 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011358 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011359 */
11360 protected void drawableStateChanged() {
11361 Drawable d = mBGDrawable;
11362 if (d != null && d.isStateful()) {
11363 d.setState(getDrawableState());
11364 }
11365 }
11366
11367 /**
11368 * Call this to force a view to update its drawable state. This will cause
11369 * drawableStateChanged to be called on this view. Views that are interested
11370 * in the new state should call getDrawableState.
11371 *
11372 * @see #drawableStateChanged
11373 * @see #getDrawableState
11374 */
11375 public void refreshDrawableState() {
11376 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
11377 drawableStateChanged();
11378
11379 ViewParent parent = mParent;
11380 if (parent != null) {
11381 parent.childDrawableStateChanged(this);
11382 }
11383 }
11384
11385 /**
11386 * Return an array of resource IDs of the drawable states representing the
11387 * current state of the view.
11388 *
11389 * @return The current drawable state
11390 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011391 * @see Drawable#setState(int[])
11392 * @see #drawableStateChanged()
11393 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011394 */
11395 public final int[] getDrawableState() {
11396 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
11397 return mDrawableState;
11398 } else {
11399 mDrawableState = onCreateDrawableState(0);
11400 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
11401 return mDrawableState;
11402 }
11403 }
11404
11405 /**
11406 * Generate the new {@link android.graphics.drawable.Drawable} state for
11407 * this view. This is called by the view
11408 * system when the cached Drawable state is determined to be invalid. To
11409 * retrieve the current state, you should use {@link #getDrawableState}.
11410 *
11411 * @param extraSpace if non-zero, this is the number of extra entries you
11412 * would like in the returned array in which you can place your own
11413 * states.
11414 *
11415 * @return Returns an array holding the current {@link Drawable} state of
11416 * the view.
11417 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011418 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011419 */
11420 protected int[] onCreateDrawableState(int extraSpace) {
11421 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
11422 mParent instanceof View) {
11423 return ((View) mParent).onCreateDrawableState(extraSpace);
11424 }
11425
11426 int[] drawableState;
11427
11428 int privateFlags = mPrivateFlags;
11429
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011430 int viewStateIndex = 0;
11431 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
11432 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
11433 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070011434 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011435 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
11436 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070011437 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
11438 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011439 // This is set if HW acceleration is requested, even if the current
11440 // process doesn't allow it. This is just to allow app preview
11441 // windows to better match their app.
11442 viewStateIndex |= VIEW_STATE_ACCELERATED;
11443 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070011444 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011445
Christopher Tate3d4bf172011-03-28 16:16:46 -070011446 final int privateFlags2 = mPrivateFlags2;
11447 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
11448 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
11449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011450 drawableState = VIEW_STATE_SETS[viewStateIndex];
11451
11452 //noinspection ConstantIfStatement
11453 if (false) {
11454 Log.i("View", "drawableStateIndex=" + viewStateIndex);
11455 Log.i("View", toString()
11456 + " pressed=" + ((privateFlags & PRESSED) != 0)
11457 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
11458 + " fo=" + hasFocus()
11459 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011460 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011461 + ": " + Arrays.toString(drawableState));
11462 }
11463
11464 if (extraSpace == 0) {
11465 return drawableState;
11466 }
11467
11468 final int[] fullState;
11469 if (drawableState != null) {
11470 fullState = new int[drawableState.length + extraSpace];
11471 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
11472 } else {
11473 fullState = new int[extraSpace];
11474 }
11475
11476 return fullState;
11477 }
11478
11479 /**
11480 * Merge your own state values in <var>additionalState</var> into the base
11481 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011482 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011483 *
11484 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011485 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011486 * own additional state values.
11487 *
11488 * @param additionalState The additional state values you would like
11489 * added to <var>baseState</var>; this array is not modified.
11490 *
11491 * @return As a convenience, the <var>baseState</var> array you originally
11492 * passed into the function is returned.
11493 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011494 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011495 */
11496 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
11497 final int N = baseState.length;
11498 int i = N - 1;
11499 while (i >= 0 && baseState[i] == 0) {
11500 i--;
11501 }
11502 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
11503 return baseState;
11504 }
11505
11506 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070011507 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
11508 * on all Drawable objects associated with this view.
11509 */
11510 public void jumpDrawablesToCurrentState() {
11511 if (mBGDrawable != null) {
11512 mBGDrawable.jumpToCurrentState();
11513 }
11514 }
11515
11516 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011517 * Sets the background color for this view.
11518 * @param color the color of the background
11519 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011520 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011521 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070011522 if (mBGDrawable instanceof ColorDrawable) {
11523 ((ColorDrawable) mBGDrawable).setColor(color);
11524 } else {
11525 setBackgroundDrawable(new ColorDrawable(color));
11526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011527 }
11528
11529 /**
11530 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070011531 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011532 * @param resid The identifier of the resource.
11533 * @attr ref android.R.styleable#View_background
11534 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011535 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011536 public void setBackgroundResource(int resid) {
11537 if (resid != 0 && resid == mBackgroundResource) {
11538 return;
11539 }
11540
11541 Drawable d= null;
11542 if (resid != 0) {
11543 d = mResources.getDrawable(resid);
11544 }
11545 setBackgroundDrawable(d);
11546
11547 mBackgroundResource = resid;
11548 }
11549
11550 /**
11551 * Set the background to a given Drawable, or remove the background. If the
11552 * background has padding, this View's padding is set to the background's
11553 * padding. However, when a background is removed, this View's padding isn't
11554 * touched. If setting the padding is desired, please use
11555 * {@link #setPadding(int, int, int, int)}.
11556 *
11557 * @param d The Drawable to use as the background, or null to remove the
11558 * background
11559 */
11560 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070011561 if (d == mBGDrawable) {
11562 return;
11563 }
11564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011565 boolean requestLayout = false;
11566
11567 mBackgroundResource = 0;
11568
11569 /*
11570 * Regardless of whether we're setting a new background or not, we want
11571 * to clear the previous drawable.
11572 */
11573 if (mBGDrawable != null) {
11574 mBGDrawable.setCallback(null);
11575 unscheduleDrawable(mBGDrawable);
11576 }
11577
11578 if (d != null) {
11579 Rect padding = sThreadLocal.get();
11580 if (padding == null) {
11581 padding = new Rect();
11582 sThreadLocal.set(padding);
11583 }
11584 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011585 switch (d.getResolvedLayoutDirectionSelf()) {
11586 case LAYOUT_DIRECTION_RTL:
11587 setPadding(padding.right, padding.top, padding.left, padding.bottom);
11588 break;
11589 case LAYOUT_DIRECTION_LTR:
11590 default:
11591 setPadding(padding.left, padding.top, padding.right, padding.bottom);
11592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011593 }
11594
11595 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
11596 // if it has a different minimum size, we should layout again
11597 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
11598 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
11599 requestLayout = true;
11600 }
11601
11602 d.setCallback(this);
11603 if (d.isStateful()) {
11604 d.setState(getDrawableState());
11605 }
11606 d.setVisible(getVisibility() == VISIBLE, false);
11607 mBGDrawable = d;
11608
11609 if ((mPrivateFlags & SKIP_DRAW) != 0) {
11610 mPrivateFlags &= ~SKIP_DRAW;
11611 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
11612 requestLayout = true;
11613 }
11614 } else {
11615 /* Remove the background */
11616 mBGDrawable = null;
11617
11618 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
11619 /*
11620 * This view ONLY drew the background before and we're removing
11621 * the background, so now it won't draw anything
11622 * (hence we SKIP_DRAW)
11623 */
11624 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
11625 mPrivateFlags |= SKIP_DRAW;
11626 }
11627
11628 /*
11629 * When the background is set, we try to apply its padding to this
11630 * View. When the background is removed, we don't touch this View's
11631 * padding. This is noted in the Javadocs. Hence, we don't need to
11632 * requestLayout(), the invalidate() below is sufficient.
11633 */
11634
11635 // The old background's minimum size could have affected this
11636 // View's layout, so let's requestLayout
11637 requestLayout = true;
11638 }
11639
Romain Guy8f1344f52009-05-15 16:03:59 -070011640 computeOpaqueFlags();
11641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011642 if (requestLayout) {
11643 requestLayout();
11644 }
11645
11646 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011647 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011648 }
11649
11650 /**
11651 * Gets the background drawable
11652 * @return The drawable used as the background for this view, if any.
11653 */
11654 public Drawable getBackground() {
11655 return mBGDrawable;
11656 }
11657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011658 /**
11659 * Sets the padding. The view may add on the space required to display
11660 * the scrollbars, depending on the style and visibility of the scrollbars.
11661 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
11662 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
11663 * from the values set in this call.
11664 *
11665 * @attr ref android.R.styleable#View_padding
11666 * @attr ref android.R.styleable#View_paddingBottom
11667 * @attr ref android.R.styleable#View_paddingLeft
11668 * @attr ref android.R.styleable#View_paddingRight
11669 * @attr ref android.R.styleable#View_paddingTop
11670 * @param left the left padding in pixels
11671 * @param top the top padding in pixels
11672 * @param right the right padding in pixels
11673 * @param bottom the bottom padding in pixels
11674 */
11675 public void setPadding(int left, int top, int right, int bottom) {
11676 boolean changed = false;
11677
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011678 mUserPaddingRelative = false;
11679
Adam Powell20232d02010-12-08 21:08:53 -080011680 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011681 mUserPaddingRight = right;
11682 mUserPaddingBottom = bottom;
11683
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011684 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070011685
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011686 // Common case is there are no scroll bars.
11687 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011688 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080011689 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011690 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080011691 switch (mVerticalScrollbarPosition) {
11692 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011693 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11694 left += offset;
11695 } else {
11696 right += offset;
11697 }
11698 break;
Adam Powell20232d02010-12-08 21:08:53 -080011699 case SCROLLBAR_POSITION_RIGHT:
11700 right += offset;
11701 break;
11702 case SCROLLBAR_POSITION_LEFT:
11703 left += offset;
11704 break;
11705 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011706 }
Adam Powell20232d02010-12-08 21:08:53 -080011707 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011708 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
11709 ? 0 : getHorizontalScrollbarHeight();
11710 }
11711 }
Romain Guy8506ab42009-06-11 17:35:47 -070011712
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011713 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011714 changed = true;
11715 mPaddingLeft = left;
11716 }
11717 if (mPaddingTop != top) {
11718 changed = true;
11719 mPaddingTop = top;
11720 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011721 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011722 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011723 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011724 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011725 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011726 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011727 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011728 }
11729
11730 if (changed) {
11731 requestLayout();
11732 }
11733 }
11734
11735 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011736 * Sets the relative padding. The view may add on the space required to display
11737 * the scrollbars, depending on the style and visibility of the scrollbars.
11738 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
11739 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
11740 * from the values set in this call.
11741 *
11742 * @attr ref android.R.styleable#View_padding
11743 * @attr ref android.R.styleable#View_paddingBottom
11744 * @attr ref android.R.styleable#View_paddingStart
11745 * @attr ref android.R.styleable#View_paddingEnd
11746 * @attr ref android.R.styleable#View_paddingTop
11747 * @param start the start padding in pixels
11748 * @param top the top padding in pixels
11749 * @param end the end padding in pixels
11750 * @param bottom the bottom padding in pixels
11751 *
11752 * @hide
11753 */
11754 public void setPaddingRelative(int start, int top, int end, int bottom) {
11755 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070011756
11757 mUserPaddingStart = start;
11758 mUserPaddingEnd = end;
11759
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011760 switch(getResolvedLayoutDirection()) {
11761 case LAYOUT_DIRECTION_RTL:
11762 setPadding(end, top, start, bottom);
11763 break;
11764 case LAYOUT_DIRECTION_LTR:
11765 default:
11766 setPadding(start, top, end, bottom);
11767 }
11768 }
11769
11770 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011771 * Returns the top padding of this view.
11772 *
11773 * @return the top padding in pixels
11774 */
11775 public int getPaddingTop() {
11776 return mPaddingTop;
11777 }
11778
11779 /**
11780 * Returns the bottom padding of this view. If there are inset and enabled
11781 * scrollbars, this value may include the space required to display the
11782 * scrollbars as well.
11783 *
11784 * @return the bottom padding in pixels
11785 */
11786 public int getPaddingBottom() {
11787 return mPaddingBottom;
11788 }
11789
11790 /**
11791 * Returns the left padding of this view. If there are inset and enabled
11792 * scrollbars, this value may include the space required to display the
11793 * scrollbars as well.
11794 *
11795 * @return the left padding in pixels
11796 */
11797 public int getPaddingLeft() {
11798 return mPaddingLeft;
11799 }
11800
11801 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011802 * Returns the start padding of this view. If there are inset and enabled
11803 * scrollbars, this value may include the space required to display the
11804 * scrollbars as well.
11805 *
11806 * @return the start padding in pixels
11807 *
11808 * @hide
11809 */
11810 public int getPaddingStart() {
11811 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11812 mPaddingRight : mPaddingLeft;
11813 }
11814
11815 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011816 * Returns the right padding of this view. If there are inset and enabled
11817 * scrollbars, this value may include the space required to display the
11818 * scrollbars as well.
11819 *
11820 * @return the right padding in pixels
11821 */
11822 public int getPaddingRight() {
11823 return mPaddingRight;
11824 }
11825
11826 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011827 * Returns the end padding of this view. If there are inset and enabled
11828 * scrollbars, this value may include the space required to display the
11829 * scrollbars as well.
11830 *
11831 * @return the end padding in pixels
11832 *
11833 * @hide
11834 */
11835 public int getPaddingEnd() {
11836 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11837 mPaddingLeft : mPaddingRight;
11838 }
11839
11840 /**
11841 * Return if the padding as been set thru relative values
11842 * {@link #setPaddingRelative(int, int, int, int)} or thru
11843 * @attr ref android.R.styleable#View_paddingStart or
11844 * @attr ref android.R.styleable#View_paddingEnd
11845 *
11846 * @return true if the padding is relative or false if it is not.
11847 *
11848 * @hide
11849 */
11850 public boolean isPaddingRelative() {
11851 return mUserPaddingRelative;
11852 }
11853
11854 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011855 * Changes the selection state of this view. A view can be selected or not.
11856 * Note that selection is not the same as focus. Views are typically
11857 * selected in the context of an AdapterView like ListView or GridView;
11858 * the selected view is the view that is highlighted.
11859 *
11860 * @param selected true if the view must be selected, false otherwise
11861 */
11862 public void setSelected(boolean selected) {
11863 if (((mPrivateFlags & SELECTED) != 0) != selected) {
11864 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070011865 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080011866 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011867 refreshDrawableState();
11868 dispatchSetSelected(selected);
11869 }
11870 }
11871
11872 /**
11873 * Dispatch setSelected to all of this View's children.
11874 *
11875 * @see #setSelected(boolean)
11876 *
11877 * @param selected The new selected state
11878 */
11879 protected void dispatchSetSelected(boolean selected) {
11880 }
11881
11882 /**
11883 * Indicates the selection state of this view.
11884 *
11885 * @return true if the view is selected, false otherwise
11886 */
11887 @ViewDebug.ExportedProperty
11888 public boolean isSelected() {
11889 return (mPrivateFlags & SELECTED) != 0;
11890 }
11891
11892 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011893 * Changes the activated state of this view. A view can be activated or not.
11894 * Note that activation is not the same as selection. Selection is
11895 * a transient property, representing the view (hierarchy) the user is
11896 * currently interacting with. Activation is a longer-term state that the
11897 * user can move views in and out of. For example, in a list view with
11898 * single or multiple selection enabled, the views in the current selection
11899 * set are activated. (Um, yeah, we are deeply sorry about the terminology
11900 * here.) The activated state is propagated down to children of the view it
11901 * is set on.
11902 *
11903 * @param activated true if the view must be activated, false otherwise
11904 */
11905 public void setActivated(boolean activated) {
11906 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
11907 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011908 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011909 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070011910 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011911 }
11912 }
11913
11914 /**
11915 * Dispatch setActivated to all of this View's children.
11916 *
11917 * @see #setActivated(boolean)
11918 *
11919 * @param activated The new activated state
11920 */
11921 protected void dispatchSetActivated(boolean activated) {
11922 }
11923
11924 /**
11925 * Indicates the activation state of this view.
11926 *
11927 * @return true if the view is activated, false otherwise
11928 */
11929 @ViewDebug.ExportedProperty
11930 public boolean isActivated() {
11931 return (mPrivateFlags & ACTIVATED) != 0;
11932 }
11933
11934 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011935 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
11936 * observer can be used to get notifications when global events, like
11937 * layout, happen.
11938 *
11939 * The returned ViewTreeObserver observer is not guaranteed to remain
11940 * valid for the lifetime of this View. If the caller of this method keeps
11941 * a long-lived reference to ViewTreeObserver, it should always check for
11942 * the return value of {@link ViewTreeObserver#isAlive()}.
11943 *
11944 * @return The ViewTreeObserver for this view's hierarchy.
11945 */
11946 public ViewTreeObserver getViewTreeObserver() {
11947 if (mAttachInfo != null) {
11948 return mAttachInfo.mTreeObserver;
11949 }
11950 if (mFloatingTreeObserver == null) {
11951 mFloatingTreeObserver = new ViewTreeObserver();
11952 }
11953 return mFloatingTreeObserver;
11954 }
11955
11956 /**
11957 * <p>Finds the topmost view in the current view hierarchy.</p>
11958 *
11959 * @return the topmost view containing this view
11960 */
11961 public View getRootView() {
11962 if (mAttachInfo != null) {
11963 final View v = mAttachInfo.mRootView;
11964 if (v != null) {
11965 return v;
11966 }
11967 }
Romain Guy8506ab42009-06-11 17:35:47 -070011968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011969 View parent = this;
11970
11971 while (parent.mParent != null && parent.mParent instanceof View) {
11972 parent = (View) parent.mParent;
11973 }
11974
11975 return parent;
11976 }
11977
11978 /**
11979 * <p>Computes the coordinates of this view on the screen. The argument
11980 * must be an array of two integers. After the method returns, the array
11981 * contains the x and y location in that order.</p>
11982 *
11983 * @param location an array of two integers in which to hold the coordinates
11984 */
11985 public void getLocationOnScreen(int[] location) {
11986 getLocationInWindow(location);
11987
11988 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070011989 if (info != null) {
11990 location[0] += info.mWindowLeft;
11991 location[1] += info.mWindowTop;
11992 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011993 }
11994
11995 /**
11996 * <p>Computes the coordinates of this view in its window. The argument
11997 * must be an array of two integers. After the method returns, the array
11998 * contains the x and y location in that order.</p>
11999 *
12000 * @param location an array of two integers in which to hold the coordinates
12001 */
12002 public void getLocationInWindow(int[] location) {
12003 if (location == null || location.length < 2) {
12004 throw new IllegalArgumentException("location must be an array of "
12005 + "two integers");
12006 }
12007
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012008 location[0] = mLeft;
12009 location[1] = mTop;
12010 if (mTransformationInfo != null) {
12011 location[0] += (int) (mTransformationInfo.mTranslationX + 0.5f);
12012 location[1] += (int) (mTransformationInfo.mTranslationY + 0.5f);
12013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012014
12015 ViewParent viewParent = mParent;
12016 while (viewParent instanceof View) {
12017 final View view = (View)viewParent;
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012018 location[0] += view.mLeft - view.mScrollX;
12019 location[1] += view.mTop - view.mScrollY;
12020 if (view.mTransformationInfo != null) {
12021 location[0] += (int) (view.mTransformationInfo.mTranslationX + 0.5f);
12022 location[1] += (int) (view.mTransformationInfo.mTranslationY + 0.5f);
12023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012024 viewParent = view.mParent;
12025 }
Romain Guy8506ab42009-06-11 17:35:47 -070012026
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012027 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012028 // *cough*
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012029 final ViewRootImpl vr = (ViewRootImpl)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012030 location[1] -= vr.mCurScrollY;
12031 }
12032 }
12033
12034 /**
12035 * {@hide}
12036 * @param id the id of the view to be found
12037 * @return the view of the specified id, null if cannot be found
12038 */
12039 protected View findViewTraversal(int id) {
12040 if (id == mID) {
12041 return this;
12042 }
12043 return null;
12044 }
12045
12046 /**
12047 * {@hide}
12048 * @param tag the tag of the view to be found
12049 * @return the view of specified tag, null if cannot be found
12050 */
12051 protected View findViewWithTagTraversal(Object tag) {
12052 if (tag != null && tag.equals(mTag)) {
12053 return this;
12054 }
12055 return null;
12056 }
12057
12058 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012059 * {@hide}
12060 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070012061 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080012062 * @return The first view that matches the predicate or null.
12063 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070012064 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080012065 if (predicate.apply(this)) {
12066 return this;
12067 }
12068 return null;
12069 }
12070
12071 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012072 * Look for a child view with the given id. If this view has the given
12073 * id, return this view.
12074 *
12075 * @param id The id to search for.
12076 * @return The view that has the given id in the hierarchy or null
12077 */
12078 public final View findViewById(int id) {
12079 if (id < 0) {
12080 return null;
12081 }
12082 return findViewTraversal(id);
12083 }
12084
12085 /**
12086 * Look for a child view with the given tag. If this view has the given
12087 * tag, return this view.
12088 *
12089 * @param tag The tag to search for, using "tag.equals(getTag())".
12090 * @return The View that has the given tag in the hierarchy or null
12091 */
12092 public final View findViewWithTag(Object tag) {
12093 if (tag == null) {
12094 return null;
12095 }
12096 return findViewWithTagTraversal(tag);
12097 }
12098
12099 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012100 * {@hide}
12101 * Look for a child view that matches the specified predicate.
12102 * If this view matches the predicate, return this view.
12103 *
12104 * @param predicate The predicate to evaluate.
12105 * @return The first view that matches the predicate or null.
12106 */
12107 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070012108 return findViewByPredicateTraversal(predicate, null);
12109 }
12110
12111 /**
12112 * {@hide}
12113 * Look for a child view that matches the specified predicate,
12114 * starting with the specified view and its descendents and then
12115 * recusively searching the ancestors and siblings of that view
12116 * until this view is reached.
12117 *
12118 * This method is useful in cases where the predicate does not match
12119 * a single unique view (perhaps multiple views use the same id)
12120 * and we are trying to find the view that is "closest" in scope to the
12121 * starting view.
12122 *
12123 * @param start The view to start from.
12124 * @param predicate The predicate to evaluate.
12125 * @return The first view that matches the predicate or null.
12126 */
12127 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
12128 View childToSkip = null;
12129 for (;;) {
12130 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
12131 if (view != null || start == this) {
12132 return view;
12133 }
12134
12135 ViewParent parent = start.getParent();
12136 if (parent == null || !(parent instanceof View)) {
12137 return null;
12138 }
12139
12140 childToSkip = start;
12141 start = (View) parent;
12142 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080012143 }
12144
12145 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012146 * Sets the identifier for this view. The identifier does not have to be
12147 * unique in this view's hierarchy. The identifier should be a positive
12148 * number.
12149 *
12150 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070012151 * @see #getId()
12152 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012153 *
12154 * @param id a number used to identify the view
12155 *
12156 * @attr ref android.R.styleable#View_id
12157 */
12158 public void setId(int id) {
12159 mID = id;
12160 }
12161
12162 /**
12163 * {@hide}
12164 *
12165 * @param isRoot true if the view belongs to the root namespace, false
12166 * otherwise
12167 */
12168 public void setIsRootNamespace(boolean isRoot) {
12169 if (isRoot) {
12170 mPrivateFlags |= IS_ROOT_NAMESPACE;
12171 } else {
12172 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
12173 }
12174 }
12175
12176 /**
12177 * {@hide}
12178 *
12179 * @return true if the view belongs to the root namespace, false otherwise
12180 */
12181 public boolean isRootNamespace() {
12182 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
12183 }
12184
12185 /**
12186 * Returns this view's identifier.
12187 *
12188 * @return a positive integer used to identify the view or {@link #NO_ID}
12189 * if the view has no ID
12190 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012191 * @see #setId(int)
12192 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012193 * @attr ref android.R.styleable#View_id
12194 */
12195 @ViewDebug.CapturedViewProperty
12196 public int getId() {
12197 return mID;
12198 }
12199
12200 /**
12201 * Returns this view's tag.
12202 *
12203 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070012204 *
12205 * @see #setTag(Object)
12206 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012207 */
12208 @ViewDebug.ExportedProperty
12209 public Object getTag() {
12210 return mTag;
12211 }
12212
12213 /**
12214 * Sets the tag associated with this view. A tag can be used to mark
12215 * a view in its hierarchy and does not have to be unique within the
12216 * hierarchy. Tags can also be used to store data within a view without
12217 * resorting to another data structure.
12218 *
12219 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070012220 *
12221 * @see #getTag()
12222 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012223 */
12224 public void setTag(final Object tag) {
12225 mTag = tag;
12226 }
12227
12228 /**
Romain Guyd90a3312009-05-06 14:54:28 -070012229 * Returns the tag associated with this view and the specified key.
12230 *
12231 * @param key The key identifying the tag
12232 *
12233 * @return the Object stored in this view as a tag
12234 *
12235 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070012236 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070012237 */
12238 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012239 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070012240 return null;
12241 }
12242
12243 /**
12244 * Sets a tag associated with this view and a key. A tag can be used
12245 * to mark a view in its hierarchy and does not have to be unique within
12246 * the hierarchy. Tags can also be used to store data within a view
12247 * without resorting to another data structure.
12248 *
12249 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070012250 * application to ensure it is unique (see the <a
12251 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
12252 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070012253 * the Android framework or not associated with any package will cause
12254 * an {@link IllegalArgumentException} to be thrown.
12255 *
12256 * @param key The key identifying the tag
12257 * @param tag An Object to tag the view with
12258 *
12259 * @throws IllegalArgumentException If they specified key is not valid
12260 *
12261 * @see #setTag(Object)
12262 * @see #getTag(int)
12263 */
12264 public void setTag(int key, final Object tag) {
12265 // If the package id is 0x00 or 0x01, it's either an undefined package
12266 // or a framework id
12267 if ((key >>> 24) < 2) {
12268 throw new IllegalArgumentException("The key must be an application-specific "
12269 + "resource id.");
12270 }
12271
Adam Powell2b2f6d62011-09-23 11:15:39 -070012272 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012273 }
12274
12275 /**
12276 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
12277 * framework id.
12278 *
12279 * @hide
12280 */
12281 public void setTagInternal(int key, Object tag) {
12282 if ((key >>> 24) != 0x1) {
12283 throw new IllegalArgumentException("The key must be a framework-specific "
12284 + "resource id.");
12285 }
12286
Adam Powell2b2f6d62011-09-23 11:15:39 -070012287 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012288 }
12289
Adam Powell2b2f6d62011-09-23 11:15:39 -070012290 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012291 if (mKeyedTags == null) {
12292 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070012293 }
12294
Adam Powell7db82ac2011-09-22 19:44:04 -070012295 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012296 }
12297
12298 /**
Romain Guy13922e02009-05-12 17:56:14 -070012299 * @param consistency The type of consistency. See ViewDebug for more information.
12300 *
12301 * @hide
12302 */
12303 protected boolean dispatchConsistencyCheck(int consistency) {
12304 return onConsistencyCheck(consistency);
12305 }
12306
12307 /**
12308 * Method that subclasses should implement to check their consistency. The type of
12309 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070012310 *
Romain Guy13922e02009-05-12 17:56:14 -070012311 * @param consistency The type of consistency. See ViewDebug for more information.
12312 *
12313 * @throws IllegalStateException if the view is in an inconsistent state.
12314 *
12315 * @hide
12316 */
12317 protected boolean onConsistencyCheck(int consistency) {
12318 boolean result = true;
12319
12320 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
12321 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
12322
12323 if (checkLayout) {
12324 if (getParent() == null) {
12325 result = false;
12326 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12327 "View " + this + " does not have a parent.");
12328 }
12329
12330 if (mAttachInfo == null) {
12331 result = false;
12332 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12333 "View " + this + " is not attached to a window.");
12334 }
12335 }
12336
12337 if (checkDrawing) {
12338 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
12339 // from their draw() method
12340
12341 if ((mPrivateFlags & DRAWN) != DRAWN &&
12342 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
12343 result = false;
12344 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12345 "View " + this + " was invalidated but its drawing cache is valid.");
12346 }
12347 }
12348
12349 return result;
12350 }
12351
12352 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012353 * Prints information about this view in the log output, with the tag
12354 * {@link #VIEW_LOG_TAG}.
12355 *
12356 * @hide
12357 */
12358 public void debug() {
12359 debug(0);
12360 }
12361
12362 /**
12363 * Prints information about this view in the log output, with the tag
12364 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
12365 * indentation defined by the <code>depth</code>.
12366 *
12367 * @param depth the indentation level
12368 *
12369 * @hide
12370 */
12371 protected void debug(int depth) {
12372 String output = debugIndent(depth - 1);
12373
12374 output += "+ " + this;
12375 int id = getId();
12376 if (id != -1) {
12377 output += " (id=" + id + ")";
12378 }
12379 Object tag = getTag();
12380 if (tag != null) {
12381 output += " (tag=" + tag + ")";
12382 }
12383 Log.d(VIEW_LOG_TAG, output);
12384
12385 if ((mPrivateFlags & FOCUSED) != 0) {
12386 output = debugIndent(depth) + " FOCUSED";
12387 Log.d(VIEW_LOG_TAG, output);
12388 }
12389
12390 output = debugIndent(depth);
12391 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
12392 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
12393 + "} ";
12394 Log.d(VIEW_LOG_TAG, output);
12395
12396 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
12397 || mPaddingBottom != 0) {
12398 output = debugIndent(depth);
12399 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
12400 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
12401 Log.d(VIEW_LOG_TAG, output);
12402 }
12403
12404 output = debugIndent(depth);
12405 output += "mMeasureWidth=" + mMeasuredWidth +
12406 " mMeasureHeight=" + mMeasuredHeight;
12407 Log.d(VIEW_LOG_TAG, output);
12408
12409 output = debugIndent(depth);
12410 if (mLayoutParams == null) {
12411 output += "BAD! no layout params";
12412 } else {
12413 output = mLayoutParams.debug(output);
12414 }
12415 Log.d(VIEW_LOG_TAG, output);
12416
12417 output = debugIndent(depth);
12418 output += "flags={";
12419 output += View.printFlags(mViewFlags);
12420 output += "}";
12421 Log.d(VIEW_LOG_TAG, output);
12422
12423 output = debugIndent(depth);
12424 output += "privateFlags={";
12425 output += View.printPrivateFlags(mPrivateFlags);
12426 output += "}";
12427 Log.d(VIEW_LOG_TAG, output);
12428 }
12429
12430 /**
12431 * Creates an string of whitespaces used for indentation.
12432 *
12433 * @param depth the indentation level
12434 * @return a String containing (depth * 2 + 3) * 2 white spaces
12435 *
12436 * @hide
12437 */
12438 protected static String debugIndent(int depth) {
12439 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
12440 for (int i = 0; i < (depth * 2) + 3; i++) {
12441 spaces.append(' ').append(' ');
12442 }
12443 return spaces.toString();
12444 }
12445
12446 /**
12447 * <p>Return the offset of the widget's text baseline from the widget's top
12448 * boundary. If this widget does not support baseline alignment, this
12449 * method returns -1. </p>
12450 *
12451 * @return the offset of the baseline within the widget's bounds or -1
12452 * if baseline alignment is not supported
12453 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012454 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012455 public int getBaseline() {
12456 return -1;
12457 }
12458
12459 /**
12460 * Call this when something has changed which has invalidated the
12461 * layout of this view. This will schedule a layout pass of the view
12462 * tree.
12463 */
12464 public void requestLayout() {
12465 if (ViewDebug.TRACE_HIERARCHY) {
12466 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
12467 }
12468
12469 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012470 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012471
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070012472 if (mParent != null) {
12473 if (mLayoutParams != null) {
12474 mLayoutParams.resolveWithDirection(getResolvedLayoutDirection());
12475 }
12476 if (!mParent.isLayoutRequested()) {
12477 mParent.requestLayout();
12478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012479 }
12480 }
12481
12482 /**
12483 * Forces this view to be laid out during the next layout pass.
12484 * This method does not call requestLayout() or forceLayout()
12485 * on the parent.
12486 */
12487 public void forceLayout() {
12488 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012489 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012490 }
12491
12492 /**
12493 * <p>
12494 * This is called to find out how big a view should be. The parent
12495 * supplies constraint information in the width and height parameters.
12496 * </p>
12497 *
12498 * <p>
12499 * The actual mesurement work of a view is performed in
12500 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
12501 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
12502 * </p>
12503 *
12504 *
12505 * @param widthMeasureSpec Horizontal space requirements as imposed by the
12506 * parent
12507 * @param heightMeasureSpec Vertical space requirements as imposed by the
12508 * parent
12509 *
12510 * @see #onMeasure(int, int)
12511 */
12512 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
12513 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
12514 widthMeasureSpec != mOldWidthMeasureSpec ||
12515 heightMeasureSpec != mOldHeightMeasureSpec) {
12516
12517 // first clears the measured dimension flag
12518 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
12519
12520 if (ViewDebug.TRACE_HIERARCHY) {
12521 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
12522 }
12523
12524 // measure ourselves, this should set the measured dimension flag back
12525 onMeasure(widthMeasureSpec, heightMeasureSpec);
12526
12527 // flag not set, setMeasuredDimension() was not invoked, we raise
12528 // an exception to warn the developer
12529 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
12530 throw new IllegalStateException("onMeasure() did not set the"
12531 + " measured dimension by calling"
12532 + " setMeasuredDimension()");
12533 }
12534
12535 mPrivateFlags |= LAYOUT_REQUIRED;
12536 }
12537
12538 mOldWidthMeasureSpec = widthMeasureSpec;
12539 mOldHeightMeasureSpec = heightMeasureSpec;
12540 }
12541
12542 /**
12543 * <p>
12544 * Measure the view and its content to determine the measured width and the
12545 * measured height. This method is invoked by {@link #measure(int, int)} and
12546 * should be overriden by subclasses to provide accurate and efficient
12547 * measurement of their contents.
12548 * </p>
12549 *
12550 * <p>
12551 * <strong>CONTRACT:</strong> When overriding this method, you
12552 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
12553 * measured width and height of this view. Failure to do so will trigger an
12554 * <code>IllegalStateException</code>, thrown by
12555 * {@link #measure(int, int)}. Calling the superclass'
12556 * {@link #onMeasure(int, int)} is a valid use.
12557 * </p>
12558 *
12559 * <p>
12560 * The base class implementation of measure defaults to the background size,
12561 * unless a larger size is allowed by the MeasureSpec. Subclasses should
12562 * override {@link #onMeasure(int, int)} to provide better measurements of
12563 * their content.
12564 * </p>
12565 *
12566 * <p>
12567 * If this method is overridden, it is the subclass's responsibility to make
12568 * sure the measured height and width are at least the view's minimum height
12569 * and width ({@link #getSuggestedMinimumHeight()} and
12570 * {@link #getSuggestedMinimumWidth()}).
12571 * </p>
12572 *
12573 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
12574 * The requirements are encoded with
12575 * {@link android.view.View.MeasureSpec}.
12576 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
12577 * The requirements are encoded with
12578 * {@link android.view.View.MeasureSpec}.
12579 *
12580 * @see #getMeasuredWidth()
12581 * @see #getMeasuredHeight()
12582 * @see #setMeasuredDimension(int, int)
12583 * @see #getSuggestedMinimumHeight()
12584 * @see #getSuggestedMinimumWidth()
12585 * @see android.view.View.MeasureSpec#getMode(int)
12586 * @see android.view.View.MeasureSpec#getSize(int)
12587 */
12588 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
12589 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
12590 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
12591 }
12592
12593 /**
12594 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
12595 * measured width and measured height. Failing to do so will trigger an
12596 * exception at measurement time.</p>
12597 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080012598 * @param measuredWidth The measured width of this view. May be a complex
12599 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12600 * {@link #MEASURED_STATE_TOO_SMALL}.
12601 * @param measuredHeight The measured height of this view. May be a complex
12602 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12603 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012604 */
12605 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
12606 mMeasuredWidth = measuredWidth;
12607 mMeasuredHeight = measuredHeight;
12608
12609 mPrivateFlags |= MEASURED_DIMENSION_SET;
12610 }
12611
12612 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080012613 * Merge two states as returned by {@link #getMeasuredState()}.
12614 * @param curState The current state as returned from a view or the result
12615 * of combining multiple views.
12616 * @param newState The new view state to combine.
12617 * @return Returns a new integer reflecting the combination of the two
12618 * states.
12619 */
12620 public static int combineMeasuredStates(int curState, int newState) {
12621 return curState | newState;
12622 }
12623
12624 /**
12625 * Version of {@link #resolveSizeAndState(int, int, int)}
12626 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
12627 */
12628 public static int resolveSize(int size, int measureSpec) {
12629 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
12630 }
12631
12632 /**
12633 * Utility to reconcile a desired size and state, with constraints imposed
12634 * by a MeasureSpec. Will take the desired size, unless a different size
12635 * is imposed by the constraints. The returned value is a compound integer,
12636 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
12637 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
12638 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012639 *
12640 * @param size How big the view wants to be
12641 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080012642 * @return Size information bit mask as defined by
12643 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012644 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080012645 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012646 int result = size;
12647 int specMode = MeasureSpec.getMode(measureSpec);
12648 int specSize = MeasureSpec.getSize(measureSpec);
12649 switch (specMode) {
12650 case MeasureSpec.UNSPECIFIED:
12651 result = size;
12652 break;
12653 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080012654 if (specSize < size) {
12655 result = specSize | MEASURED_STATE_TOO_SMALL;
12656 } else {
12657 result = size;
12658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012659 break;
12660 case MeasureSpec.EXACTLY:
12661 result = specSize;
12662 break;
12663 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080012664 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012665 }
12666
12667 /**
12668 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070012669 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012670 * by the MeasureSpec.
12671 *
12672 * @param size Default size for this view
12673 * @param measureSpec Constraints imposed by the parent
12674 * @return The size this view should be.
12675 */
12676 public static int getDefaultSize(int size, int measureSpec) {
12677 int result = size;
12678 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070012679 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012680
12681 switch (specMode) {
12682 case MeasureSpec.UNSPECIFIED:
12683 result = size;
12684 break;
12685 case MeasureSpec.AT_MOST:
12686 case MeasureSpec.EXACTLY:
12687 result = specSize;
12688 break;
12689 }
12690 return result;
12691 }
12692
12693 /**
12694 * Returns the suggested minimum height that the view should use. This
12695 * returns the maximum of the view's minimum height
12696 * and the background's minimum height
12697 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
12698 * <p>
12699 * When being used in {@link #onMeasure(int, int)}, the caller should still
12700 * ensure the returned height is within the requirements of the parent.
12701 *
12702 * @return The suggested minimum height of the view.
12703 */
12704 protected int getSuggestedMinimumHeight() {
12705 int suggestedMinHeight = mMinHeight;
12706
12707 if (mBGDrawable != null) {
12708 final int bgMinHeight = mBGDrawable.getMinimumHeight();
12709 if (suggestedMinHeight < bgMinHeight) {
12710 suggestedMinHeight = bgMinHeight;
12711 }
12712 }
12713
12714 return suggestedMinHeight;
12715 }
12716
12717 /**
12718 * Returns the suggested minimum width that the view should use. This
12719 * returns the maximum of the view's minimum width)
12720 * and the background's minimum width
12721 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
12722 * <p>
12723 * When being used in {@link #onMeasure(int, int)}, the caller should still
12724 * ensure the returned width is within the requirements of the parent.
12725 *
12726 * @return The suggested minimum width of the view.
12727 */
12728 protected int getSuggestedMinimumWidth() {
12729 int suggestedMinWidth = mMinWidth;
12730
12731 if (mBGDrawable != null) {
12732 final int bgMinWidth = mBGDrawable.getMinimumWidth();
12733 if (suggestedMinWidth < bgMinWidth) {
12734 suggestedMinWidth = bgMinWidth;
12735 }
12736 }
12737
12738 return suggestedMinWidth;
12739 }
12740
12741 /**
12742 * Sets the minimum height of the view. It is not guaranteed the view will
12743 * be able to achieve this minimum height (for example, if its parent layout
12744 * constrains it with less available height).
12745 *
12746 * @param minHeight The minimum height the view will try to be.
12747 */
12748 public void setMinimumHeight(int minHeight) {
12749 mMinHeight = minHeight;
12750 }
12751
12752 /**
12753 * Sets the minimum width of the view. It is not guaranteed the view will
12754 * be able to achieve this minimum width (for example, if its parent layout
12755 * constrains it with less available width).
12756 *
12757 * @param minWidth The minimum width the view will try to be.
12758 */
12759 public void setMinimumWidth(int minWidth) {
12760 mMinWidth = minWidth;
12761 }
12762
12763 /**
12764 * Get the animation currently associated with this view.
12765 *
12766 * @return The animation that is currently playing or
12767 * scheduled to play for this view.
12768 */
12769 public Animation getAnimation() {
12770 return mCurrentAnimation;
12771 }
12772
12773 /**
12774 * Start the specified animation now.
12775 *
12776 * @param animation the animation to start now
12777 */
12778 public void startAnimation(Animation animation) {
12779 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
12780 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012781 invalidateParentCaches();
12782 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012783 }
12784
12785 /**
12786 * Cancels any animations for this view.
12787 */
12788 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080012789 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080012790 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080012791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012792 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012793 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012794 }
12795
12796 /**
12797 * Sets the next animation to play for this view.
12798 * If you want the animation to play immediately, use
12799 * startAnimation. This method provides allows fine-grained
12800 * control over the start time and invalidation, but you
12801 * must make sure that 1) the animation has a start time set, and
12802 * 2) the view will be invalidated when the animation is supposed to
12803 * start.
12804 *
12805 * @param animation The next animation, or null.
12806 */
12807 public void setAnimation(Animation animation) {
12808 mCurrentAnimation = animation;
12809 if (animation != null) {
12810 animation.reset();
12811 }
12812 }
12813
12814 /**
12815 * Invoked by a parent ViewGroup to notify the start of the animation
12816 * currently associated with this view. If you override this method,
12817 * always call super.onAnimationStart();
12818 *
12819 * @see #setAnimation(android.view.animation.Animation)
12820 * @see #getAnimation()
12821 */
12822 protected void onAnimationStart() {
12823 mPrivateFlags |= ANIMATION_STARTED;
12824 }
12825
12826 /**
12827 * Invoked by a parent ViewGroup to notify the end of the animation
12828 * currently associated with this view. If you override this method,
12829 * always call super.onAnimationEnd();
12830 *
12831 * @see #setAnimation(android.view.animation.Animation)
12832 * @see #getAnimation()
12833 */
12834 protected void onAnimationEnd() {
12835 mPrivateFlags &= ~ANIMATION_STARTED;
12836 }
12837
12838 /**
12839 * Invoked if there is a Transform that involves alpha. Subclass that can
12840 * draw themselves with the specified alpha should return true, and then
12841 * respect that alpha when their onDraw() is called. If this returns false
12842 * then the view may be redirected to draw into an offscreen buffer to
12843 * fulfill the request, which will look fine, but may be slower than if the
12844 * subclass handles it internally. The default implementation returns false.
12845 *
12846 * @param alpha The alpha (0..255) to apply to the view's drawing
12847 * @return true if the view can draw with the specified alpha.
12848 */
12849 protected boolean onSetAlpha(int alpha) {
12850 return false;
12851 }
12852
12853 /**
12854 * This is used by the RootView to perform an optimization when
12855 * the view hierarchy contains one or several SurfaceView.
12856 * SurfaceView is always considered transparent, but its children are not,
12857 * therefore all View objects remove themselves from the global transparent
12858 * region (passed as a parameter to this function).
12859 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012860 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012861 *
12862 * @return Returns true if the effective visibility of the view at this
12863 * point is opaque, regardless of the transparent region; returns false
12864 * if it is possible for underlying windows to be seen behind the view.
12865 *
12866 * {@hide}
12867 */
12868 public boolean gatherTransparentRegion(Region region) {
12869 final AttachInfo attachInfo = mAttachInfo;
12870 if (region != null && attachInfo != null) {
12871 final int pflags = mPrivateFlags;
12872 if ((pflags & SKIP_DRAW) == 0) {
12873 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
12874 // remove it from the transparent region.
12875 final int[] location = attachInfo.mTransparentLocation;
12876 getLocationInWindow(location);
12877 region.op(location[0], location[1], location[0] + mRight - mLeft,
12878 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
12879 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
12880 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
12881 // exists, so we remove the background drawable's non-transparent
12882 // parts from this transparent region.
12883 applyDrawableToTransparentRegion(mBGDrawable, region);
12884 }
12885 }
12886 return true;
12887 }
12888
12889 /**
12890 * Play a sound effect for this view.
12891 *
12892 * <p>The framework will play sound effects for some built in actions, such as
12893 * clicking, but you may wish to play these effects in your widget,
12894 * for instance, for internal navigation.
12895 *
12896 * <p>The sound effect will only be played if sound effects are enabled by the user, and
12897 * {@link #isSoundEffectsEnabled()} is true.
12898 *
12899 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
12900 */
12901 public void playSoundEffect(int soundConstant) {
12902 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
12903 return;
12904 }
12905 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
12906 }
12907
12908 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012909 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012910 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012911 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012912 *
12913 * <p>The framework will provide haptic feedback for some built in actions,
12914 * such as long presses, but you may wish to provide feedback for your
12915 * own widget.
12916 *
12917 * <p>The feedback will only be performed if
12918 * {@link #isHapticFeedbackEnabled()} is true.
12919 *
12920 * @param feedbackConstant One of the constants defined in
12921 * {@link HapticFeedbackConstants}
12922 */
12923 public boolean performHapticFeedback(int feedbackConstant) {
12924 return performHapticFeedback(feedbackConstant, 0);
12925 }
12926
12927 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012928 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012929 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012930 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012931 *
12932 * @param feedbackConstant One of the constants defined in
12933 * {@link HapticFeedbackConstants}
12934 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
12935 */
12936 public boolean performHapticFeedback(int feedbackConstant, int flags) {
12937 if (mAttachInfo == null) {
12938 return false;
12939 }
Romain Guyf607bdc2010-09-10 19:20:06 -070012940 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070012941 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012942 && !isHapticFeedbackEnabled()) {
12943 return false;
12944 }
Romain Guy812ccbe2010-06-01 14:07:24 -070012945 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
12946 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012947 }
12948
12949 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012950 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040012951 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
12952 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080012953 */
12954 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040012955 if (visibility != mSystemUiVisibility) {
12956 mSystemUiVisibility = visibility;
12957 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12958 mParent.recomputeViewAttributes(this);
12959 }
Joe Onorato664644d2011-01-23 17:53:23 -080012960 }
12961 }
12962
12963 /**
12964 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040012965 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
12966 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080012967 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080012968 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080012969 return mSystemUiVisibility;
12970 }
12971
Scott Mainec6331b2011-05-24 16:55:56 -070012972 /**
12973 * Set a listener to receive callbacks when the visibility of the system bar changes.
12974 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
12975 */
Joe Onorato664644d2011-01-23 17:53:23 -080012976 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
12977 mOnSystemUiVisibilityChangeListener = l;
12978 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12979 mParent.recomputeViewAttributes(this);
12980 }
12981 }
12982
12983 /**
12984 */
12985 public void dispatchSystemUiVisibilityChanged(int visibility) {
12986 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080012987 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080012988 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080012989 }
12990 }
12991
12992 /**
Joe Malin32736f02011-01-19 16:14:20 -080012993 * Creates an image that the system displays during the drag and drop
12994 * operation. This is called a &quot;drag shadow&quot;. The default implementation
12995 * for a DragShadowBuilder based on a View returns an image that has exactly the same
12996 * appearance as the given View. The default also positions the center of the drag shadow
12997 * directly under the touch point. If no View is provided (the constructor with no parameters
12998 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
12999 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
13000 * default is an invisible drag shadow.
13001 * <p>
13002 * You are not required to use the View you provide to the constructor as the basis of the
13003 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
13004 * anything you want as the drag shadow.
13005 * </p>
13006 * <p>
13007 * You pass a DragShadowBuilder object to the system when you start the drag. The system
13008 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
13009 * size and position of the drag shadow. It uses this data to construct a
13010 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
13011 * so that your application can draw the shadow image in the Canvas.
13012 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013013 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013014 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070013015 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070013016
13017 /**
Joe Malin32736f02011-01-19 16:14:20 -080013018 * Constructs a shadow image builder based on a View. By default, the resulting drag
13019 * shadow will have the same appearance and dimensions as the View, with the touch point
13020 * over the center of the View.
13021 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070013022 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013023 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070013024 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070013025 }
13026
Christopher Tate17ed60c2011-01-18 12:50:26 -080013027 /**
13028 * Construct a shadow builder object with no associated View. This
13029 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
13030 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
13031 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080013032 * reference to any View object. If they are not overridden, then the result is an
13033 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080013034 */
13035 public DragShadowBuilder() {
13036 mView = new WeakReference<View>(null);
13037 }
13038
13039 /**
13040 * Returns the View object that had been passed to the
13041 * {@link #View.DragShadowBuilder(View)}
13042 * constructor. If that View parameter was {@code null} or if the
13043 * {@link #View.DragShadowBuilder()}
13044 * constructor was used to instantiate the builder object, this method will return
13045 * null.
13046 *
13047 * @return The View object associate with this builder object.
13048 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070013049 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070013050 final public View getView() {
13051 return mView.get();
13052 }
13053
Christopher Tate2c095f32010-10-04 14:13:40 -070013054 /**
Joe Malin32736f02011-01-19 16:14:20 -080013055 * Provides the metrics for the shadow image. These include the dimensions of
13056 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070013057 * be centered under the touch location while dragging.
13058 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013059 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080013060 * same as the dimensions of the View itself and centers the shadow under
13061 * the touch point.
13062 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013063 *
Joe Malin32736f02011-01-19 16:14:20 -080013064 * @param shadowSize A {@link android.graphics.Point} containing the width and height
13065 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
13066 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
13067 * image.
13068 *
13069 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
13070 * shadow image that should be underneath the touch point during the drag and drop
13071 * operation. Your application must set {@link android.graphics.Point#x} to the
13072 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070013073 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013074 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070013075 final View view = mView.get();
13076 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013077 shadowSize.set(view.getWidth(), view.getHeight());
13078 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070013079 } else {
13080 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
13081 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013082 }
13083
13084 /**
Joe Malin32736f02011-01-19 16:14:20 -080013085 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
13086 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013087 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070013088 *
Joe Malin32736f02011-01-19 16:14:20 -080013089 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070013090 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013091 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070013092 final View view = mView.get();
13093 if (view != null) {
13094 view.draw(canvas);
13095 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013096 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070013097 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013098 }
13099 }
13100
13101 /**
Joe Malin32736f02011-01-19 16:14:20 -080013102 * Starts a drag and drop operation. When your application calls this method, it passes a
13103 * {@link android.view.View.DragShadowBuilder} object to the system. The
13104 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
13105 * to get metrics for the drag shadow, and then calls the object's
13106 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
13107 * <p>
13108 * Once the system has the drag shadow, it begins the drag and drop operation by sending
13109 * drag events to all the View objects in your application that are currently visible. It does
13110 * this either by calling the View object's drag listener (an implementation of
13111 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
13112 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
13113 * Both are passed a {@link android.view.DragEvent} object that has a
13114 * {@link android.view.DragEvent#getAction()} value of
13115 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
13116 * </p>
13117 * <p>
13118 * Your application can invoke startDrag() on any attached View object. The View object does not
13119 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
13120 * be related to the View the user selected for dragging.
13121 * </p>
13122 * @param data A {@link android.content.ClipData} object pointing to the data to be
13123 * transferred by the drag and drop operation.
13124 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
13125 * drag shadow.
13126 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
13127 * drop operation. This Object is put into every DragEvent object sent by the system during the
13128 * current drag.
13129 * <p>
13130 * myLocalState is a lightweight mechanism for the sending information from the dragged View
13131 * to the target Views. For example, it can contain flags that differentiate between a
13132 * a copy operation and a move operation.
13133 * </p>
13134 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
13135 * so the parameter should be set to 0.
13136 * @return {@code true} if the method completes successfully, or
13137 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
13138 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070013139 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013140 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013141 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070013142 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013143 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070013144 }
13145 boolean okay = false;
13146
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013147 Point shadowSize = new Point();
13148 Point shadowTouchPoint = new Point();
13149 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070013150
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013151 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
13152 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
13153 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070013154 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013155
Chris Tatea32dcf72010-10-14 12:13:50 -070013156 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013157 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
13158 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070013159 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013160 Surface surface = new Surface();
13161 try {
13162 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013163 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070013164 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070013165 + " surface=" + surface);
13166 if (token != null) {
13167 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070013168 try {
Chris Tate6b391282010-10-14 15:48:59 -070013169 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013170 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070013171 } finally {
13172 surface.unlockCanvasAndPost(canvas);
13173 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013174
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013175 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080013176
13177 // Cache the local state object for delivery with DragEvents
13178 root.setLocalDragState(myLocalState);
13179
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013180 // repurpose 'shadowSize' for the last touch point
13181 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070013182
Christopher Tatea53146c2010-09-07 11:57:52 -070013183 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013184 shadowSize.x, shadowSize.y,
13185 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070013186 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070013187
13188 // Off and running! Release our local surface instance; the drag
13189 // shadow surface is now managed by the system process.
13190 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070013191 }
13192 } catch (Exception e) {
13193 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
13194 surface.destroy();
13195 }
13196
13197 return okay;
13198 }
13199
Christopher Tatea53146c2010-09-07 11:57:52 -070013200 /**
Joe Malin32736f02011-01-19 16:14:20 -080013201 * Handles drag events sent by the system following a call to
13202 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
13203 *<p>
13204 * When the system calls this method, it passes a
13205 * {@link android.view.DragEvent} object. A call to
13206 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
13207 * in DragEvent. The method uses these to determine what is happening in the drag and drop
13208 * operation.
13209 * @param event The {@link android.view.DragEvent} sent by the system.
13210 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
13211 * in DragEvent, indicating the type of drag event represented by this object.
13212 * @return {@code true} if the method was successful, otherwise {@code false}.
13213 * <p>
13214 * The method should return {@code true} in response to an action type of
13215 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
13216 * operation.
13217 * </p>
13218 * <p>
13219 * The method should also return {@code true} in response to an action type of
13220 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
13221 * {@code false} if it didn't.
13222 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013223 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070013224 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070013225 return false;
13226 }
13227
13228 /**
Joe Malin32736f02011-01-19 16:14:20 -080013229 * Detects if this View is enabled and has a drag event listener.
13230 * If both are true, then it calls the drag event listener with the
13231 * {@link android.view.DragEvent} it received. If the drag event listener returns
13232 * {@code true}, then dispatchDragEvent() returns {@code true}.
13233 * <p>
13234 * For all other cases, the method calls the
13235 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
13236 * method and returns its result.
13237 * </p>
13238 * <p>
13239 * This ensures that a drag event is always consumed, even if the View does not have a drag
13240 * event listener. However, if the View has a listener and the listener returns true, then
13241 * onDragEvent() is not called.
13242 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013243 */
13244 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080013245 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070013246 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
13247 && mOnDragListener.onDrag(this, event)) {
13248 return true;
13249 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013250 return onDragEvent(event);
13251 }
13252
Christopher Tate3d4bf172011-03-28 16:16:46 -070013253 boolean canAcceptDrag() {
13254 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
13255 }
13256
Christopher Tatea53146c2010-09-07 11:57:52 -070013257 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070013258 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
13259 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070013260 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070013261 */
13262 public void onCloseSystemDialogs(String reason) {
13263 }
Joe Malin32736f02011-01-19 16:14:20 -080013264
Dianne Hackbornffa42482009-09-23 22:20:11 -070013265 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013266 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070013267 * update a Region being computed for
13268 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013269 * that any non-transparent parts of the Drawable are removed from the
13270 * given transparent region.
13271 *
13272 * @param dr The Drawable whose transparency is to be applied to the region.
13273 * @param region A Region holding the current transparency information,
13274 * where any parts of the region that are set are considered to be
13275 * transparent. On return, this region will be modified to have the
13276 * transparency information reduced by the corresponding parts of the
13277 * Drawable that are not transparent.
13278 * {@hide}
13279 */
13280 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
13281 if (DBG) {
13282 Log.i("View", "Getting transparent region for: " + this);
13283 }
13284 final Region r = dr.getTransparentRegion();
13285 final Rect db = dr.getBounds();
13286 final AttachInfo attachInfo = mAttachInfo;
13287 if (r != null && attachInfo != null) {
13288 final int w = getRight()-getLeft();
13289 final int h = getBottom()-getTop();
13290 if (db.left > 0) {
13291 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
13292 r.op(0, 0, db.left, h, Region.Op.UNION);
13293 }
13294 if (db.right < w) {
13295 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
13296 r.op(db.right, 0, w, h, Region.Op.UNION);
13297 }
13298 if (db.top > 0) {
13299 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
13300 r.op(0, 0, w, db.top, Region.Op.UNION);
13301 }
13302 if (db.bottom < h) {
13303 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
13304 r.op(0, db.bottom, w, h, Region.Op.UNION);
13305 }
13306 final int[] location = attachInfo.mTransparentLocation;
13307 getLocationInWindow(location);
13308 r.translate(location[0], location[1]);
13309 region.op(r, Region.Op.INTERSECT);
13310 } else {
13311 region.op(db, Region.Op.DIFFERENCE);
13312 }
13313 }
13314
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013315 private void checkForLongClick(int delayOffset) {
13316 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
13317 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013318
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013319 if (mPendingCheckForLongPress == null) {
13320 mPendingCheckForLongPress = new CheckForLongPress();
13321 }
13322 mPendingCheckForLongPress.rememberWindowAttachCount();
13323 postDelayed(mPendingCheckForLongPress,
13324 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013326 }
13327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013328 /**
13329 * Inflate a view from an XML resource. This convenience method wraps the {@link
13330 * LayoutInflater} class, which provides a full range of options for view inflation.
13331 *
13332 * @param context The Context object for your activity or application.
13333 * @param resource The resource ID to inflate
13334 * @param root A view group that will be the parent. Used to properly inflate the
13335 * layout_* parameters.
13336 * @see LayoutInflater
13337 */
13338 public static View inflate(Context context, int resource, ViewGroup root) {
13339 LayoutInflater factory = LayoutInflater.from(context);
13340 return factory.inflate(resource, root);
13341 }
Romain Guy33e72ae2010-07-17 12:40:29 -070013342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013343 /**
Adam Powell637d3372010-08-25 14:37:03 -070013344 * Scroll the view with standard behavior for scrolling beyond the normal
13345 * content boundaries. Views that call this method should override
13346 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
13347 * results of an over-scroll operation.
13348 *
13349 * Views can use this method to handle any touch or fling-based scrolling.
13350 *
13351 * @param deltaX Change in X in pixels
13352 * @param deltaY Change in Y in pixels
13353 * @param scrollX Current X scroll value in pixels before applying deltaX
13354 * @param scrollY Current Y scroll value in pixels before applying deltaY
13355 * @param scrollRangeX Maximum content scroll range along the X axis
13356 * @param scrollRangeY Maximum content scroll range along the Y axis
13357 * @param maxOverScrollX Number of pixels to overscroll by in either direction
13358 * along the X axis.
13359 * @param maxOverScrollY Number of pixels to overscroll by in either direction
13360 * along the Y axis.
13361 * @param isTouchEvent true if this scroll operation is the result of a touch event.
13362 * @return true if scrolling was clamped to an over-scroll boundary along either
13363 * axis, false otherwise.
13364 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013365 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070013366 protected boolean overScrollBy(int deltaX, int deltaY,
13367 int scrollX, int scrollY,
13368 int scrollRangeX, int scrollRangeY,
13369 int maxOverScrollX, int maxOverScrollY,
13370 boolean isTouchEvent) {
13371 final int overScrollMode = mOverScrollMode;
13372 final boolean canScrollHorizontal =
13373 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
13374 final boolean canScrollVertical =
13375 computeVerticalScrollRange() > computeVerticalScrollExtent();
13376 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
13377 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
13378 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
13379 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
13380
13381 int newScrollX = scrollX + deltaX;
13382 if (!overScrollHorizontal) {
13383 maxOverScrollX = 0;
13384 }
13385
13386 int newScrollY = scrollY + deltaY;
13387 if (!overScrollVertical) {
13388 maxOverScrollY = 0;
13389 }
13390
13391 // Clamp values if at the limits and record
13392 final int left = -maxOverScrollX;
13393 final int right = maxOverScrollX + scrollRangeX;
13394 final int top = -maxOverScrollY;
13395 final int bottom = maxOverScrollY + scrollRangeY;
13396
13397 boolean clampedX = false;
13398 if (newScrollX > right) {
13399 newScrollX = right;
13400 clampedX = true;
13401 } else if (newScrollX < left) {
13402 newScrollX = left;
13403 clampedX = true;
13404 }
13405
13406 boolean clampedY = false;
13407 if (newScrollY > bottom) {
13408 newScrollY = bottom;
13409 clampedY = true;
13410 } else if (newScrollY < top) {
13411 newScrollY = top;
13412 clampedY = true;
13413 }
13414
13415 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
13416
13417 return clampedX || clampedY;
13418 }
13419
13420 /**
13421 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
13422 * respond to the results of an over-scroll operation.
13423 *
13424 * @param scrollX New X scroll value in pixels
13425 * @param scrollY New Y scroll value in pixels
13426 * @param clampedX True if scrollX was clamped to an over-scroll boundary
13427 * @param clampedY True if scrollY was clamped to an over-scroll boundary
13428 */
13429 protected void onOverScrolled(int scrollX, int scrollY,
13430 boolean clampedX, boolean clampedY) {
13431 // Intentionally empty.
13432 }
13433
13434 /**
13435 * Returns the over-scroll mode for this view. The result will be
13436 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13437 * (allow over-scrolling only if the view content is larger than the container),
13438 * or {@link #OVER_SCROLL_NEVER}.
13439 *
13440 * @return This view's over-scroll mode.
13441 */
13442 public int getOverScrollMode() {
13443 return mOverScrollMode;
13444 }
13445
13446 /**
13447 * Set the over-scroll mode for this view. Valid over-scroll modes are
13448 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13449 * (allow over-scrolling only if the view content is larger than the container),
13450 * or {@link #OVER_SCROLL_NEVER}.
13451 *
13452 * Setting the over-scroll mode of a view will have an effect only if the
13453 * view is capable of scrolling.
13454 *
13455 * @param overScrollMode The new over-scroll mode for this view.
13456 */
13457 public void setOverScrollMode(int overScrollMode) {
13458 if (overScrollMode != OVER_SCROLL_ALWAYS &&
13459 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
13460 overScrollMode != OVER_SCROLL_NEVER) {
13461 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
13462 }
13463 mOverScrollMode = overScrollMode;
13464 }
13465
13466 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013467 * Gets a scale factor that determines the distance the view should scroll
13468 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
13469 * @return The vertical scroll scale factor.
13470 * @hide
13471 */
13472 protected float getVerticalScrollFactor() {
13473 if (mVerticalScrollFactor == 0) {
13474 TypedValue outValue = new TypedValue();
13475 if (!mContext.getTheme().resolveAttribute(
13476 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
13477 throw new IllegalStateException(
13478 "Expected theme to define listPreferredItemHeight.");
13479 }
13480 mVerticalScrollFactor = outValue.getDimension(
13481 mContext.getResources().getDisplayMetrics());
13482 }
13483 return mVerticalScrollFactor;
13484 }
13485
13486 /**
13487 * Gets a scale factor that determines the distance the view should scroll
13488 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
13489 * @return The horizontal scroll scale factor.
13490 * @hide
13491 */
13492 protected float getHorizontalScrollFactor() {
13493 // TODO: Should use something else.
13494 return getVerticalScrollFactor();
13495 }
13496
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013497 /**
13498 * Return the value specifying the text direction or policy that was set with
13499 * {@link #setTextDirection(int)}.
13500 *
13501 * @return the defined text direction. It can be one of:
13502 *
13503 * {@link #TEXT_DIRECTION_INHERIT},
13504 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13505 * {@link #TEXT_DIRECTION_ANY_RTL},
13506 * {@link #TEXT_DIRECTION_LTR},
13507 * {@link #TEXT_DIRECTION_RTL},
13508 *
13509 * @hide
13510 */
13511 public int getTextDirection() {
13512 return mTextDirection;
13513 }
13514
13515 /**
13516 * Set the text direction.
13517 *
13518 * @param textDirection the direction to set. Should be one of:
13519 *
13520 * {@link #TEXT_DIRECTION_INHERIT},
13521 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13522 * {@link #TEXT_DIRECTION_ANY_RTL},
13523 * {@link #TEXT_DIRECTION_LTR},
13524 * {@link #TEXT_DIRECTION_RTL},
13525 *
13526 * @hide
13527 */
13528 public void setTextDirection(int textDirection) {
13529 if (textDirection != mTextDirection) {
13530 mTextDirection = textDirection;
13531 resetResolvedTextDirection();
13532 requestLayout();
13533 }
13534 }
13535
13536 /**
13537 * Return the resolved text direction.
13538 *
13539 * @return the resolved text direction. Return one of:
13540 *
Doug Feltcb3791202011-07-07 11:57:48 -070013541 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13542 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013543 * {@link #TEXT_DIRECTION_LTR},
13544 * {@link #TEXT_DIRECTION_RTL},
13545 *
13546 * @hide
13547 */
13548 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013549 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013550 resolveTextDirection();
13551 }
13552 return mResolvedTextDirection;
13553 }
13554
13555 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013556 * Resolve the text direction.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013557 *
13558 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013559 */
13560 protected void resolveTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013561 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
13562 mResolvedTextDirection = mTextDirection;
13563 return;
13564 }
13565 if (mParent != null && mParent instanceof ViewGroup) {
13566 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
13567 return;
13568 }
13569 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013570 }
13571
13572 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013573 * Reset resolved text direction. Will be resolved during a call to getResolvedTextDirection().
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013574 *
13575 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013576 */
13577 protected void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013578 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013579 }
13580
Chet Haaseb39f0512011-05-24 14:36:40 -070013581 //
13582 // Properties
13583 //
13584 /**
13585 * A Property wrapper around the <code>alpha</code> functionality handled by the
13586 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
13587 */
Romain Guy02ccac62011-06-24 13:20:23 -070013588 public static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013589 @Override
13590 public void setValue(View object, float value) {
13591 object.setAlpha(value);
13592 }
13593
13594 @Override
13595 public Float get(View object) {
13596 return object.getAlpha();
13597 }
13598 };
13599
13600 /**
13601 * A Property wrapper around the <code>translationX</code> functionality handled by the
13602 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
13603 */
13604 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
13605 @Override
13606 public void setValue(View object, float value) {
13607 object.setTranslationX(value);
13608 }
13609
13610 @Override
13611 public Float get(View object) {
13612 return object.getTranslationX();
13613 }
13614 };
13615
13616 /**
13617 * A Property wrapper around the <code>translationY</code> functionality handled by the
13618 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
13619 */
13620 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
13621 @Override
13622 public void setValue(View object, float value) {
13623 object.setTranslationY(value);
13624 }
13625
13626 @Override
13627 public Float get(View object) {
13628 return object.getTranslationY();
13629 }
13630 };
13631
13632 /**
13633 * A Property wrapper around the <code>x</code> functionality handled by the
13634 * {@link View#setX(float)} and {@link View#getX()} methods.
13635 */
13636 public static Property<View, Float> X = new FloatProperty<View>("x") {
13637 @Override
13638 public void setValue(View object, float value) {
13639 object.setX(value);
13640 }
13641
13642 @Override
13643 public Float get(View object) {
13644 return object.getX();
13645 }
13646 };
13647
13648 /**
13649 * A Property wrapper around the <code>y</code> functionality handled by the
13650 * {@link View#setY(float)} and {@link View#getY()} methods.
13651 */
13652 public static Property<View, Float> Y = new FloatProperty<View>("y") {
13653 @Override
13654 public void setValue(View object, float value) {
13655 object.setY(value);
13656 }
13657
13658 @Override
13659 public Float get(View object) {
13660 return object.getY();
13661 }
13662 };
13663
13664 /**
13665 * A Property wrapper around the <code>rotation</code> functionality handled by the
13666 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
13667 */
13668 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
13669 @Override
13670 public void setValue(View object, float value) {
13671 object.setRotation(value);
13672 }
13673
13674 @Override
13675 public Float get(View object) {
13676 return object.getRotation();
13677 }
13678 };
13679
13680 /**
13681 * A Property wrapper around the <code>rotationX</code> functionality handled by the
13682 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
13683 */
13684 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
13685 @Override
13686 public void setValue(View object, float value) {
13687 object.setRotationX(value);
13688 }
13689
13690 @Override
13691 public Float get(View object) {
13692 return object.getRotationX();
13693 }
13694 };
13695
13696 /**
13697 * A Property wrapper around the <code>rotationY</code> functionality handled by the
13698 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
13699 */
13700 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
13701 @Override
13702 public void setValue(View object, float value) {
13703 object.setRotationY(value);
13704 }
13705
13706 @Override
13707 public Float get(View object) {
13708 return object.getRotationY();
13709 }
13710 };
13711
13712 /**
13713 * A Property wrapper around the <code>scaleX</code> functionality handled by the
13714 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
13715 */
13716 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
13717 @Override
13718 public void setValue(View object, float value) {
13719 object.setScaleX(value);
13720 }
13721
13722 @Override
13723 public Float get(View object) {
13724 return object.getScaleX();
13725 }
13726 };
13727
13728 /**
13729 * A Property wrapper around the <code>scaleY</code> functionality handled by the
13730 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
13731 */
13732 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
13733 @Override
13734 public void setValue(View object, float value) {
13735 object.setScaleY(value);
13736 }
13737
13738 @Override
13739 public Float get(View object) {
13740 return object.getScaleY();
13741 }
13742 };
13743
Jeff Brown33bbfd22011-02-24 20:55:35 -080013744 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013745 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
13746 * Each MeasureSpec represents a requirement for either the width or the height.
13747 * A MeasureSpec is comprised of a size and a mode. There are three possible
13748 * modes:
13749 * <dl>
13750 * <dt>UNSPECIFIED</dt>
13751 * <dd>
13752 * The parent has not imposed any constraint on the child. It can be whatever size
13753 * it wants.
13754 * </dd>
13755 *
13756 * <dt>EXACTLY</dt>
13757 * <dd>
13758 * The parent has determined an exact size for the child. The child is going to be
13759 * given those bounds regardless of how big it wants to be.
13760 * </dd>
13761 *
13762 * <dt>AT_MOST</dt>
13763 * <dd>
13764 * The child can be as large as it wants up to the specified size.
13765 * </dd>
13766 * </dl>
13767 *
13768 * MeasureSpecs are implemented as ints to reduce object allocation. This class
13769 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
13770 */
13771 public static class MeasureSpec {
13772 private static final int MODE_SHIFT = 30;
13773 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
13774
13775 /**
13776 * Measure specification mode: The parent has not imposed any constraint
13777 * on the child. It can be whatever size it wants.
13778 */
13779 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
13780
13781 /**
13782 * Measure specification mode: The parent has determined an exact size
13783 * for the child. The child is going to be given those bounds regardless
13784 * of how big it wants to be.
13785 */
13786 public static final int EXACTLY = 1 << MODE_SHIFT;
13787
13788 /**
13789 * Measure specification mode: The child can be as large as it wants up
13790 * to the specified size.
13791 */
13792 public static final int AT_MOST = 2 << MODE_SHIFT;
13793
13794 /**
13795 * Creates a measure specification based on the supplied size and mode.
13796 *
13797 * The mode must always be one of the following:
13798 * <ul>
13799 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
13800 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
13801 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
13802 * </ul>
13803 *
13804 * @param size the size of the measure specification
13805 * @param mode the mode of the measure specification
13806 * @return the measure specification based on size and mode
13807 */
13808 public static int makeMeasureSpec(int size, int mode) {
13809 return size + mode;
13810 }
13811
13812 /**
13813 * Extracts the mode from the supplied measure specification.
13814 *
13815 * @param measureSpec the measure specification to extract the mode from
13816 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
13817 * {@link android.view.View.MeasureSpec#AT_MOST} or
13818 * {@link android.view.View.MeasureSpec#EXACTLY}
13819 */
13820 public static int getMode(int measureSpec) {
13821 return (measureSpec & MODE_MASK);
13822 }
13823
13824 /**
13825 * Extracts the size from the supplied measure specification.
13826 *
13827 * @param measureSpec the measure specification to extract the size from
13828 * @return the size in pixels defined in the supplied measure specification
13829 */
13830 public static int getSize(int measureSpec) {
13831 return (measureSpec & ~MODE_MASK);
13832 }
13833
13834 /**
13835 * Returns a String representation of the specified measure
13836 * specification.
13837 *
13838 * @param measureSpec the measure specification to convert to a String
13839 * @return a String with the following format: "MeasureSpec: MODE SIZE"
13840 */
13841 public static String toString(int measureSpec) {
13842 int mode = getMode(measureSpec);
13843 int size = getSize(measureSpec);
13844
13845 StringBuilder sb = new StringBuilder("MeasureSpec: ");
13846
13847 if (mode == UNSPECIFIED)
13848 sb.append("UNSPECIFIED ");
13849 else if (mode == EXACTLY)
13850 sb.append("EXACTLY ");
13851 else if (mode == AT_MOST)
13852 sb.append("AT_MOST ");
13853 else
13854 sb.append(mode).append(" ");
13855
13856 sb.append(size);
13857 return sb.toString();
13858 }
13859 }
13860
13861 class CheckForLongPress implements Runnable {
13862
13863 private int mOriginalWindowAttachCount;
13864
13865 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070013866 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013867 && mOriginalWindowAttachCount == mWindowAttachCount) {
13868 if (performLongClick()) {
13869 mHasPerformedLongPress = true;
13870 }
13871 }
13872 }
13873
13874 public void rememberWindowAttachCount() {
13875 mOriginalWindowAttachCount = mWindowAttachCount;
13876 }
13877 }
Joe Malin32736f02011-01-19 16:14:20 -080013878
Adam Powelle14579b2009-12-16 18:39:52 -080013879 private final class CheckForTap implements Runnable {
13880 public void run() {
13881 mPrivateFlags &= ~PREPRESSED;
13882 mPrivateFlags |= PRESSED;
13883 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013884 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080013885 }
13886 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013887
Adam Powella35d7682010-03-12 14:48:13 -080013888 private final class PerformClick implements Runnable {
13889 public void run() {
13890 performClick();
13891 }
13892 }
13893
Dianne Hackborn63042d62011-01-26 18:56:29 -080013894 /** @hide */
13895 public void hackTurnOffWindowResizeAnim(boolean off) {
13896 mAttachInfo.mTurnOffWindowResizeAnim = off;
13897 }
Joe Malin32736f02011-01-19 16:14:20 -080013898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013899 /**
Chet Haasea00f3862011-02-22 06:34:40 -080013900 * This method returns a ViewPropertyAnimator object, which can be used to animate
13901 * specific properties on this View.
13902 *
13903 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
13904 */
13905 public ViewPropertyAnimator animate() {
13906 if (mAnimator == null) {
13907 mAnimator = new ViewPropertyAnimator(this);
13908 }
13909 return mAnimator;
13910 }
13911
13912 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013913 * Interface definition for a callback to be invoked when a key event is
13914 * dispatched to this view. The callback will be invoked before the key
13915 * event is given to the view.
13916 */
13917 public interface OnKeyListener {
13918 /**
13919 * Called when a key is dispatched to a view. This allows listeners to
13920 * get a chance to respond before the target view.
13921 *
13922 * @param v The view the key has been dispatched to.
13923 * @param keyCode The code for the physical key that was pressed
13924 * @param event The KeyEvent object containing full information about
13925 * the event.
13926 * @return True if the listener has consumed the event, false otherwise.
13927 */
13928 boolean onKey(View v, int keyCode, KeyEvent event);
13929 }
13930
13931 /**
13932 * Interface definition for a callback to be invoked when a touch event is
13933 * dispatched to this view. The callback will be invoked before the touch
13934 * event is given to the view.
13935 */
13936 public interface OnTouchListener {
13937 /**
13938 * Called when a touch event is dispatched to a view. This allows listeners to
13939 * get a chance to respond before the target view.
13940 *
13941 * @param v The view the touch event has been dispatched to.
13942 * @param event The MotionEvent object containing full information about
13943 * the event.
13944 * @return True if the listener has consumed the event, false otherwise.
13945 */
13946 boolean onTouch(View v, MotionEvent event);
13947 }
13948
13949 /**
Jeff Brown10b62902011-06-20 16:40:37 -070013950 * Interface definition for a callback to be invoked when a hover event is
13951 * dispatched to this view. The callback will be invoked before the hover
13952 * event is given to the view.
13953 */
13954 public interface OnHoverListener {
13955 /**
13956 * Called when a hover event is dispatched to a view. This allows listeners to
13957 * get a chance to respond before the target view.
13958 *
13959 * @param v The view the hover event has been dispatched to.
13960 * @param event The MotionEvent object containing full information about
13961 * the event.
13962 * @return True if the listener has consumed the event, false otherwise.
13963 */
13964 boolean onHover(View v, MotionEvent event);
13965 }
13966
13967 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013968 * Interface definition for a callback to be invoked when a generic motion event is
13969 * dispatched to this view. The callback will be invoked before the generic motion
13970 * event is given to the view.
13971 */
13972 public interface OnGenericMotionListener {
13973 /**
13974 * Called when a generic motion event is dispatched to a view. This allows listeners to
13975 * get a chance to respond before the target view.
13976 *
13977 * @param v The view the generic motion event has been dispatched to.
13978 * @param event The MotionEvent object containing full information about
13979 * the event.
13980 * @return True if the listener has consumed the event, false otherwise.
13981 */
13982 boolean onGenericMotion(View v, MotionEvent event);
13983 }
13984
13985 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013986 * Interface definition for a callback to be invoked when a view has been clicked and held.
13987 */
13988 public interface OnLongClickListener {
13989 /**
13990 * Called when a view has been clicked and held.
13991 *
13992 * @param v The view that was clicked and held.
13993 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080013994 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013995 */
13996 boolean onLongClick(View v);
13997 }
13998
13999 /**
Chris Tate32affef2010-10-18 15:29:21 -070014000 * Interface definition for a callback to be invoked when a drag is being dispatched
14001 * to this view. The callback will be invoked before the hosting view's own
14002 * onDrag(event) method. If the listener wants to fall back to the hosting view's
14003 * onDrag(event) behavior, it should return 'false' from this callback.
14004 */
14005 public interface OnDragListener {
14006 /**
14007 * Called when a drag event is dispatched to a view. This allows listeners
14008 * to get a chance to override base View behavior.
14009 *
Joe Malin32736f02011-01-19 16:14:20 -080014010 * @param v The View that received the drag event.
14011 * @param event The {@link android.view.DragEvent} object for the drag event.
14012 * @return {@code true} if the drag event was handled successfully, or {@code false}
14013 * if the drag event was not handled. Note that {@code false} will trigger the View
14014 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070014015 */
14016 boolean onDrag(View v, DragEvent event);
14017 }
14018
14019 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014020 * Interface definition for a callback to be invoked when the focus state of
14021 * a view changed.
14022 */
14023 public interface OnFocusChangeListener {
14024 /**
14025 * Called when the focus state of a view has changed.
14026 *
14027 * @param v The view whose state has changed.
14028 * @param hasFocus The new focus state of v.
14029 */
14030 void onFocusChange(View v, boolean hasFocus);
14031 }
14032
14033 /**
14034 * Interface definition for a callback to be invoked when a view is clicked.
14035 */
14036 public interface OnClickListener {
14037 /**
14038 * Called when a view has been clicked.
14039 *
14040 * @param v The view that was clicked.
14041 */
14042 void onClick(View v);
14043 }
14044
14045 /**
14046 * Interface definition for a callback to be invoked when the context menu
14047 * for this view is being built.
14048 */
14049 public interface OnCreateContextMenuListener {
14050 /**
14051 * Called when the context menu for this view is being built. It is not
14052 * safe to hold onto the menu after this method returns.
14053 *
14054 * @param menu The context menu that is being built
14055 * @param v The view for which the context menu is being built
14056 * @param menuInfo Extra information about the item for which the
14057 * context menu should be shown. This information will vary
14058 * depending on the class of v.
14059 */
14060 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
14061 }
14062
Joe Onorato664644d2011-01-23 17:53:23 -080014063 /**
14064 * Interface definition for a callback to be invoked when the status bar changes
14065 * visibility.
14066 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070014067 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080014068 */
14069 public interface OnSystemUiVisibilityChangeListener {
14070 /**
14071 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070014072 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080014073 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040014074 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
14075 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080014076 */
14077 public void onSystemUiVisibilityChange(int visibility);
14078 }
14079
Adam Powell4afd62b2011-02-18 15:02:18 -080014080 /**
14081 * Interface definition for a callback to be invoked when this view is attached
14082 * or detached from its window.
14083 */
14084 public interface OnAttachStateChangeListener {
14085 /**
14086 * Called when the view is attached to a window.
14087 * @param v The view that was attached
14088 */
14089 public void onViewAttachedToWindow(View v);
14090 /**
14091 * Called when the view is detached from a window.
14092 * @param v The view that was detached
14093 */
14094 public void onViewDetachedFromWindow(View v);
14095 }
14096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014097 private final class UnsetPressedState implements Runnable {
14098 public void run() {
14099 setPressed(false);
14100 }
14101 }
14102
14103 /**
14104 * Base class for derived classes that want to save and restore their own
14105 * state in {@link android.view.View#onSaveInstanceState()}.
14106 */
14107 public static class BaseSavedState extends AbsSavedState {
14108 /**
14109 * Constructor used when reading from a parcel. Reads the state of the superclass.
14110 *
14111 * @param source
14112 */
14113 public BaseSavedState(Parcel source) {
14114 super(source);
14115 }
14116
14117 /**
14118 * Constructor called by derived classes when creating their SavedState objects
14119 *
14120 * @param superState The state of the superclass of this view
14121 */
14122 public BaseSavedState(Parcelable superState) {
14123 super(superState);
14124 }
14125
14126 public static final Parcelable.Creator<BaseSavedState> CREATOR =
14127 new Parcelable.Creator<BaseSavedState>() {
14128 public BaseSavedState createFromParcel(Parcel in) {
14129 return new BaseSavedState(in);
14130 }
14131
14132 public BaseSavedState[] newArray(int size) {
14133 return new BaseSavedState[size];
14134 }
14135 };
14136 }
14137
14138 /**
14139 * A set of information given to a view when it is attached to its parent
14140 * window.
14141 */
14142 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014143 interface Callbacks {
14144 void playSoundEffect(int effectId);
14145 boolean performHapticFeedback(int effectId, boolean always);
14146 }
14147
14148 /**
14149 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
14150 * to a Handler. This class contains the target (View) to invalidate and
14151 * the coordinates of the dirty rectangle.
14152 *
14153 * For performance purposes, this class also implements a pool of up to
14154 * POOL_LIMIT objects that get reused. This reduces memory allocations
14155 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014156 */
Romain Guyd928d682009-03-31 17:52:16 -070014157 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014158 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070014159 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
14160 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070014161 public InvalidateInfo newInstance() {
14162 return new InvalidateInfo();
14163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014164
Romain Guyd928d682009-03-31 17:52:16 -070014165 public void onAcquired(InvalidateInfo element) {
14166 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014167
Romain Guyd928d682009-03-31 17:52:16 -070014168 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070014169 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070014170 }
14171 }, POOL_LIMIT)
14172 );
14173
14174 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014175 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014176
14177 View target;
14178
14179 int left;
14180 int top;
14181 int right;
14182 int bottom;
14183
Romain Guyd928d682009-03-31 17:52:16 -070014184 public void setNextPoolable(InvalidateInfo element) {
14185 mNext = element;
14186 }
14187
14188 public InvalidateInfo getNextPoolable() {
14189 return mNext;
14190 }
14191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014192 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070014193 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014194 }
14195
14196 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070014197 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014198 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014199
14200 public boolean isPooled() {
14201 return mIsPooled;
14202 }
14203
14204 public void setPooled(boolean isPooled) {
14205 mIsPooled = isPooled;
14206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014207 }
14208
14209 final IWindowSession mSession;
14210
14211 final IWindow mWindow;
14212
14213 final IBinder mWindowToken;
14214
14215 final Callbacks mRootCallbacks;
14216
Romain Guy59a12ca2011-06-09 17:48:21 -070014217 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080014218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014219 /**
14220 * The top view of the hierarchy.
14221 */
14222 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070014223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014224 IBinder mPanelParentWindowToken;
14225 Surface mSurface;
14226
Romain Guyb051e892010-09-28 19:09:36 -070014227 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014228 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070014229 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080014230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014231 /**
Romain Guy8506ab42009-06-11 17:35:47 -070014232 * Scale factor used by the compatibility mode
14233 */
14234 float mApplicationScale;
14235
14236 /**
14237 * Indicates whether the application is in compatibility mode
14238 */
14239 boolean mScalingRequired;
14240
14241 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014242 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080014243 */
14244 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080014245
Dianne Hackborn63042d62011-01-26 18:56:29 -080014246 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014247 * Left position of this view's window
14248 */
14249 int mWindowLeft;
14250
14251 /**
14252 * Top position of this view's window
14253 */
14254 int mWindowTop;
14255
14256 /**
Adam Powell26153a32010-11-08 15:22:27 -080014257 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070014258 */
Adam Powell26153a32010-11-08 15:22:27 -080014259 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070014260
14261 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014262 * For windows that are full-screen but using insets to layout inside
14263 * of the screen decorations, these are the current insets for the
14264 * content of the window.
14265 */
14266 final Rect mContentInsets = new Rect();
14267
14268 /**
14269 * For windows that are full-screen but using insets to layout inside
14270 * of the screen decorations, these are the current insets for the
14271 * actual visible parts of the window.
14272 */
14273 final Rect mVisibleInsets = new Rect();
14274
14275 /**
14276 * The internal insets given by this window. This value is
14277 * supplied by the client (through
14278 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
14279 * be given to the window manager when changed to be used in laying
14280 * out windows behind it.
14281 */
14282 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
14283 = new ViewTreeObserver.InternalInsetsInfo();
14284
14285 /**
14286 * All views in the window's hierarchy that serve as scroll containers,
14287 * used to determine if the window can be resized or must be panned
14288 * to adjust for a soft input area.
14289 */
14290 final ArrayList<View> mScrollContainers = new ArrayList<View>();
14291
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070014292 final KeyEvent.DispatcherState mKeyDispatchState
14293 = new KeyEvent.DispatcherState();
14294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014295 /**
14296 * Indicates whether the view's window currently has the focus.
14297 */
14298 boolean mHasWindowFocus;
14299
14300 /**
14301 * The current visibility of the window.
14302 */
14303 int mWindowVisibility;
14304
14305 /**
14306 * Indicates the time at which drawing started to occur.
14307 */
14308 long mDrawingTime;
14309
14310 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070014311 * Indicates whether or not ignoring the DIRTY_MASK flags.
14312 */
14313 boolean mIgnoreDirtyState;
14314
14315 /**
Romain Guy02ccac62011-06-24 13:20:23 -070014316 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
14317 * to avoid clearing that flag prematurely.
14318 */
14319 boolean mSetIgnoreDirtyState = false;
14320
14321 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014322 * Indicates whether the view's window is currently in touch mode.
14323 */
14324 boolean mInTouchMode;
14325
14326 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014327 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014328 * the next time it performs a traversal
14329 */
14330 boolean mRecomputeGlobalAttributes;
14331
14332 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014333 * Set during a traveral if any views want to keep the screen on.
14334 */
14335 boolean mKeepScreenOn;
14336
14337 /**
Joe Onorato664644d2011-01-23 17:53:23 -080014338 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
14339 */
14340 int mSystemUiVisibility;
14341
14342 /**
14343 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
14344 * attached.
14345 */
14346 boolean mHasSystemUiListeners;
14347
14348 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014349 * Set if the visibility of any views has changed.
14350 */
14351 boolean mViewVisibilityChanged;
14352
14353 /**
14354 * Set to true if a view has been scrolled.
14355 */
14356 boolean mViewScrollChanged;
14357
14358 /**
14359 * Global to the view hierarchy used as a temporary for dealing with
14360 * x/y points in the transparent region computations.
14361 */
14362 final int[] mTransparentLocation = new int[2];
14363
14364 /**
14365 * Global to the view hierarchy used as a temporary for dealing with
14366 * x/y points in the ViewGroup.invalidateChild implementation.
14367 */
14368 final int[] mInvalidateChildLocation = new int[2];
14369
Chet Haasec3aa3612010-06-17 08:50:37 -070014370
14371 /**
14372 * Global to the view hierarchy used as a temporary for dealing with
14373 * x/y location when view is transformed.
14374 */
14375 final float[] mTmpTransformLocation = new float[2];
14376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014377 /**
14378 * The view tree observer used to dispatch global events like
14379 * layout, pre-draw, touch mode change, etc.
14380 */
14381 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
14382
14383 /**
14384 * A Canvas used by the view hierarchy to perform bitmap caching.
14385 */
14386 Canvas mCanvas;
14387
14388 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014389 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014390 * handler can be used to pump events in the UI events queue.
14391 */
14392 final Handler mHandler;
14393
14394 /**
14395 * Identifier for messages requesting the view to be invalidated.
14396 * Such messages should be sent to {@link #mHandler}.
14397 */
14398 static final int INVALIDATE_MSG = 0x1;
14399
14400 /**
14401 * Identifier for messages requesting the view to invalidate a region.
14402 * Such messages should be sent to {@link #mHandler}.
14403 */
14404 static final int INVALIDATE_RECT_MSG = 0x2;
14405
14406 /**
14407 * Temporary for use in computing invalidate rectangles while
14408 * calling up the hierarchy.
14409 */
14410 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070014411
14412 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070014413 * Temporary for use in computing hit areas with transformed views
14414 */
14415 final RectF mTmpTransformRect = new RectF();
14416
14417 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070014418 * Temporary list for use in collecting focusable descendents of a view.
14419 */
14420 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
14421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014422 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014423 * The id of the window for accessibility purposes.
14424 */
14425 int mAccessibilityWindowId = View.NO_ID;
14426
14427 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014428 * Creates a new set of attachment information with the specified
14429 * events handler and thread.
14430 *
14431 * @param handler the events handler the view must use
14432 */
14433 AttachInfo(IWindowSession session, IWindow window,
14434 Handler handler, Callbacks effectPlayer) {
14435 mSession = session;
14436 mWindow = window;
14437 mWindowToken = window.asBinder();
14438 mHandler = handler;
14439 mRootCallbacks = effectPlayer;
14440 }
14441 }
14442
14443 /**
14444 * <p>ScrollabilityCache holds various fields used by a View when scrolling
14445 * is supported. This avoids keeping too many unused fields in most
14446 * instances of View.</p>
14447 */
Mike Cleronf116bf82009-09-27 19:14:12 -070014448 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080014449
Mike Cleronf116bf82009-09-27 19:14:12 -070014450 /**
14451 * Scrollbars are not visible
14452 */
14453 public static final int OFF = 0;
14454
14455 /**
14456 * Scrollbars are visible
14457 */
14458 public static final int ON = 1;
14459
14460 /**
14461 * Scrollbars are fading away
14462 */
14463 public static final int FADING = 2;
14464
14465 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080014466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014467 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070014468 public int scrollBarDefaultDelayBeforeFade;
14469 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014470
14471 public int scrollBarSize;
14472 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070014473 public float[] interpolatorValues;
14474 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014475
14476 public final Paint paint;
14477 public final Matrix matrix;
14478 public Shader shader;
14479
Mike Cleronf116bf82009-09-27 19:14:12 -070014480 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
14481
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014482 private static final float[] OPAQUE = { 255 };
14483 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080014484
Mike Cleronf116bf82009-09-27 19:14:12 -070014485 /**
14486 * When fading should start. This time moves into the future every time
14487 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
14488 */
14489 public long fadeStartTime;
14490
14491
14492 /**
14493 * The current state of the scrollbars: ON, OFF, or FADING
14494 */
14495 public int state = OFF;
14496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014497 private int mLastColor;
14498
Mike Cleronf116bf82009-09-27 19:14:12 -070014499 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014500 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
14501 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070014502 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
14503 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014504
14505 paint = new Paint();
14506 matrix = new Matrix();
14507 // use use a height of 1, and then wack the matrix each time we
14508 // actually use it.
14509 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014511 paint.setShader(shader);
14512 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070014513 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014514 }
Romain Guy8506ab42009-06-11 17:35:47 -070014515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014516 public void setFadeColor(int color) {
14517 if (color != 0 && color != mLastColor) {
14518 mLastColor = color;
14519 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070014520
Romain Guye55e1a72009-08-27 10:42:26 -070014521 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
14522 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014524 paint.setShader(shader);
14525 // Restore the default transfer mode (src_over)
14526 paint.setXfermode(null);
14527 }
14528 }
Joe Malin32736f02011-01-19 16:14:20 -080014529
Mike Cleronf116bf82009-09-27 19:14:12 -070014530 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070014531 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070014532 if (now >= fadeStartTime) {
14533
14534 // the animation fades the scrollbars out by changing
14535 // the opacity (alpha) from fully opaque to fully
14536 // transparent
14537 int nextFrame = (int) now;
14538 int framesCount = 0;
14539
14540 Interpolator interpolator = scrollBarInterpolator;
14541
14542 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014543 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070014544
14545 // End transparent
14546 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014547 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070014548
14549 state = FADING;
14550
14551 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080014552 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070014553 }
14554 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070014555 }
Mike Cleronf116bf82009-09-27 19:14:12 -070014556
Svetoslav Ganova0156172011-06-26 17:55:44 -070014557 /**
14558 * Resuable callback for sending
14559 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
14560 */
14561 private class SendViewScrolledAccessibilityEvent implements Runnable {
14562 public volatile boolean mIsPending;
14563
14564 public void run() {
14565 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
14566 mIsPending = false;
14567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014568 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070014569
14570 /**
14571 * <p>
14572 * This class represents a delegate that can be registered in a {@link View}
14573 * to enhance accessibility support via composition rather via inheritance.
14574 * It is specifically targeted to widget developers that extend basic View
14575 * classes i.e. classes in package android.view, that would like their
14576 * applications to be backwards compatible.
14577 * </p>
14578 * <p>
14579 * A scenario in which a developer would like to use an accessibility delegate
14580 * is overriding a method introduced in a later API version then the minimal API
14581 * version supported by the application. For example, the method
14582 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
14583 * in API version 4 when the accessibility APIs were first introduced. If a
14584 * developer would like his application to run on API version 4 devices (assuming
14585 * all other APIs used by the application are version 4 or lower) and take advantage
14586 * of this method, instead of overriding the method which would break the application's
14587 * backwards compatibility, he can override the corresponding method in this
14588 * delegate and register the delegate in the target View if the API version of
14589 * the system is high enough i.e. the API version is same or higher to the API
14590 * version that introduced
14591 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
14592 * </p>
14593 * <p>
14594 * Here is an example implementation:
14595 * </p>
14596 * <code><pre><p>
14597 * if (Build.VERSION.SDK_INT >= 14) {
14598 * // If the API version is equal of higher than the version in
14599 * // which onInitializeAccessibilityNodeInfo was introduced we
14600 * // register a delegate with a customized implementation.
14601 * View view = findViewById(R.id.view_id);
14602 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
14603 * public void onInitializeAccessibilityNodeInfo(View host,
14604 * AccessibilityNodeInfo info) {
14605 * // Let the default implementation populate the info.
14606 * super.onInitializeAccessibilityNodeInfo(host, info);
14607 * // Set some other information.
14608 * info.setEnabled(host.isEnabled());
14609 * }
14610 * });
14611 * }
14612 * </code></pre></p>
14613 * <p>
14614 * This delegate contains methods that correspond to the accessibility methods
14615 * in View. If a delegate has been specified the implementation in View hands
14616 * off handling to the corresponding method in this delegate. The default
14617 * implementation the delegate methods behaves exactly as the corresponding
14618 * method in View for the case of no accessibility delegate been set. Hence,
14619 * to customize the behavior of a View method, clients can override only the
14620 * corresponding delegate method without altering the behavior of the rest
14621 * accessibility related methods of the host view.
14622 * </p>
14623 */
14624 public static class AccessibilityDelegate {
14625
14626 /**
14627 * Sends an accessibility event of the given type. If accessibility is not
14628 * enabled this method has no effect.
14629 * <p>
14630 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
14631 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
14632 * been set.
14633 * </p>
14634 *
14635 * @param host The View hosting the delegate.
14636 * @param eventType The type of the event to send.
14637 *
14638 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
14639 */
14640 public void sendAccessibilityEvent(View host, int eventType) {
14641 host.sendAccessibilityEventInternal(eventType);
14642 }
14643
14644 /**
14645 * Sends an accessibility event. This method behaves exactly as
14646 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
14647 * empty {@link AccessibilityEvent} and does not perform a check whether
14648 * accessibility is enabled.
14649 * <p>
14650 * The default implementation behaves as
14651 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14652 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
14653 * the case of no accessibility delegate been set.
14654 * </p>
14655 *
14656 * @param host The View hosting the delegate.
14657 * @param event The event to send.
14658 *
14659 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14660 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
14661 */
14662 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
14663 host.sendAccessibilityEventUncheckedInternal(event);
14664 }
14665
14666 /**
14667 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
14668 * to its children for adding their text content to the event.
14669 * <p>
14670 * The default implementation behaves as
14671 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14672 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
14673 * the case of no accessibility delegate been set.
14674 * </p>
14675 *
14676 * @param host The View hosting the delegate.
14677 * @param event The event.
14678 * @return True if the event population was completed.
14679 *
14680 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14681 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
14682 */
14683 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14684 return host.dispatchPopulateAccessibilityEventInternal(event);
14685 }
14686
14687 /**
14688 * Gives a chance to the host View to populate the accessibility event with its
14689 * text content.
14690 * <p>
14691 * The default implementation behaves as
14692 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
14693 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
14694 * the case of no accessibility delegate been set.
14695 * </p>
14696 *
14697 * @param host The View hosting the delegate.
14698 * @param event The accessibility event which to populate.
14699 *
14700 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
14701 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
14702 */
14703 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
14704 host.onPopulateAccessibilityEventInternal(event);
14705 }
14706
14707 /**
14708 * Initializes an {@link AccessibilityEvent} with information about the
14709 * the host View which is the event source.
14710 * <p>
14711 * The default implementation behaves as
14712 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
14713 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
14714 * the case of no accessibility delegate been set.
14715 * </p>
14716 *
14717 * @param host The View hosting the delegate.
14718 * @param event The event to initialize.
14719 *
14720 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
14721 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
14722 */
14723 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
14724 host.onInitializeAccessibilityEventInternal(event);
14725 }
14726
14727 /**
14728 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
14729 * <p>
14730 * The default implementation behaves as
14731 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14732 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
14733 * the case of no accessibility delegate been set.
14734 * </p>
14735 *
14736 * @param host The View hosting the delegate.
14737 * @param info The instance to initialize.
14738 *
14739 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14740 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
14741 */
14742 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
14743 host.onInitializeAccessibilityNodeInfoInternal(info);
14744 }
14745
14746 /**
14747 * Called when a child of the host View has requested sending an
14748 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
14749 * to augment the event.
14750 * <p>
14751 * The default implementation behaves as
14752 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14753 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
14754 * the case of no accessibility delegate been set.
14755 * </p>
14756 *
14757 * @param host The View hosting the delegate.
14758 * @param child The child which requests sending the event.
14759 * @param event The event to be sent.
14760 * @return True if the event should be sent
14761 *
14762 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14763 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
14764 */
14765 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
14766 AccessibilityEvent event) {
14767 return host.onRequestSendAccessibilityEventInternal(child, event);
14768 }
14769 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014770}